Enable getting window from routing id and process id.

This commit is contained in:
Cheng Zhao 2013-04-30 20:32:23 +08:00
parent de94e4f37e
commit 74c519ac3f
2 changed files with 31 additions and 0 deletions

View 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;

View 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);