electron/atom/browser/api/atom_api_power_save_blocker.cc

137 строки
4.2 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>
2016-09-06 11:24:37 +03:00
#include "content/public/browser/browser_thread.h"
2015-06-21 15:57:42 +03:00
#include "native_mate/dictionary.h"
2016-09-06 11:24:37 +03:00
#include "atom/common/node_includes.h"
2016-11-30 10:30:03 +03:00
using content::BrowserThread;
2015-06-21 15:57:42 +03:00
namespace mate {
template<>
2016-09-06 11:24:37 +03:00
struct Converter<device::PowerSaveBlocker::PowerSaveBlockerType> {
2015-06-21 15:57:42 +03:00
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
2016-09-06 11:24:37 +03:00
device::PowerSaveBlocker::PowerSaveBlockerType* out) {
using device::PowerSaveBlocker;
std::string type;
2015-06-21 15:57:42 +03:00
if (!ConvertFromV8(isolate, val, &type))
return false;
if (type == "prevent-app-suspension")
*out = PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension;
else if (type == "prevent-display-sleep")
*out = PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep;
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_blocker_type_(
2016-09-06 11:24:37 +03:00
device::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension) {
2016-04-25 04:17:54 +03:00
Init(isolate);
2015-06-21 15:57:42 +03:00
}
PowerSaveBlocker::~PowerSaveBlocker() {
}
2015-06-22 05:23:58 +03:00
void PowerSaveBlocker::UpdatePowerSaveBlocker() {
if (power_save_blocker_types_.empty()) {
power_save_blocker_.reset();
return;
}
// |kPowerSaveBlockPreventAppSuspension| keeps system active, but allows
// screen to be turned off.
// |kPowerSaveBlockPreventDisplaySleep| keeps system and screen active, has a
// higher precedence level than |kPowerSaveBlockPreventAppSuspension|.
//
// Only the highest-precedence blocker type takes effect.
2016-09-06 11:24:37 +03:00
device::PowerSaveBlocker::PowerSaveBlockerType new_blocker_type =
device::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension;
2015-06-22 05:23:58 +03:00
for (const auto& element : power_save_blocker_types_) {
if (element.second ==
2016-09-06 11:24:37 +03:00
device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep) {
2015-06-22 05:23:58 +03:00
new_blocker_type =
2016-09-06 11:24:37 +03:00
device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep;
2015-06-22 05:23:58 +03:00
break;
}
}
if (!power_save_blocker_ || new_blocker_type != current_blocker_type_) {
2016-09-06 11:24:37 +03:00
std::unique_ptr<device::PowerSaveBlocker> new_blocker(
new device::PowerSaveBlocker(
2015-06-22 05:23:58 +03:00
new_blocker_type,
2016-09-06 11:24:37 +03:00
device::PowerSaveBlocker::kReasonOther,
ATOM_PRODUCT_NAME,
2016-11-30 10:30:03 +03:00
BrowserThread::GetTaskRunnerForThread(BrowserThread::UI),
BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)));
2015-06-22 05:23:58 +03:00
power_save_blocker_.swap(new_blocker);
current_blocker_type_ = new_blocker_type;
}
}
int PowerSaveBlocker::Start(
2016-09-06 11:24:37 +03:00
device::PowerSaveBlocker::PowerSaveBlockerType type) {
2015-06-22 05:23:58 +03:00
static int count = 0;
power_save_blocker_types_[count] = type;
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 = power_save_blocker_types_.erase(id) > 0;
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 power_save_blocker_types_.find(id) != power_save_blocker_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(
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 {
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("powerSaveBlocker", atom::api::PowerSaveBlocker::Create(isolate));
}
} // namespace
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_power_save_blocker, Initialize);