Track child web contents

This commit is contained in:
Ryohei Ikegami 2017-04-06 12:15:27 +09:00
parent 659722924d
commit 34ed1a9ef8
8 changed files with 55 additions and 55 deletions

View file

@ -13,6 +13,7 @@
#include "atom/browser/atom_browser_client.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/child_web_contents_tracker.h"
#include "atom/browser/lib/bluetooth_chooser.h"
#include "atom/browser/native_window.h"
#include "atom/browser/net/atom_network_delegate.h"
@ -466,6 +467,7 @@ void WebContents::AddNewContents(content::WebContents* source,
const gfx::Rect& initial_rect,
bool user_gesture,
bool* was_blocked) {
new ChildWebContentsTracker(new_contents);
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
auto api_web_contents = CreateFrom(isolate(), new_contents);

View file

@ -9,7 +9,6 @@
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/browser.h"
#include "atom/browser/native_window.h"
#include "atom/browser/root_web_contents_tracker.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/gfx_converter.h"
@ -101,7 +100,6 @@ Window::Window(v8::Isolate* isolate, v8::Local<v8::Object> wrapper,
// Creates the WebContents used by BrowserWindow.
web_contents = WebContents::Create(isolate, web_preferences);
new RootWebContentsTracker(web_contents.get()->web_contents());
}
Init(isolate, wrapper, options, web_contents);

View file

@ -15,8 +15,8 @@
#include "atom/browser/atom_quota_permission_context.h"
#include "atom/browser/atom_resource_dispatcher_host_delegate.h"
#include "atom/browser/atom_speech_recognition_manager_delegate.h"
#include "atom/browser/child_web_contents_tracker.h"
#include "atom/browser/native_window.h"
#include "atom/browser/root_web_contents_tracker.h"
#include "atom/browser/web_contents_permission_helper.h"
#include "atom/browser/web_contents_preferences.h"
#include "atom/browser/window_list.h"
@ -105,7 +105,7 @@ bool AtomBrowserClient::ShouldCreateNewSiteInstance(
}
auto web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
if (RootWebContentsTracker::IsRootWebContents(web_contents)) {
if (!ChildWebContentsTracker::IsChildWebContents(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 reuse process

View file

@ -0,0 +1,28 @@
#include "child_web_contents_tracker.h"
#include <unordered_set>
namespace atom {
namespace {
std::unordered_set<content::WebContents*> g_child_web_contents;
} // namespace
ChildWebContentsTracker::ChildWebContentsTracker(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {
g_child_web_contents.insert(web_contents);
}
bool ChildWebContentsTracker::IsChildWebContents(
content::WebContents* web_contents) {
return g_child_web_contents.find(web_contents) != g_child_web_contents.end();
}
void ChildWebContentsTracker::WebContentsDestroyed() {
g_child_web_contents.erase(web_contents());
delete this;
}
} // namespace atom

View file

@ -0,0 +1,21 @@
#ifndef ATOM_BROWSER_CHILD_WEB_CONTENTS_TRACKER_H_
#define ATOM_BROWSER_CHILD_WEB_CONTENTS_TRACKER_H_
#include "content/public/browser/web_contents_observer.h"
namespace atom {
// ChildWebContentsTracker tracks root WebContents created by
// `new BrowserWindow`.
class ChildWebContentsTracker : public content::WebContentsObserver {
public:
ChildWebContentsTracker(content::WebContents* web_contents);
static bool IsChildWebContents(content::WebContents* web_contents);
protected:
void WebContentsDestroyed() override;
};
} // namespace atom
#endif // ATOM_BROWSER_CHILD_WEB_CONTENTS_TRACKER_H_

View file

@ -1,28 +0,0 @@
#include "root_web_contents_tracker.h"
#include <unordered_set>
namespace atom {
namespace {
std::unordered_set<content::WebContents*> g_root_web_contents;
} // namespace
RootWebContentsTracker::RootWebContentsTracker(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {
g_root_web_contents.insert(web_contents);
}
bool RootWebContentsTracker::IsRootWebContents(
content::WebContents* web_contents) {
return g_root_web_contents.find(web_contents) != g_root_web_contents.end();
}
void RootWebContentsTracker::WebContentsDestroyed() {
g_root_web_contents.erase(web_contents());
delete this;
}
} // namespace atom

View file

@ -1,21 +0,0 @@
#ifndef ATOM_BROWSER_ROOT_WEB_CONTENTS_TRACKER_H_
#define ATOM_BROWSER_ROOT_WEB_CONTENTS_TRACKER_H_
#include "content/public/browser/web_contents_observer.h"
namespace atom {
// RootWebContentsTracker tracks root WebContents created by
// `new BrowserWindow`.
class RootWebContentsTracker : public content::WebContentsObserver {
public:
RootWebContentsTracker(content::WebContents* web_contents);
static bool IsRootWebContents(content::WebContents* web_contents);
protected:
void WebContentsDestroyed() override;
};
} // namespace atom
#endif // ATOM_BROWSER_ROOT_WEB_CONTENTS_TRACKER_H_

View file

@ -198,6 +198,8 @@
'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',
'atom/browser/common_web_contents_delegate.cc',
@ -273,8 +275,6 @@
'atom/browser/relauncher.h',
'atom/browser/render_process_preferences.cc',
'atom/browser/render_process_preferences.h',
'atom/browser/root_web_contents_tracker.cc',
'atom/browser/root_web_contents_tracker.h',
'atom/browser/ui/accelerator_util.cc',
'atom/browser/ui/accelerator_util.h',
'atom/browser/ui/accelerator_util_mac.mm',