From 74c519ac3f6054d0ab04f347c06789674574ff26 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 30 Apr 2013 20:32:23 +0800 Subject: [PATCH] Enable getting window from routing id and process id. --- browser/native_window.cc | 23 +++++++++++++++++++++++ browser/native_window.h | 8 ++++++++ 2 files changed, 31 insertions(+) diff --git a/browser/native_window.cc b/browser/native_window.cc index 092d3a9085e9..69d0d0f85a74 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -4,6 +4,7 @@ #include "browser/native_window.h" +#include #include #include "base/utf_string_conversions.h" @@ -29,6 +30,9 @@ using content::NavigationEntry; namespace atom { +// static +std::vector NativeWindow::windows_; + NativeWindow::NativeWindow(content::WebContents* web_contents, base::DictionaryValue* options) : content::WebContentsObserver(web_contents), @@ -36,12 +40,16 @@ NativeWindow::NativeWindow(content::WebContents* web_contents, brightray::InspectableWebContents::Create(web_contents)) { web_contents->SetDelegate(this); + windows_.push_back(this); + // Get notified of title updated message. registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED, content::Source(web_contents)); } NativeWindow::~NativeWindow() { + windows_.erase(std::remove(windows_.begin(), windows_.end(), this), + windows_.end()); } // static @@ -50,6 +58,21 @@ NativeWindow* NativeWindow::Create(base::DictionaryValue* options) { return Create(content::WebContents::Create(create_params), options); } +// static +NativeWindow* NativeWindow::FromProcessIDAndRoutingID(int process_id, + int routing_id) { + // Stupid iterating. + for (auto window : windows_) { + 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) + return window; + } + + return NULL; +} + void NativeWindow::InitFromOptions(base::DictionaryValue* options) { // Setup window from options. int x, y; diff --git a/browser/native_window.h b/browser/native_window.h index b42f26d4fc98..440ec8076fd9 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -6,6 +6,7 @@ #define ATOM_BROWSER_NATIVE_WINDOW_H_ #include +#include #include "base/basictypes.h" #include "base/compiler_specific.h" @@ -52,6 +53,10 @@ class NativeWindow : public content::WebContentsDelegate, // Create window with new WebContents. static NativeWindow* Create(base::DictionaryValue* options); + // Find a window from its process id and routing id. + static NativeWindow* FromProcessIDAndRoutingID(int process_id, + int routing_id); + void InitFromOptions(base::DictionaryValue* options); virtual void Close() = 0; @@ -133,6 +138,9 @@ class NativeWindow : public content::WebContentsDelegate, // Observers of this window. ObserverList observers_; + // Stores all windows. + static std::vector windows_; + scoped_ptr inspectable_web_contents_; DISALLOW_COPY_AND_ASSIGN(NativeWindow);