2015-06-21 06:57:42 -06:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-02-04 13:19:40 -07:00
|
|
|
#include "shell/browser/api/electron_api_power_save_blocker.h"
|
2015-06-21 06:57:42 -06:00
|
|
|
|
2015-06-23 22:49:43 -06:00
|
|
|
#include <string>
|
|
|
|
|
2023-05-30 02:28:43 -06:00
|
|
|
#include "base/containers/contains.h"
|
2023-02-03 04:43:42 -07:00
|
|
|
#include "base/functional/callback_helpers.h"
|
2020-01-17 11:41:52 -07:00
|
|
|
#include "content/public/browser/device_service.h"
|
2019-05-01 14:24:01 -06:00
|
|
|
#include "gin/dictionary.h"
|
|
|
|
#include "gin/function_template.h"
|
2018-10-23 23:12:58 -06:00
|
|
|
#include "services/device/public/mojom/wake_lock_provider.mojom.h"
|
|
|
|
#include "services/service_manager/public/cpp/connector.h"
|
2019-06-19 14:46:59 -06:00
|
|
|
#include "shell/common/node_includes.h"
|
2015-06-21 06:57:42 -06:00
|
|
|
|
2019-05-01 14:24:01 -06:00
|
|
|
namespace gin {
|
2015-06-21 06:57:42 -06:00
|
|
|
|
2018-04-17 19:55:30 -06:00
|
|
|
template <>
|
2018-04-11 03:00:59 -06:00
|
|
|
struct Converter<device::mojom::WakeLockType> {
|
2015-06-21 06:57:42 -06:00
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
2018-04-11 03:00:59 -06:00
|
|
|
device::mojom::WakeLockType* out) {
|
2015-06-23 22:49:43 -06:00
|
|
|
std::string type;
|
2015-06-21 06:57:42 -06:00
|
|
|
if (!ConvertFromV8(isolate, val, &type))
|
|
|
|
return false;
|
2015-06-23 22:49:43 -06:00
|
|
|
if (type == "prevent-app-suspension")
|
2018-04-11 03:00:59 -06:00
|
|
|
*out = device::mojom::WakeLockType::kPreventAppSuspension;
|
2015-06-23 22:49:43 -06:00
|
|
|
else if (type == "prevent-display-sleep")
|
2018-04-11 03:00:59 -06:00
|
|
|
*out = device::mojom::WakeLockType::kPreventDisplaySleep;
|
2015-06-23 22:49:43 -06:00
|
|
|
else
|
|
|
|
return false;
|
2015-06-21 06:57:42 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-01 14:24:01 -06:00
|
|
|
} // namespace gin
|
2015-06-21 06:57:42 -06:00
|
|
|
|
2022-06-29 13:55:47 -06:00
|
|
|
namespace electron::api {
|
2015-06-21 06:57:42 -06:00
|
|
|
|
2019-05-01 14:24:01 -06:00
|
|
|
gin::WrapperInfo PowerSaveBlocker::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
2016-04-24 19:17:54 -06:00
|
|
|
PowerSaveBlocker::PowerSaveBlocker(v8::Isolate* isolate)
|
2021-01-26 11:16:21 -07:00
|
|
|
: current_lock_type_(device::mojom::WakeLockType::kPreventAppSuspension) {}
|
2015-06-21 06:57:42 -06:00
|
|
|
|
2019-09-16 16:12:00 -06:00
|
|
|
PowerSaveBlocker::~PowerSaveBlocker() = default;
|
2015-06-21 06:57:42 -06:00
|
|
|
|
2015-06-21 20:23:58 -06:00
|
|
|
void PowerSaveBlocker::UpdatePowerSaveBlocker() {
|
2018-10-23 23:12:58 -06:00
|
|
|
if (wake_lock_types_.empty()) {
|
|
|
|
if (is_wake_lock_active_) {
|
|
|
|
GetWakeLock()->CancelWakeLock();
|
|
|
|
is_wake_lock_active_ = false;
|
|
|
|
}
|
2015-06-21 20:23:58 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-11 03:00:59 -06:00
|
|
|
// |WakeLockType::kPreventAppSuspension| keeps system active, but allows
|
2015-06-21 20:23:58 -06:00
|
|
|
// screen to be turned off.
|
2018-04-11 03:00:59 -06:00
|
|
|
// |WakeLockType::kPreventDisplaySleep| keeps system and screen active, has a
|
|
|
|
// higher precedence level than |WakeLockType::kPreventAppSuspension|.
|
2015-06-21 20:23:58 -06:00
|
|
|
//
|
|
|
|
// Only the highest-precedence blocker type takes effect.
|
2018-10-23 23:12:58 -06:00
|
|
|
device::mojom::WakeLockType new_lock_type =
|
2018-04-11 03:00:59 -06:00
|
|
|
device::mojom::WakeLockType::kPreventAppSuspension;
|
2018-10-23 23:12:58 -06:00
|
|
|
for (const auto& element : wake_lock_types_) {
|
2018-09-19 05:10:26 -06:00
|
|
|
if (element.second == device::mojom::WakeLockType::kPreventDisplaySleep) {
|
2018-10-23 23:12:58 -06:00
|
|
|
new_lock_type = device::mojom::WakeLockType::kPreventDisplaySleep;
|
2015-06-21 20:23:58 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:12:58 -06:00
|
|
|
if (current_lock_type_ != new_lock_type) {
|
|
|
|
GetWakeLock()->ChangeType(new_lock_type, base::DoNothing());
|
|
|
|
current_lock_type_ = new_lock_type;
|
|
|
|
}
|
|
|
|
if (!is_wake_lock_active_) {
|
|
|
|
GetWakeLock()->RequestWakeLock();
|
|
|
|
is_wake_lock_active_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
device::mojom::WakeLock* PowerSaveBlocker::GetWakeLock() {
|
|
|
|
if (!wake_lock_) {
|
2019-12-10 17:22:35 -07:00
|
|
|
mojo::Remote<device::mojom::WakeLockProvider> wake_lock_provider;
|
2020-01-17 11:41:52 -07:00
|
|
|
content::GetDeviceService().BindWakeLockProvider(
|
2019-12-10 17:22:35 -07:00
|
|
|
wake_lock_provider.BindNewPipeAndPassReceiver());
|
2018-10-23 23:12:58 -06:00
|
|
|
|
|
|
|
wake_lock_provider->GetWakeLockWithoutContext(
|
|
|
|
device::mojom::WakeLockType::kPreventAppSuspension,
|
2019-06-17 14:37:55 -06:00
|
|
|
device::mojom::WakeLockReason::kOther, ELECTRON_PRODUCT_NAME,
|
2019-12-10 17:22:35 -07:00
|
|
|
wake_lock_.BindNewPipeAndPassReceiver());
|
2015-06-21 20:23:58 -06:00
|
|
|
}
|
2018-10-23 23:12:58 -06:00
|
|
|
return wake_lock_.get();
|
2015-06-21 20:23:58 -06:00
|
|
|
}
|
|
|
|
|
2018-04-11 03:00:59 -06:00
|
|
|
int PowerSaveBlocker::Start(device::mojom::WakeLockType type) {
|
2015-06-21 20:23:58 -06:00
|
|
|
static int count = 0;
|
2018-10-23 23:12:58 -06:00
|
|
|
wake_lock_types_[count] = type;
|
2015-06-21 20:23:58 -06:00
|
|
|
UpdatePowerSaveBlocker();
|
|
|
|
return count++;
|
2015-06-21 06:57:42 -06:00
|
|
|
}
|
|
|
|
|
2015-06-21 20:23:58 -06:00
|
|
|
bool PowerSaveBlocker::Stop(int id) {
|
2018-10-23 23:12:58 -06:00
|
|
|
bool success = wake_lock_types_.erase(id) > 0;
|
2015-06-21 20:23:58 -06:00
|
|
|
UpdatePowerSaveBlocker();
|
|
|
|
return success;
|
2015-06-21 06:57:42 -06:00
|
|
|
}
|
|
|
|
|
2023-05-30 02:28:43 -06:00
|
|
|
bool PowerSaveBlocker::IsStarted(int id) const {
|
|
|
|
return base::Contains(wake_lock_types_, id);
|
2015-06-21 06:57:42 -06:00
|
|
|
}
|
|
|
|
|
2016-04-24 19:17:54 -06:00
|
|
|
// static
|
2019-05-01 14:24:01 -06:00
|
|
|
gin::Handle<PowerSaveBlocker> PowerSaveBlocker::Create(v8::Isolate* isolate) {
|
|
|
|
return gin::CreateHandle(isolate, new PowerSaveBlocker(isolate));
|
2015-06-21 06:57:42 -06:00
|
|
|
}
|
|
|
|
|
2019-05-01 14:24:01 -06:00
|
|
|
gin::ObjectTemplateBuilder PowerSaveBlocker::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return gin::Wrappable<PowerSaveBlocker>::GetObjectTemplateBuilder(isolate)
|
2016-04-24 19:17:54 -06:00
|
|
|
.SetMethod("start", &PowerSaveBlocker::Start)
|
|
|
|
.SetMethod("stop", &PowerSaveBlocker::Stop)
|
|
|
|
.SetMethod("isStarted", &PowerSaveBlocker::IsStarted);
|
2015-06-21 06:57:42 -06:00
|
|
|
}
|
|
|
|
|
2024-03-06 04:45:28 -07:00
|
|
|
const char* PowerSaveBlocker::GetTypeName() {
|
|
|
|
return "PowerSaveBlocker";
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:55:47 -06:00
|
|
|
} // namespace electron::api
|
2015-06-21 06:57:42 -06:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-04-17 19:55:30 -06:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2015-06-21 06:57:42 -06:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2019-05-01 14:24:01 -06:00
|
|
|
gin::Dictionary dict(isolate, exports);
|
2019-06-19 15:23:04 -06:00
|
|
|
dict.Set("powerSaveBlocker",
|
|
|
|
electron::api::PowerSaveBlocker::Create(isolate));
|
2015-06-21 06:57:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-02-08 18:31:38 -07:00
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_power_save_blocker,
|
|
|
|
Initialize)
|