refactor: ginify powerMonitor (#22751)

This commit is contained in:
Jeremy Apthorp 2020-03-24 09:03:29 -07:00 committed by GitHub
parent aeae0d47bd
commit 07cd70a37e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 216 additions and 176 deletions

View file

@ -4,22 +4,6 @@
Process: [Main](../glossary.md#main-process) Process: [Main](../glossary.md#main-process)
This module cannot be used until the `ready` event of the `app`
module is emitted.
For example:
```javascript
const { app, powerMonitor } = require('electron')
app.whenReady().then(() => {
powerMonitor.on('suspend', () => {
console.log('The system is going to sleep')
})
})
```
## Events ## Events
The `powerMonitor` module emits the following events: The `powerMonitor` module emits the following events:

View file

@ -187,7 +187,6 @@ filenames = {
"shell/browser/javascript_environment.h", "shell/browser/javascript_environment.h",
"shell/browser/lib/bluetooth_chooser.cc", "shell/browser/lib/bluetooth_chooser.cc",
"shell/browser/lib/bluetooth_chooser.h", "shell/browser/lib/bluetooth_chooser.h",
"shell/browser/lib/power_observer.h",
"shell/browser/lib/power_observer_linux.cc", "shell/browser/lib/power_observer_linux.cc",
"shell/browser/lib/power_observer_linux.h", "shell/browser/lib/power_observer_linux.h",
"shell/browser/linux/unity_service.cc", "shell/browser/linux/unity_service.cc",
@ -526,6 +525,7 @@ filenames = {
"shell/common/gin_helper/object_template_builder.h", "shell/common/gin_helper/object_template_builder.h",
"shell/common/gin_helper/persistent_dictionary.cc", "shell/common/gin_helper/persistent_dictionary.cc",
"shell/common/gin_helper/persistent_dictionary.h", "shell/common/gin_helper/persistent_dictionary.h",
"shell/common/gin_helper/pinnable.h",
"shell/common/gin_helper/promise.cc", "shell/common/gin_helper/promise.cc",
"shell/common/gin_helper/promise.h", "shell/common/gin_helper/promise.h",
"shell/common/gin_helper/trackable_object.cc", "shell/common/gin_helper/trackable_object.cc",

View file

@ -1,38 +1,46 @@
'use strict'; import { EventEmitter } from 'events';
import { app } from 'electron';
import { createLazyInstance } from '../utils'; const { createPowerMonitor, getSystemIdleState, getSystemIdleTime } = process.electronBinding('power_monitor');
const { EventEmitter } = require('events'); class PowerMonitor extends EventEmitter {
const { createPowerMonitor, PowerMonitor } = process.electronBinding('power_monitor'); constructor () {
super();
// PowerMonitor is an EventEmitter. // Don't start the event source until both a) the app is ready and b)
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype); // there's a listener registered for a powerMonitor event.
this.once('newListener', () => {
const powerMonitor = createLazyInstance(createPowerMonitor, PowerMonitor, true); app.whenReady().then(() => {
const pm = createPowerMonitor();
pm.emit = this.emit.bind(this);
if (process.platform === 'linux') { if (process.platform === 'linux') {
// In order to delay system shutdown when e.preventDefault() is invoked // On Linux, we inhibit shutdown in order to give the app a chance to
// on a powerMonitor 'shutdown' event, we need an org.freedesktop.login1 // decide whether or not it wants to prevent the shutdown. We don't
// shutdown delay lock. For more details see the "Taking Delay Locks" // inhibit the shutdown event unless there's a listener for it. This
// section of https://www.freedesktop.org/wiki/Software/systemd/inhibit/ // keeps the C++ code informed about whether there are any listeners.
// pm.setListeningForShutdown(this.listenerCount('shutdown') > 0);
// So here we watch for 'shutdown' listeners to be added or removed and this.on('newListener', (event) => {
// set or unset our shutdown delay lock accordingly. if (event === 'shutdown') {
const { app } = require('electron'); pm.setListeningForShutdown(this.listenerCount('shutdown') + 1 > 0);
app.whenReady().then(() => { }
powerMonitor.on('newListener', (event: string) => { });
// whenever the listener count is incremented to one... this.on('removeListener', (event) => {
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) { if (event === 'shutdown') {
powerMonitor.blockShutdown(); pm.setListeningForShutdown(this.listenerCount('shutdown') > 0);
} }
}); });
powerMonitor.on('removeListener', (event: string) => {
// whenever the listener count is decremented to zero...
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
powerMonitor.unblockShutdown();
} }
}); });
}); });
} }
module.exports = powerMonitor; getSystemIdleState (idleThreshold: number) {
return getSystemIdleState(idleThreshold);
}
getSystemIdleTime () {
return getSystemIdleTime();
}
}
module.exports = new PowerMonitor();

View file

@ -6,10 +6,10 @@
#include "base/power_monitor/power_monitor.h" #include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_device_source.h" #include "base/power_monitor/power_monitor_device_source.h"
#include "gin/dictionary.h"
#include "gin/handle.h" #include "gin/handle.h"
#include "shell/browser/browser.h" #include "shell/browser/browser.h"
#include "shell/common/gin_converters/callback_converter.h" #include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h" #include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/node_includes.h" #include "shell/common/node_includes.h"
@ -39,16 +39,16 @@ namespace electron {
namespace api { namespace api {
gin::WrapperInfo PowerMonitor::kWrapperInfo = {gin::kEmbedderNativeGin};
PowerMonitor::PowerMonitor(v8::Isolate* isolate) { PowerMonitor::PowerMonitor(v8::Isolate* isolate) {
#if defined(OS_LINUX) #if defined(OS_MACOSX)
SetShutdownHandler(base::BindRepeating(&PowerMonitor::ShouldShutdown,
base::Unretained(this)));
#elif defined(OS_MACOSX)
Browser::Get()->SetShutdownHandler(base::BindRepeating( Browser::Get()->SetShutdownHandler(base::BindRepeating(
&PowerMonitor::ShouldShutdown, base::Unretained(this))); &PowerMonitor::ShouldShutdown, base::Unretained(this)));
#endif #endif
base::PowerMonitor::AddObserver(this); base::PowerMonitor::AddObserver(this);
Init(isolate);
#if defined(OS_MACOSX) || defined(OS_WIN) #if defined(OS_MACOSX) || defined(OS_WIN)
InitPlatformSpecificMonitors(); InitPlatformSpecificMonitors();
#endif #endif
@ -62,16 +62,6 @@ bool PowerMonitor::ShouldShutdown() {
return !Emit("shutdown"); return !Emit("shutdown");
} }
#if defined(OS_LINUX)
void PowerMonitor::BlockShutdown() {
PowerObserverLinux::BlockShutdown();
}
void PowerMonitor::UnblockShutdown() {
PowerObserverLinux::UnblockShutdown();
}
#endif
void PowerMonitor::OnPowerStateChange(bool on_battery_power) { void PowerMonitor::OnPowerStateChange(bool on_battery_power) {
if (on_battery_power) if (on_battery_power)
Emit("on-battery"); Emit("on-battery");
@ -87,46 +77,41 @@ void PowerMonitor::OnResume() {
Emit("resume"); Emit("resume");
} }
ui::IdleState PowerMonitor::GetSystemIdleState(v8::Isolate* isolate, #if defined(OS_LINUX)
int idle_threshold) { void PowerMonitor::SetListeningForShutdown(bool is_listening) {
if (idle_threshold > 0) { if (is_listening) {
return ui::CalculateIdleState(idle_threshold); // unretained is OK because we own power_observer_linux_
power_observer_linux_.SetShutdownHandler(base::BindRepeating(
&PowerMonitor::ShouldShutdown, base::Unretained(this)));
} else { } else {
isolate->ThrowException(v8::Exception::TypeError(gin::StringToV8( power_observer_linux_.SetShutdownHandler(base::RepeatingCallback<bool()>());
isolate, "Invalid idle threshold, must be greater than 0")));
return ui::IDLE_STATE_UNKNOWN;
} }
} }
#endif
int PowerMonitor::GetSystemIdleTime() {
return ui::CalculateIdleTime();
}
// static // static
v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) { v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
if (!Browser::Get()->is_ready()) { CHECK(Browser::Get()->is_ready());
isolate->ThrowException(v8::Exception::Error( auto* pm = new PowerMonitor(isolate);
gin::StringToV8(isolate, auto handle = gin::CreateHandle(isolate, pm).ToV8();
"The 'powerMonitor' module can't be used before the " pm->Pin(isolate);
"app 'ready' event"))); return handle;
return v8::Null(isolate);
} }
return gin::CreateHandle(isolate, new PowerMonitor(isolate)).ToV8(); gin::ObjectTemplateBuilder PowerMonitor::GetObjectTemplateBuilder(
} v8::Isolate* isolate) {
auto builder =
// static gin_helper::EventEmitterMixin<PowerMonitor>::GetObjectTemplateBuilder(
void PowerMonitor::BuildPrototype(v8::Isolate* isolate, isolate);
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(gin::StringToV8(isolate, "PowerMonitor"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
#if defined(OS_LINUX) #if defined(OS_LINUX)
.SetMethod("blockShutdown", &PowerMonitor::BlockShutdown) builder.SetMethod("setListeningForShutdown",
.SetMethod("unblockShutdown", &PowerMonitor::UnblockShutdown) &PowerMonitor::SetListeningForShutdown);
#endif #endif
.SetMethod("getSystemIdleState", &PowerMonitor::GetSystemIdleState) return builder;
.SetMethod("getSystemIdleTime", &PowerMonitor::GetSystemIdleTime); }
const char* PowerMonitor::GetTypeName() {
return "PowerMonitor";
} }
} // namespace api } // namespace api
@ -137,16 +122,31 @@ namespace {
using electron::api::PowerMonitor; using electron::api::PowerMonitor;
ui::IdleState GetSystemIdleState(v8::Isolate* isolate, int idle_threshold) {
if (idle_threshold > 0) {
return ui::CalculateIdleState(idle_threshold);
} else {
isolate->ThrowException(v8::Exception::TypeError(gin::StringToV8(
isolate, "Invalid idle threshold, must be greater than 0")));
return ui::IDLE_STATE_UNKNOWN;
}
}
int GetSystemIdleTime() {
return ui::CalculateIdleTime();
}
void Initialize(v8::Local<v8::Object> exports, void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, v8::Local<v8::Context> context,
void* priv) { void* priv) {
v8::Isolate* isolate = context->GetIsolate(); v8::Isolate* isolate = context->GetIsolate();
gin::Dictionary dict(isolate, exports); gin_helper::Dictionary dict(isolate, exports);
dict.Set("createPowerMonitor", base::BindRepeating(&PowerMonitor::Create)); dict.SetMethod("createPowerMonitor",
dict.Set("PowerMonitor", PowerMonitor::GetConstructor(isolate) base::BindRepeating(&PowerMonitor::Create));
->GetFunction(context) dict.SetMethod("getSystemIdleState",
.ToLocalChecked()); base::BindRepeating(&GetSystemIdleState));
dict.SetMethod("getSystemIdleTime", base::BindRepeating(&GetSystemIdleTime));
} }
} // namespace } // namespace

View file

@ -5,36 +5,44 @@
#ifndef SHELL_BROWSER_API_ELECTRON_API_POWER_MONITOR_H_ #ifndef SHELL_BROWSER_API_ELECTRON_API_POWER_MONITOR_H_
#define SHELL_BROWSER_API_ELECTRON_API_POWER_MONITOR_H_ #define SHELL_BROWSER_API_ELECTRON_API_POWER_MONITOR_H_
#include "base/compiler_specific.h" #include "base/power_monitor/power_observer.h"
#include "shell/browser/lib/power_observer.h" #include "gin/wrappable.h"
#include "shell/common/gin_helper/trackable_object.h" #include "shell/browser/event_emitter_mixin.h"
#include "shell/common/gin_helper/pinnable.h"
#include "ui/base/idle/idle.h" #include "ui/base/idle/idle.h"
#if defined(OS_LINUX)
#include "shell/browser/lib/power_observer_linux.h"
#endif
namespace electron { namespace electron {
namespace api { namespace api {
class PowerMonitor : public gin_helper::TrackableObject<PowerMonitor>, class PowerMonitor : public gin::Wrappable<PowerMonitor>,
public PowerObserver { public gin_helper::EventEmitterMixin<PowerMonitor>,
public gin_helper::Pinnable<PowerMonitor>,
public base::PowerObserver {
public: public:
static v8::Local<v8::Value> Create(v8::Isolate* isolate); static v8::Local<v8::Value> Create(v8::Isolate* isolate);
static void BuildPrototype(v8::Isolate* isolate, // gin::Wrappable
v8::Local<v8::FunctionTemplate> prototype); static gin::WrapperInfo kWrapperInfo;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const char* GetTypeName() override;
protected: private:
explicit PowerMonitor(v8::Isolate* isolate); explicit PowerMonitor(v8::Isolate* isolate);
~PowerMonitor() override; ~PowerMonitor() override;
#if defined(OS_LINUX)
void SetListeningForShutdown(bool);
#endif
// Called by native calles. // Called by native calles.
bool ShouldShutdown(); bool ShouldShutdown();
#if defined(OS_LINUX)
// Private JS APIs.
void BlockShutdown();
void UnblockShutdown();
#endif
#if defined(OS_MACOSX) || defined(OS_WIN) #if defined(OS_MACOSX) || defined(OS_WIN)
void InitPlatformSpecificMonitors(); void InitPlatformSpecificMonitors();
#endif #endif
@ -44,10 +52,6 @@ class PowerMonitor : public gin_helper::TrackableObject<PowerMonitor>,
void OnSuspend() override; void OnSuspend() override;
void OnResume() override; void OnResume() override;
private:
ui::IdleState GetSystemIdleState(v8::Isolate* isolate, int idle_threshold);
int GetSystemIdleTime();
#if defined(OS_WIN) #if defined(OS_WIN)
// Static callback invoked when a message comes in to our messaging window. // Static callback invoked when a message comes in to our messaging window.
static LRESULT CALLBACK WndProcStatic(HWND hwnd, static LRESULT CALLBACK WndProcStatic(HWND hwnd,
@ -70,6 +74,12 @@ class PowerMonitor : public gin_helper::TrackableObject<PowerMonitor>,
HWND window_; HWND window_;
#endif #endif
#if defined(OS_LINUX)
PowerObserverLinux power_observer_linux_{this};
#endif
v8::Global<v8::Value> pinned_;
DISALLOW_COPY_AND_ASSIGN(PowerMonitor); DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
}; };

View file

@ -1,26 +0,0 @@
// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_BROWSER_LIB_POWER_OBSERVER_H_
#define SHELL_BROWSER_LIB_POWER_OBSERVER_H_
#include "base/macros.h"
#if defined(OS_LINUX)
#include "shell/browser/lib/power_observer_linux.h"
#else
#include "base/power_monitor/power_observer.h"
#endif // defined(OS_LINUX)
namespace electron {
#if defined(OS_LINUX)
typedef PowerObserverLinux PowerObserver;
#else
typedef base::PowerObserver PowerObserver;
#endif // defined(OS_LINUX)
} // namespace electron
#endif // SHELL_BROWSER_LIB_POWER_OBSERVER_H_

View file

@ -9,6 +9,8 @@
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "device/bluetooth/dbus/bluez_dbus_thread_manager.h" #include "device/bluetooth/dbus/bluez_dbus_thread_manager.h"
namespace { namespace {
@ -17,22 +19,19 @@ const char kLogindServiceName[] = "org.freedesktop.login1";
const char kLogindObjectPath[] = "/org/freedesktop/login1"; const char kLogindObjectPath[] = "/org/freedesktop/login1";
const char kLogindManagerInterface[] = "org.freedesktop.login1.Manager"; const char kLogindManagerInterface[] = "org.freedesktop.login1.Manager";
std::string get_executable_basename() { base::FilePath::StringType GetExecutableBaseName() {
char buf[4096]; return base::CommandLine::ForCurrentProcess()
size_t buf_size = sizeof(buf); ->GetProgram()
std::string rv("electron"); .BaseName()
if (!uv_exepath(buf, &buf_size)) { .value();
rv = strrchr(static_cast<const char*>(buf), '/') + 1;
}
return rv;
} }
} // namespace } // namespace
namespace electron { namespace electron {
PowerObserverLinux::PowerObserverLinux() PowerObserverLinux::PowerObserverLinux(base::PowerObserver* observer)
: lock_owner_name_(get_executable_basename()), weak_ptr_factory_(this) { : observer_(observer), lock_owner_name_(GetExecutableBaseName()) {
auto* bus = bluez::BluezDBusThreadManager::Get()->GetSystemBus(); auto* bus = bluez::BluezDBusThreadManager::Get()->GetSystemBus();
if (!bus) { if (!bus) {
LOG(WARNING) << "Failed to get system bus connection"; LOG(WARNING) << "Failed to get system bus connection";
@ -114,6 +113,15 @@ void PowerObserverLinux::UnblockShutdown() {
} }
void PowerObserverLinux::SetShutdownHandler(base::Callback<bool()> handler) { void PowerObserverLinux::SetShutdownHandler(base::Callback<bool()> handler) {
// In order to delay system shutdown when e.preventDefault() is invoked
// on a powerMonitor 'shutdown' event, we need an org.freedesktop.login1
// shutdown delay lock. For more details see the "Taking Delay Locks"
// section of https://www.freedesktop.org/wiki/Software/systemd/inhibit/
if (handler && !should_shutdown_) {
BlockShutdown();
} else if (!handler && should_shutdown_) {
UnblockShutdown();
}
should_shutdown_ = std::move(handler); should_shutdown_ = std::move(handler);
} }
@ -133,11 +141,13 @@ void PowerObserverLinux::OnPrepareForSleep(dbus::Signal* signal) {
return; return;
} }
if (suspending) { if (suspending) {
OnSuspend(); observer_->OnSuspend();
UnblockSleep(); UnblockSleep();
} else { } else {
BlockSleep(); BlockSleep();
OnResume();
observer_->OnResume();
} }
} }

View file

@ -7,6 +7,7 @@
#include <string> #include <string>
#include "base/callback.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/power_monitor/power_observer.h" #include "base/power_monitor/power_observer.h"
@ -16,20 +17,19 @@
namespace electron { namespace electron {
class PowerObserverLinux : public base::PowerObserver { class PowerObserverLinux {
public: public:
PowerObserverLinux(); explicit PowerObserverLinux(base::PowerObserver* observer);
~PowerObserverLinux() override; ~PowerObserverLinux();
protected: void SetShutdownHandler(base::Callback<bool()> should_shutdown);
private:
void BlockSleep(); void BlockSleep();
void UnblockSleep(); void UnblockSleep();
void BlockShutdown(); void BlockShutdown();
void UnblockShutdown(); void UnblockShutdown();
void SetShutdownHandler(base::Callback<bool()> should_shutdown);
private:
void OnLoginServiceAvailable(bool available); void OnLoginServiceAvailable(bool available);
void OnInhibitResponse(base::ScopedFD* scoped_fd, dbus::Response* response); void OnInhibitResponse(base::ScopedFD* scoped_fd, dbus::Response* response);
void OnPrepareForSleep(dbus::Signal* signal); void OnPrepareForSleep(dbus::Signal* signal);
@ -38,13 +38,14 @@ class PowerObserverLinux : public base::PowerObserver {
const std::string& signal, const std::string& signal,
bool success); bool success);
base::Callback<bool()> should_shutdown_; base::RepeatingCallback<bool()> should_shutdown_;
base::PowerObserver* observer_;
scoped_refptr<dbus::ObjectProxy> logind_; scoped_refptr<dbus::ObjectProxy> logind_;
std::string lock_owner_name_; std::string lock_owner_name_;
base::ScopedFD sleep_lock_; base::ScopedFD sleep_lock_;
base::ScopedFD shutdown_lock_; base::ScopedFD shutdown_lock_;
base::WeakPtrFactory<PowerObserverLinux> weak_ptr_factory_; base::WeakPtrFactory<PowerObserverLinux> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(PowerObserverLinux); DISALLOW_COPY_AND_ASSIGN(PowerObserverLinux);
}; };

View file

@ -0,0 +1,34 @@
// Copyright (c) 2020 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_COMMON_GIN_HELPER_PINNABLE_H_
#define SHELL_COMMON_GIN_HELPER_PINNABLE_H_
#include "v8/include/v8.h"
namespace gin_helper {
template <typename T>
class Pinnable {
protected:
// Prevent the object from being garbage collected until Unpin() is called.
void Pin(v8::Isolate* isolate) {
v8::Locker locker(isolate);
v8::HandleScope scope(isolate);
v8::Local<v8::Value> wrapper;
if (static_cast<T*>(this)->GetWrapper(isolate).ToLocal(&wrapper)) {
pinned_.Reset(isolate, wrapper);
}
}
// Allow the object to be garbage collected.
void Unpin() { pinned_.Reset(); }
private:
v8::Global<v8::Value> pinned_;
};
} // namespace gin_helper
#endif // SHELL_COMMON_GIN_HELPER_PINNABLE_H_

View file

@ -44,8 +44,24 @@ describe('powerMonitor', () => {
dbusMockPowerMonitor = require('electron').powerMonitor; dbusMockPowerMonitor = require('electron').powerMonitor;
}); });
it('should call Inhibit to delay suspend', async () => { it('should call Inhibit to delay suspend once a listener is added', async () => {
// No calls to dbus until a listener is added
{
const calls = await getCalls(); const calls = await getCalls();
expect(calls).to.be.an('array').that.has.lengthOf(0);
}
// Add a dummy listener to engage the monitors
dbusMockPowerMonitor.on('dummy-event', () => {});
try {
let retriesRemaining = 3;
// There doesn't seem to be a way to get a notification when a call
// happens, so poll `getCalls` a few times to reduce flake.
let calls: any[] = [];
while (retriesRemaining-- > 0) {
calls = await getCalls();
if (calls.length > 0) break;
await new Promise(resolve => setTimeout(resolve, 1000));
}
expect(calls).to.be.an('array').that.has.lengthOf(1); expect(calls).to.be.an('array').that.has.lengthOf(1);
expect(calls[0].slice(1)).to.deep.equal([ expect(calls[0].slice(1)).to.deep.equal([
'Inhibit', [ 'Inhibit', [
@ -55,6 +71,9 @@ describe('powerMonitor', () => {
[[{ type: 's', child: [] }], ['delay']] [[{ type: 's', child: [] }], ['delay']]
] ]
]); ]);
} finally {
dbusMockPowerMonitor.removeAllListeners('dummy-event');
}
}); });
describe('when PrepareForSleep(true) signal is sent by logind', () => { describe('when PrepareForSleep(true) signal is sent by logind', () => {