diff --git a/shell/browser/api/electron_api_tray.cc b/shell/browser/api/electron_api_tray.cc index 4e2919e4dd8c..b18a7e141b5a 100644 --- a/shell/browser/api/electron_api_tray.cc +++ b/shell/browser/api/electron_api_tray.cc @@ -93,14 +93,20 @@ gin::Handle Tray::New(gin_helper::ErrorThrower thrower, void Tray::OnClicked(const gfx::Rect& bounds, const gfx::Point& location, int modifiers) { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::HandleScope scope(isolate); EmitCustomEvent("click", CreateEventFromFlags(modifiers), bounds, location); } void Tray::OnDoubleClicked(const gfx::Rect& bounds, int modifiers) { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::HandleScope scope(isolate); EmitCustomEvent("double-click", CreateEventFromFlags(modifiers), bounds); } void Tray::OnRightClicked(const gfx::Rect& bounds, int modifiers) { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::HandleScope scope(isolate); EmitCustomEvent("right-click", CreateEventFromFlags(modifiers), bounds); } @@ -129,22 +135,32 @@ void Tray::OnDropText(const std::string& text) { } void Tray::OnMouseEntered(const gfx::Point& location, int modifiers) { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::HandleScope scope(isolate); EmitCustomEvent("mouse-enter", CreateEventFromFlags(modifiers), location); } void Tray::OnMouseExited(const gfx::Point& location, int modifiers) { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::HandleScope scope(isolate); EmitCustomEvent("mouse-leave", CreateEventFromFlags(modifiers), location); } void Tray::OnMouseMoved(const gfx::Point& location, int modifiers) { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::HandleScope scope(isolate); EmitCustomEvent("mouse-move", CreateEventFromFlags(modifiers), location); } void Tray::OnMouseUp(const gfx::Point& location, int modifiers) { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::HandleScope scope(isolate); EmitCustomEvent("mouse-up", CreateEventFromFlags(modifiers), location); } void Tray::OnMouseDown(const gfx::Point& location, int modifiers) { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::HandleScope scope(isolate); EmitCustomEvent("mouse-down", CreateEventFromFlags(modifiers), location); } diff --git a/shell/browser/api/electron_api_tray.h b/shell/browser/api/electron_api_tray.h index 2cf2feafbb4d..b7132f9468c5 100644 --- a/shell/browser/api/electron_api_tray.h +++ b/shell/browser/api/electron_api_tray.h @@ -12,6 +12,7 @@ #include "gin/handle.h" #include "gin/wrappable.h" #include "shell/browser/event_emitter_mixin.h" +#include "shell/browser/javascript_environment.h" #include "shell/browser/ui/tray_icon.h" #include "shell/browser/ui/tray_icon_observer.h" #include "shell/common/gin_converters/guid_converter.h" diff --git a/shell/browser/event_emitter_mixin.h b/shell/browser/event_emitter_mixin.h index 781df5754173..0bcb3ea13f27 100644 --- a/shell/browser/event_emitter_mixin.h +++ b/shell/browser/event_emitter_mixin.h @@ -39,6 +39,7 @@ class EventEmitterMixin { v8::Local custom_event, Args&&... args) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::HandleScope scope(isolate); v8::Local wrapper; if (!static_cast(this)->GetWrapper(isolate).ToLocal(&wrapper)) return false; diff --git a/shell/browser/javascript_environment.cc b/shell/browser/javascript_environment.cc index e304e57fe067..910d90087379 100644 --- a/shell/browser/javascript_environment.cc +++ b/shell/browser/javascript_environment.cc @@ -20,6 +20,10 @@ #include "shell/common/node_includes.h" #include "tracing/trace_event.h" +namespace { +v8::Isolate* g_isolate; +} + namespace electron { JavascriptEnvironment::JavascriptEnvironment(uv_loop_t* event_loop) @@ -46,6 +50,7 @@ JavascriptEnvironment::~JavascriptEnvironment() { context_.Get(isolate_)->Exit(); } isolate_->Exit(); + g_isolate = nullptr; } v8::Isolate* JavascriptEnvironment::Initialize(uv_loop_t* event_loop) { @@ -73,10 +78,17 @@ v8::Isolate* JavascriptEnvironment::Initialize(uv_loop_t* event_loop) { v8::Isolate* isolate = v8::Isolate::Allocate(); platform_->RegisterIsolate(isolate, event_loop); + g_isolate = isolate; return isolate; } +// static +v8::Isolate* JavascriptEnvironment::GetIsolate() { + CHECK(g_isolate); + return g_isolate; +} + void JavascriptEnvironment::OnMessageLoopCreated() { DCHECK(!microtasks_runner_); microtasks_runner_ = std::make_unique(isolate()); diff --git a/shell/browser/javascript_environment.h b/shell/browser/javascript_environment.h index 578060684670..17ce402d3a77 100644 --- a/shell/browser/javascript_environment.h +++ b/shell/browser/javascript_environment.h @@ -34,6 +34,8 @@ class JavascriptEnvironment { return v8::Local::New(isolate_, context_); } + static v8::Isolate* GetIsolate(); + private: v8::Isolate* Initialize(uv_loop_t* event_loop); // Leaked on exit.