Add PowerSaveBlocker APIs.

This commit is contained in:
Haojian Wu 2015-06-21 20:57:42 +08:00
parent a1cbd11b5b
commit 7c5afdd388
5 changed files with 152 additions and 0 deletions

View file

@ -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<content::PowerSaveBlocker::PowerSaveBlockerType> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
content::PowerSaveBlocker::PowerSaveBlockerType* out) {
using content::PowerSaveBlocker;
int type;
if (!ConvertFromV8(isolate, val, &type))
return false;
switch (static_cast<PowerSaveBlocker::PowerSaveBlockerType>(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> PowerSaveBlocker::Create(v8::Isolate* isolate) {
return CreateHandle(isolate, new PowerSaveBlocker);
}
} // 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);

View file

@ -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<PowerSaveBlocker> 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<content::PowerSaveBlocker> power_save_blocker_;
DISALLOW_COPY_AND_ASSIGN(PowerSaveBlocker);
};
} // namespace api
} // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_POWER_SAVE_BLOCKER_H_

View file

@ -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

View file

@ -35,6 +35,7 @@ REFERENCE_MODULE(atom_browser_content_tracing);
REFERENCE_MODULE(atom_browser_dialog); REFERENCE_MODULE(atom_browser_dialog);
REFERENCE_MODULE(atom_browser_menu); REFERENCE_MODULE(atom_browser_menu);
REFERENCE_MODULE(atom_browser_power_monitor); REFERENCE_MODULE(atom_browser_power_monitor);
REFERENCE_MODULE(atom_browser_power_save_blocker);
REFERENCE_MODULE(atom_browser_protocol); REFERENCE_MODULE(atom_browser_protocol);
REFERENCE_MODULE(atom_browser_global_shortcut); REFERENCE_MODULE(atom_browser_global_shortcut);
REFERENCE_MODULE(atom_browser_tray); REFERENCE_MODULE(atom_browser_tray);

View file

@ -20,6 +20,7 @@
'atom/browser/api/lib/menu-item.coffee', 'atom/browser/api/lib/menu-item.coffee',
'atom/browser/api/lib/navigation-controller.coffee', 'atom/browser/api/lib/navigation-controller.coffee',
'atom/browser/api/lib/power-monitor.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/protocol.coffee',
'atom/browser/api/lib/screen.coffee', 'atom/browser/api/lib/screen.coffee',
'atom/browser/api/lib/tray.coffee', 'atom/browser/api/lib/tray.coffee',
@ -80,6 +81,8 @@
'atom/browser/api/atom_api_menu_mac.mm', 'atom/browser/api/atom_api_menu_mac.mm',
'atom/browser/api/atom_api_power_monitor.cc', 'atom/browser/api/atom_api_power_monitor.cc',
'atom/browser/api/atom_api_power_monitor.h', '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.cc',
'atom/browser/api/atom_api_protocol.h', 'atom/browser/api/atom_api_protocol.h',
'atom/browser/api/atom_api_screen.cc', 'atom/browser/api/atom_api_screen.cc',