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"
|
|
|
|
|
2015-06-24 07:49:43 +03:00
|
|
|
#include <string>
|
|
|
|
|
2018-10-25 09:09:49 +03:00
|
|
|
#include "base/task/post_task.h"
|
2018-04-09 12:16:52 +03:00
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2018-10-24 08:12:58 +03:00
|
|
|
#include "content/public/common/service_manager_connection.h"
|
2015-06-21 15:57:42 +03:00
|
|
|
#include "native_mate/dictionary.h"
|
2018-10-24 08:12:58 +03:00
|
|
|
#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 <>
|
2018-04-11 12:00:59 +03:00
|
|
|
struct Converter<device::mojom::WakeLockType> {
|
2015-06-21 15:57:42 +03:00
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
2018-04-11 12:00:59 +03:00
|
|
|
device::mojom::WakeLockType* out) {
|
2015-06-24 07:49:43 +03:00
|
|
|
std::string type;
|
2015-06-21 15:57:42 +03:00
|
|
|
if (!ConvertFromV8(isolate, val, &type))
|
|
|
|
return false;
|
2015-06-24 07:49:43 +03:00
|
|
|
if (type == "prevent-app-suspension")
|
2018-04-11 12:00:59 +03:00
|
|
|
*out = device::mojom::WakeLockType::kPreventAppSuspension;
|
2015-06-24 07:49:43 +03:00
|
|
|
else if (type == "prevent-display-sleep")
|
2018-04-11 12:00:59 +03:00
|
|
|
*out = device::mojom::WakeLockType::kPreventDisplaySleep;
|
2015-06-24 07:49:43 +03:00
|
|
|
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)
|
2018-10-24 08:12:58 +03:00
|
|
|
: 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() {
|
2018-10-24 08:12:58 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-11 12:00:59 +03:00
|
|
|
// |WakeLockType::kPreventAppSuspension| keeps system active, but allows
|
2015-06-22 05:23:58 +03:00
|
|
|
// screen to be turned off.
|
2018-04-11 12:00:59 +03:00
|
|
|
// |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.
|
2018-10-24 08:12:58 +03:00
|
|
|
device::mojom::WakeLockType new_lock_type =
|
2018-04-11 12:00:59 +03:00
|
|
|
device::mojom::WakeLockType::kPreventAppSuspension;
|
2018-10-24 08:12:58 +03:00
|
|
|
for (const auto& element : wake_lock_types_) {
|
2018-09-19 14:10:26 +03:00
|
|
|
if (element.second == device::mojom::WakeLockType::kPreventDisplaySleep) {
|
2018-10-24 08:12:58 +03:00
|
|
|
new_lock_type = device::mojom::WakeLockType::kPreventDisplaySleep;
|
2015-06-22 05:23:58 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:12:58 +03: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_) {
|
|
|
|
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
|
|
|
}
|
2018-10-24 08:12:58 +03:00
|
|
|
return wake_lock_.get();
|
2015-06-22 05:23:58 +03:00
|
|
|
}
|
|
|
|
|
2018-04-11 12:00:59 +03:00
|
|
|
int PowerSaveBlocker::Start(device::mojom::WakeLockType type) {
|
2015-06-22 05:23:58 +03:00
|
|
|
static int count = 0;
|
2018-10-24 08:12:58 +03:00
|
|
|
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) {
|
2018-10-24 08:12:58 +03:00
|
|
|
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) {
|
2018-10-24 08:12:58 +03:00
|
|
|
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"));
|
2016-08-02 12:08:12 +03:00
|
|
|
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
|
|
|
|
|
2018-01-06 18:58:24 +03:00
|
|
|
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_power_save_blocker, Initialize);
|