mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
feat: allow null when subscribing notification (#33770)
* feat: allow null when subscribing notification * docs: document null event Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
@@ -76,17 +76,17 @@ class SystemPreferences
|
||||
void PostNotification(const std::string& name,
|
||||
base::DictionaryValue user_info,
|
||||
gin::Arguments* args);
|
||||
int SubscribeNotification(const std::string& name,
|
||||
int SubscribeNotification(v8::Local<v8::Value> maybe_name,
|
||||
const NotificationCallback& callback);
|
||||
void UnsubscribeNotification(int id);
|
||||
void PostLocalNotification(const std::string& name,
|
||||
base::DictionaryValue user_info);
|
||||
int SubscribeLocalNotification(const std::string& name,
|
||||
int SubscribeLocalNotification(v8::Local<v8::Value> maybe_name,
|
||||
const NotificationCallback& callback);
|
||||
void UnsubscribeLocalNotification(int request_id);
|
||||
void PostWorkspaceNotification(const std::string& name,
|
||||
base::DictionaryValue user_info);
|
||||
int SubscribeWorkspaceNotification(const std::string& name,
|
||||
int SubscribeWorkspaceNotification(v8::Local<v8::Value> maybe_name,
|
||||
const NotificationCallback& callback);
|
||||
void UnsubscribeWorkspaceNotification(int request_id);
|
||||
v8::Local<v8::Value> GetUserDefault(v8::Isolate* isolate,
|
||||
@@ -130,7 +130,7 @@ class SystemPreferences
|
||||
~SystemPreferences() override;
|
||||
|
||||
#if BUILDFLAG(IS_MAC)
|
||||
int DoSubscribeNotification(const std::string& name,
|
||||
int DoSubscribeNotification(v8::Local<v8::Value> maybe_name,
|
||||
const NotificationCallback& callback,
|
||||
NotificationCenterKind kind);
|
||||
void DoUnsubscribeNotification(int request_id, NotificationCenterKind kind);
|
||||
|
||||
@@ -154,10 +154,11 @@ void SystemPreferences::PostNotification(const std::string& name,
|
||||
}
|
||||
|
||||
int SystemPreferences::SubscribeNotification(
|
||||
const std::string& name,
|
||||
v8::Local<v8::Value> maybe_name,
|
||||
const NotificationCallback& callback) {
|
||||
return DoSubscribeNotification(
|
||||
name, callback, NotificationCenterKind::kNSDistributedNotificationCenter);
|
||||
maybe_name, callback,
|
||||
NotificationCenterKind::kNSDistributedNotificationCenter);
|
||||
}
|
||||
|
||||
void SystemPreferences::UnsubscribeNotification(int request_id) {
|
||||
@@ -174,9 +175,9 @@ void SystemPreferences::PostLocalNotification(const std::string& name,
|
||||
}
|
||||
|
||||
int SystemPreferences::SubscribeLocalNotification(
|
||||
const std::string& name,
|
||||
v8::Local<v8::Value> maybe_name,
|
||||
const NotificationCallback& callback) {
|
||||
return DoSubscribeNotification(name, callback,
|
||||
return DoSubscribeNotification(maybe_name, callback,
|
||||
NotificationCenterKind::kNSNotificationCenter);
|
||||
}
|
||||
|
||||
@@ -196,10 +197,11 @@ void SystemPreferences::PostWorkspaceNotification(
|
||||
}
|
||||
|
||||
int SystemPreferences::SubscribeWorkspaceNotification(
|
||||
const std::string& name,
|
||||
v8::Local<v8::Value> maybe_name,
|
||||
const NotificationCallback& callback) {
|
||||
return DoSubscribeNotification(
|
||||
name, callback, NotificationCenterKind::kNSWorkspaceNotificationCenter);
|
||||
maybe_name, callback,
|
||||
NotificationCenterKind::kNSWorkspaceNotificationCenter);
|
||||
}
|
||||
|
||||
void SystemPreferences::UnsubscribeWorkspaceNotification(int request_id) {
|
||||
@@ -208,14 +210,25 @@ void SystemPreferences::UnsubscribeWorkspaceNotification(int request_id) {
|
||||
}
|
||||
|
||||
int SystemPreferences::DoSubscribeNotification(
|
||||
const std::string& name,
|
||||
v8::Local<v8::Value> maybe_name,
|
||||
const NotificationCallback& callback,
|
||||
NotificationCenterKind kind) {
|
||||
int request_id = g_next_id++;
|
||||
__block NotificationCallback copied_callback = callback;
|
||||
|
||||
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
||||
std::string name_str;
|
||||
if (!(maybe_name->IsNull() ||
|
||||
gin::ConvertFromV8(isolate, maybe_name, &name_str))) {
|
||||
isolate->ThrowException(v8::Exception::Error(
|
||||
gin::StringToV8(isolate, "Must pass null or a string")));
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto* name = maybe_name->IsNull() ? nil : base::SysUTF8ToNSString(name_str);
|
||||
|
||||
g_id_map[request_id] = [GetNotificationCenter(kind)
|
||||
addObserverForName:base::SysUTF8ToNSString(name)
|
||||
addObserverForName:name
|
||||
object:nil
|
||||
queue:nil
|
||||
usingBlock:^(NSNotification* notification) {
|
||||
|
||||
Reference in New Issue
Block a user