Skip to content
Permalink
Browse files

chore: revert promisified systemPreferences notification methods

This reverts commit eec12b3.
  • Loading branch information...
codebytere committed Jun 10, 2019
1 parent 4321df1 commit af108764c74880533a42da2a309b8728561a172e
@@ -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

@@ -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 @@ AVMediaType ParseMediaType(const std::string& media_type) {
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 @@ AVMediaType ParseMediaType(const std::string& media_type) {
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 @@ AVMediaType ParseMediaType(const std::string& media_type) {
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 @@ AVMediaType ParseMediaType(const std::string& media_type) {
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,
@@ -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)
@@ -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,15 +71,15 @@ 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.

### `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`.

0 comments on commit af10876

Please sign in to comment.
You can’t perform that action at this time.