diff --git a/atom/browser/api/atom_api_power_monitor.cc b/atom/browser/api/atom_api_power_monitor.cc index bf199270781..c96eb152750 100644 --- a/atom/browser/api/atom_api_power_monitor.cc +++ b/atom/browser/api/atom_api_power_monitor.cc @@ -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 diff --git a/atom/browser/api/atom_api_power_monitor.h b/atom/browser/api/atom_api_power_monitor.h index c7a6169d88b..e1f08d0974e 100644 --- a/atom/browser/api/atom_api_power_monitor.h +++ b/atom/browser/api/atom_api_power_monitor.h @@ -46,10 +46,8 @@ class PowerMonitor : public mate::TrackableObject, 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. diff --git a/lib/browser/api/power-monitor.js b/lib/browser/api/power-monitor.js index 5e3371dcc59..858521a9ddb 100644 --- a/lib/browser/api/power-monitor.js +++ b/lib/browser/api/power-monitor.js @@ -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