Add initial support for loading into isolated world
authorKevin Sawicki <kevinsawicki@gmail.com>
Thu, 8 Dec 2016 20:55:00 +0000 (12:55 -0800)
committerKevin Sawicki <kevinsawicki@gmail.com>
Mon, 16 Jan 2017 20:38:15 +0000 (12:38 -0800)
atom/renderer/atom_isolated_world.cc [new file with mode: 0644]
atom/renderer/atom_isolated_world.h [new file with mode: 0644]
atom/renderer/atom_renderer_client.cc
atom/renderer/atom_renderer_client.h
filenames.gypi

diff --git a/atom/renderer/atom_isolated_world.cc b/atom/renderer/atom_isolated_world.cc
new file mode 100644 (file)
index 0000000..a075462
--- /dev/null
@@ -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
diff --git a/atom/renderer/atom_isolated_world.h b/atom/renderer/atom_isolated_world.h
new file mode 100644 (file)
index 0000000..e6b4028
--- /dev/null
@@ -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_
index 5f2aec7..1f7de16 100644 (file)
@@ -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());
index 5419692..9f47678 100644 (file)
@@ -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<NodeBindings> node_bindings_;
   std::unique_ptr<AtomBindings> atom_bindings_;
   std::unique_ptr<PreferencesManager> preferences_manager_;
+  std::unique_ptr<AtomIsolatedWorld> isolated_world_;
 
   DISALLOW_COPY_AND_ASSIGN(AtomRendererClient);
 };
index b7a53a4..e1310e2 100644 (file)
       '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',