Initialize node integration after window object is cleared

This commit is contained in:
Cheng Zhao 2015-01-21 16:40:19 -08:00
parent 706f547287
commit ef15b670a9
3 changed files with 27 additions and 14 deletions

View file

@ -68,7 +68,8 @@ class AtomRenderFrameObserver : public content::RenderFrameObserver {
AtomRendererClient::AtomRendererClient() AtomRendererClient::AtomRendererClient()
: node_bindings_(NodeBindings::Create(false)), : node_bindings_(NodeBindings::Create(false)),
atom_bindings_(new AtomRendererBindings), atom_bindings_(new AtomRendererBindings),
main_frame_(nullptr) { main_frame_(nullptr),
is_initialized_(false) {
} }
AtomRendererClient::~AtomRendererClient() { AtomRendererClient::~AtomRendererClient() {
@ -137,13 +138,6 @@ void AtomRendererClient::DidCreateScriptContext(blink::WebFrame* frame,
// The first web frame is the main frame. // The first web frame is the main frame.
main_frame_ = 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. // Give the node loop a run to make sure everything is ready.
node_bindings_->RunMessageLoop(); node_bindings_->RunMessageLoop();
@ -156,12 +150,22 @@ void AtomRendererClient::DidCreateScriptContext(blink::WebFrame* frame,
// Make uv loop being wrapped by window context. // Make uv loop being wrapped by window context.
if (node_bindings_->uv_env() == nullptr) if (node_bindings_->uv_env() == nullptr)
node_bindings_->set_uv_env(env); node_bindings_->set_uv_env(env);
// Load everything.
node_bindings_->LoadEnvironment(env);
} }
void AtomRendererClient::DidClearWindowObject() { 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, bool AtomRendererClient::ShouldFork(blink::WebFrame* frame,

View file

@ -66,6 +66,9 @@ class AtomRendererClient : public content::ContentRendererClient,
// The main frame. // The main frame.
blink::WebFrame* main_frame_; blink::WebFrame* main_frame_;
// Whether we have already initialized.
bool is_initialized_;
DISALLOW_COPY_AND_ASSIGN(AtomRendererClient); DISALLOW_COPY_AND_ASSIGN(AtomRendererClient);
}; };

View file

@ -86,9 +86,15 @@ if nodeIntegration in ['true', 'all', 'except-iframe', 'manual-enable-iframe']
window.addEventListener 'unload', -> window.addEventListener 'unload', ->
process.emit 'exit' process.emit 'exit'
else else
delete global.process # The Module.runMain will run process._tickCallck() immediately, so we are
delete global.setImmediate # able to delete the symbols in this tick even though we used process.nextTick
delete global.clearImmediate # 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. # Load the script specfied by the "preload" attribute.
if preloadScript if preloadScript