diff --git a/lib/browser/api/power-monitor.ts b/lib/browser/api/power-monitor.ts index 02fdde2ed5..017de65634 100644 --- a/lib/browser/api/power-monitor.ts +++ b/lib/browser/api/power-monitor.ts @@ -1,5 +1,4 @@ import { EventEmitter } from 'events'; -import { app } from 'electron/main'; const { createPowerMonitor, @@ -14,28 +13,26 @@ class PowerMonitor extends EventEmitter { // Don't start the event source until both a) the app is ready and b) // there's a listener registered for a powerMonitor event. this.once('newListener', () => { - app.whenReady().then(() => { - const pm = createPowerMonitor(); - pm.emit = this.emit.bind(this); + const pm = createPowerMonitor(); + pm.emit = this.emit.bind(this); - if (process.platform === 'linux') { - // On Linux, we inhibit shutdown in order to give the app a chance to - // decide whether or not it wants to prevent the shutdown. We don't - // inhibit the shutdown event unless there's a listener for it. This - // keeps the C++ code informed about whether there are any listeners. - pm.setListeningForShutdown(this.listenerCount('shutdown') > 0); - this.on('newListener', (event) => { - if (event === 'shutdown') { - pm.setListeningForShutdown(this.listenerCount('shutdown') + 1 > 0); - } - }); - this.on('removeListener', (event) => { - if (event === 'shutdown') { - pm.setListeningForShutdown(this.listenerCount('shutdown') > 0); - } - }); - } - }); + if (process.platform === 'linux') { + // On Linux, we inhibit shutdown in order to give the app a chance to + // decide whether or not it wants to prevent the shutdown. We don't + // inhibit the shutdown event unless there's a listener for it. This + // keeps the C++ code informed about whether there are any listeners. + pm.setListeningForShutdown(this.listenerCount('shutdown') > 0); + this.on('newListener', (event) => { + if (event === 'shutdown') { + pm.setListeningForShutdown(this.listenerCount('shutdown') + 1 > 0); + } + }); + this.on('removeListener', (event) => { + if (event === 'shutdown') { + pm.setListeningForShutdown(this.listenerCount('shutdown') > 0); + } + }); + } }); } diff --git a/shell/browser/api/electron_api_power_monitor.cc b/shell/browser/api/electron_api_power_monitor.cc index c7ba897ab9..5878d2c7f1 100644 --- a/shell/browser/api/electron_api_power_monitor.cc +++ b/shell/browser/api/electron_api_power_monitor.cc @@ -91,7 +91,6 @@ void PowerMonitor::SetListeningForShutdown(bool is_listening) { // static v8::Local PowerMonitor::Create(v8::Isolate* isolate) { - CHECK(Browser::Get()->is_ready()); auto* pm = new PowerMonitor(isolate); auto handle = gin::CreateHandle(isolate, pm).ToV8(); pm->Pin(isolate); diff --git a/spec/api-power-monitor-spec.ts b/spec/api-power-monitor-spec.ts index 470096d339..edc9d7d466 100644 --- a/spec/api-power-monitor-spec.ts +++ b/spec/api-power-monitor-spec.ts @@ -8,7 +8,7 @@ // python-dbusmock. import { expect } from 'chai'; import * as dbus from 'dbus-native'; -import { ifdescribe } from './lib/spec-helpers'; +import { ifdescribe, startRemoteControlApp } from './lib/spec-helpers'; import { promisify } from 'util'; import { setTimeout } from 'timers/promises'; @@ -135,6 +135,11 @@ describe('powerMonitor', () => { }); }); + it('is usable before app ready', async () => { + const remoteApp = await startRemoteControlApp(['--boot-eval=globalThis.initialValue=require("electron").powerMonitor.getSystemIdleTime()']); + expect(await remoteApp.remoteEval('globalThis.initialValue')).to.be.a('number'); + }); + describe('when powerMonitor module is loaded', () => { // eslint-disable-next-line no-undef let powerMonitor: typeof Electron.powerMonitor;