Skip to content

Commit

Permalink
refactor: remove -new-contents-created event
Browse files Browse the repository at this point in the history
Chromium expects us to take ownership of WebContents in AddNewContents,
we should not create V8 wrapper in WebContentsCreated, otherwise we
would have WebContents being managed by 2 unique_ptr at the same time.
  • Loading branch information
zcbenz committed Oct 19, 2018
1 parent 94aa076 commit 3bfe073
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 74 deletions.
22 changes: 10 additions & 12 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -539,11 +539,10 @@ void WebContents::WebContentsCreated(content::WebContents* source_contents,
const std::string& frame_name,
const GURL& target_url,
content::WebContents* new_contents) {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
// Create V8 wrapper for the |new_contents|.
auto wrapper = CreateAndTake(isolate(), new_contents, BROWSER_WINDOW);
Emit("-web-contents-created", wrapper, target_url, frame_name);
ChildWebContentsTracker::CreateForWebContents(new_contents);
auto* tracker = ChildWebContentsTracker::FromWebContents(new_contents);
tracker->url = target_url;
tracker->frame_name = frame_name;
}

void WebContents::AddNewContents(
Expand All @@ -553,17 +552,16 @@ void WebContents::AddNewContents(
const gfx::Rect& initial_rect,
bool user_gesture,
bool* was_blocked) {
new ChildWebContentsTracker(new_contents.get());
auto* tracker = ChildWebContentsTracker::FromWebContents(new_contents.get());
DCHECK(tracker);

v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
// Note that the ownership of |new_contents| has already been claimed by
// the WebContentsCreated method, the release call here completes
// the ownership transfer.
auto api_web_contents = From(isolate(), new_contents.release());
DCHECK(!api_web_contents.IsEmpty());
auto api_web_contents =
CreateAndTake(isolate(), new_contents.release(), BROWSER_WINDOW);
if (Emit("-add-new-contents", api_web_contents, disposition, user_gesture,
initial_rect.x(), initial_rect.y(), initial_rect.width(),
initial_rect.height())) {
initial_rect.height(), tracker->url, tracker->frame_name)) {
api_web_contents->DestroyWebContents(true /* async */);
}
}
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/atom_browser_client.cc
Expand Up @@ -165,7 +165,7 @@ bool AtomBrowserClient::ShouldCreateNewSiteInstance(
}
auto* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
if (!ChildWebContentsTracker::IsChildWebContents(web_contents)) {
if (!ChildWebContentsTracker::FromWebContents(web_contents)) {
// Root WebContents should always create new process to make sure
// native addons are loaded correctly after reload / navigation.
// (Non-root WebContents opened by window.open() should try to
Expand Down
33 changes: 0 additions & 33 deletions atom/browser/child_web_contents_tracker.cc

This file was deleted.

20 changes: 13 additions & 7 deletions atom/browser/child_web_contents_tracker.h
Expand Up @@ -5,19 +5,25 @@
#ifndef ATOM_BROWSER_CHILD_WEB_CONTENTS_TRACKER_H_
#define ATOM_BROWSER_CHILD_WEB_CONTENTS_TRACKER_H_

#include "content/public/browser/web_contents_observer.h"
#include <string>

#include "content/public/browser/web_contents_user_data.h"

namespace atom {

// ChildWebContentsTracker tracks child WebContents
// created by native `window.open()`
class ChildWebContentsTracker : public content::WebContentsObserver {
public:
explicit ChildWebContentsTracker(content::WebContents* web_contents);
static bool IsChildWebContents(content::WebContents* web_contents);
struct ChildWebContentsTracker
: public content::WebContentsUserData<ChildWebContentsTracker> {
GURL url;
std::string frame_name;

explicit ChildWebContentsTracker(content::WebContents* web_contents) {}

private:
friend class content::WebContentsUserData<ChildWebContentsTracker>;

protected:
void WebContentsDestroyed() override;
DISALLOW_COPY_AND_ASSIGN(ChildWebContentsTracker);
};

} // namespace atom
Expand Down
1 change: 0 additions & 1 deletion filenames.gni
Expand Up @@ -239,7 +239,6 @@ filenames = {
"atom/browser/browser_mac.mm",
"atom/browser/browser_win.cc",
"atom/browser/browser_observer.h",
"atom/browser/child_web_contents_tracker.cc",
"atom/browser/child_web_contents_tracker.h",
"atom/browser/common_web_contents_delegate_mac.mm",
"atom/browser/common_web_contents_delegate_views.cc",
Expand Down
14 changes: 2 additions & 12 deletions lib/browser/api/browser-window.js
Expand Up @@ -3,7 +3,6 @@
const electron = require('electron')
const { WebContentsView, TopLevelWindow } = electron
const { BrowserWindow } = process.atomBinding('window')
const v8Util = process.atomBinding('v8_util')
const ipcMain = require('@electron/internal/browser/ipc-main-internal')

Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype)
Expand Down Expand Up @@ -32,25 +31,16 @@ BrowserWindow.prototype._init = function () {
options, additionalFeatures, postData)
})

this.webContents.on('-web-contents-created', (event, webContents, url,
frameName) => {
v8Util.setHiddenValue(webContents, 'url-framename', { url, frameName })
})

// Create a new browser window for the native implementation of
// "window.open", used in sandbox and nativeWindowOpen mode
this.webContents.on('-add-new-contents', (event, webContents, disposition,
userGesture, left, top, width,
height) => {
const urlFrameName = v8Util.getHiddenValue(webContents, 'url-framename')
userGesture, left, top, width, height, url, frameName) => {
if ((disposition !== 'foreground-tab' && disposition !== 'new-window' &&
disposition !== 'background-tab') || !urlFrameName) {
disposition !== 'background-tab')) {
event.preventDefault()
return
}

const { url, frameName } = urlFrameName
v8Util.deleteHiddenValue(webContents, 'url-framename')
const options = {
show: true,
x: left,
Expand Down
8 changes: 0 additions & 8 deletions lib/browser/guest-view-manager.js
Expand Up @@ -157,14 +157,6 @@ const createGuest = function (embedder, params) {
}
}
})
guest.on('-web-contents-created', (...args) => {
if (guest.getLastWebPreferences().nativeWindowOpen === true) {
const embedder = getEmbedder(guestInstanceId)
if (embedder != null) {
embedder.emit('-web-contents-created', ...args)
}
}
})

return guestInstanceId
}
Expand Down

0 comments on commit 3bfe073

Please sign in to comment.