From 1709e74958dfcbf070af046592e805cdbd152689 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 2 Mar 2017 17:18:00 +0900 Subject: [PATCH] Fix crash when the main frame is replaced --- atom/renderer/atom_renderer_client.cc | 15 +++++++++------ atom/renderer/atom_renderer_client.h | 3 +++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index 79aea0ee0b5f..cbea7f7a5660 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -206,7 +206,8 @@ std::vector ParseSchemesCLISwitch(const char* switch_name) { } // namespace AtomRendererClient::AtomRendererClient() - : node_bindings_(NodeBindings::Create(false)), + : node_integration_initialized_(false), + node_bindings_(NodeBindings::Create(false)), atom_bindings_(new AtomBindings) { isolated_world_ = base::CommandLine::ForCurrentProcess()->HasSwitch( switches::kContextIsolation); @@ -342,11 +343,9 @@ void AtomRendererClient::DidCreateScriptContext( if (!render_frame->IsMainFrame() && !IsDevToolsExtension(render_frame)) return; - // Whether the node binding has been initialized. - bool first_time = node_bindings_->uv_env() == nullptr; - // Prepare the node bindings. - if (first_time) { + if (!node_integration_initialized_) { + node_integration_initialized_ = true; node_bindings_->Initialize(); node_bindings_->PrepareMessageLoop(); } @@ -362,7 +361,7 @@ void AtomRendererClient::DidCreateScriptContext( // Load everything. node_bindings_->LoadEnvironment(env); - if (first_time) { + if (node_bindings_->uv_env() == nullptr) { // Make uv loop being wrapped by window context. node_bindings_->set_uv_env(env); @@ -382,6 +381,10 @@ void AtomRendererClient::WillReleaseScriptContext( if (env) mate::EmitEvent(env->isolate(), env->process_object(), "exit"); + // The main frame may be replaced. + if (env == node_bindings_->uv_env()) + node_bindings_->set_uv_env(nullptr); + // Destroy the node environment. node::FreeEnvironment(env); atom_bindings_->EnvironmentDestroyed(env); diff --git a/atom/renderer/atom_renderer_client.h b/atom/renderer/atom_renderer_client.h index a693262fed1b..c1d86cddf5e3 100644 --- a/atom/renderer/atom_renderer_client.h +++ b/atom/renderer/atom_renderer_client.h @@ -66,6 +66,9 @@ class AtomRendererClient : public content::ContentRendererClient { std::vector>* key_systems) override; + // Whether the node integration has been initialized. + bool node_integration_initialized_; + std::unique_ptr node_bindings_; std::unique_ptr atom_bindings_; std::unique_ptr preferences_manager_;