From 7c5afdd3881130a68f4f0e33156d007a1e38bffb Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Sun, 21 Jun 2015 20:57:42 +0800 Subject: [PATCH] Add PowerSaveBlocker APIs. --- atom/browser/api/atom_api_power_save_blocker.cc | 94 +++++++++++++++++++++++++ atom/browser/api/atom_api_power_save_blocker.h | 46 ++++++++++++ atom/browser/api/lib/power-save-blocker.coffee | 8 +++ atom/common/node_bindings.cc | 1 + filenames.gypi | 3 + 5 files changed, 152 insertions(+) create mode 100644 atom/browser/api/atom_api_power_save_blocker.cc create mode 100644 atom/browser/api/atom_api_power_save_blocker.h create mode 100644 atom/browser/api/lib/power-save-blocker.coffee diff --git a/atom/browser/api/atom_api_power_save_blocker.cc b/atom/browser/api/atom_api_power_save_blocker.cc new file mode 100644 index 0000000..98a84d0 --- /dev/null +++ b/atom/browser/api/atom_api_power_save_blocker.cc @@ -0,0 +1,94 @@ +// 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 "content/public/browser/power_save_blocker.h" +#include "native_mate/constructor.h" +#include "native_mate/dictionary.h" + +#include "atom/common/node_includes.h" + +namespace mate { + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + content::PowerSaveBlocker::PowerSaveBlockerType* out) { + using content::PowerSaveBlocker; + int type; + if (!ConvertFromV8(isolate, val, &type)) + return false; + switch (static_cast(type)) { + case PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension: + *out = PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension; + break; + case PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep: + *out = PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep; + break; + default: + return false; + } + return true; + } +}; + +} // namespace mate + +namespace atom { + +namespace api { + +PowerSaveBlocker::PowerSaveBlocker() { +} + +PowerSaveBlocker::~PowerSaveBlocker() { +} + +void PowerSaveBlocker::Start( + content::PowerSaveBlocker::PowerSaveBlockerType type) { + power_save_blocker_ = content::PowerSaveBlocker::Create( + type, + content::PowerSaveBlocker::kReasonOther, + "Users required"); +} + +void PowerSaveBlocker::Stop() { + power_save_blocker_.reset(); +} + +bool PowerSaveBlocker::IsStarted() { + return power_save_blocker_.get() != NULL; +} + +mate::ObjectTemplateBuilder PowerSaveBlocker::GetObjectTemplateBuilder( + v8::Isolate* isolate) { + return mate::ObjectTemplateBuilder(isolate) + .SetMethod("start", &PowerSaveBlocker::Start) + .SetMethod("stop", &PowerSaveBlocker::Stop); + .SetMethod("isStarted", &PowerSaveBlocker::IsStarted) +} + +// static +mate::Handle PowerSaveBlocker::Create(v8::Isolate* isolate) { + return CreateHandle(isolate, new PowerSaveBlocker); +} + +} // namespace api + +} // namespace atom + +namespace { + +void Initialize(v8::Local exports, v8::Local unused, + v8::Local 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); diff --git a/atom/browser/api/atom_api_power_save_blocker.h b/atom/browser/api/atom_api_power_save_blocker.h new file mode 100644 index 0000000..0333842 --- /dev/null +++ b/atom/browser/api/atom_api_power_save_blocker.h @@ -0,0 +1,46 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_API_ATOM_API_POWER_SAVE_BLOCKER_H_ +#define ATOM_BROWSER_API_ATOM_API_POWER_SAVE_BLOCKER_H_ + +#include "base/memory/scoped_ptr.h" +#include "content/public/browser/power_save_blocker.h" +#include "native_mate/handle.h" +#include "native_mate/wrappable.h" + +namespace mate { +class Dictionary; +} + +namespace atom { + +namespace api { + +class PowerSaveBlocker : public mate::Wrappable { + public: + static mate::Handle Create(v8::Isolate* isolate); + + protected: + PowerSaveBlocker(); + virtual ~PowerSaveBlocker(); + + // mate::Wrappable implementations: + mate::ObjectTemplateBuilder GetObjectTemplateBuilder( + v8::Isolate* isolate) override; + + private: + void Start(content::PowerSaveBlocker::PowerSaveBlockerType type); + void Stop(); + bool IsStarted(); + + scoped_ptr power_save_blocker_; + DISALLOW_COPY_AND_ASSIGN(PowerSaveBlocker); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_BROWSER_API_ATOM_API_POWER_SAVE_BLOCKER_H_ diff --git a/atom/browser/api/lib/power-save-blocker.coffee b/atom/browser/api/lib/power-save-blocker.coffee new file mode 100644 index 0000000..6ac8b9b --- /dev/null +++ b/atom/browser/api/lib/power-save-blocker.coffee @@ -0,0 +1,8 @@ +bindings = process.atomBinding 'power_save_blocker' + +powerSaveBlocker = bindings.powerSaveBlocker + +powerSaveBlocker.PREVENT_APP_SUSPENSION = 0 +powerSaveBlocker.PREVENT_DISPLAY_SLEEP = 1 + +module.exports = powerSaveBlocker diff --git a/atom/common/node_bindings.cc b/atom/common/node_bindings.cc index eb627d0..8e3db80 100644 --- a/atom/common/node_bindings.cc +++ b/atom/common/node_bindings.cc @@ -35,6 +35,7 @@ REFERENCE_MODULE(atom_browser_content_tracing); REFERENCE_MODULE(atom_browser_dialog); REFERENCE_MODULE(atom_browser_menu); REFERENCE_MODULE(atom_browser_power_monitor); +REFERENCE_MODULE(atom_browser_power_save_blocker); REFERENCE_MODULE(atom_browser_protocol); REFERENCE_MODULE(atom_browser_global_shortcut); REFERENCE_MODULE(atom_browser_tray); diff --git a/filenames.gypi b/filenames.gypi index 99d37b5..0c16a62 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -20,6 +20,7 @@ 'atom/browser/api/lib/menu-item.coffee', 'atom/browser/api/lib/navigation-controller.coffee', 'atom/browser/api/lib/power-monitor.coffee', + 'atom/browser/api/lib/power-save-blocker.coffee', 'atom/browser/api/lib/protocol.coffee', 'atom/browser/api/lib/screen.coffee', 'atom/browser/api/lib/tray.coffee', @@ -80,6 +81,8 @@ 'atom/browser/api/atom_api_menu_mac.mm', 'atom/browser/api/atom_api_power_monitor.cc', 'atom/browser/api/atom_api_power_monitor.h', + 'atom/browser/api/atom_api_power_save_blocker.cc', + 'atom/browser/api/atom_api_power_save_blocker.h', 'atom/browser/api/atom_api_protocol.cc', 'atom/browser/api/atom_api_protocol.h', 'atom/browser/api/atom_api_screen.cc', -- 2.7.4