From 526086d900d8c26e1fc184ea7c348e9ff80139e1 Mon Sep 17 00:00:00 2001 From: Ryohei Ikegami Date: Thu, 6 Apr 2017 11:29:01 +0900 Subject: [PATCH] Improve RootWebContentsTracker --- atom/browser/api/atom_api_window.cc | 2 ++ atom/browser/atom_browser_client.cc | 17 ++------------ atom/browser/atom_browser_client.h | 13 ----------- atom/browser/root_web_contents_tracker.cc | 28 +++++++++++++++++++++++ atom/browser/root_web_contents_tracker.h | 21 +++++++++++++++++ filenames.gypi | 2 ++ 6 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 atom/browser/root_web_contents_tracker.cc create mode 100644 atom/browser/root_web_contents_tracker.h diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 6748c9c0dd1d..2a77a46a3ae4 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -9,6 +9,7 @@ #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" @@ -100,6 +101,7 @@ Window::Window(v8::Isolate* isolate, v8::Local 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); diff --git a/atom/browser/atom_browser_client.cc b/atom/browser/atom_browser_client.cc index 3a9bef9f87de..0a480dcd80aa 100644 --- a/atom/browser/atom_browser_client.cc +++ b/atom/browser/atom_browser_client.cc @@ -16,6 +16,7 @@ #include "atom/browser/atom_resource_dispatcher_host_delegate.h" #include "atom/browser/atom_speech_recognition_manager_delegate.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" @@ -104,7 +105,7 @@ bool AtomBrowserClient::ShouldCreateNewSiteInstance( } auto web_contents = content::WebContents::FromRenderFrameHost(render_frame_host); - if (root_web_contents_.find(web_contents) != root_web_contents_.end()) { + if (RootWebContentsTracker::IsRootWebContents(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 @@ -162,8 +163,6 @@ void AtomBrowserClient::RenderProcessWillLaunch( AddProcessPreferences(host->GetID(), process_prefs); // ensure the ProcessPreferences is removed later host->AddObserver(this); - - new RootWebContentsTracker(web_contents, this); } content::SpeechRecognitionManagerDelegate* @@ -407,16 +406,4 @@ void AtomBrowserClient::RenderProcessHostDestroyed( RemoveProcessPreferences(process_id); } -AtomBrowserClient::RootWebContentsTracker::RootWebContentsTracker( - content::WebContents* web_contents, - AtomBrowserClient* client) - : content::WebContentsObserver(web_contents), client_(client) { - client->root_web_contents_.insert(web_contents); -} - -void AtomBrowserClient::RootWebContentsTracker::WebContentsDestroyed() { - client_->root_web_contents_.erase(web_contents()); - delete this; -} - } // namespace atom diff --git a/atom/browser/atom_browser_client.h b/atom/browser/atom_browser_client.h index c1a229edae84..cd73b559e608 100644 --- a/atom/browser/atom_browser_client.h +++ b/atom/browser/atom_browser_client.h @@ -12,7 +12,6 @@ #include "brightray/browser/browser_client.h" #include "content/public/browser/render_process_host_observer.h" -#include "content/public/browser/web_contents_observer.h" namespace content { class QuotaPermissionContext; @@ -137,18 +136,6 @@ class AtomBrowserClient : public brightray::BrowserClient, Delegate* delegate_; - class RootWebContentsTracker : public content::WebContentsObserver { - public: - RootWebContentsTracker(content::WebContents* web_contents, - AtomBrowserClient* client); - void WebContentsDestroyed() override; - - private: - AtomBrowserClient* client_; - }; - - std::set root_web_contents_; - DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient); }; diff --git a/atom/browser/root_web_contents_tracker.cc b/atom/browser/root_web_contents_tracker.cc new file mode 100644 index 000000000000..dfdc8a1ffbd8 --- /dev/null +++ b/atom/browser/root_web_contents_tracker.cc @@ -0,0 +1,28 @@ +#include "root_web_contents_tracker.h" +#include + +namespace atom { + +namespace { + +std::unordered_set 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 diff --git a/atom/browser/root_web_contents_tracker.h b/atom/browser/root_web_contents_tracker.h new file mode 100644 index 000000000000..4a6ae0776d83 --- /dev/null +++ b/atom/browser/root_web_contents_tracker.h @@ -0,0 +1,21 @@ +#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_ diff --git a/filenames.gypi b/filenames.gypi index 935d7033153a..a45662beafda 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -273,6 +273,8 @@ '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',