feat: add object to subscribe notification callback (#19110)

This commit is contained in:
Louis Rose 2019-07-11 18:06:31 +01:00 committed by Jeremy Apthorp
parent 6d34314457
commit 79114ff40a
3 changed files with 18 additions and 4 deletions

View file

@ -82,13 +82,15 @@ that contains the user information dictionary sent along with the notification.
* `callback` Function * `callback` Function
* `event` String * `event` String
* `userInfo` Object * `userInfo` Object
* `object` String
Returns `Number` - The ID of this subscription Returns `Number` - The ID of this subscription
Subscribes to native notifications of macOS, `callback` will be called with Subscribes to native notifications of macOS, `callback` will be called with
`callback(event, userInfo)` when the corresponding `event` happens. The `callback(event, userInfo)` when the corresponding `event` happens. The
`userInfo` is an Object that contains the user information dictionary sent `userInfo` is an Object that contains the user information dictionary sent
along with the notification. along with the notification. The `object` is the sender of the notification,
and only supports `NSString` values for now.
The `id` of the subscriber is returned, which can be used to unsubscribe the The `id` of the subscriber is returned, which can be used to unsubscribe the
`event`. `event`.
@ -107,6 +109,7 @@ example values of `event` are:
* `callback` Function * `callback` Function
* `event` String * `event` String
* `userInfo` Object * `userInfo` Object
* `object` String
Returns `Number` - The ID of this subscription Returns `Number` - The ID of this subscription
@ -119,6 +122,7 @@ This is necessary for events such as `NSUserDefaultsDidChangeNotification`.
* `callback` Function * `callback` Function
* `event` String * `event` String
* `userInfo` Object * `userInfo` Object
* `object` String
Same as `subscribeNotification`, but uses `NSWorkspace.sharedWorkspace.notificationCenter`. Same as `subscribeNotification`, but uses `NSWorkspace.sharedWorkspace.notificationCenter`.
This is necessary for events such as `NSWorkspaceDidActivateApplicationNotification`. This is necessary for events such as `NSWorkspaceDidActivateApplicationNotification`.

View file

@ -67,7 +67,8 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
#elif defined(OS_MACOSX) #elif defined(OS_MACOSX)
using NotificationCallback = using NotificationCallback =
base::RepeatingCallback<void(const std::string&, base::RepeatingCallback<void(const std::string&,
const base::DictionaryValue&)>; const base::DictionaryValue&,
const std::string&)>;
void PostNotification(const std::string& name, void PostNotification(const std::string& name,
const base::DictionaryValue& user_info, const base::DictionaryValue& user_info,

View file

@ -210,13 +210,22 @@ int SystemPreferences::DoSubscribeNotification(
usingBlock:^(NSNotification* notification) { usingBlock:^(NSNotification* notification) {
std::unique_ptr<base::DictionaryValue> user_info = std::unique_ptr<base::DictionaryValue> user_info =
NSDictionaryToDictionaryValue(notification.userInfo); NSDictionaryToDictionaryValue(notification.userInfo);
std::string object = "";
if ([notification.object isKindOfClass:[NSString class]]) {
object = base::SysNSStringToUTF8(notification.object);
}
if (user_info) { if (user_info) {
copied_callback.Run( copied_callback.Run(
base::SysNSStringToUTF8(notification.name), *user_info); base::SysNSStringToUTF8(notification.name),
*user_info,
object);
} else { } else {
copied_callback.Run( copied_callback.Run(
base::SysNSStringToUTF8(notification.name), base::SysNSStringToUTF8(notification.name),
base::DictionaryValue()); base::DictionaryValue(),
object);
} }
}]; }];
return request_id; return request_id;