Make //ui/base/idle API synchronous
https://chromium-review.googlesource.com/c/chromium/src/+/1379183
This commit is contained in:
parent
dd4a31633f
commit
547add94d0
3 changed files with 36 additions and 12 deletions
|
@ -84,19 +84,19 @@ void PowerMonitor::OnResume() {
|
|||
Emit("resume");
|
||||
}
|
||||
|
||||
void PowerMonitor::QuerySystemIdleState(v8::Isolate* isolate,
|
||||
int idle_threshold,
|
||||
const ui::IdleCallback& callback) {
|
||||
ui::IdleState PowerMonitor::QuerySystemIdleState(v8::Isolate* isolate,
|
||||
int idle_threshold) {
|
||||
if (idle_threshold > 0) {
|
||||
ui::CalculateIdleState(idle_threshold, callback);
|
||||
return ui::CalculateIdleState(idle_threshold);
|
||||
} else {
|
||||
isolate->ThrowException(v8::Exception::TypeError(mate::StringToV8(
|
||||
isolate, "Invalid idle threshold, must be greater than 0")));
|
||||
return ui::IDLE_STATE_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
void PowerMonitor::QuerySystemIdleTime(const ui::IdleTimeCallback& callback) {
|
||||
ui::CalculateIdleTime(callback);
|
||||
int PowerMonitor::QuerySystemIdleTime() {
|
||||
return ui::CalculateIdleTime();
|
||||
}
|
||||
|
||||
// static
|
||||
|
@ -122,8 +122,8 @@ void PowerMonitor::BuildPrototype(v8::Isolate* isolate,
|
|||
.SetMethod("blockShutdown", &PowerMonitor::BlockShutdown)
|
||||
.SetMethod("unblockShutdown", &PowerMonitor::UnblockShutdown)
|
||||
#endif
|
||||
.SetMethod("querySystemIdleState", &PowerMonitor::QuerySystemIdleState)
|
||||
.SetMethod("querySystemIdleTime", &PowerMonitor::QuerySystemIdleTime);
|
||||
.SetMethod("_querySystemIdleState", &PowerMonitor::QuerySystemIdleState)
|
||||
.SetMethod("_querySystemIdleTime", &PowerMonitor::QuerySystemIdleTime);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
|
|
@ -46,10 +46,8 @@ class PowerMonitor : public mate::TrackableObject<PowerMonitor>,
|
|||
void OnResume() override;
|
||||
|
||||
private:
|
||||
void QuerySystemIdleState(v8::Isolate* isolate,
|
||||
int idle_threshold,
|
||||
const ui::IdleCallback& callback);
|
||||
void QuerySystemIdleTime(const ui::IdleTimeCallback& callback);
|
||||
ui::IdleState QuerySystemIdleState(v8::Isolate* isolate, int idle_threshold);
|
||||
int QuerySystemIdleTime();
|
||||
|
||||
#if defined(OS_WIN)
|
||||
// Static callback invoked when a message comes in to our messaging window.
|
||||
|
|
|
@ -22,4 +22,30 @@ if (process.platform === 'linux') {
|
|||
})
|
||||
}
|
||||
|
||||
// TODO(deepak1556): Deprecate async api in favor of sync version in 5.0
|
||||
powerMonitor.querySystemIdleState = function (threshold, callback) {
|
||||
if (typeof threshold !== 'number') {
|
||||
throw new Error('Must pass threshold as a number')
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('Must pass callback as a function argument')
|
||||
}
|
||||
|
||||
const idleState = this._querySystemIdleState(threshold)
|
||||
|
||||
process.nextTick(() => callback(idleState))
|
||||
}
|
||||
|
||||
// TODO(deepak1556): Deprecate async api in favor of sync version in 5.0
|
||||
powerMonitor.querySystemIdleTime = function (callback) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('Must pass function as an argument')
|
||||
}
|
||||
|
||||
const idleTime = this._querySystemIdleTime()
|
||||
|
||||
process.nextTick(() => callback(idleTime))
|
||||
}
|
||||
|
||||
module.exports = powerMonitor
|
||||
|
|
Loading…
Reference in a new issue