diff --git a/atom/renderer/atom_render_view_observer.cc b/atom/renderer/atom_render_view_observer.cc index 65cc9b5c51e6..12e8c1eae14c 100644 --- a/atom/renderer/atom_render_view_observer.cc +++ b/atom/renderer/atom_render_view_observer.cc @@ -94,7 +94,7 @@ void AtomRenderViewObserver::EmitIPCEvent(blink::WebFrame* frame, v8::Isolate* isolate = blink::mainThreadIsolate(); v8::HandleScope handle_scope(isolate); - v8::Local context = renderer_client_->GetContext(); + v8::Local context = renderer_client_->GetAPIContext(isolate); v8::Context::Scope context_scope(context); // Only emit IPC event for context with node integration. diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index 6415bee31b99..336ec7e6f87d 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -292,6 +292,9 @@ void AtomRendererClient::DidCreateScriptContext( if (!render_frame->IsMainFrame() && !IsDevToolsExtension(render_frame)) return; + api_context_.Reset(context->GetIsolate(), context); + api_context_.SetWeak(); + // Whether the node binding has been initialized. bool first_time = node_bindings_->uv_env() == nullptr; @@ -321,10 +324,6 @@ void AtomRendererClient::DidCreateScriptContext( } } -v8::Local AtomRendererClient::GetContext() { - return node_bindings_->uv_env()->context(); -} - void AtomRendererClient::WillReleaseScriptContext( v8::Handle context, content::RenderFrame* render_frame) { // Only allow node integration for the main frame, unless it is a devtools @@ -367,4 +366,8 @@ void AtomRendererClient::AddSupportedKeySystems( AddChromeKeySystems(key_systems); } +v8::Local AtomRendererClient::GetAPIContext(v8::Isolate* isolate) { + return api_context_.Get(isolate); +} + } // namespace atom diff --git a/atom/renderer/atom_renderer_client.h b/atom/renderer/atom_renderer_client.h index 7fab5dde86a2..755cc8fd7d08 100644 --- a/atom/renderer/atom_renderer_client.h +++ b/atom/renderer/atom_renderer_client.h @@ -27,7 +27,8 @@ class AtomRendererClient : public content::ContentRendererClient { void WillReleaseScriptContext( v8::Handle context, content::RenderFrame* render_frame); - v8::Local GetContext(); + // Get the context that the Electron API is running in. + v8::Local GetAPIContext(v8::Isolate* isolate); private: enum NodeIntegration { @@ -66,6 +67,7 @@ class AtomRendererClient : public content::ContentRendererClient { std::unique_ptr node_bindings_; std::unique_ptr atom_bindings_; std::unique_ptr preferences_manager_; + v8::Persistent api_context_; DISALLOW_COPY_AND_ASSIGN(AtomRendererClient); };