]> khamidou — GitHub mirrors - electron.git/commitdiff
Enable getting window from routing id and process id.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 30 Apr 2013 12:32:23 +0000 (20:32 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 30 Apr 2013 12:32:23 +0000 (20:32 +0800)
browser/native_window.cc
browser/native_window.h

index 092d3a9085e91f35e0162564ed8fc79b0010a60b..69d0d0f85a74ca28292e17af789beafe99a86967 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "browser/native_window.h"
 
+#include <algorithm>
 #include <string>
 
 #include "base/utf_string_conversions.h"
@@ -29,6 +30,9 @@ using content::NavigationEntry;
 
 namespace atom {
 
+// static
+std::vector<NativeWindow*> 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<content::WebContents>(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;
index b42f26d4fc9853d9afca02f81d1f6587ee5ce100..440ec8076fd9b395d3ddaba2cfaa924ca684407c 100644 (file)
@@ -6,6 +6,7 @@
 #define ATOM_BROWSER_NATIVE_WINDOW_H_
 
 #include <iosfwd>
+#include <vector>
 
 #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<NativeWindowObserver> observers_;
 
+  // Stores all windows.
+  static std::vector<NativeWindow*> windows_;
+
   scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
 
   DISALLOW_COPY_AND_ASSIGN(NativeWindow);