]> khamidou — GitHub mirrors - electron.git/commitdiff
Initialize node integration after window object is cleared
authorCheng Zhao <zcbenz@gmail.com>
Thu, 22 Jan 2015 00:40:19 +0000 (16:40 -0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 22 Jan 2015 00:40:19 +0000 (16:40 -0800)
atom/renderer/atom_renderer_client.cc
atom/renderer/atom_renderer_client.h
atom/renderer/lib/init.coffee

index 21cb05c94b145545c5866d70ef992fa1d2e0e309..f40a89028e5fcd64d0b1f229ac7de8df91c052a9 100644 (file)
@@ -68,7 +68,8 @@ class AtomRenderFrameObserver : public content::RenderFrameObserver {
 AtomRendererClient::AtomRendererClient()
     : node_bindings_(NodeBindings::Create(false)),
       atom_bindings_(new AtomRendererBindings),
-      main_frame_(nullptr) {
+      main_frame_(nullptr),
+      is_initialized_(false) {
 }
 
 AtomRendererClient::~AtomRendererClient() {
@@ -137,13 +138,6 @@ void AtomRendererClient::DidCreateScriptContext(blink::WebFrame* frame,
   // The first web frame is the main frame.
   main_frame_ = frame;
 
-  v8::Context::Scope scope(context);
-
-  // Check the existance of process object to prevent duplicate initialization.
-  if (context->Global()->Has(
-        mate::StringToV8(context->GetIsolate(), "process")))
-    return;
-
   // Give the node loop a run to make sure everything is ready.
   node_bindings_->RunMessageLoop();
 
@@ -156,12 +150,22 @@ void AtomRendererClient::DidCreateScriptContext(blink::WebFrame* frame,
   // Make uv loop being wrapped by window context.
   if (node_bindings_->uv_env() == nullptr)
     node_bindings_->set_uv_env(env);
-
-  // Load everything.
-  node_bindings_->LoadEnvironment(env);
 }
 
 void AtomRendererClient::DidClearWindowObject() {
+  if (!main_frame_ || is_initialized_)
+    return;
+
+  is_initialized_ = true;
+
+  v8::Local<v8::Context> context = main_frame_->mainWorldScriptContext();
+  v8::Context::Scope scope(context);
+
+  node::Environment* env = node::Environment::GetCurrent(context);
+  DCHECK(env);
+
+  // Load everything.
+  node_bindings_->LoadEnvironment(env);
 }
 
 bool AtomRendererClient::ShouldFork(blink::WebFrame* frame,
index ad8e6ca7561198689fe68d9d3edd425d0aeda8c8..870706b837cffe6bb04d7cb9d17967e1651f9022 100644 (file)
@@ -66,6 +66,9 @@ class AtomRendererClient : public content::ContentRendererClient,
   // The main frame.
   blink::WebFrame* main_frame_;
 
+  // Whether we have already initialized.
+  bool is_initialized_;
+
   DISALLOW_COPY_AND_ASSIGN(AtomRendererClient);
 };
 
index 4ea9281aa881aa41b35ecf478f5d0646579528e7..11f933a0a838418a839a033371fe76111013632c 100644 (file)
@@ -86,9 +86,15 @@ if nodeIntegration in ['true', 'all', 'except-iframe', 'manual-enable-iframe']
   window.addEventListener 'unload', ->
     process.emit 'exit'
 else
-  delete global.process
-  delete global.setImmediate
-  delete global.clearImmediate
+  # The Module.runMain will run process._tickCallck() immediately, so we are
+  # able to delete the symbols in this tick even though we used process.nextTick
+  # to schedule it.
+  # It is important that we put this in process.nextTick, if we delete them now
+  # some code in node.js will complain about "process not defined".
+  process.nextTick ->
+    delete global.process
+    delete global.setImmediate
+    delete global.clearImmediate
 
 # Load the script specfied by the "preload" attribute.
 if preloadScript