Improve RootWebContentsTracker

This commit is contained in:
Ryohei Ikegami 2017-04-06 11:29:01 +09:00
parent b683584859
commit 526086d900
6 changed files with 55 additions and 28 deletions

View file

@ -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<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

@ -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

View file

@ -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<content::WebContents*> root_web_contents_;
DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient);
};

View file

@ -0,0 +1,28 @@
#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

@ -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_

View file

@ -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',