Browse Source

Make //ui/base/idle API synchronous

https://chromium-review.googlesource.com/c/chromium/src/+/1379183
deepak1556 6 years ago
parent
commit
52db4693e8

+ 8 - 8
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

+ 2 - 4
atom/browser/api/atom_api_power_monitor.h

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

+ 26 - 0
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