chore: revert promisified systemPreferences notification methods

This reverts commit eec12b399a.
This commit is contained in:
Shelley Vohr 2019-06-10 09:34:21 -07:00 committed by GitHub
parent 4321df13f2
commit af108764c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 117 deletions

View file

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

View file

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

View file

@ -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.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) - [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) - [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.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) - [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) - [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage)

View file

@ -62,7 +62,7 @@ Returns `Boolean` - Whether the Swipe between pages setting is on.
### `systemPreferences.postNotification(event, userInfo[, deliverImmediately])` _macOS_ ### `systemPreferences.postNotification(event, userInfo[, deliverImmediately])` _macOS_
* `event` String * `event` String
* `userInfo` Record<string, unknown> * `userInfo` Object
* `deliverImmediately` Boolean (optional) - `true` to post notifications immediately even when the subscribing app is inactive. * `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 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_ ### `systemPreferences.postLocalNotification(event, userInfo)` _macOS_
* `event` String * `event` String
* `userInfo` Record<string, unknown> * `userInfo` Object
Posts `event` as native notifications of macOS. The `userInfo` is an Object Posts `event` as native notifications of macOS. The `userInfo` is an Object
that contains the user information dictionary sent along with the notification. 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_ ### `systemPreferences.postWorkspaceNotification(event, userInfo)` _macOS_
* `event` String * `event` String
* `userInfo` Record<string, unknown> * `userInfo` Object
Posts `event` as native notifications of macOS. The `userInfo` is an Object Posts `event` as native notifications of macOS. The `userInfo` is an Object
that contains the user information dictionary sent along with the notification. 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 * `event` String
* `callback` Function * `callback` Function
* `event` String * `event` String
* `userInfo` Record<string, unknown> * `userInfo` Object
Returns `Number` - The ID of this subscription Returns `Number` - The ID of this subscription
@ -109,93 +109,43 @@ example values of `event` are:
* `AppleColorPreferencesChangedNotification` * `AppleColorPreferencesChangedNotification`
* `AppleShowScrollBarsSettingChanged` * `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_ ### `systemPreferences.subscribeLocalNotification(event, callback)` _macOS_
* `event` String * `event` String
* `callback` Function * `callback` Function
* `event` String * `event` String
* `userInfo` Record<string, unknown> * `userInfo` Object
Returns `Number` - The ID of this subscription Returns `Number` - The ID of this subscription
Same as `subscribeNotification`, but uses `NSNotificationCenter` for local defaults. Same as `subscribeNotification`, but uses `NSNotificationCenter` for local defaults.
This is necessary for events such as `NSUserDefaultsDidChangeNotification`. 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_ ### `systemPreferences.subscribeWorkspaceNotification(event, callback)` _macOS_
* `event` String * `event` String
* `callback` Function * `callback` Function
* `event` String * `event` String
* `userInfo` Record<string, unknown> * `userInfo` Object
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>
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`.
### `systemPreferences.unsubscribeNotification(id)` _macOS_ ### `systemPreferences.unsubscribeNotification(id)` _macOS_
* `id` Number * `id` Integer
Removes the subscriber with `id`. Removes the subscriber with `id`.
### `systemPreferences.unsubscribeLocalNotification(id)` _macOS_ ### `systemPreferences.unsubscribeLocalNotification(id)` _macOS_
* `id` Number * `id` Integer
Same as `unsubscribeNotification`, but removes the subscriber from `NSNotificationCenter`. Same as `unsubscribeNotification`, but removes the subscriber from `NSNotificationCenter`.
### `systemPreferences.unsubscribeWorkspaceNotification(id)` _macOS_ ### `systemPreferences.unsubscribeWorkspaceNotification(id)` _macOS_
* `id` Number * `id` Integer
Same as `unsubscribeNotification`, but removes the subscriber from `NSWorkspace.sharedWorkspace.notificationCenter`. Same as `unsubscribeNotification`, but removes the subscriber from `NSWorkspace.sharedWorkspace.notificationCenter`.