From 7c69c2846b4e5ce2e0e7d533069b82dd4c49dd66 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 20 May 2015 10:27:16 +0800 Subject: [PATCH] Simplify how we find NativeWindow from WebContents --- atom/browser/api/atom_api_web_contents.cc | 4 +--- atom/browser/atom_browser_client.cc | 3 +-- atom/browser/native_window.cc | 13 ++++--------- atom/browser/native_window.h | 4 ++-- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index e228a064f413..6da2fbf86f46 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -51,9 +51,7 @@ v8::Persistent template_; NativeWindow* GetWindowFromGuest(const content::WebContents* guest) { WebViewManager::WebViewInfo info; if (WebViewManager::GetInfoForProcess(guest->GetRenderProcessHost(), &info)) - return NativeWindow::FromRenderView( - info.embedder->GetRenderProcessHost()->GetID(), - info.embedder->GetRoutingID()); + return NativeWindow::FromWebContents(info.embedder); else return nullptr; } diff --git a/atom/browser/atom_browser_client.cc b/atom/browser/atom_browser_client.cc index fbc09d9fe15f..23674035e1d3 100644 --- a/atom/browser/atom_browser_client.cc +++ b/atom/browser/atom_browser_client.cc @@ -124,8 +124,7 @@ void AtomBrowserClient::OverrideWebkitPrefs( return; } - NativeWindow* window = NativeWindow::FromRenderView( - process->GetID(), render_view_host->GetRoutingID()); + NativeWindow* window = NativeWindow::FromWebContents(web_contents); if (window) window->OverrideWebkitPrefs(prefs); } diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 5bef1acfb872..8bc2c4616fe8 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -169,18 +169,13 @@ NativeWindow* NativeWindow::Create(const mate::Dictionary& options) { } // static -NativeWindow* NativeWindow::FromRenderView(int process_id, int routing_id) { - // Stupid iterating. +NativeWindow* NativeWindow::FromWebContents( + content::WebContents* web_contents) { WindowList& window_list = *WindowList::GetInstance(); - for (auto w = window_list.begin(); w != window_list.end(); ++w) { - auto& window = *w; - content::WebContents* web_contents = window->GetWebContents(); - int window_process_id = web_contents->GetRenderProcessHost()->GetID(); - int window_routing_id = web_contents->GetRoutingID(); - if (window_routing_id == routing_id && window_process_id == process_id) + for (NativeWindow* window : window_list) { + if (window->GetWebContents() == web_contents) return window; } - return nullptr; } diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 32c3493cdbf8..39c58625ad2d 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -91,8 +91,8 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, // managing the window's live. static NativeWindow* Create(const mate::Dictionary& options); - // Find a window from its process id and routing id. - static NativeWindow* FromRenderView(int process_id, int routing_id); + // Find a window from its WebContents + static NativeWindow* FromWebContents(content::WebContents* web_contents); void InitFromOptions(const mate::Dictionary& options);