2015-06-21 12:57:42 +00: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.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/electron_api_power_save_blocker.h"
|
2015-06-21 12:57:42 +00:00
|
|
|
|
2015-06-24 04:49:43 +00:00
|
|
|
#include <string>
|
|
|
|
|
2020-11-14 00:16:56 +00:00
|
|
|
#include "base/callback_helpers.h"
|
2018-04-09 09:16:52 +00:00
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2020-01-17 18:41:52 +00:00
|
|
|
#include "content/public/browser/device_service.h"
|
2019-05-01 20:24:01 +00:00
|
|
|
#include "gin/dictionary.h"
|
|
|
|
#include "gin/function_template.h"
|
2018-10-24 05:12:58 +00:00
|
|
|
#include "services/device/public/mojom/wake_lock_provider.mojom.h"
|
|
|
|
#include "services/service_manager/public/cpp/connector.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2015-06-21 12:57:42 +00:00
|
|
|
|
2019-05-01 20:24:01 +00:00
|
|
|
namespace gin {
|
2015-06-21 12:57:42 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <>
|
2018-04-11 09:00:59 +00:00
|
|
|
struct Converter<device::mojom::WakeLockType> {
|
2015-06-21 12:57:42 +00:00
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
2018-04-11 09:00:59 +00:00
|
|
|
device::mojom::WakeLockType* out) {
|
2015-06-24 04:49:43 +00:00
|
|
|
std::string type;
|
2015-06-21 12:57:42 +00:00
|
|
|
if (!ConvertFromV8(isolate, val, &type))
|
|
|
|
return false;
|
2015-06-24 04:49:43 +00:00
|
|
|
if (type == "prevent-app-suspension")
|
2018-04-11 09:00:59 +00:00
|
|
|
*out = device::mojom::WakeLockType::kPreventAppSuspension;
|
2015-06-24 04:49:43 +00:00
|
|
|
else if (type == "prevent-display-sleep")
|
2018-04-11 09:00:59 +00:00
|
|
|
*out = device::mojom::WakeLockType::kPreventDisplaySleep;
|
2015-06-24 04:49:43 +00:00
|
|
|
else
|
|
|
|
return false;
|
2015-06-21 12:57:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-01 20:24:01 +00:00
|
|
|
} // namespace gin
|
2015-06-21 12:57:42 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2019-05-01 20:24:01 +00:00
|
|
|
gin::WrapperInfo PowerSaveBlocker::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
PowerSaveBlocker::PowerSaveBlocker(v8::Isolate* isolate)
|
2021-01-26 18:16:21 +00:00
|
|
|
: current_lock_type_(device::mojom::WakeLockType::kPreventAppSuspension) {}
|
2015-06-21 12:57:42 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
PowerSaveBlocker::~PowerSaveBlocker() = default;
|
2015-06-21 12:57:42 +00:00
|
|
|
|
2015-06-22 02:23:58 +00:00
|
|
|
void PowerSaveBlocker::UpdatePowerSaveBlocker() {
|
2018-10-24 05:12:58 +00:00
|
|
|
if (wake_lock_types_.empty()) {
|
|
|
|
if (is_wake_lock_active_) {
|
|
|
|
GetWakeLock()->CancelWakeLock();
|
|
|
|
is_wake_lock_active_ = false;
|
|
|
|
}
|
2015-06-22 02:23:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-11 09:00:59 +00:00
|
|
|
// |WakeLockType::kPreventAppSuspension| keeps system active, but allows
|
2015-06-22 02:23:58 +00:00
|
|
|
// screen to be turned off.
|
2018-04-11 09:00:59 +00:00
|
|
|
// |WakeLockType::kPreventDisplaySleep| keeps system and screen active, has a
|
|
|
|
// higher precedence level than |WakeLockType::kPreventAppSuspension|.
|
2015-06-22 02:23:58 +00:00
|
|
|
//
|
|
|
|
// Only the highest-precedence blocker type takes effect.
|
2018-10-24 05:12:58 +00:00
|
|
|
device::mojom::WakeLockType new_lock_type =
|
2018-04-11 09:00:59 +00:00
|
|
|
device::mojom::WakeLockType::kPreventAppSuspension;
|
2018-10-24 05:12:58 +00:00
|
|
|
for (const auto& element : wake_lock_types_) {
|
2018-09-19 11:10:26 +00:00
|
|
|
if (element.second == device::mojom::WakeLockType::kPreventDisplaySleep) {
|
2018-10-24 05:12:58 +00:00
|
|
|
new_lock_type = device::mojom::WakeLockType::kPreventDisplaySleep;
|
2015-06-22 02:23:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 05:12:58 +00: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-11 00:22:35 +00:00
|
|
|
mojo::Remote<device::mojom::WakeLockProvider> wake_lock_provider;
|
2020-01-17 18:41:52 +00:00
|
|
|
content::GetDeviceService().BindWakeLockProvider(
|
2019-12-11 00:22:35 +00:00
|
|
|
wake_lock_provider.BindNewPipeAndPassReceiver());
|
2018-10-24 05:12:58 +00:00
|
|
|
|
|
|
|
wake_lock_provider->GetWakeLockWithoutContext(
|
|
|
|
device::mojom::WakeLockType::kPreventAppSuspension,
|
2019-06-17 20:37:55 +00:00
|
|
|
device::mojom::WakeLockReason::kOther, ELECTRON_PRODUCT_NAME,
|
2019-12-11 00:22:35 +00:00
|
|
|
wake_lock_.BindNewPipeAndPassReceiver());
|
2015-06-22 02:23:58 +00:00
|
|
|
}
|
2018-10-24 05:12:58 +00:00
|
|
|
return wake_lock_.get();
|
2015-06-22 02:23:58 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 09:00:59 +00:00
|
|
|
int PowerSaveBlocker::Start(device::mojom::WakeLockType type) {
|
2015-06-22 02:23:58 +00:00
|
|
|
static int count = 0;
|
2018-10-24 05:12:58 +00:00
|
|
|
wake_lock_types_[count] = type;
|
2015-06-22 02:23:58 +00:00
|
|
|
UpdatePowerSaveBlocker();
|
|
|
|
return count++;
|
2015-06-21 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 02:23:58 +00:00
|
|
|
bool PowerSaveBlocker::Stop(int id) {
|
2018-10-24 05:12:58 +00:00
|
|
|
bool success = wake_lock_types_.erase(id) > 0;
|
2015-06-22 02:23:58 +00:00
|
|
|
UpdatePowerSaveBlocker();
|
|
|
|
return success;
|
2015-06-21 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 02:23:58 +00:00
|
|
|
bool PowerSaveBlocker::IsStarted(int id) {
|
2018-10-24 05:12:58 +00:00
|
|
|
return wake_lock_types_.find(id) != wake_lock_types_.end();
|
2015-06-21 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
// static
|
2019-05-01 20:24:01 +00:00
|
|
|
gin::Handle<PowerSaveBlocker> PowerSaveBlocker::Create(v8::Isolate* isolate) {
|
|
|
|
return gin::CreateHandle(isolate, new PowerSaveBlocker(isolate));
|
2015-06-21 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:24:01 +00:00
|
|
|
gin::ObjectTemplateBuilder PowerSaveBlocker::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return gin::Wrappable<PowerSaveBlocker>::GetObjectTemplateBuilder(isolate)
|
2016-04-25 01:17:54 +00:00
|
|
|
.SetMethod("start", &PowerSaveBlocker::Start)
|
|
|
|
.SetMethod("stop", &PowerSaveBlocker::Stop)
|
|
|
|
.SetMethod("isStarted", &PowerSaveBlocker::IsStarted);
|
2015-06-21 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace electron
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2015-06-21 12:57:42 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2019-05-01 20:24:01 +00:00
|
|
|
gin::Dictionary dict(isolate, exports);
|
2015-06-21 12:57:42 +00:00
|
|
|
dict.Set("powerSaveBlocker",
|
|
|
|
electron::api::PowerSaveBlocker::Create(isolate));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_power_save_blocker,
|
|
|
|
Initialize)
|