Pass userInfo in subscribeNotification
authorCheng Zhao <zcbenz@gmail.com>
Wed, 18 May 2016 05:40:19 +0000 (14:40 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 18 May 2016 05:40:19 +0000 (14:40 +0900)
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
atom/browser/mac/dict_util.mm
docs/api/system-preferences.md

index b8c6654..2b11aad 100644 (file)
@@ -5,6 +5,7 @@
 #include "atom/browser/api/atom_api_system_preferences.h"
 
 #include "atom/common/native_mate_converters/callback.h"
+#include "atom/common/native_mate_converters/value_converter.h"
 #include "atom/common/node_includes.h"
 #include "native_mate/dictionary.h"
 
index fed1c52..7779ce0 100644 (file)
 #include "base/callback.h"
 #include "native_mate/handle.h"
 
+namespace base {
+class DictionaryValue;
+}
+
 namespace atom {
 
 namespace api {
@@ -22,11 +26,16 @@ 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)
   int SubscribeNotification(const std::string& name,
-                            const base::Closure& callback);
+                            const NotificationCallback& callback);
   void UnsubscribeNotification(int id);
   v8::Local<v8::Value> GetUserDefault(const std::string& name,
                                       const std::string& type);
index 2d12b27..1bc65a1 100644 (file)
@@ -8,8 +8,10 @@
 
 #import <Cocoa/Cocoa.h>
 
+#include "atom/browser/mac/dict_util.h"
 #include "atom/common/native_mate_converters/gurl_converter.h"
 #include "base/strings/sys_string_conversions.h"
+#include "base/values.h"
 #include "net/base/mac/url_conversions.h"
 
 namespace atom {
@@ -25,16 +27,26 @@ std::map<int, id> g_id_map;
 
 }  // namespace
 
-int SystemPreferences::SubscribeNotification(const std::string& name,
-                                             const base::Closure& callback) {
+int SystemPreferences::SubscribeNotification(
+    const std::string& name, const NotificationCallback& callback) {
   int request_id = g_next_id++;
-  __block base::Closure copied_callback = callback;
+  __block NotificationCallback copied_callback = callback;
   g_id_map[request_id] = [[NSDistributedNotificationCenter defaultCenter]
       addObserverForName:base::SysUTF8ToNSString(name)
       object:nil
       queue:nil
       usingBlock:^(NSNotification* notification) {
-        copied_callback.Run();
+        scoped_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;
index 6a98e4c..04837eb 100644 (file)
@@ -26,6 +26,9 @@ NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value)
 
 scoped_ptr<base::DictionaryValue> NSDictionaryToDictionaryValue(
     NSDictionary* dict) {
+  if (!dict)
+    return nullptr;
+
   NSData* data = [NSJSONSerialization dataWithJSONObject:dict
                                                  options:0
                                                    error:nil];
index df7ef89..8f4dc89 100644 (file)
@@ -13,12 +13,16 @@ This method returns `true` if the system is in Dark Mode, and `false` otherwise.
 * `event` String
 * `callback` Function
 
-Subscribes to native notifications of OS X, `callback` will be called when the
-corresponding `event` happens. The `id` of the subscriber is returned, which can
-be used to unsubscribe the `event`.
+Subscribes to native notifications of OS X, `callback` will be called with
+`callback(event, userInfo)` when the corresponding `event` happens. The
+`userInfo` is an Object that contains the user information dictionary sent
+along with the notification.
+
+The `id` of the subscriber is returned, which can be used to unsubscribe the
+`event`.
 
 Under the hood this API subscribes to `NSDistributedNotificationCenter`,
-possible values of `event` are:
+example values of `event` are:
 
 * `AppleInterfaceThemeChangedNotification`
 * `AppleAquaColorVariantChanged`