diff --git a/atom/renderer/atom_isolated_world.cc b/atom/renderer/atom_isolated_world.cc new file mode 100644 index 000000000000..a0754627e32e --- /dev/null +++ b/atom/renderer/atom_isolated_world.cc @@ -0,0 +1,61 @@ +// Copyright (c) 2016 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/renderer/atom_isolated_world.h" + +#include "third_party/WebKit/public/web/WebLocalFrame.h" +#include "third_party/WebKit/public/web/WebScriptSource.h" + +namespace atom { + +NodeBindings* AtomIsolatedWorld::node_bindings_ = nullptr; +node::Environment* AtomIsolatedWorld::env_ = nullptr; + +AtomIsolatedWorld::AtomIsolatedWorld(NodeBindings* node_bindings) : + v8::Extension("ElectronIsolatedWorldExtension", + "native function SetupNode();") { + node_bindings_ = node_bindings; + env_ = nullptr; +} + +AtomIsolatedWorld::~AtomIsolatedWorld() { + node_bindings_ = nullptr; + env_ = nullptr; +} + +node::Environment* AtomIsolatedWorld::CreateEnvironment( + content::RenderFrame* frame) { + blink::WebScriptSource source("SetupNode()"); + frame->GetWebFrame()->executeScriptInIsolatedWorld( + 1, + &source, + 1, + 1, + nullptr); + return env_; +} + +v8::Local AtomIsolatedWorld::GetNativeFunctionTemplate( + v8::Isolate* isolate, + v8::Local name) { + if (name->Equals(v8::String::NewFromUtf8(isolate, "SetupNode"))) + return v8::FunctionTemplate::New(isolate, SetupNode); + return v8::Local(); +} + +// static +void AtomIsolatedWorld::SetupNode( + const v8::FunctionCallbackInfo& args) { + env_ = node_bindings_->CreateEnvironment( + args.GetIsolate()->GetCurrentContext()); +} + +// static +AtomIsolatedWorld* AtomIsolatedWorld::Create(NodeBindings* node_bindings) { + AtomIsolatedWorld* world = new AtomIsolatedWorld(node_bindings); + content::RenderThread::Get()->RegisterExtension(world); + return world; +} + +} // namespace atom diff --git a/atom/renderer/atom_isolated_world.h b/atom/renderer/atom_isolated_world.h new file mode 100644 index 000000000000..e6b402808725 --- /dev/null +++ b/atom/renderer/atom_isolated_world.h @@ -0,0 +1,35 @@ +// Copyright (c) 2016 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_RENDERER_ATOM_ISOLATED_WORLD_H_ +#define ATOM_RENDERER_ATOM_ISOLATED_WORLD_H_ + +#include "atom/common/node_bindings.h" +#include "content/public/renderer/render_frame.h" +#include "content/public/renderer/render_thread.h" + +namespace atom { + +class AtomIsolatedWorld : public v8::Extension { + public: + explicit AtomIsolatedWorld(NodeBindings* node_bindings); + ~AtomIsolatedWorld() override; + node::Environment* CreateEnvironment(content::RenderFrame* frame); + v8::Local GetNativeFunctionTemplate( + v8::Isolate* isolate, + v8::Local name) override; + + static AtomIsolatedWorld* Create(NodeBindings* node_bindings); + + private: + static void SetupNode(const v8::FunctionCallbackInfo& args); + + private: + static NodeBindings* node_bindings_; + static node::Environment* env_; +}; + +} // namespace atom + +#endif // ATOM_RENDERER_ATOM_ISOLATED_WORLD_H_ diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index 5f2aec74e353..1f7de1686957 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -14,6 +14,7 @@ #include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/node_bindings.h" #include "atom/common/options_switches.h" +#include "atom/renderer/atom_isolated_world.h" #include "atom/renderer/atom_render_view_observer.h" #include "atom/renderer/content_settings_observer.h" #include "atom/renderer/guest_view_container.h" @@ -147,6 +148,8 @@ void AtomRendererClient::RenderThreadStarted() { blink::WebCustomElement::addEmbedderCustomElementName("webview"); blink::WebCustomElement::addEmbedderCustomElementName("browserplugin"); + isolated_world_.reset(AtomIsolatedWorld::Create(node_bindings_.get())); + OverrideNodeArrayBuffer(); preferences_manager_.reset(new PreferencesManager); @@ -275,7 +278,7 @@ void AtomRendererClient::DidCreateScriptContext( } // Setup node environment for each window. - node::Environment* env = node_bindings_->CreateEnvironment(context); + node::Environment* env = isolated_world_->CreateEnvironment(render_frame); // Add Electron extended APIs. atom_bindings_->BindTo(env->isolate(), env->process_object()); diff --git a/atom/renderer/atom_renderer_client.h b/atom/renderer/atom_renderer_client.h index 5419692d2aeb..9f47678fec44 100644 --- a/atom/renderer/atom_renderer_client.h +++ b/atom/renderer/atom_renderer_client.h @@ -13,6 +13,7 @@ namespace atom { class AtomBindings; +class AtomIsolatedWorld; class PreferencesManager; class NodeBindings; @@ -64,6 +65,7 @@ class AtomRendererClient : public content::ContentRendererClient { std::unique_ptr node_bindings_; std::unique_ptr atom_bindings_; std::unique_ptr preferences_manager_; + std::unique_ptr isolated_world_; DISALLOW_COPY_AND_ASSIGN(AtomRendererClient); }; diff --git a/filenames.gypi b/filenames.gypi index b7a53a484057..e1310e2444e7 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -453,6 +453,8 @@ 'atom/renderer/api/atom_api_spell_check_client.h', 'atom/renderer/api/atom_api_web_frame.cc', 'atom/renderer/api/atom_api_web_frame.h', + 'atom/renderer/atom_isolated_world.cc', + 'atom/renderer/atom_isolated_world.h', 'atom/renderer/atom_render_view_observer.cc', 'atom/renderer/atom_render_view_observer.h', 'atom/renderer/atom_renderer_client.cc',