refactor: eliminate duplicate code (#29174)

This commit is contained in:
Milan Burda 2021-05-24 04:32:55 +02:00 committed by GitHub
parent 6330f8be9f
commit 241cceb2c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 66 additions and 151 deletions

View file

@ -123,6 +123,19 @@ std::string ConvertSystemPermission(
}
}
NSNotificationCenter* GetNotificationCenter(NotificationCenterKind kind) {
switch (kind) {
case NotificationCenterKind::kNSDistributedNotificationCenter:
return [NSDistributedNotificationCenter defaultCenter];
case NotificationCenterKind::kNSNotificationCenter:
return [NSNotificationCenter defaultCenter];
case NotificationCenterKind::kNSWorkspaceNotificationCenter:
return [[NSWorkspace sharedWorkspace] notificationCenter];
default:
return nil;
}
}
} // namespace
void SystemPreferences::PostNotification(const std::string& name,
@ -199,22 +212,8 @@ int SystemPreferences::DoSubscribeNotification(
NotificationCenterKind kind) {
int request_id = g_next_id++;
__block NotificationCallback copied_callback = callback;
NSNotificationCenter* center;
switch (kind) {
case NotificationCenterKind::kNSDistributedNotificationCenter:
center = [NSDistributedNotificationCenter defaultCenter];
break;
case NotificationCenterKind::kNSNotificationCenter:
center = [NSNotificationCenter defaultCenter];
break;
case NotificationCenterKind::kNSWorkspaceNotificationCenter:
center = [[NSWorkspace sharedWorkspace] notificationCenter];
break;
default:
break;
}
g_id_map[request_id] = [center
g_id_map[request_id] = [GetNotificationCenter(kind)
addObserverForName:base::SysUTF8ToNSString(name)
object:nil
queue:nil
@ -243,21 +242,7 @@ void SystemPreferences::DoUnsubscribeNotification(int request_id,
auto iter = g_id_map.find(request_id);
if (iter != g_id_map.end()) {
id observer = iter->second;
NSNotificationCenter* center;
switch (kind) {
case NotificationCenterKind::kNSDistributedNotificationCenter:
center = [NSDistributedNotificationCenter defaultCenter];
break;
case NotificationCenterKind::kNSNotificationCenter:
center = [NSNotificationCenter defaultCenter];
break;
case NotificationCenterKind::kNSWorkspaceNotificationCenter:
center = [[NSWorkspace sharedWorkspace] notificationCenter];
break;
default:
break;
}
[center removeObserver:observer];
[GetNotificationCenter(kind) removeObserver:observer];
g_id_map.erase(iter);
}
}