Browse Source

chore: revert promisified systemPreferences notification methods

This reverts commit eec12b399abcc5d5a52cb705d29469f574da7f65.
Shelley Vohr 5 years ago
parent
commit
af108764c7

+ 13 - 10
atom/browser/api/atom_api_system_preferences.h

@@ -65,22 +65,25 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
   void OnFinishLaunching(const base::DictionaryValue& launch_info) override;
 
 #elif defined(OS_MACOSX)
+  using NotificationCallback =
+      base::RepeatingCallback<void(const std::string&,
+                                   const base::DictionaryValue&)>;
+
   void PostNotification(const std::string& name,
                         const base::DictionaryValue& user_info,
                         mate::Arguments* args);
-  v8::Local<v8::Promise> SubscribeNotification(v8::Isolate* isolate,
-                                               const std::string& name);
+  int SubscribeNotification(const std::string& name,
+                            const NotificationCallback& callback);
   void UnsubscribeNotification(int id);
   void PostLocalNotification(const std::string& name,
                              const base::DictionaryValue& user_info);
-  v8::Local<v8::Promise> SubscribeLocalNotification(v8::Isolate* isolate,
-                                                    const std::string& name);
+  int SubscribeLocalNotification(const std::string& name,
+                                 const NotificationCallback& callback);
   void UnsubscribeLocalNotification(int request_id);
   void PostWorkspaceNotification(const std::string& name,
                                  const base::DictionaryValue& user_info);
-  v8::Local<v8::Promise> SubscribeWorkspaceNotification(
-      v8::Isolate* isolate,
-      const std::string& name);
+  int SubscribeWorkspaceNotification(const std::string& name,
+                                     const NotificationCallback& callback);
   void UnsubscribeWorkspaceNotification(int request_id);
   v8::Local<v8::Value> GetUserDefault(const std::string& name,
                                       const std::string& type);
@@ -122,9 +125,9 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
   ~SystemPreferences() override;
 
 #if defined(OS_MACOSX)
-  void DoSubscribeNotification(const std::string& name,
-                               util::Promise promise,
-                               NotificationCenterKind kind);
+  int DoSubscribeNotification(const std::string& name,
+                              const NotificationCallback& callback,
+                              NotificationCenterKind kind);
   void DoUnsubscribeNotification(int request_id, NotificationCenterKind kind);
 #endif
 

+ 27 - 45
atom/browser/api/atom_api_system_preferences_mac.mm

@@ -26,7 +26,6 @@
 #include "base/strings/sys_string_conversions.h"
 #include "base/threading/sequenced_task_runner_handle.h"
 #include "base/values.h"
-#include "native_mate/dictionary.h"
 #include "native_mate/object_template_builder.h"
 #include "net/base/mac/url_conversions.h"
 
@@ -132,15 +131,11 @@ void SystemPreferences::PostNotification(const std::string& name,
             deliverImmediately:immediate];
 }
 
-v8::Local<v8::Promise> SystemPreferences::SubscribeNotification(
-    v8::Isolate* isolate,
-    const std::string& name) {
-  util::Promise promise(isolate);
-  v8::Local<v8::Promise> handle = promise.GetHandle();
-
-  DoSubscribeNotification(name, std::move(promise),
-                          kNSDistributedNotificationCenter);
-  return handle;
+int SystemPreferences::SubscribeNotification(
+    const std::string& name,
+    const NotificationCallback& callback) {
+  return DoSubscribeNotification(name, callback,
+                                 kNSDistributedNotificationCenter);
 }
 
 void SystemPreferences::UnsubscribeNotification(int request_id) {
@@ -156,14 +151,10 @@ void SystemPreferences::PostLocalNotification(
                       userInfo:DictionaryValueToNSDictionary(user_info)];
 }
 
-v8::Local<v8::Promise> SystemPreferences::SubscribeLocalNotification(
-    v8::Isolate* isolate,
-    const std::string& name) {
-  util::Promise promise(isolate);
-  v8::Local<v8::Promise> handle = promise.GetHandle();
-
-  DoSubscribeNotification(name, std::move(promise), kNSNotificationCenter);
-  return handle;
+int SystemPreferences::SubscribeLocalNotification(
+    const std::string& name,
+    const NotificationCallback& callback) {
+  return DoSubscribeNotification(name, callback, kNSNotificationCenter);
 }
 
 void SystemPreferences::UnsubscribeLocalNotification(int request_id) {
@@ -180,27 +171,23 @@ void SystemPreferences::PostWorkspaceNotification(
                       userInfo:DictionaryValueToNSDictionary(user_info)];
 }
 
-v8::Local<v8::Promise> SystemPreferences::SubscribeWorkspaceNotification(
-    v8::Isolate* isolate,
-    const std::string& name) {
-  util::Promise promise(isolate);
-  v8::Local<v8::Promise> handle = promise.GetHandle();
-
-  DoSubscribeNotification(name, std::move(promise),
-                          kNSWorkspaceNotificationCenter);
-  return handle;
+int SystemPreferences::SubscribeWorkspaceNotification(
+    const std::string& name,
+    const NotificationCallback& callback) {
+  return DoSubscribeNotification(name, callback,
+                                 kNSWorkspaceNotificationCenter);
 }
 
 void SystemPreferences::UnsubscribeWorkspaceNotification(int request_id) {
   DoUnsubscribeNotification(request_id, kNSWorkspaceNotificationCenter);
 }
 
-void SystemPreferences::DoSubscribeNotification(const std::string& name,
-                                                util::Promise promise,
-                                                NotificationCenterKind kind) {
+int SystemPreferences::DoSubscribeNotification(
+    const std::string& name,
+    const NotificationCallback& callback,
+    NotificationCenterKind kind) {
   int request_id = g_next_id++;
-
-  __block util::Promise p = std::move(promise);
+  __block NotificationCallback copied_callback = callback;
   NSNotificationCenter* center;
   switch (kind) {
     case kNSDistributedNotificationCenter:
@@ -221,23 +208,18 @@ void SystemPreferences::DoSubscribeNotification(const std::string& name,
                   object:nil
                    queue:nil
               usingBlock:^(NSNotification* notification) {
-                mate::Dictionary dict =
-                    mate::Dictionary::CreateEmpty(p.isolate());
-                dict.Set("id", request_id);
-                dict.Set("event", base::SysNSStringToUTF8(notification.name));
-
-                std::unique_ptr<base::DictionaryValue> info =
+                std::unique_ptr<base::DictionaryValue> user_info =
                     NSDictionaryToDictionaryValue(notification.userInfo);
-                if (info) {
-                  base::Value user_info =
-                      base::Value::FromUniquePtrValue(std::move(info));
-                  dict.Set("userInfo", user_info);
+                if (user_info) {
+                  copied_callback.Run(
+                      base::SysNSStringToUTF8(notification.name), *user_info);
                 } else {
-                  base::Value empty_dict(base::Value::Type::DICTIONARY);
-                  dict.Set("userInfo", empty_dict);
+                  copied_callback.Run(
+                      base::SysNSStringToUTF8(notification.name),
+                      base::DictionaryValue());
                 }
-                std::move(p).Resolve(dict.GetHandle());
               }];
+  return request_id;
 }
 
 void SystemPreferences::DoUnsubscribeNotification(int request_id,

+ 0 - 3
docs/api/modernization/promisification.md

@@ -45,9 +45,6 @@ When a majority of affected functions are migrated, this flag will be enabled by
 - [ses.clearCache(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#clearCache)
 - [ses.getBlobData(identifier, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getBlobData)
 - [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal)
-- [systemPreferences.subscribeNotification(event, callback)](https://github.com/electron/electron/blob/master/docs/api/system-preferences.md#subscribeNotification)
-- [systemPreferences.subscribeLocalNotification(event, callback)](https://github.com/electron/electron/blob/master/docs/api/system-preferences.md#subscribeLocalNotification)
-- [systemPreferences.subscribeWorkspaceNotification(event, callback)](https://github.com/electron/electron/blob/master/docs/api/system-preferences.md#subscribeWorkspaceNotification)
 - [webFrame.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-frame.md#executeJavaScript)
 - [webFrame.executeJavaScriptInIsolatedWorld(worldId, scripts[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-frame.md#executeJavaScriptInIsolatedWorld)
 - [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage)

+ 9 - 59
docs/api/system-preferences.md

@@ -62,7 +62,7 @@ Returns `Boolean` - Whether the Swipe between pages setting is on.
 ### `systemPreferences.postNotification(event, userInfo[, deliverImmediately])` _macOS_
 
 * `event` String
-* `userInfo` Record<string, unknown>
+* `userInfo` Object
 * `deliverImmediately` Boolean (optional) - `true` to post notifications immediately even when the subscribing app is inactive.
 
 Posts `event` as native notifications of macOS. The `userInfo` is an Object
@@ -71,7 +71,7 @@ that contains the user information dictionary sent along with the notification.
 ### `systemPreferences.postLocalNotification(event, userInfo)` _macOS_
 
 * `event` String
-* `userInfo` Record<string, unknown>
+* `userInfo` Object
 
 Posts `event` as native notifications of macOS. The `userInfo` is an Object
 that contains the user information dictionary sent along with the notification.
@@ -79,7 +79,7 @@ that contains the user information dictionary sent along with the notification.
 ### `systemPreferences.postWorkspaceNotification(event, userInfo)` _macOS_
 
 * `event` String
-* `userInfo` Record<string, unknown>
+* `userInfo` Object
 
 Posts `event` as native notifications of macOS. The `userInfo` is an Object
 that contains the user information dictionary sent along with the notification.
@@ -89,7 +89,7 @@ that contains the user information dictionary sent along with the notification.
 * `event` String
 * `callback` Function
   * `event` String
-  * `userInfo` Record<string, unknown>
+  * `userInfo` Object
 
 Returns `Number` - The ID of this subscription
 
@@ -109,93 +109,43 @@ example values of `event` are:
 * `AppleColorPreferencesChangedNotification`
 * `AppleShowScrollBarsSettingChanged`
 
-**[Deprecated](modernization/promisification.md)**
-
-### `systemPreferences.subscribeNotification(event)` _macOS_
-
-* `event` String
-
-Returns `Promise<Object>` - Resolves with an object containing the following items:
-  * `id` Number - The ID of this subscription, which can be used to unsubscribe the
-`event`.
-  * `event` String
-  * `userInfo` Record<string, unknown>
-
-Subscribes to native notifications of macOS.
-
-Under the hood this API subscribes to `NSDistributedNotificationCenter`,
-example values of `event` are:
-
-* `AppleInterfaceThemeChangedNotification`
-* `AppleAquaColorVariantChanged`
-* `AppleColorPreferencesChangedNotification`
-* `AppleShowScrollBarsSettingChanged`
-
 ### `systemPreferences.subscribeLocalNotification(event, callback)` _macOS_
 
 * `event` String
 * `callback` Function
   * `event` String
-  * `userInfo` Record<string, unknown>
+  * `userInfo` Object
 
 Returns `Number` - The ID of this subscription
 
 Same as `subscribeNotification`, but uses `NSNotificationCenter` for local defaults.
 This is necessary for events such as `NSUserDefaultsDidChangeNotification`.
 
-**[Deprecated](modernization/promisification.md)**
-
-### `systemPreferences.subscribeLocalNotification(event)` _macOS_
-
-* `event` String
-
-Returns `Promise<Object>` - Resolves with an object containing the following items:
-  * `id` Number - The ID of this subscription
-  * `event` String
-  * `userInfo` Record<string, unknown>
-
-Same as `subscribeNotification`, but uses `NSNotificationCenter` for local defaults.
-This is necessary for events such as `NSUserDefaultsDidChangeNotification`.
-
 ### `systemPreferences.subscribeWorkspaceNotification(event, callback)` _macOS_
 
 * `event` String
 * `callback` Function
   * `event` String
-  * `userInfo` Record<string, unknown>
-
-Same as `subscribeNotification`, but uses `NSWorkspace.sharedWorkspace.notificationCenter`.
-This is necessary for events such as `NSWorkspaceDidActivateApplicationNotification`.
-
-**[Deprecated](modernization/promisification.md)**
-
-### `systemPreferences.subscribeWorkspaceNotification(event)` _macOS_
-
-* `event` String
-
-Returns `Promise<Object>` - Resolves with an object containing the following items:
-  * `id` Number - The ID of this subscription
-  * `event` String
-  * `userInfo` Record<string, unknown>
+  * `userInfo` Object
 
 Same as `subscribeNotification`, but uses `NSWorkspace.sharedWorkspace.notificationCenter`.
 This is necessary for events such as `NSWorkspaceDidActivateApplicationNotification`.
 
 ### `systemPreferences.unsubscribeNotification(id)` _macOS_
 
-* `id` Number
+* `id` Integer
 
 Removes the subscriber with `id`.
 
 ### `systemPreferences.unsubscribeLocalNotification(id)` _macOS_
 
-* `id` Number
+* `id` Integer
 
 Same as `unsubscribeNotification`, but removes the subscriber from `NSNotificationCenter`.
 
 ### `systemPreferences.unsubscribeWorkspaceNotification(id)` _macOS_
 
-* `id` Number
+* `id` Integer
 
 Same as `unsubscribeNotification`, but removes the subscriber from `NSWorkspace.sharedWorkspace.notificationCenter`.