Skip to content

Commit

Permalink
App: fix child windows not inheriting mainWindow properties, includin…
Browse files Browse the repository at this point in the history
…g userAgent (nativefier#1174)

When a new child window is spawned (such as for a Google login popup), those child windows were not receiving the mainWindow's properties. Chiefly among this was the userAgent which caused a bug in nativefier#831
  • Loading branch information
TheCleric authored May 1, 2021
1 parent fb61d49 commit d6639fd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
20 changes: 15 additions & 5 deletions app/src/components/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export function saveAppArgs(newAppArgs: any) {
);
}
}

export type createWindowResult = {
window: BrowserWindow;
setupWindow: (window: BrowserWindow) => void;
};

/**
* @param {{}} nativefierOptions AppArgs from nativefier.json
* @param {function} onAppQuit
Expand All @@ -115,7 +121,7 @@ export function createMainWindow(
nativefierOptions,
onAppQuit,
setDockBadge,
): BrowserWindow {
): createWindowResult {
const options = { ...nativefierOptions };
const mainWindowState = windowStateKeeper({
defaultWidth: options.width || 1280,
Expand Down Expand Up @@ -292,6 +298,12 @@ export function createMainWindow(

const createNewWindow: (url: string) => BrowserWindow = (url: string) => {
const window = new BrowserWindow(DEFAULT_WINDOW_OPTIONS);
setupWindow(window);
window.loadURL(url); // eslint-disable-line @typescript-eslint/no-floating-promises
return window;
};

function setupWindow(window: BrowserWindow): void {
if (options.userAgent) {
window.webContents.userAgent = options.userAgent;
}
Expand All @@ -305,9 +317,7 @@ export function createMainWindow(
window.webContents.on('new-window', onNewWindow);
window.webContents.on('will-navigate', onWillNavigate);
window.webContents.on('will-prevent-unload', onWillPreventUnload);
window.loadURL(url); // eslint-disable-line @typescript-eslint/no-floating-promises
return window;
};
}

const createNewTab = (url: string, foreground: boolean): BrowserWindow => {
log.debug('createNewTab', { url, foreground });
Expand Down Expand Up @@ -552,5 +562,5 @@ export function createMainWindow(
}
});

return mainWindow;
return { window: mainWindow, setupWindow };
}
42 changes: 41 additions & 1 deletion app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ if (appArgs.processEnvs) {
}

let mainWindow: BrowserWindow;
let setupWindow: (BrowserWindow) => void;

if (typeof appArgs.flashPluginDir === 'string') {
app.commandLine.appendSwitch('ppapi-flash-path', appArgs.flashPluginDir);
Expand Down Expand Up @@ -251,7 +252,14 @@ if (shouldQuit) {
}

function onReady(): void {
mainWindow = createMainWindow(appArgs, app.quit.bind(this), setDockBadge);
const createWindowResult = createMainWindow(
appArgs,
app.quit.bind(this),
setDockBadge,
);
log.debug('onReady', createWindowResult);
mainWindow = createWindowResult.window;
setupWindow = createWindowResult.setupWindow;
createTrayIcon(appArgs, mainWindow);

// Register global shortcuts
Expand Down Expand Up @@ -344,3 +352,35 @@ app.on('login', (event, webContents, request, authInfo, callback) => {
createLoginWindow(callback);
}
});

app.on(
'accessibility-support-changed',
(event: Event, accessibilitySupportEnabled: boolean) => {
log.debug('app.accessibility-support-changed', {
event,
accessibilitySupportEnabled,
});
},
);

app.on(
'activity-was-continued',
(event: Event, type: string, userInfo: any) => {
log.debug('app.activity-was-continued', { event, type, userInfo });
},
);

app.on('browser-window-blur', (event: Event, window: BrowserWindow) => {
log.debug('app.browser-window-blur', { event, window });
});

app.on('browser-window-created', (event: Event, window: BrowserWindow) => {
log.debug('app.browser-window-created', { event, window });
if (setupWindow !== undefined) {
setupWindow(window);
}
});

app.on('browser-window-focus', (event: Event, window: BrowserWindow) => {
log.debug('app.browser-window-focus', { event, window });
});
2 changes: 1 addition & 1 deletion docs/manual-test
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ printf "\n***** Test checklist *****

printf "\n***** Running app *****\n"
if [ "$(uname -s)" = "Darwin" ]; then
open -a 'app-darwin-x64/app.app'
open -a "$tmp_dir/app-darwin-x64/app.app"
else
"$tmp_dir/app-linux-x64/app"
fi
Expand Down

0 comments on commit d6639fd

Please sign in to comment.