mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
PowerMonitor registered OS-level callbacks (HWND UserData and WTS/suspend notifications on Windows, shutdown handler and lock-screen observer on macOS) but never cleaned them up in its destructor. The JS layer also only held the native object in a closure-local variable, allowing GC to reclaim it while those registrations still referenced freed memory. Retain the native PowerMonitor at module level in power-monitor.ts so it cannot be garbage-collected. Add DestroyPlatformSpecificMonitors() to properly tear down OS registrations on destruction: on Windows, unregister WTS and suspend notifications, clear GWLP_USERDATA, and destroy the HWND; on macOS, remove the emitter from the global MacLockMonitor and reset the Browser shutdown handler. Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
112 lines
3.2 KiB
Plaintext
112 lines
3.2 KiB
Plaintext
// Copyright (c) 2013 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "shell/browser/api/electron_api_power_monitor.h"
|
|
|
|
#include <vector>
|
|
|
|
#import <ApplicationServices/ApplicationServices.h>
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
@interface MacLockMonitor : NSObject {
|
|
@private
|
|
std::vector<electron::api::PowerMonitor*> emitters;
|
|
}
|
|
|
|
- (void)addEmitter:(electron::api::PowerMonitor*)monitor_;
|
|
- (void)removeEmitter:(electron::api::PowerMonitor*)monitor_;
|
|
|
|
@end
|
|
|
|
@implementation MacLockMonitor
|
|
|
|
- (id)init {
|
|
if ((self = [super init])) {
|
|
NSDistributedNotificationCenter* distributed_center =
|
|
[NSDistributedNotificationCenter defaultCenter];
|
|
// A notification that the screen was locked.
|
|
[distributed_center addObserver:self
|
|
selector:@selector(onScreenLocked:)
|
|
name:@"com.apple.screenIsLocked"
|
|
object:nil];
|
|
// A notification that the screen was unlocked by the user.
|
|
[distributed_center addObserver:self
|
|
selector:@selector(onScreenUnlocked:)
|
|
name:@"com.apple.screenIsUnlocked"
|
|
object:nil];
|
|
|
|
NSNotificationCenter* shared_center =
|
|
[[NSWorkspace sharedWorkspace] notificationCenter];
|
|
|
|
// A notification that the workspace posts when the user session becomes
|
|
// active.
|
|
[shared_center addObserver:self
|
|
selector:@selector(onUserDidBecomeActive:)
|
|
name:NSWorkspaceSessionDidBecomeActiveNotification
|
|
object:nil];
|
|
// A notification that the workspace posts when the user session becomes
|
|
// inactive.
|
|
[shared_center addObserver:self
|
|
selector:@selector(onUserDidResignActive:)
|
|
name:NSWorkspaceSessionDidResignActiveNotification
|
|
object:nil];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc {
|
|
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
|
|
}
|
|
|
|
- (void)addEmitter:(electron::api::PowerMonitor*)monitor_ {
|
|
self->emitters.push_back(monitor_);
|
|
}
|
|
|
|
- (void)removeEmitter:(electron::api::PowerMonitor*)monitor_ {
|
|
std::erase(self->emitters, monitor_);
|
|
}
|
|
|
|
- (void)onScreenLocked:(NSNotification*)notification {
|
|
for (auto* emitter : self->emitters) {
|
|
emitter->Emit("lock-screen");
|
|
}
|
|
}
|
|
|
|
- (void)onScreenUnlocked:(NSNotification*)notification {
|
|
for (auto* emitter : self->emitters) {
|
|
emitter->Emit("unlock-screen");
|
|
}
|
|
}
|
|
|
|
- (void)onUserDidBecomeActive:(NSNotification*)notification {
|
|
for (auto* emitter : self->emitters) {
|
|
emitter->Emit("user-did-become-active");
|
|
}
|
|
}
|
|
|
|
- (void)onUserDidResignActive:(NSNotification*)notification {
|
|
for (auto* emitter : self->emitters) {
|
|
emitter->Emit("user-did-resign-active");
|
|
}
|
|
}
|
|
|
|
@end
|
|
|
|
namespace electron::api {
|
|
|
|
static MacLockMonitor* g_lock_monitor = nil;
|
|
|
|
void PowerMonitor::InitPlatformSpecificMonitors() {
|
|
if (!g_lock_monitor)
|
|
g_lock_monitor = [[MacLockMonitor alloc] init];
|
|
[g_lock_monitor addEmitter:this];
|
|
}
|
|
|
|
void PowerMonitor::DestroyPlatformSpecificMonitors() {
|
|
if (g_lock_monitor)
|
|
[g_lock_monitor removeEmitter:this];
|
|
}
|
|
|
|
} // namespace electron::api
|