Add initial support for loading into isolated world

This commit is contained in:
Kevin Sawicki 2016-12-08 12:55:00 -08:00
parent 4ebe54043a
commit 736befe90f
5 changed files with 104 additions and 1 deletions

View file

@ -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<v8::FunctionTemplate> AtomIsolatedWorld::GetNativeFunctionTemplate(
v8::Isolate* isolate,
v8::Local<v8::String> name) {
if (name->Equals(v8::String::NewFromUtf8(isolate, "SetupNode")))
return v8::FunctionTemplate::New(isolate, SetupNode);
return v8::Local<v8::FunctionTemplate>();
}
// static
void AtomIsolatedWorld::SetupNode(
const v8::FunctionCallbackInfo<v8::Value>& 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

View file

@ -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<v8::FunctionTemplate> GetNativeFunctionTemplate(
v8::Isolate* isolate,
v8::Local<v8::String> name) override;
static AtomIsolatedWorld* Create(NodeBindings* node_bindings);
private:
static void SetupNode(const v8::FunctionCallbackInfo<v8::Value>& args);
private:
static NodeBindings* node_bindings_;
static node::Environment* env_;
};
} // namespace atom
#endif // ATOM_RENDERER_ATOM_ISOLATED_WORLD_H_

View file

@ -14,6 +14,7 @@
#include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/node_bindings.h" #include "atom/common/node_bindings.h"
#include "atom/common/options_switches.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/atom_render_view_observer.h"
#include "atom/renderer/content_settings_observer.h" #include "atom/renderer/content_settings_observer.h"
#include "atom/renderer/guest_view_container.h" #include "atom/renderer/guest_view_container.h"
@ -147,6 +148,8 @@ void AtomRendererClient::RenderThreadStarted() {
blink::WebCustomElement::addEmbedderCustomElementName("webview"); blink::WebCustomElement::addEmbedderCustomElementName("webview");
blink::WebCustomElement::addEmbedderCustomElementName("browserplugin"); blink::WebCustomElement::addEmbedderCustomElementName("browserplugin");
isolated_world_.reset(AtomIsolatedWorld::Create(node_bindings_.get()));
OverrideNodeArrayBuffer(); OverrideNodeArrayBuffer();
preferences_manager_.reset(new PreferencesManager); preferences_manager_.reset(new PreferencesManager);
@ -275,7 +278,7 @@ void AtomRendererClient::DidCreateScriptContext(
} }
// Setup node environment for each window. // 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. // Add Electron extended APIs.
atom_bindings_->BindTo(env->isolate(), env->process_object()); atom_bindings_->BindTo(env->isolate(), env->process_object());

View file

@ -13,6 +13,7 @@
namespace atom { namespace atom {
class AtomBindings; class AtomBindings;
class AtomIsolatedWorld;
class PreferencesManager; class PreferencesManager;
class NodeBindings; class NodeBindings;
@ -64,6 +65,7 @@ class AtomRendererClient : public content::ContentRendererClient {
std::unique_ptr<NodeBindings> node_bindings_; std::unique_ptr<NodeBindings> node_bindings_;
std::unique_ptr<AtomBindings> atom_bindings_; std::unique_ptr<AtomBindings> atom_bindings_;
std::unique_ptr<PreferencesManager> preferences_manager_; std::unique_ptr<PreferencesManager> preferences_manager_;
std::unique_ptr<AtomIsolatedWorld> isolated_world_;
DISALLOW_COPY_AND_ASSIGN(AtomRendererClient); DISALLOW_COPY_AND_ASSIGN(AtomRendererClient);
}; };

View file

@ -453,6 +453,8 @@
'atom/renderer/api/atom_api_spell_check_client.h', 'atom/renderer/api/atom_api_spell_check_client.h',
'atom/renderer/api/atom_api_web_frame.cc', 'atom/renderer/api/atom_api_web_frame.cc',
'atom/renderer/api/atom_api_web_frame.h', '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.cc',
'atom/renderer/atom_render_view_observer.h', 'atom/renderer/atom_render_view_observer.h',
'atom/renderer/atom_renderer_client.cc', 'atom/renderer/atom_renderer_client.cc',