electron/atom/browser/api/atom_api_power_save_blocker.cc

155 строки
4.7 KiB
C++
Исходник Обычный вид История

2015-06-21 15:57:42 +03: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.
#include "atom/browser/api/atom_api_power_save_blocker.h"
#include <string>
#include "base/task/post_task.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/public/common/service_manager_connection.h"
2015-06-21 15:57:42 +03:00
#include "native_mate/dictionary.h"
#include "services/device/public/mojom/constants.mojom.h"
#include "services/device/public/mojom/wake_lock_provider.mojom.h"
#include "services/service_manager/public/cpp/connector.h"
2015-06-21 15:57:42 +03:00
2016-09-06 11:24:37 +03:00
#include "atom/common/node_includes.h"
2015-06-21 15:57:42 +03:00
namespace mate {
2018-04-18 04:55:30 +03:00
template <>
struct Converter<device::mojom::WakeLockType> {
2015-06-21 15:57:42 +03:00
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
device::mojom::WakeLockType* out) {
std::string type;
2015-06-21 15:57:42 +03:00
if (!ConvertFromV8(isolate, val, &type))
return false;
if (type == "prevent-app-suspension")
*out = device::mojom::WakeLockType::kPreventAppSuspension;
else if (type == "prevent-display-sleep")
*out = device::mojom::WakeLockType::kPreventDisplaySleep;
else
return false;
2015-06-21 15:57:42 +03:00
return true;
}
};
} // namespace mate
namespace atom {
namespace api {
2016-04-25 04:17:54 +03:00
PowerSaveBlocker::PowerSaveBlocker(v8::Isolate* isolate)
: current_lock_type_(device::mojom::WakeLockType::kPreventAppSuspension),
is_wake_lock_active_(false) {
2016-04-25 04:17:54 +03:00
Init(isolate);
2015-06-21 15:57:42 +03:00
}
2018-04-18 04:55:30 +03:00
PowerSaveBlocker::~PowerSaveBlocker() {}
2015-06-21 15:57:42 +03:00
2015-06-22 05:23:58 +03:00
void PowerSaveBlocker::UpdatePowerSaveBlocker() {
if (wake_lock_types_.empty()) {
if (is_wake_lock_active_) {
GetWakeLock()->CancelWakeLock();
is_wake_lock_active_ = false;
}
2015-06-22 05:23:58 +03:00
return;
}
// |WakeLockType::kPreventAppSuspension| keeps system active, but allows
2015-06-22 05:23:58 +03:00
// screen to be turned off.
// |WakeLockType::kPreventDisplaySleep| keeps system and screen active, has a
// higher precedence level than |WakeLockType::kPreventAppSuspension|.
2015-06-22 05:23:58 +03:00
//
// Only the highest-precedence blocker type takes effect.
device::mojom::WakeLockType new_lock_type =
device::mojom::WakeLockType::kPreventAppSuspension;
for (const auto& element : wake_lock_types_) {
if (element.second == device::mojom::WakeLockType::kPreventDisplaySleep) {
new_lock_type = device::mojom::WakeLockType::kPreventDisplaySleep;
2015-06-22 05:23:58 +03:00
break;
}
}
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_) {
device::mojom::WakeLockProviderPtr wake_lock_provider;
DCHECK(content::ServiceManagerConnection::GetForProcess());
auto* connector =
content::ServiceManagerConnection::GetForProcess()->GetConnector();
connector->BindInterface(device::mojom::kServiceName,
mojo::MakeRequest(&wake_lock_provider));
wake_lock_provider->GetWakeLockWithoutContext(
device::mojom::WakeLockType::kPreventAppSuspension,
device::mojom::WakeLockReason::kOther, ATOM_PRODUCT_NAME,
mojo::MakeRequest(&wake_lock_));
2015-06-22 05:23:58 +03:00
}
return wake_lock_.get();
2015-06-22 05:23:58 +03:00
}
int PowerSaveBlocker::Start(device::mojom::WakeLockType type) {
2015-06-22 05:23:58 +03:00
static int count = 0;
wake_lock_types_[count] = type;
2015-06-22 05:23:58 +03:00
UpdatePowerSaveBlocker();
return count++;
2015-06-21 15:57:42 +03:00
}
2015-06-22 05:23:58 +03:00
bool PowerSaveBlocker::Stop(int id) {
bool success = wake_lock_types_.erase(id) > 0;
2015-06-22 05:23:58 +03:00
UpdatePowerSaveBlocker();
return success;
2015-06-21 15:57:42 +03:00
}
2015-06-22 05:23:58 +03:00
bool PowerSaveBlocker::IsStarted(int id) {
return wake_lock_types_.find(id) != wake_lock_types_.end();
2015-06-21 15:57:42 +03:00
}
2016-04-25 04:17:54 +03:00
// static
mate::Handle<PowerSaveBlocker> PowerSaveBlocker::Create(v8::Isolate* isolate) {
2016-04-25 04:40:19 +03:00
return mate::CreateHandle(isolate, new PowerSaveBlocker(isolate));
2015-06-21 15:57:42 +03:00
}
// static
2016-04-25 04:17:54 +03:00
void PowerSaveBlocker::BuildPrototype(
2018-04-18 04:55:30 +03:00
v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
2016-08-02 13:28:12 +03:00
prototype->SetClassName(mate::StringToV8(isolate, "PowerSaveBlocker"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
2016-04-25 04:17:54 +03:00
.SetMethod("start", &PowerSaveBlocker::Start)
.SetMethod("stop", &PowerSaveBlocker::Stop)
.SetMethod("isStarted", &PowerSaveBlocker::IsStarted);
2015-06-21 15:57:42 +03:00
}
} // namespace api
} // namespace atom
namespace {
2018-04-18 04:55:30 +03:00
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
2015-06-21 15:57:42 +03:00
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("powerSaveBlocker", atom::api::PowerSaveBlocker::Create(isolate));
}
} // namespace
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_power_save_blocker, Initialize);