diff --git a/shell/browser/api/electron_api_power_monitor.h b/shell/browser/api/electron_api_power_monitor.h index b788c20c1766..96893515b7ed 100644 --- a/shell/browser/api/electron_api_power_monitor.h +++ b/shell/browser/api/electron_api_power_monitor.h @@ -78,8 +78,6 @@ class PowerMonitor : public gin::Wrappable, PowerObserverLinux power_observer_linux_{this}; #endif - v8::Global pinned_; - DISALLOW_COPY_AND_ASSIGN(PowerMonitor); }; diff --git a/shell/browser/api/electron_api_power_monitor_win.cc b/shell/browser/api/electron_api_power_monitor_win.cc index 221089e1ac82..bf1468cd9703 100644 --- a/shell/browser/api/electron_api_power_monitor_win.cc +++ b/shell/browser/api/electron_api_power_monitor_win.cc @@ -9,6 +9,7 @@ #include "base/win/windows_types.h" #include "base/win/wrapped_window_proc.h" +#include "content/public/browser/browser_task_traits.h" #include "ui/base/win/shell.h" #include "ui/gfx/win/hwnd_util.h" @@ -81,16 +82,29 @@ LRESULT CALLBACK PowerMonitor::WndProc(HWND hwnd, } if (should_treat_as_current_session) { if (wparam == WTS_SESSION_LOCK) { - Emit("lock-screen"); + // Unretained is OK because this object is eternally pinned. + content::GetUIThreadTaskRunner({})->PostTask( + FROM_HERE, + base::BindOnce([](PowerMonitor* pm) { pm->Emit("lock-screen"); }, + base::Unretained(this))); } else if (wparam == WTS_SESSION_UNLOCK) { - Emit("unlock-screen"); + content::GetUIThreadTaskRunner({})->PostTask( + FROM_HERE, + base::BindOnce([](PowerMonitor* pm) { pm->Emit("unlock-screen"); }, + base::Unretained(this))); } } } else if (message == WM_POWERBROADCAST) { if (wparam == PBT_APMRESUMEAUTOMATIC) { - Emit("resume"); + content::GetUIThreadTaskRunner({})->PostTask( + FROM_HERE, + base::BindOnce([](PowerMonitor* pm) { pm->Emit("resume"); }, + base::Unretained(this))); } else if (wparam == PBT_APMSUSPEND) { - Emit("suspend"); + content::GetUIThreadTaskRunner({})->PostTask( + FROM_HERE, + base::BindOnce([](PowerMonitor* pm) { pm->Emit("suspend"); }, + base::Unretained(this))); } } return ::DefWindowProc(hwnd, message, wparam, lparam);