Skip to content
Permalink
Browse files

refactor: ginify powerMonitor (#22751)

  • Loading branch information
nornagon committed Mar 24, 2020
1 parent aeae0d4 commit 07cd70a37e62a0203e5d2cc9b2e25f04363a94cc
@@ -4,22 +4,6 @@
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

The `powerMonitor` module emits the following events:
@@ -187,7 +187,6 @@ filenames = {
"shell/browser/javascript_environment.h",
"shell/browser/lib/bluetooth_chooser.cc",
"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.h",
"shell/browser/linux/unity_service.cc",
@@ -526,6 +525,7 @@ filenames = {
"shell/common/gin_helper/object_template_builder.h",
"shell/common/gin_helper/persistent_dictionary.cc",
"shell/common/gin_helper/persistent_dictionary.h",
"shell/common/gin_helper/pinnable.h",
"shell/common/gin_helper/promise.cc",
"shell/common/gin_helper/promise.h",
"shell/common/gin_helper/trackable_object.cc",
@@ -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');
const { createPowerMonitor, PowerMonitor } = process.electronBinding('power_monitor');
class PowerMonitor extends EventEmitter {
constructor () {
super();
// 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);

// PowerMonitor is an EventEmitter.
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype);
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);
}
});
}
});
});
}

const powerMonitor = createLazyInstance(createPowerMonitor, PowerMonitor, true);
getSystemIdleState (idleThreshold: number) {
return getSystemIdleState(idleThreshold);
}

if (process.platform === 'linux') {
// 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/
//
// So here we watch for 'shutdown' listeners to be added or removed and
// set or unset our shutdown delay lock accordingly.
const { app } = require('electron');
app.whenReady().then(() => {
powerMonitor.on('newListener', (event: string) => {
// whenever the listener count is incremented to one...
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
powerMonitor.blockShutdown();
}
});
powerMonitor.on('removeListener', (event: string) => {
// whenever the listener count is decremented to zero...
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
powerMonitor.unblockShutdown();
}
});
});
getSystemIdleTime () {
return getSystemIdleTime();
}
}

module.exports = powerMonitor;
module.exports = new PowerMonitor();
@@ -6,10 +6,10 @@

#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_device_source.h"
#include "gin/dictionary.h"
#include "gin/handle.h"
#include "shell/browser/browser.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/node_includes.h"

@@ -39,16 +39,16 @@ namespace electron {

namespace api {

gin::WrapperInfo PowerMonitor::kWrapperInfo = {gin::kEmbedderNativeGin};

PowerMonitor::PowerMonitor(v8::Isolate* isolate) {
#if defined(OS_LINUX)
SetShutdownHandler(base::BindRepeating(&PowerMonitor::ShouldShutdown,
base::Unretained(this)));
#elif defined(OS_MACOSX)
#if defined(OS_MACOSX)
Browser::Get()->SetShutdownHandler(base::BindRepeating(
&PowerMonitor::ShouldShutdown, base::Unretained(this)));
#endif

base::PowerMonitor::AddObserver(this);
Init(isolate);

#if defined(OS_MACOSX) || defined(OS_WIN)
InitPlatformSpecificMonitors();
#endif
@@ -62,16 +62,6 @@ bool PowerMonitor::ShouldShutdown() {
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) {
if (on_battery_power)
Emit("on-battery");
@@ -87,46 +77,41 @@ void PowerMonitor::OnResume() {
Emit("resume");
}

ui::IdleState PowerMonitor::GetSystemIdleState(v8::Isolate* isolate,
int idle_threshold) {
if (idle_threshold > 0) {
return ui::CalculateIdleState(idle_threshold);
#if defined(OS_LINUX)
void PowerMonitor::SetListeningForShutdown(bool is_listening) {
if (is_listening) {
// unretained is OK because we own power_observer_linux_
power_observer_linux_.SetShutdownHandler(base::BindRepeating(
&PowerMonitor::ShouldShutdown, base::Unretained(this)));
} else {
isolate->ThrowException(v8::Exception::TypeError(gin::StringToV8(
isolate, "Invalid idle threshold, must be greater than 0")));
return ui::IDLE_STATE_UNKNOWN;
power_observer_linux_.SetShutdownHandler(base::RepeatingCallback<bool()>());
}
}

int PowerMonitor::GetSystemIdleTime() {
return ui::CalculateIdleTime();
}
#endif

// static
v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
if (!Browser::Get()->is_ready()) {
isolate->ThrowException(v8::Exception::Error(
gin::StringToV8(isolate,
"The 'powerMonitor' module can't be used before the "
"app 'ready' event")));
return v8::Null(isolate);
}

return gin::CreateHandle(isolate, new PowerMonitor(isolate)).ToV8();
CHECK(Browser::Get()->is_ready());
auto* pm = new PowerMonitor(isolate);
auto handle = gin::CreateHandle(isolate, pm).ToV8();
pm->Pin(isolate);
return handle;
}

// static
void PowerMonitor::BuildPrototype(v8::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())
gin::ObjectTemplateBuilder PowerMonitor::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
auto builder =
gin_helper::EventEmitterMixin<PowerMonitor>::GetObjectTemplateBuilder(
isolate);
#if defined(OS_LINUX)
.SetMethod("blockShutdown", &PowerMonitor::BlockShutdown)
.SetMethod("unblockShutdown", &PowerMonitor::UnblockShutdown)
builder.SetMethod("setListeningForShutdown",
&PowerMonitor::SetListeningForShutdown);
#endif
.SetMethod("getSystemIdleState", &PowerMonitor::GetSystemIdleState)
.SetMethod("getSystemIdleTime", &PowerMonitor::GetSystemIdleTime);
return builder;
}

const char* PowerMonitor::GetTypeName() {
return "PowerMonitor";
}

} // namespace api
@@ -137,16 +122,31 @@ namespace {

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,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
gin::Dictionary dict(isolate, exports);
dict.Set("createPowerMonitor", base::BindRepeating(&PowerMonitor::Create));
dict.Set("PowerMonitor", PowerMonitor::GetConstructor(isolate)
->GetFunction(context)
.ToLocalChecked());
gin_helper::Dictionary dict(isolate, exports);
dict.SetMethod("createPowerMonitor",
base::BindRepeating(&PowerMonitor::Create));
dict.SetMethod("getSystemIdleState",
base::BindRepeating(&GetSystemIdleState));
dict.SetMethod("getSystemIdleTime", base::BindRepeating(&GetSystemIdleTime));
}

} // namespace
@@ -5,36 +5,44 @@
#ifndef SHELL_BROWSER_API_ELECTRON_API_POWER_MONITOR_H_
#define SHELL_BROWSER_API_ELECTRON_API_POWER_MONITOR_H_

#include "base/compiler_specific.h"
#include "shell/browser/lib/power_observer.h"
#include "shell/common/gin_helper/trackable_object.h"
#include "base/power_monitor/power_observer.h"
#include "gin/wrappable.h"
#include "shell/browser/event_emitter_mixin.h"
#include "shell/common/gin_helper/pinnable.h"
#include "ui/base/idle/idle.h"

#if defined(OS_LINUX)
#include "shell/browser/lib/power_observer_linux.h"
#endif

namespace electron {

namespace api {

class PowerMonitor : public gin_helper::TrackableObject<PowerMonitor>,
public PowerObserver {
class PowerMonitor : public gin::Wrappable<PowerMonitor>,
public gin_helper::EventEmitterMixin<PowerMonitor>,
public gin_helper::Pinnable<PowerMonitor>,
public base::PowerObserver {
public:
static v8::Local<v8::Value> Create(v8::Isolate* isolate);

static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
// gin::Wrappable
static gin::WrapperInfo kWrapperInfo;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const char* GetTypeName() override;

protected:
private:
explicit PowerMonitor(v8::Isolate* isolate);
~PowerMonitor() override;

// Called by native calles.
bool ShouldShutdown();

#if defined(OS_LINUX)
// Private JS APIs.
void BlockShutdown();
void UnblockShutdown();
void SetListeningForShutdown(bool);
#endif

// Called by native calles.
bool ShouldShutdown();

#if defined(OS_MACOSX) || defined(OS_WIN)
void InitPlatformSpecificMonitors();
#endif
@@ -44,10 +52,6 @@ class PowerMonitor : public gin_helper::TrackableObject<PowerMonitor>,
void OnSuspend() override;
void OnResume() override;

private:
ui::IdleState GetSystemIdleState(v8::Isolate* isolate, int idle_threshold);
int GetSystemIdleTime();

#if defined(OS_WIN)
// Static callback invoked when a message comes in to our messaging window.
static LRESULT CALLBACK WndProcStatic(HWND hwnd,
@@ -70,6 +74,12 @@ class PowerMonitor : public gin_helper::TrackableObject<PowerMonitor>,
HWND window_;
#endif

#if defined(OS_LINUX)
PowerObserverLinux power_observer_linux_{this};
#endif

v8::Global<v8::Value> pinned_;

DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
};

This file was deleted.

0 comments on commit 07cd70a

Please sign in to comment.
You can’t perform that action at this time.