Fix crash when the main frame is replaced

This commit is contained in:
Cheng Zhao 2017-03-02 17:18:00 +09:00
parent 24574f7299
commit 1709e74958
2 changed files with 12 additions and 6 deletions

View file

@ -206,7 +206,8 @@ std::vector<std::string> ParseSchemesCLISwitch(const char* switch_name) {
} // namespace } // namespace
AtomRendererClient::AtomRendererClient() AtomRendererClient::AtomRendererClient()
: node_bindings_(NodeBindings::Create(false)), : node_integration_initialized_(false),
node_bindings_(NodeBindings::Create(false)),
atom_bindings_(new AtomBindings) { atom_bindings_(new AtomBindings) {
isolated_world_ = base::CommandLine::ForCurrentProcess()->HasSwitch( isolated_world_ = base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kContextIsolation); switches::kContextIsolation);
@ -342,11 +343,9 @@ void AtomRendererClient::DidCreateScriptContext(
if (!render_frame->IsMainFrame() && !IsDevToolsExtension(render_frame)) if (!render_frame->IsMainFrame() && !IsDevToolsExtension(render_frame))
return; return;
// Whether the node binding has been initialized.
bool first_time = node_bindings_->uv_env() == nullptr;
// Prepare the node bindings. // Prepare the node bindings.
if (first_time) { if (!node_integration_initialized_) {
node_integration_initialized_ = true;
node_bindings_->Initialize(); node_bindings_->Initialize();
node_bindings_->PrepareMessageLoop(); node_bindings_->PrepareMessageLoop();
} }
@ -362,7 +361,7 @@ void AtomRendererClient::DidCreateScriptContext(
// Load everything. // Load everything.
node_bindings_->LoadEnvironment(env); node_bindings_->LoadEnvironment(env);
if (first_time) { if (node_bindings_->uv_env() == nullptr) {
// Make uv loop being wrapped by window context. // Make uv loop being wrapped by window context.
node_bindings_->set_uv_env(env); node_bindings_->set_uv_env(env);
@ -382,6 +381,10 @@ void AtomRendererClient::WillReleaseScriptContext(
if (env) if (env)
mate::EmitEvent(env->isolate(), env->process_object(), "exit"); 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. // Destroy the node environment.
node::FreeEnvironment(env); node::FreeEnvironment(env);
atom_bindings_->EnvironmentDestroyed(env); atom_bindings_->EnvironmentDestroyed(env);

View file

@ -66,6 +66,9 @@ class AtomRendererClient : public content::ContentRendererClient {
std::vector<std::unique_ptr<::media::KeySystemProperties>>* key_systems) std::vector<std::unique_ptr<::media::KeySystemProperties>>* key_systems)
override; override;
// Whether the node integration has been initialized.
bool node_integration_initialized_;
std::unique_ptr<NodeBindings> node_bindings_; std::unique_ptr<NodeBindings> node_bindings_;
std::unique_ptr<AtomBindings> atom_bindings_; std::unique_ptr<AtomBindings> atom_bindings_;
std::unique_ptr<PreferencesManager> preferences_manager_; std::unique_ptr<PreferencesManager> preferences_manager_;