Update guest process id when navigating
authorCheng Zhao <zcbenz@gmail.com>
Sun, 26 Apr 2015 07:30:31 +0000 (15:30 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 27 Apr 2015 07:11:58 +0000 (15:11 +0800)
atom/browser/atom_browser_client.cc
atom/browser/web_view_manager.cc
atom/browser/web_view_manager.h

index c3bcb94..8f1bb41 100644 (file)
@@ -151,11 +151,21 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(
   // uses the old going-to-be-swapped render process, then we try to find the
   // window from the swapped render process.
   if (!window && dying_render_process_) {
-    child_process_id = dying_render_process_->GetID();
+    int dying_process_id = dying_render_process_->GetID();
     WindowList::const_iterator iter = std::find_if(
-        list->begin(), list->end(), FindByProcessId(child_process_id));
-    if (iter != list->end())
+        list->begin(), list->end(), FindByProcessId(dying_process_id));
+    if (iter != list->end()) {
       window = *iter;
+      child_process_id = dying_process_id;
+    } else {
+      // It appears that the dying process doesn't belong to a BrowserWindow,
+      // then it must be a guest process, we should update its process ID in the
+      // WebViewManager here.
+      auto child_process = content::RenderProcessHost::FromID(child_process_id);
+      // Update the process ID in webview guests.
+      WebViewManager::UpdateGuestProcessID(dying_render_process_,
+                                           child_process);
+    }
   }
 
   if (window) {
index 91124f9..9de3088 100644 (file)
 
 namespace atom {
 
-// static
-bool WebViewManager::GetInfoForProcess(content::RenderProcessHost* process,
-                                       WebViewInfo* info) {
+namespace {
+
+WebViewManager* GetManagerFromProcess(content::RenderProcessHost* process) {
   if (!process)
-    return false;
+    return nullptr;
   auto context = process->GetBrowserContext();
   if (!context)
-    return false;
-  auto manager = context->GetGuestManager();
+    return nullptr;
+  return static_cast<WebViewManager*>(context->GetGuestManager());
+}
+
+}  // namespace
+
+// static
+bool WebViewManager::GetInfoForProcess(content::RenderProcessHost* process,
+                                       WebViewInfo* info) {
+  auto manager = GetManagerFromProcess(process);
   if (!manager)
     return false;
-  return static_cast<WebViewManager*>(manager)->GetInfo(process->GetID(), info);
+  return manager->GetInfo(process->GetID(), info);
+}
+
+// static
+void WebViewManager::UpdateGuestProcessID(
+    content::RenderProcessHost* old_process,
+    content::RenderProcessHost* new_process) {
+  auto manager = GetManagerFromProcess(old_process);
+  if (manager) {
+    base::AutoLock auto_lock(manager->lock_);
+    int old_id = old_process->GetID();
+    int new_id = new_process->GetID();
+    manager->webview_info_map_[new_id] = manager->webview_info_map_[old_id];
+    manager->webview_info_map_.erase(old_id);
+  }
 }
 
 WebViewManager::WebViewManager(content::BrowserContext* context) {
index 87f3c56..2327b2e 100644 (file)
@@ -34,6 +34,10 @@ class WebViewManager : public content::BrowserPluginGuestManager {
   static bool GetInfoForProcess(content::RenderProcessHost* process,
                                 WebViewInfo* info);
 
+  // Updates the guest process ID.
+  static void UpdateGuestProcessID(content::RenderProcessHost* old_process,
+                                   content::RenderProcessHost* new_process);
+
   explicit WebViewManager(content::BrowserContext* context);
   virtual ~WebViewManager();