diff --git a/renderer/api/atom_renderer_bindings.cc b/renderer/api/atom_renderer_bindings.cc index 0957f7c1d24d..f9d3975490d6 100644 --- a/renderer/api/atom_renderer_bindings.cc +++ b/renderer/api/atom_renderer_bindings.cc @@ -4,9 +4,6 @@ #include "renderer/api/atom_renderer_bindings.h" -#include - -#include "base/logging.h" #include "common/v8/native_type_conversions.h" #include "content/public/renderer/render_view.h" #include "third_party/WebKit/public/web/WebFrame.h" @@ -14,9 +11,7 @@ #include "common/v8/node_common.h" -using content::RenderView; using content::V8ValueConverter; -using WebKit::WebFrame; namespace atom { @@ -32,14 +27,13 @@ v8::Handle GetProcessObject(v8::Handle context) { } // namespace -AtomRendererBindings::AtomRendererBindings(RenderView* render_view) - : render_view_(render_view) { +AtomRendererBindings::AtomRendererBindings() { } AtomRendererBindings::~AtomRendererBindings() { } -void AtomRendererBindings::BindToFrame(WebFrame* frame) { +void AtomRendererBindings::BindToFrame(WebKit::WebFrame* frame) { v8::HandleScope handle_scope(node_isolate); v8::Handle context = frame->mainWorldScriptContext(); @@ -47,19 +41,19 @@ void AtomRendererBindings::BindToFrame(WebFrame* frame) { return; v8::Context::Scope scope(context); - AtomBindings::BindTo(GetProcessObject(context)); } -void AtomRendererBindings::OnBrowserMessage(const string16& channel, +void AtomRendererBindings::OnBrowserMessage(content::RenderView* render_view, + const string16& channel, const base::ListValue& args) { - if (!render_view_->GetWebView()) + if (!render_view->GetWebView()) return; v8::HandleScope handle_scope(node_isolate); v8::Local context = - render_view_->GetWebView()->mainFrame()->mainWorldScriptContext(); + render_view->GetWebView()->mainFrame()->mainWorldScriptContext(); if (context.IsEmpty()) return; diff --git a/renderer/api/atom_renderer_bindings.h b/renderer/api/atom_renderer_bindings.h index 39a5be91968f..7eb43bf7adfc 100644 --- a/renderer/api/atom_renderer_bindings.h +++ b/renderer/api/atom_renderer_bindings.h @@ -25,19 +25,18 @@ namespace atom { class AtomRendererBindings : public AtomBindings { public: - explicit AtomRendererBindings(content::RenderView* render_view); + AtomRendererBindings(); virtual ~AtomRendererBindings(); // Call BindTo for process object of the frame. void BindToFrame(WebKit::WebFrame* frame); // Dispatch messages from browser. - void OnBrowserMessage(const string16& channel, + void OnBrowserMessage(content::RenderView* render_view, + const string16& channel, const base::ListValue& args); private: - content::RenderView* render_view_; - DISALLOW_COPY_AND_ASSIGN(AtomRendererBindings); }; diff --git a/renderer/atom_render_view_observer.cc b/renderer/atom_render_view_observer.cc index 673435e048e1..960be5c0207f 100644 --- a/renderer/atom_render_view_observer.cc +++ b/renderer/atom_render_view_observer.cc @@ -5,12 +5,11 @@ #include "renderer/atom_render_view_observer.h" #include "common/api/api_messages.h" -#include "common/node_bindings.h" #include "ipc/ipc_message_macros.h" #include "renderer/api/atom_renderer_bindings.h" #include "renderer/atom_renderer_client.h" -#include "third_party/WebKit/public/web/WebDocument.h" #include "third_party/WebKit/public/web/WebDraggableRegion.h" +#include "third_party/WebKit/public/web/WebDocument.h" #include "third_party/WebKit/public/web/WebFrame.h" #include "common/v8/node_common.h" @@ -23,36 +22,12 @@ AtomRenderViewObserver::AtomRenderViewObserver( content::RenderView* render_view, AtomRendererClient* renderer_client) : content::RenderViewObserver(render_view), - atom_bindings_(new AtomRendererBindings(render_view)), renderer_client_(renderer_client) { } AtomRenderViewObserver::~AtomRenderViewObserver() { } -void AtomRenderViewObserver::DidClearWindowObject(WebFrame* frame) { - // Get the context. - v8::HandleScope handle_scope(node_isolate); - v8::Handle context = frame->mainWorldScriptContext(); - if (context.IsEmpty()) - return; - - v8::Context::Scope scope(context); - - // Check the existance of process object to prevent duplicate initialization. - if (context->Global()->Has(v8::String::New("process"))) - return; - - // Give the node loop a run to make sure everything is ready. - renderer_client_->node_bindings()->RunMessageLoop(); - - // Setup node environment for each window. - renderer_client_->node_bindings()->CreateEnvironment(context); - - // Add atom-shell extended APIs. - atom_bindings()->BindToFrame(frame); -} - void AtomRenderViewObserver::DraggableRegionsChanged(WebKit::WebFrame* frame) { WebKit::WebVector webregions = frame->document().draggableRegions(); @@ -78,7 +53,8 @@ bool AtomRenderViewObserver::OnMessageReceived(const IPC::Message& message) { void AtomRenderViewObserver::OnBrowserMessage(const string16& channel, const base::ListValue& args) { - atom_bindings()->OnBrowserMessage(channel, args); + renderer_client_->atom_bindings()->OnBrowserMessage( + render_view(), channel, args); } } // namespace atom diff --git a/renderer/atom_render_view_observer.h b/renderer/atom_render_view_observer.h index 12c15fa63675..213bd384cd57 100644 --- a/renderer/atom_render_view_observer.h +++ b/renderer/atom_render_view_observer.h @@ -5,7 +5,6 @@ #ifndef ATOM_RENDERER_ATOM_RENDER_VIEW_OBSERVER_ #define ATOM_RENDERER_ATOM_RENDER_VIEW_OBSERVER_ -#include "base/memory/scoped_ptr.h" #include "content/public/renderer/render_view_observer.h" namespace base { @@ -14,21 +13,16 @@ class ListValue; namespace atom { -class AtomRendererBindings; class AtomRendererClient; -class AtomRenderViewObserver : content::RenderViewObserver { +class AtomRenderViewObserver : public content::RenderViewObserver { public: explicit AtomRenderViewObserver(content::RenderView* render_view, AtomRendererClient* renderer_client); - AtomRendererBindings* atom_bindings() const { return atom_bindings_.get(); } - protected: virtual ~AtomRenderViewObserver(); - virtual void DidClearWindowObject(WebKit::WebFrame*) OVERRIDE; - private: // content::RenderViewObserver implementation. virtual void DraggableRegionsChanged(WebKit::WebFrame* frame) OVERRIDE; @@ -37,8 +31,6 @@ class AtomRenderViewObserver : content::RenderViewObserver { void OnBrowserMessage(const string16& channel, const base::ListValue& args); - scoped_ptr atom_bindings_; - // Weak reference to renderer client. AtomRendererClient* renderer_client_; diff --git a/renderer/atom_renderer_client.cc b/renderer/atom_renderer_client.cc index 3574b02cacad..f42e31087bc4 100644 --- a/renderer/atom_renderer_client.cc +++ b/renderer/atom_renderer_client.cc @@ -5,6 +5,7 @@ #include "renderer/atom_renderer_client.h" #include "common/node_bindings.h" +#include "renderer/api/atom_renderer_bindings.h" #include "renderer/atom_render_view_observer.h" #include "common/v8/node_common.h" @@ -12,7 +13,8 @@ namespace atom { AtomRendererClient::AtomRendererClient() - : node_bindings_(NodeBindings::Create(false)) { + : node_bindings_(NodeBindings::Create(false)), + atom_bindings_(new AtomRendererBindings) { } AtomRendererClient::~AtomRendererClient() { @@ -35,4 +37,29 @@ void AtomRendererClient::RenderViewCreated(content::RenderView* render_view) { new AtomRenderViewObserver(render_view, this); } +void AtomRendererClient::DidCreateScriptContext(WebKit::WebFrame* frame, + v8::Handle context, + int extension_group, + int world_id) { + v8::Context::Scope scope(context); + + // Check the existance of process object to prevent duplicate initialization. + if (context->Global()->Has(v8::String::New("process"))) + return; + + // Give the node loop a run to make sure everything is ready. + node_bindings_->RunMessageLoop(); + + // Setup node environment for each window. + node_bindings_->CreateEnvironment(context); + + // Add atom-shell extended APIs. + atom_bindings_->BindToFrame(frame); +} + +void AtomRendererClient::WillReleaseScriptContext(WebKit::WebFrame* frame, + v8::Handle, + int world_id) { +} + } // namespace atom diff --git a/renderer/atom_renderer_client.h b/renderer/atom_renderer_client.h index 46c79e75d645..e66ea87000a4 100644 --- a/renderer/atom_renderer_client.h +++ b/renderer/atom_renderer_client.h @@ -2,13 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef ATOM_RENDERER_ATOM_RENDERER_CLIENT_ -#define ATOM_RENDERER_ATOM_RENDERER_CLIENT_ +#ifndef ATOM_RENDERER_ATOM_RENDERER_CLIENT_H_ +#define ATOM_RENDERER_ATOM_RENDERER_CLIENT_H_ #include "content/public/renderer/content_renderer_client.h" namespace atom { +class AtomRendererBindings; class NodeBindings; class AtomRendererClient : public content::ContentRendererClient { @@ -16,17 +17,25 @@ class AtomRendererClient : public content::ContentRendererClient { AtomRendererClient(); virtual ~AtomRendererClient(); - NodeBindings* node_bindings() const { return node_bindings_.get(); } + AtomRendererBindings* atom_bindings() const { return atom_bindings_.get(); } private: virtual void RenderThreadStarted() OVERRIDE; virtual void RenderViewCreated(content::RenderView*) OVERRIDE; + virtual void DidCreateScriptContext(WebKit::WebFrame* frame, + v8::Handle context, + int extension_group, + int world_id) OVERRIDE; + virtual void WillReleaseScriptContext(WebKit::WebFrame* frame, + v8::Handle, + int world_id) OVERRIDE; scoped_ptr node_bindings_; + scoped_ptr atom_bindings_; DISALLOW_COPY_AND_ASSIGN(AtomRendererClient); }; } // namespace atom -#endif // ATOM_RENDERER_ATOM_RENDERER_CLIENT_ +#endif // ATOM_RENDERER_ATOM_RENDERER_CLIENT_H_