diff --git a/atom/browser/atom_browser_client.cc b/atom/browser/atom_browser_client.cc index 9a3ad00fa4cd..6b3419272293 100644 --- a/atom/browser/atom_browser_client.cc +++ b/atom/browser/atom_browser_client.cc @@ -20,6 +20,7 @@ #include "atom/common/options_switches.h" #include "base/command_line.h" #include "base/files/file_util.h" +#include "base/stl_util.h" #include "base/strings/string_util.h" #include "base/strings/string_number_conversions.h" #include "chrome/browser/printing/printing_message_filter.h" @@ -62,20 +63,14 @@ enum ProcessOwner { ProcessOwner GetProcessOwner(int process_id, NativeWindow** window, WebViewManager::WebViewInfo* info) { - content::RenderViewHost* rvh = content::RenderViewHost::FromID( - process_id, kDefaultRoutingID); - if (!rvh) - return OWNER_NONE; - auto web_contents = content::WebContents::FromRenderViewHost(rvh); + content::WebContents* web_contents = content::WebContents::FromRenderViewHost( + content::RenderViewHost::FromID(process_id, kDefaultRoutingID)); if (!web_contents) return OWNER_NONE; // First search for NativeWindow. - for (auto native_window : *WindowList::GetInstance()) - if (web_contents == native_window->web_contents()) { - *window = native_window; - return OWNER_NATIVE_WINDOW; - } + if ((*window = NativeWindow::FromWebContents(web_contents))) + return OWNER_NATIVE_WINDOW; // Then search for guest WebContents. if (WebViewManager::GetInfoForWebContents(web_contents, info)) @@ -189,6 +184,13 @@ void AtomBrowserClient::OverrideSiteInstanceForNavigation( return; *new_instance = content::SiteInstance::CreateForURL(browser_context, url); + + // Remember the original renderer process of the pending renderer process. + auto current_process = current_instance->GetProcess(); + auto pending_process = (*new_instance)->GetProcess(); + pending_processes_[pending_process->GetID()] = current_process->GetID(); + // Clear the entry in map when process ends. + current_process->AddObserver(this); } void AtomBrowserClient::AppendExtraCommandLineSwitches( @@ -212,6 +214,10 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches( } #endif + // If the process is a pending process, we should use the old one. + if (ContainsKey(pending_processes_, process_id)) + process_id = pending_processes_[process_id]; + NativeWindow* window; WebViewManager::WebViewInfo info; ProcessOwner owner = GetProcessOwner(process_id, &window, &info); @@ -268,4 +274,15 @@ brightray::BrowserMainParts* AtomBrowserClient::OverrideCreateBrowserMainParts( return new AtomBrowserMainParts; } +void AtomBrowserClient::RenderProcessHostDestroyed( + content::RenderProcessHost* host) { + int process_id = host->GetID(); + for (const auto& entry : pending_processes_) { + if (entry.first == process_id || entry.second == process_id) { + pending_processes_.erase(entry.first); + break; + } + } +} + } // namespace atom diff --git a/atom/browser/atom_browser_client.h b/atom/browser/atom_browser_client.h index f403a519d2f0..a0217efede9f 100644 --- a/atom/browser/atom_browser_client.h +++ b/atom/browser/atom_browser_client.h @@ -5,10 +5,12 @@ #ifndef ATOM_BROWSER_ATOM_BROWSER_CLIENT_H_ #define ATOM_BROWSER_ATOM_BROWSER_CLIENT_H_ +#include #include #include #include "brightray/browser/browser_client.h" +#include "content/public/browser/render_process_host_observer.h" namespace content { class QuotaPermissionContext; @@ -21,7 +23,8 @@ class SSLCertRequestInfo; namespace atom { -class AtomBrowserClient : public brightray::BrowserClient { +class AtomBrowserClient : public brightray::BrowserClient, + public content::RenderProcessHostObserver { public: AtomBrowserClient(); virtual ~AtomBrowserClient(); @@ -54,10 +57,17 @@ class AtomBrowserClient : public brightray::BrowserClient { net::SSLCertRequestInfo* cert_request_info, scoped_ptr delegate) override; - private: + // brightray::BrowserClient: brightray::BrowserMainParts* OverrideCreateBrowserMainParts( const content::MainFunctionParams&) override; + // content::RenderProcessHostObserver: + void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; + + private: + // pending_render_process => current_render_process. + std::map pending_processes_; + DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient); };