Add systemPreferences.subscribeLocalNotification. (#6150)
authorCharlie Hess <ifightnoman@gmail.com>
Tue, 21 Jun 2016 00:48:42 +0000 (17:48 -0700)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 21 Jun 2016 00:48:42 +0000 (00:48 +0000)
atom/browser/api/atom_api_system_preferences.cc
atom/browser/api/atom_api_system_preferences.h
atom/browser/api/atom_api_system_preferences_mac.mm
docs/api/system-preferences.md

index 2b11aad..c342136 100644 (file)
@@ -53,6 +53,10 @@ void SystemPreferences::BuildPrototype(
                  &SystemPreferences::SubscribeNotification)
       .SetMethod("unsubscribeNotification",
                  &SystemPreferences::UnsubscribeNotification)
+      .SetMethod("subscribeLocalNotification",
+                 &SystemPreferences::SubscribeLocalNotification)
+      .SetMethod("unsubscribeLocalNotification",
+                 &SystemPreferences::UnsubscribeLocalNotification)
       .SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
 #endif
       .SetMethod("isDarkMode", &SystemPreferences::IsDarkMode);
index 7779ce0..9a5cf99 100644 (file)
@@ -26,17 +26,18 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
   static void BuildPrototype(v8::Isolate* isolate,
                              v8::Local<v8::ObjectTemplate> prototype);
 
-#if defined(OS_MACOSX)
-  using NotificationCallback = base::Callback<
-      void(const std::string&, const base::DictionaryValue&)>;
-#endif
-
 #if defined(OS_WIN)
   bool IsAeroGlassEnabled();
 #elif defined(OS_MACOSX)
+  using NotificationCallback = base::Callback<
+    void(const std::string&, const base::DictionaryValue&)>;
+
   int SubscribeNotification(const std::string& name,
                             const NotificationCallback& callback);
   void UnsubscribeNotification(int id);
+  int SubscribeLocalNotification(const std::string& name,
+                                 const NotificationCallback& callback);
+  void UnsubscribeLocalNotification(int request_id);
   v8::Local<v8::Value> GetUserDefault(const std::string& name,
                                       const std::string& type);
 #endif
@@ -46,6 +47,13 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
   explicit SystemPreferences(v8::Isolate* isolate);
   ~SystemPreferences() override;
 
+#if defined(OS_MACOSX)
+  int DoSubscribeNotification(const std::string& name,
+                              const NotificationCallback& callback,
+                              bool is_local);
+  void DoUnsubscribeNotification(int request_id, bool is_local);
+#endif
+
  private:
   DISALLOW_COPY_AND_ASSIGN(SystemPreferences);
 };
index 7201150..6f7055a 100644 (file)
@@ -30,34 +30,59 @@ std::map<int, id> g_id_map;
 
 int SystemPreferences::SubscribeNotification(
     const std::string& name, const NotificationCallback& callback) {
+  return DoSubscribeNotification(name, callback, false);
+}
+
+void SystemPreferences::UnsubscribeNotification(int request_id) {
+  DoUnsubscribeNotification(request_id, false);
+}
+
+int SystemPreferences::SubscribeLocalNotification(
+    const std::string& name, const NotificationCallback& callback) {
+  return DoSubscribeNotification(name, callback, true);
+}
+
+void SystemPreferences::UnsubscribeLocalNotification(int request_id) {
+  DoUnsubscribeNotification(request_id, true);
+}
+
+int SystemPreferences::DoSubscribeNotification(const std::string& name,
+  const NotificationCallback& callback, bool is_local) {
   int request_id = g_next_id++;
   __block NotificationCallback copied_callback = callback;
-  g_id_map[request_id] = [[NSDistributedNotificationCenter defaultCenter]
-      addObserverForName:base::SysUTF8ToNSString(name)
-      object:nil
-      queue:nil
-      usingBlock:^(NSNotification* notification) {
-        std::unique_ptr<base::DictionaryValue> user_info =
-            NSDictionaryToDictionaryValue(notification.userInfo);
-        if (user_info) {
-          copied_callback.Run(
-              base::SysNSStringToUTF8(notification.name),
-              *user_info);
-        } else {
-          copied_callback.Run(
-              base::SysNSStringToUTF8(notification.name),
-              base::DictionaryValue());
-        }
+  NSNotificationCenter* center = is_local ?
+    [NSNotificationCenter defaultCenter] :
+    [NSDistributedNotificationCenter defaultCenter];
+
+  g_id_map[request_id] = [center
+    addObserverForName:base::SysUTF8ToNSString(name)
+    object:nil
+    queue:nil
+    usingBlock:^(NSNotification* notification) {
+      std::unique_ptr<base::DictionaryValue> user_info =
+        NSDictionaryToDictionaryValue(notification.userInfo);
+      if (user_info) {
+        copied_callback.Run(
+          base::SysNSStringToUTF8(notification.name),
+          *user_info);
+      } else {
+        copied_callback.Run(
+          base::SysNSStringToUTF8(notification.name),
+          base::DictionaryValue());
       }
+    }
   ];
   return request_id;
 }
 
-void SystemPreferences::UnsubscribeNotification(int request_id) {
+void SystemPreferences::DoUnsubscribeNotification(int request_id, bool is_local) {
   auto iter = g_id_map.find(request_id);
   if (iter != g_id_map.end()) {
     id observer = iter->second;
-    [[NSDistributedNotificationCenter defaultCenter] removeObserver:observer];
+    NSNotificationCenter* center = is_local ?
+      [NSNotificationCenter defaultCenter] :
+      [NSDistributedNotificationCenter defaultCenter];
+    [center removeObserver:observer];
     g_id_map.erase(iter);
   }
 }
index a47975d..586422e 100644 (file)
@@ -40,6 +40,17 @@ example values of `event` are:
 
 Removes the subscriber with `id`.
 
+### `systemPreferences.subscribeLocalNotification(event, callback)` _macOS_
+
+Same as `subscribeNotification`, but uses `NSNotificationCenter` for local defaults.
+This is necessary for events such as:
+
+* `NSUserDefaultsDidChangeNotification`
+
+### `systemPreferences.unsubscribeLocalNotification(id)` _macOS_
+
+Same as `unsubscribeNotification`, but removes the subscriber from `NSNotificationCenter`.
+
 ### `systemPreferences.getUserDefault(key, type)` _macOS_
 
 * `key` String