More updates: use string instead of integer to identify blocker type.
This commit is contained in:
parent
532f75fcab
commit
13784e6551
4 changed files with 29 additions and 43 deletions
|
@ -4,18 +4,12 @@
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_power_save_blocker.h"
|
#include "atom/browser/api/atom_api_power_save_blocker.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "content/public/browser/power_save_blocker.h"
|
#include "content/public/browser/power_save_blocker.h"
|
||||||
#include "native_mate/constructor.h"
|
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
const char kPowerSaveBlockerDescription[] = "Electron";
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
|
@ -24,19 +18,15 @@ struct Converter<content::PowerSaveBlocker::PowerSaveBlockerType> {
|
||||||
v8::Local<v8::Value> val,
|
v8::Local<v8::Value> val,
|
||||||
content::PowerSaveBlocker::PowerSaveBlockerType* out) {
|
content::PowerSaveBlocker::PowerSaveBlockerType* out) {
|
||||||
using content::PowerSaveBlocker;
|
using content::PowerSaveBlocker;
|
||||||
int type;
|
std::string type;
|
||||||
if (!ConvertFromV8(isolate, val, &type))
|
if (!ConvertFromV8(isolate, val, &type))
|
||||||
return false;
|
return false;
|
||||||
switch (static_cast<PowerSaveBlocker::PowerSaveBlockerType>(type)) {
|
if (type == "prevent-app-suspension")
|
||||||
case PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension:
|
*out = PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension;
|
||||||
*out = PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension;
|
else if (type == "prevent-display-sleep")
|
||||||
break;
|
*out = PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep;
|
||||||
case PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep:
|
else
|
||||||
*out = PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep;
|
return false;
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -47,7 +37,9 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
PowerSaveBlocker::PowerSaveBlocker() {
|
PowerSaveBlocker::PowerSaveBlocker()
|
||||||
|
: current_blocker_type_(
|
||||||
|
content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension) {
|
||||||
}
|
}
|
||||||
|
|
||||||
PowerSaveBlocker::~PowerSaveBlocker() {
|
PowerSaveBlocker::~PowerSaveBlocker() {
|
||||||
|
@ -81,7 +73,7 @@ void PowerSaveBlocker::UpdatePowerSaveBlocker() {
|
||||||
content::PowerSaveBlocker::Create(
|
content::PowerSaveBlocker::Create(
|
||||||
new_blocker_type,
|
new_blocker_type,
|
||||||
content::PowerSaveBlocker::kReasonOther,
|
content::PowerSaveBlocker::kReasonOther,
|
||||||
kPowerSaveBlockerDescription);
|
ATOM_PRODUCT_NAME);
|
||||||
power_save_blocker_.swap(new_blocker);
|
power_save_blocker_.swap(new_blocker);
|
||||||
current_blocker_type_ = new_blocker_type;
|
current_blocker_type_ = new_blocker_type;
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,8 +44,8 @@ class PowerSaveBlocker : public mate::Wrappable {
|
||||||
content::PowerSaveBlocker::PowerSaveBlockerType current_blocker_type_;
|
content::PowerSaveBlocker::PowerSaveBlockerType current_blocker_type_;
|
||||||
|
|
||||||
// Map from id to the corresponding blocker type for each request.
|
// Map from id to the corresponding blocker type for each request.
|
||||||
typedef std::map<int, content::PowerSaveBlocker::PowerSaveBlockerType>
|
using PowerSaveBlockerTypeMap =
|
||||||
PowerSaveBlockerTypeMap;
|
std::map<int, content::PowerSaveBlocker::PowerSaveBlockerType>;
|
||||||
PowerSaveBlockerTypeMap power_save_blocker_types_;
|
PowerSaveBlockerTypeMap power_save_blocker_types_;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,3 @@
|
||||||
bindings = process.atomBinding 'power_save_blocker'
|
bindings = process.atomBinding 'power_save_blocker'
|
||||||
|
|
||||||
powerSaveBlocker = bindings.powerSaveBlocker
|
module.exports = bindings.powerSaveBlocker
|
||||||
|
|
||||||
powerSaveBlocker.PREVENT_APP_SUSPENSION = 0
|
|
||||||
powerSaveBlocker.PREVENT_DISPLAY_SLEEP = 1
|
|
||||||
|
|
||||||
module.exports = powerSaveBlocker
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ An example is:
|
||||||
```javascript
|
```javascript
|
||||||
var powerSaveBlocker = require('power-save-blocker');
|
var powerSaveBlocker = require('power-save-blocker');
|
||||||
|
|
||||||
var id = powerSaveBlocker.start(powerSaveBlocker.PREVENT_DISPLAY_SLEEP);
|
var id = powerSaveBlocker.start('prevent-display-sleep');
|
||||||
console.log(powerSaveBlocker.isStarted(id));
|
console.log(powerSaveBlocker.isStarted(id));
|
||||||
|
|
||||||
powerSaveBlocker.stop(id);
|
powerSaveBlocker.stop(id);
|
||||||
|
@ -16,25 +16,24 @@ powerSaveBlocker.stop(id);
|
||||||
|
|
||||||
## powerSaveBlocker.start(type)
|
## powerSaveBlocker.start(type)
|
||||||
|
|
||||||
* `type` - Power save blocker type
|
* `type` String - Power save blocker type
|
||||||
* powerSaveBlocker.PREVENT_APP_SUSPENSION - Prevent the application from being
|
* `prevent-app-suspension` - Prevent the application from being suspended.
|
||||||
suspended. Keeps system active, but allows screen to be turned off.
|
Keeps system active, but allows screen to be turned off. Example use cases:
|
||||||
Example use cases: downloading a file, playing audio.
|
downloading a file, playing audio.
|
||||||
* powerSaveBlocker.PREVENT_DISPLAY_SLEEP - Prevent the display from going to sleep.
|
* `prevent-display-sleep`- Prevent the display from going to sleep. Keeps system
|
||||||
Keeps system and screen active.
|
and screen active. Example use case: playing video.
|
||||||
Example use case: playing video.
|
|
||||||
|
|
||||||
Starts the power save blocker preventing the system entering lower-power mode.
|
Starts the power save blocker preventing the system entering lower-power mode.
|
||||||
Returns an integer identified the power save blocker.
|
Returns an integer identified the power save blocker.
|
||||||
|
|
||||||
**Note:**
|
**Note:**
|
||||||
`PREVENT_DISPLAY_SLEEP` has higher precedence level than `PREVENT_APP_SUSPENSION`.
|
`prevent-display-sleep` has higher precedence level than `prevent-app-suspension`.
|
||||||
Only the highest precedence type takes effect. In other words, `PREVENT_DISPLAY_SLEEP`
|
Only the highest precedence type takes effect. In other words, `prevent-display-sleep`
|
||||||
always take precedence over `PREVENT_APP_SUSPENSION`.
|
always take precedence over `prevent-app-suspension`.
|
||||||
|
|
||||||
For example, an API calling A requests for `PREVENT_APP_SUSPENSION`, and
|
For example, an API calling A requests for `prevent-app-suspension`, and
|
||||||
another calling B requests for `PREVENT_DISPLAY_SLEEP`. `PREVENT_DISPLAY_SLEEP`
|
another calling B requests for `prevent-display-sleep`. `prevent-display-sleep`
|
||||||
will be used until B stops its request. After that, `PREVENT_APP_SUSPENSION` is used.
|
will be used until B stops its request. After that, `prevent-app-suspension` is used.
|
||||||
|
|
||||||
## powerSaveBlocker.stop(id)
|
## powerSaveBlocker.stop(id)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue