|
@@ -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
|