electron/shell/browser/api/electron_api_power_monitor.cc
electron-roller[bot] 30fbeec036
chore: bump chromium to 131.0.6734.0 (main) (#43769)
* chore: bump chromium in DEPS to 130.0.6723.4

* chore: bump chromium in DEPS to 131.0.6724.0

* chore: update patches

* chore: update libc++ filenames

* 5844369: controlledframe: Disable Web Bluetooth for <webview> & <controlledframe>
https://chromium-review.googlesource.com/c/chromium/src/+/5844369

* (multiple CLs): Use an opaque type for FrameTreeNode IDs

5807683: Use an opaque type for FrameTreeNode IDs, part 1 | https://chromium-review.googlesource.com/c/chromium/src/+/5807683
5829746: Use an opaque type for FrameTreeNode IDs, part 2 | https://chromium-review.googlesource.com/c/chromium/src/+/5829746
5836903: Use an opaque type for FrameTreeNode IDs, part 7 | https://chromium-review.googlesource.com/c/chromium/src/+/5836903
5837249: Use an opaque type for FrameTreeNode IDs, part 8 | https://chromium-review.googlesource.com/c/chromium/src/+/5837249
5836564: Use an opaque type for FrameTreeNode IDs, part 12 | https://chromium-review.googlesource.com/c/chromium/src/+/5836564
5837180: Use an opaque type for FrameTreeNode IDs, part 15 | https://chromium-review.googlesource.com/c/chromium/src/+/5837180

* 5822889: [task] Make GetForegroundTaskRunner non-virtual
https://chromium-review.googlesource.com/c/v8/v8/+/5822889

* 5833297: Remove unused inner WebContents attach params
https://chromium-review.googlesource.com/c/chromium/src/+/5833297

* 5806403: Shift PowerMonitor to non static
https://chromium-review.googlesource.com/c/chromium/src/+/5806403

* 5666874: [3/N] Remove old OnPowerChange in PowerObserver
https://chromium-review.googlesource.com/c/chromium/src/+/5666874

* 5829085: [v8] Differentiate between UserVisible and BestEffort task runners
https://chromium-review.googlesource.com/c/chromium/src/+/5829085

* 5791112: [webrtc] Use `c/b/permissions/system` for system permissions
https://chromium-review.googlesource.com/c/chromium/src/+/5791112

* 5825636: [Extensions] Create WebContentsObservers with ExtensionsBrowserClient
https://chromium-review.googlesource.com/c/chromium/src/+/5825636

* fixup! (multiple CLs): Use an opaque type for FrameTreeNode IDs

* fixup! 5791112: [webrtc] Use `c/b/permissions/system` for system permissions https://chromium-review.googlesource.com/c/chromium/src/+/5791112

* chore: bump chromium in DEPS to 131.0.6726.0

* chore: update patches

* chore: update libc++ filenames

* 5858119: Declutter: Allow opening to a specific feature
https://chromium-review.googlesource.com/c/chromium/src/+/5858119

* fix: macOS SDK 15 error

Not sure exactly what changed in the upgrade to macOS SDK 15, but it triggered a new error:

```
electron/shell/browser/ui/message_box_mac.mm:84:7: error: multiple methods named 'highlight:' found with mismatched result, parameter type or attributes
```

The `highlight:` selector a few lines down was ambiguous because the object type of the `NSArray` was not specified. Specifying `NSButton` as the element type makes the selector unambiguous for type checking.

* 5854143: [File Download Access Prevention] Obfuscate download file for enterprise deep scan
https://chromium-review.googlesource.com/c/chromium/src/+/5854143

* 5854811: Use kNotAllowedError instead of kSecurityError for Web MIDI
https://chromium-review.googlesource.com/c/chromium/src/+/5854811

* chore: bump chromium in DEPS to 131.0.6728.0

* chore: update patches

* disable invalid test

* chore: bump chromium in DEPS to 131.0.6730.0

* chore: update patches

* update build tools target commit for new macOS SDK

* chore: update libc++ file names

* chore: bump chromium in DEPS to 131.0.6732.0

* chore: bump chromium in DEPS to 131.0.6734.0

* 5856527: [UI] Use mojo enum for `WindowShowState` in ui/

https://chromium-review.googlesource.com/c/chromium/src/+/5856527

* chore: update build-tools sha to include macOD 15.0 SDK

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: clavin <clavin@electronjs.org>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: alice <alice@makenotion.com>
2024-09-25 06:19:39 -05:00

213 lines
6.5 KiB
C++

// 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 "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_device_source.h"
#include "base/power_monitor/power_observer.h"
#include "gin/data_object_builder.h"
#include "gin/handle.h"
#include "shell/browser/browser.h"
#include "shell/browser/javascript_environment.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"
namespace gin {
template <>
struct Converter<ui::IdleState> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const ui::IdleState& in) {
switch (in) {
case ui::IDLE_STATE_ACTIVE:
return StringToV8(isolate, "active");
case ui::IDLE_STATE_IDLE:
return StringToV8(isolate, "idle");
case ui::IDLE_STATE_LOCKED:
return StringToV8(isolate, "locked");
case ui::IDLE_STATE_UNKNOWN:
default:
return StringToV8(isolate, "unknown");
}
}
};
template <>
struct Converter<base::PowerThermalObserver::DeviceThermalState> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const base::PowerThermalObserver::DeviceThermalState& in) {
switch (in) {
case base::PowerThermalObserver::DeviceThermalState::kUnknown:
return StringToV8(isolate, "unknown");
case base::PowerThermalObserver::DeviceThermalState::kNominal:
return StringToV8(isolate, "nominal");
case base::PowerThermalObserver::DeviceThermalState::kFair:
return StringToV8(isolate, "fair");
case base::PowerThermalObserver::DeviceThermalState::kSerious:
return StringToV8(isolate, "serious");
case base::PowerThermalObserver::DeviceThermalState::kCritical:
return StringToV8(isolate, "critical");
}
}
};
} // namespace gin
namespace electron::api {
gin::WrapperInfo PowerMonitor::kWrapperInfo = {gin::kEmbedderNativeGin};
PowerMonitor::PowerMonitor(v8::Isolate* isolate) {
#if BUILDFLAG(IS_MAC)
Browser::Get()->SetShutdownHandler(base::BindRepeating(
&PowerMonitor::ShouldShutdown, base::Unretained(this)));
#endif
auto* power_monitor = base::PowerMonitor::GetInstance();
power_monitor->AddPowerStateObserver(this);
power_monitor->AddPowerSuspendObserver(this);
power_monitor->AddPowerThermalObserver(this);
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
InitPlatformSpecificMonitors();
#endif
}
PowerMonitor::~PowerMonitor() {
auto* power_monitor = base::PowerMonitor::GetInstance();
power_monitor->RemovePowerStateObserver(this);
power_monitor->RemovePowerSuspendObserver(this);
power_monitor->RemovePowerThermalObserver(this);
}
bool PowerMonitor::ShouldShutdown() {
return !Emit("shutdown");
}
void PowerMonitor::OnBatteryPowerStatusChange(
BatteryPowerStatus battery_power_status) {
switch (battery_power_status) {
case BatteryPowerStatus::kBatteryPower:
Emit("on-battery");
break;
case BatteryPowerStatus::kExternalPower:
Emit("on-ac");
break;
case BatteryPowerStatus::kUnknown:
// Ignored
break;
}
}
void PowerMonitor::OnSuspend() {
Emit("suspend");
}
void PowerMonitor::OnResume() {
Emit("resume");
}
void PowerMonitor::OnThermalStateChange(DeviceThermalState new_state) {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
EmitWithoutEvent(
"thermal-state-change",
gin::DataObjectBuilder(isolate).Set("state", new_state).Build());
}
void PowerMonitor::OnSpeedLimitChange(int speed_limit) {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
EmitWithoutEvent(
"speed-limit-change",
gin::DataObjectBuilder(isolate).Set("limit", speed_limit).Build());
}
#if BUILDFLAG(IS_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 {
power_observer_linux_.SetShutdownHandler(base::RepeatingCallback<bool()>());
}
}
#endif
// static
v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
auto* pm = new PowerMonitor(isolate);
auto handle = gin::CreateHandle(isolate, pm).ToV8();
pm->Pin(isolate);
return handle;
}
gin::ObjectTemplateBuilder PowerMonitor::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
auto builder =
gin_helper::EventEmitterMixin<PowerMonitor>::GetObjectTemplateBuilder(
isolate);
#if BUILDFLAG(IS_LINUX)
builder.SetMethod("setListeningForShutdown",
&PowerMonitor::SetListeningForShutdown);
#endif
return builder;
}
const char* PowerMonitor::GetTypeName() {
return "PowerMonitor";
}
} // namespace electron::api
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();
}
bool IsOnBatteryPower() {
return base::PowerMonitor::GetInstance()->IsOnBatteryPower();
}
base::PowerThermalObserver::DeviceThermalState GetCurrentThermalState() {
return base::PowerMonitor::GetInstance()->GetCurrentThermalState();
}
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_helper::Dictionary dict(isolate, exports);
dict.SetMethod("createPowerMonitor",
base::BindRepeating(&PowerMonitor::Create));
dict.SetMethod("getSystemIdleState",
base::BindRepeating(&GetSystemIdleState));
dict.SetMethod("getCurrentThermalState",
base::BindRepeating(&GetCurrentThermalState));
dict.SetMethod("getSystemIdleTime", base::BindRepeating(&GetSystemIdleTime));
dict.SetMethod("isOnBatteryPower", base::BindRepeating(&IsOnBatteryPower));
}
} // namespace
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_power_monitor, Initialize)