Repository navigation
Add option to use native window.open() in non-sandboxed webcontents - #8963
Conversation
|
According to @zcbenz, it is not safe to have multiple v8 contexts running in parallel due to node.js not supporting multiple instances. See also #1865 (comment) |
|
Probably the problem would be similar to web worker support (#8852) . |
|
Looks like reloading doesn't work well in this mode currently. |
a73f5ab to
c3c67f7
Compare
|
I ensured renderer process restart after reloading, and reloading now works well. |
c04f8c0 to
6f9dbd4
Compare
| content::SiteInstance* current_instance, | ||
| const GURL& url) { | ||
|
|
||
| if (g_suppress_renderer_process_restart) { |
There was a problem hiding this comment.
I wonder if it is possible to have a race condition on this variable. Imagine the following chain of events:
- renderer with
nativeWindowOpeninvokeswindow.open - soon after, another renderer without
nativeWindowOpeninvokeswindow.open
Can you guarantee that ShouldCreateNewSiteInstance will be invoked for the first navigation before the second navigation?
Also, imagine two renderers with nativeWindowOpen invoking window.open in parallel, both would cause g_suppress_renderer_process_restart to be set, but it seems possible that only one would read it as true.
There was a problem hiding this comment.
Did you mean g_reuse_renderer_process_for_new_window ? g_suppress_renderer_process_restart is only used during history navigations, I am not sure why it would affect this mode alone.
There was a problem hiding this comment.
Did you mean g_reuse_renderer_process_for_new_window ?
Yes that's what I mean, thanks.
There was a problem hiding this comment.
Can I only allow about:blank as synchronously-opened window for now?
electron/lib/browser/guest-window-manager.js
Lines 113 to 148 in 3f7b3c4
In the current implementation, if the URL opened with
window.open is not about:blank, createGuest causes OverrideSiteInstanceForNavigation, and that's why I did workaround with g_reuse_renderer_process_for_new_window variable and prevented renderer process spawn in ShouldCreateNewSiteInstance.
If about:blank is only allowed as synchronously-opened window, I can remove g_reuse_renderer_process_for_new_window completely.
There was a problem hiding this comment.
that's why I did workaround with g_reuse_renderer_process_for_new_window variable and prevented renderer process spawn in ShouldCreateNewSiteInstance.
The render process is not reused for same origin child window, process.pid returns different values. g_reuse_render_process_for_new_window is being modified on two different threads, which causes a race.
| ### Use Native `window.open()` | ||
|
|
||
| If you want to use native `window.open()` implementation, pass `useNativeWindowOpen: true` in `webPreferences` option. | ||
| Native `window.open()` allows synchronous access to opened windows so it is convenient choise if you need to open a dialog or a preferences window. |
| height: 100 | ||
| }) | ||
| modal = new BrowserWindow(options) | ||
| event.newGuest = modal |
There was a problem hiding this comment.
modal is not defined anywhere, maybe just event.newGuest = new BrowserWindow(options).
| disposition, options, | ||
| additionalFeatures, postData) { | ||
| options = mergeBrowserWindowOptions(event.sender, options) | ||
| const {webContents} = options |
There was a problem hiding this comment.
It is better to use options.webContents directly, a single webContents can have lots of meanings and it is hard to find its declaration when reading the code.
| // integration. | ||
| if (IsSandboxed(web_contents)) | ||
| command_line->AppendSwitch(switches::kEnableSandbox); | ||
| if (UsesNativeWindowOpen(web_contents)) |
There was a problem hiding this comment.
We should just use if (web_preferences.GetBoolean("nativeWindowOpen", &b) && b), calling UsesNativeWindowOpen() does unnecessary checks here.
| if ((event.sender.isGuest() && !event.sender.allowPopups) || event.defaultPrevented) { | ||
| if (newGuest !== undefined && newGuest !== null) { | ||
| if (webContents === newGuest.webContents) { | ||
| event.defaultPrevented = false |
There was a problem hiding this comment.
Should add a comment on why doing this.
|
I changed code not to use |
|
FYI libchromiumcontent was updated in #9219. |
|
|
||
| const {guestInstanceId, openerId} = process | ||
| const hiddenPage = process.argv.includes('--hidden-page') | ||
| const usesNativeWindowOpen = process.argv.includes('--native-window-open') |
There was a problem hiding this comment.
It would be great to support this as well when contextIsolation is enabled, which mean this variable would need to be threaded through in lib/isolated_renderer/init.js.
| <script type="text/javascript" charset="utf-8"> | ||
| const {ipcRenderer} = require("electron"); | ||
| const tests = { | ||
| 'blank': () => { |
There was a problem hiding this comment.
I think it might be nice to split up this file into 3 separate files, one for each case being tested. Then we don't risk one test case possibly influencing another since they will each be self-contained.
…into native-window-open
|
Thanks! |
|
Thanks so much for the work. I really need this. It seems like it has a bug with webview. Try this: Go to Log in > Login with Google, Electron will crash. Update: It's related to #8378, not this PR. |
|
If I'm using native |
|
I found a hack. Using native window.open( ... ) // returns a native window object.
const win = BrowserWindow.getFocusedWindow() // returns the BrowserWindow of the window we just opened.Is this reliable? Is there a better way? |
This pull request adds
nativeWindowOpenoption to use nativewindow.open()in non-sandboxed webcontents.It would be useful when you opens dialogs or preferences windows from your app, as it allows synchronous access to DOM elements in the new windows.
Also synchronous
window.open()would be faster because it doesn't need another new process and you could share the same instance of JavaScript libraries/frameworks among windows.This is a simple use case example: https://github.com/seanchas116/electron-native-window-open-example
I don't know the risk of running multiple Node contexts in the same process, so I would be glad to know it.
Close #8959