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.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_power_save_blocker.h"
|
|
|
|
|
2015-06-24 04:49:43 +00:00
|
|
|
#include <string>
|
|
|
|
|
2018-04-09 09:16:52 +00:00
|
|
|
#include "base/task_scheduler/post_task.h"
|
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2015-06-21 12:57:42 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
|
2016-09-06 08:24:37 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
2015-06-21 12:57:42 +00:00
|
|
|
namespace mate {
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
PowerSaveBlocker::PowerSaveBlocker(v8::Isolate* isolate)
|
2015-06-24 04:49:43 +00:00
|
|
|
: current_blocker_type_(
|
2018-04-11 09:00:59 +00:00
|
|
|
device::mojom::WakeLockType::kPreventAppSuspension) {
|
2016-04-25 01:17:54 +00:00
|
|
|
Init(isolate);
|
2015-06-21 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
PowerSaveBlocker::~PowerSaveBlocker() {}
|
2015-06-21 12:57:42 +00:00
|
|
|
|
2015-06-22 02:23:58 +00:00
|
|
|
void PowerSaveBlocker::UpdatePowerSaveBlocker() {
|
|
|
|
if (power_save_blocker_types_.empty()) {
|
|
|
|
power_save_blocker_.reset();
|
|
|
|
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-04-11 09:00:59 +00:00
|
|
|
device::mojom::WakeLockType new_blocker_type =
|
|
|
|
device::mojom::WakeLockType::kPreventAppSuspension;
|
2015-06-22 02:23:58 +00:00
|
|
|
for (const auto& element : power_save_blocker_types_) {
|
2018-09-19 11:10:26 +00:00
|
|
|
if (element.second == device::mojom::WakeLockType::kPreventDisplaySleep) {
|
|
|
|
new_blocker_type = device::mojom::WakeLockType::kPreventDisplaySleep;
|
2015-06-22 02:23:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!power_save_blocker_ || new_blocker_type != current_blocker_type_) {
|
2018-06-18 07:32:55 +00:00
|
|
|
auto new_blocker = std::make_unique<device::PowerSaveBlocker>(
|
2018-04-11 09:00:59 +00:00
|
|
|
new_blocker_type, device::mojom::WakeLockReason::kOther,
|
2018-04-09 09:16:52 +00:00
|
|
|
ATOM_PRODUCT_NAME, base::ThreadTaskRunnerHandle::Get(),
|
|
|
|
// This task runner may be used by some device service
|
|
|
|
// implementation bits to interface with dbus client code, which in
|
|
|
|
// turn imposes some subtle thread affinity on the clients. We
|
|
|
|
// therefore require a single-thread runner.
|
|
|
|
base::CreateSingleThreadTaskRunnerWithTraits(
|
|
|
|
{base::MayBlock(), base::TaskPriority::BACKGROUND}));
|
2015-06-22 02:23:58 +00:00
|
|
|
power_save_blocker_.swap(new_blocker);
|
|
|
|
current_blocker_type_ = new_blocker_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
power_save_blocker_types_[count] = type;
|
|
|
|
UpdatePowerSaveBlocker();
|
|
|
|
return count++;
|
2015-06-21 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 02:23:58 +00:00
|
|
|
bool PowerSaveBlocker::Stop(int id) {
|
|
|
|
bool success = power_save_blocker_types_.erase(id) > 0;
|
|
|
|
UpdatePowerSaveBlocker();
|
|
|
|
return success;
|
2015-06-21 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 02:23:58 +00:00
|
|
|
bool PowerSaveBlocker::IsStarted(int id) {
|
|
|
|
return power_save_blocker_types_.find(id) != power_save_blocker_types_.end();
|
2015-06-21 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
// static
|
|
|
|
mate::Handle<PowerSaveBlocker> PowerSaveBlocker::Create(v8::Isolate* isolate) {
|
2016-04-25 01:40:19 +00:00
|
|
|
return mate::CreateHandle(isolate, new PowerSaveBlocker(isolate));
|
2015-06-21 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2016-04-25 01:17:54 +00:00
|
|
|
void PowerSaveBlocker::BuildPrototype(
|
2018-04-18 01:55:30 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
2016-08-02 10:28:12 +00:00
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "PowerSaveBlocker"));
|
2016-08-02 09:08:12 +00:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
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 atom
|
|
|
|
|
|
|
|
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();
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
|
|
|
dict.Set("powerSaveBlocker", atom::api::PowerSaveBlocker::Create(isolate));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-01-06 15:58:24 +00:00
|
|
|
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_power_save_blocker, Initialize);
|