Browse Source

fix: missing handlescopes in event emission (#23140)

* fix: missing event emitter handlescopes

* refactor: add static getter to js env
shelley vohr 5 years ago
parent
commit
ac5c30a707

+ 16 - 0
shell/browser/api/electron_api_tray.cc

@@ -93,14 +93,20 @@ gin::Handle<Tray> 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);
 }
 

+ 1 - 0
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"

+ 1 - 0
shell/browser/event_emitter_mixin.h

@@ -39,6 +39,7 @@ class EventEmitterMixin {
                        v8::Local<v8::Object> custom_event,
                        Args&&... args) {
     v8::Isolate* isolate = v8::Isolate::GetCurrent();
+    v8::HandleScope scope(isolate);
     v8::Local<v8::Object> wrapper;
     if (!static_cast<T*>(this)->GetWrapper(isolate).ToLocal(&wrapper))
       return false;

+ 12 - 0
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<MicrotasksRunner>(isolate());

+ 2 - 0
shell/browser/javascript_environment.h

@@ -34,6 +34,8 @@ class JavascriptEnvironment {
     return v8::Local<v8::Context>::New(isolate_, context_);
   }
 
+  static v8::Isolate* GetIsolate();
+
  private:
   v8::Isolate* Initialize(uv_loop_t* event_loop);
   // Leaked on exit.