Renderer can now use process.send to send messages to browser.

This commit is contained in:
Cheng Zhao 2013-04-22 21:32:48 +08:00
parent a7c3bdbf5d
commit 66a0abe799
17 changed files with 167 additions and 54 deletions

View file

@ -5,6 +5,7 @@
#include "browser/api/atom_browser_bindings.h"
#include "base/logging.h"
#include "base/values.h"
#include "content/public/browser/browser_thread.h"
#include "vendor/node/src/node.h"
#include "vendor/node/src/node_internals.h"
@ -35,4 +36,10 @@ void AtomBrowserBindings::AfterLoad() {
DCHECK(!browser_main_parts_.IsEmpty());
}
void AtomBrowserBindings::OnRendererMessage(
int routing_id, const base::ListValue& args) {
LOG(ERROR) << "OnRendererMessage routing_id:" << routing_id
<< " args:" << args;
}
} // namespace atom

View file

@ -7,6 +7,10 @@
#include "common/api/atom_bindings.h"
namespace base {
class ListValue;
}
namespace atom {
class AtomBrowserBindings : public AtomBindings {
@ -17,6 +21,9 @@ class AtomBrowserBindings : public AtomBindings {
// Called when the node.js main script has been loaded.
virtual void AfterLoad();
// Called when received a message from renderer.
void OnRendererMessage(int routing_id, const base::ListValue& args);
// The require('atom').browserMainParts object.
v8::Handle<v8::Object> browser_main_parts() {
return browser_main_parts_;

View file

@ -5,17 +5,12 @@
#include "browser/atom_browser_context.h"
#include "browser/api/atom_api_objects_registry.h"
#include "browser/atom_browser_main_parts.h"
namespace atom {
// static
AtomBrowserContext* AtomBrowserContext::self_;
AtomBrowserContext::AtomBrowserContext()
: objects_registry_(new api::ObjectsRegistry) {
DCHECK(!self_);
self_ = this;
}
AtomBrowserContext::~AtomBrowserContext() {
@ -23,9 +18,8 @@ AtomBrowserContext::~AtomBrowserContext() {
// static
AtomBrowserContext* AtomBrowserContext::Get() {
DCHECK(self_);
return self_;
return static_cast<AtomBrowserContext*>(
AtomBrowserMainParts::Get()->browser_context());
}
} // namespace atom

View file

@ -19,7 +19,6 @@ class AtomBrowserContext : public brightray::BrowserContext {
AtomBrowserContext();
virtual ~AtomBrowserContext();
// We assume there is only one BrowserContext per browser process.
static AtomBrowserContext* Get();
api::ObjectsRegistry* objects_registry() const {
@ -27,8 +26,6 @@ class AtomBrowserContext : public brightray::BrowserContext {
}
private:
static AtomBrowserContext* self_;
scoped_ptr<api::ObjectsRegistry> objects_registry_;
DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext);

View file

@ -5,6 +5,7 @@
#include "browser/atom_browser_main_parts.h"
#include "browser/api/atom_browser_bindings.h"
#include "browser/atom_browser_client.h"
#include "browser/atom_browser_context.h"
#include "browser/native_window.h"
#include "common/node_bindings.h"
@ -13,14 +14,25 @@
namespace atom {
// static
AtomBrowserMainParts* AtomBrowserMainParts::self_ = NULL;
AtomBrowserMainParts::AtomBrowserMainParts()
: atom_bindings_(new AtomBrowserBindings),
node_bindings_(NodeBindings::Create(true)) {
DCHECK(!self_) << "Cannot have two AtomBrowserMainParts";
self_ = this;
}
AtomBrowserMainParts::~AtomBrowserMainParts() {
}
// static
AtomBrowserMainParts* AtomBrowserMainParts::Get() {
DCHECK(self_);
return self_;
}
brightray::BrowserContext* AtomBrowserMainParts::CreateBrowserContext() {
return new AtomBrowserContext();
}

View file

@ -17,6 +17,10 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
AtomBrowserMainParts();
virtual ~AtomBrowserMainParts();
static AtomBrowserMainParts* Get();
AtomBrowserBindings* atom_bindings() { return atom_bindings_.get(); }
protected:
// Implementations of brightray::BrowserMainParts.
virtual brightray::BrowserContext* CreateBrowserContext() OVERRIDE;
@ -30,6 +34,8 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
scoped_ptr<AtomBrowserBindings> atom_bindings_;
scoped_ptr<NodeBindings> node_bindings_;
static AtomBrowserMainParts* self_;
DISALLOW_COPY_AND_ASSIGN(AtomBrowserMainParts);
};

View file

@ -8,15 +8,18 @@
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "brightray/browser/browser_context.h"
#include "brightray/browser/inspectable_web_contents.h"
#include "brightray/browser/inspectable_web_contents_view.h"
#include "browser/api/atom_browser_bindings.h"
#include "browser/atom_browser_context.h"
#include "browser/atom_browser_main_parts.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "common/api/api_messages.h"
#include "common/options_switches.h"
#include "ipc/ipc_message_macros.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
@ -27,13 +30,11 @@ namespace atom {
NativeWindow::NativeWindow(content::WebContents* web_contents,
base::DictionaryValue* options)
: inspectable_web_contents_(
: content::WebContentsObserver(web_contents),
inspectable_web_contents_(
brightray::InspectableWebContents::Create(web_contents)) {
web_contents->SetDelegate(this);
// Add window as an observer of the web contents.
content::WebContentsObserver::Observe(web_contents);
// Get notified of title updated message.
registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED,
content::Source<content::WebContents>(web_contents));
@ -129,7 +130,13 @@ void NativeWindow::WebContentsCreated(
}
bool NativeWindow::OnMessageReceived(const IPC::Message& message) {
return false;
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(NativeWindow, message)
IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void NativeWindow::Observe(int type,
@ -152,4 +159,9 @@ void NativeWindow::Observe(int type,
}
}
void NativeWindow::OnRendererMessage(const base::ListValue& args) {
AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessage(
GetWebContents()->GetRoutingID(), args);
}
} // namespace atom

View file

@ -19,6 +19,7 @@
namespace base {
class DictionaryValue;
class ListValue;
}
namespace brightray {
@ -119,6 +120,8 @@ class NativeWindow : public content::WebContentsDelegate,
const content::NotificationDetails& details) OVERRIDE;
private:
void OnRendererMessage(const base::ListValue& args);
// Notification manager.
content::NotificationRegistrar registrar_;