From c9b813a1f9ae9b6211c5bf338812997113d1c465 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Tue, 8 Dec 2020 05:39:33 +0100 Subject: [PATCH] refactor: convert more C++ enums to C++11 enum classes (#26850) --- .../api/electron_api_system_preferences.h | 2 +- .../electron_api_system_preferences_mac.mm | 32 +++++++------ shell/browser/api/electron_api_web_request.cc | 48 +++++++++++-------- shell/browser/api/electron_api_web_request.h | 4 +- 4 files changed, 48 insertions(+), 38 deletions(-) diff --git a/shell/browser/api/electron_api_system_preferences.h b/shell/browser/api/electron_api_system_preferences.h index 55045a62c16e..8ab140adb9c7 100644 --- a/shell/browser/api/electron_api_system_preferences.h +++ b/shell/browser/api/electron_api_system_preferences.h @@ -26,7 +26,7 @@ namespace electron { namespace api { #if defined(OS_MAC) -enum NotificationCenterKind { +enum class NotificationCenterKind { kNSDistributedNotificationCenter = 0, kNSNotificationCenter, kNSWorkspaceNotificationCenter, diff --git a/shell/browser/api/electron_api_system_preferences_mac.mm b/shell/browser/api/electron_api_system_preferences_mac.mm index a06879bfb18c..43318c456d2e 100644 --- a/shell/browser/api/electron_api_system_preferences_mac.mm +++ b/shell/browser/api/electron_api_system_preferences_mac.mm @@ -140,12 +140,13 @@ void SystemPreferences::PostNotification(const std::string& name, int SystemPreferences::SubscribeNotification( const std::string& name, const NotificationCallback& callback) { - return DoSubscribeNotification(name, callback, - kNSDistributedNotificationCenter); + return DoSubscribeNotification( + name, callback, NotificationCenterKind::kNSDistributedNotificationCenter); } void SystemPreferences::UnsubscribeNotification(int request_id) { - DoUnsubscribeNotification(request_id, kNSDistributedNotificationCenter); + DoUnsubscribeNotification( + request_id, NotificationCenterKind::kNSDistributedNotificationCenter); } void SystemPreferences::PostLocalNotification(const std::string& name, @@ -159,11 +160,13 @@ void SystemPreferences::PostLocalNotification(const std::string& name, int SystemPreferences::SubscribeLocalNotification( const std::string& name, const NotificationCallback& callback) { - return DoSubscribeNotification(name, callback, kNSNotificationCenter); + return DoSubscribeNotification(name, callback, + NotificationCenterKind::kNSNotificationCenter); } void SystemPreferences::UnsubscribeLocalNotification(int request_id) { - DoUnsubscribeNotification(request_id, kNSNotificationCenter); + DoUnsubscribeNotification(request_id, + NotificationCenterKind::kNSNotificationCenter); } void SystemPreferences::PostWorkspaceNotification( @@ -179,12 +182,13 @@ void SystemPreferences::PostWorkspaceNotification( int SystemPreferences::SubscribeWorkspaceNotification( const std::string& name, const NotificationCallback& callback) { - return DoSubscribeNotification(name, callback, - kNSWorkspaceNotificationCenter); + return DoSubscribeNotification( + name, callback, NotificationCenterKind::kNSWorkspaceNotificationCenter); } void SystemPreferences::UnsubscribeWorkspaceNotification(int request_id) { - DoUnsubscribeNotification(request_id, kNSWorkspaceNotificationCenter); + DoUnsubscribeNotification( + request_id, NotificationCenterKind::kNSWorkspaceNotificationCenter); } int SystemPreferences::DoSubscribeNotification( @@ -195,13 +199,13 @@ int SystemPreferences::DoSubscribeNotification( __block NotificationCallback copied_callback = callback; NSNotificationCenter* center; switch (kind) { - case kNSDistributedNotificationCenter: + case NotificationCenterKind::kNSDistributedNotificationCenter: center = [NSDistributedNotificationCenter defaultCenter]; break; - case kNSNotificationCenter: + case NotificationCenterKind::kNSNotificationCenter: center = [NSNotificationCenter defaultCenter]; break; - case kNSWorkspaceNotificationCenter: + case NotificationCenterKind::kNSWorkspaceNotificationCenter: center = [[NSWorkspace sharedWorkspace] notificationCenter]; break; default: @@ -239,13 +243,13 @@ void SystemPreferences::DoUnsubscribeNotification(int request_id, id observer = iter->second; NSNotificationCenter* center; switch (kind) { - case kNSDistributedNotificationCenter: + case NotificationCenterKind::kNSDistributedNotificationCenter: center = [NSDistributedNotificationCenter defaultCenter]; break; - case kNSNotificationCenter: + case NotificationCenterKind::kNSNotificationCenter: center = [NSNotificationCenter defaultCenter]; break; - case kNSWorkspaceNotificationCenter: + case NotificationCenterKind::kNSWorkspaceNotificationCenter: center = [[NSWorkspace sharedWorkspace] notificationCenter]; break; default: diff --git a/shell/browser/api/electron_api_web_request.cc b/shell/browser/api/electron_api_web_request.cc index f69ddad18d61..68fcb8878e4e 100644 --- a/shell/browser/api/electron_api_web_request.cc +++ b/shell/browser/api/electron_api_web_request.cc @@ -257,21 +257,26 @@ WebRequest::~WebRequest() { gin::ObjectTemplateBuilder WebRequest::GetObjectTemplateBuilder( v8::Isolate* isolate) { return gin::Wrappable::GetObjectTemplateBuilder(isolate) - .SetMethod("onBeforeRequest", - &WebRequest::SetResponseListener) - .SetMethod("onBeforeSendHeaders", - &WebRequest::SetResponseListener) - .SetMethod("onHeadersReceived", - &WebRequest::SetResponseListener) + .SetMethod( + "onBeforeRequest", + &WebRequest::SetResponseListener) + .SetMethod( + "onBeforeSendHeaders", + &WebRequest::SetResponseListener) + .SetMethod( + "onHeadersReceived", + &WebRequest::SetResponseListener) .SetMethod("onSendHeaders", - &WebRequest::SetSimpleListener) + &WebRequest::SetSimpleListener) .SetMethod("onBeforeRedirect", - &WebRequest::SetSimpleListener) - .SetMethod("onResponseStarted", - &WebRequest::SetSimpleListener) + &WebRequest::SetSimpleListener) + .SetMethod( + "onResponseStarted", + &WebRequest::SetSimpleListener) .SetMethod("onErrorOccurred", - &WebRequest::SetSimpleListener) - .SetMethod("onCompleted", &WebRequest::SetSimpleListener); + &WebRequest::SetSimpleListener) + .SetMethod("onCompleted", + &WebRequest::SetSimpleListener); } const char* WebRequest::GetTypeName() { @@ -286,8 +291,8 @@ int WebRequest::OnBeforeRequest(extensions::WebRequestInfo* info, const network::ResourceRequest& request, net::CompletionOnceCallback callback, GURL* new_url) { - return HandleResponseEvent(kOnBeforeRequest, info, std::move(callback), - new_url, request); + return HandleResponseEvent(ResponseEvent::kOnBeforeRequest, info, + std::move(callback), new_url, request); } int WebRequest::OnBeforeSendHeaders(extensions::WebRequestInfo* info, @@ -295,7 +300,7 @@ int WebRequest::OnBeforeSendHeaders(extensions::WebRequestInfo* info, BeforeSendHeadersCallback callback, net::HttpRequestHeaders* headers) { return HandleResponseEvent( - kOnBeforeSendHeaders, info, + ResponseEvent::kOnBeforeSendHeaders, info, base::BindOnce(std::move(callback), std::set(), std::set()), headers, request, *headers); @@ -312,25 +317,26 @@ int WebRequest::OnHeadersReceived( original_response_headers ? original_response_headers->GetStatusLine() : std::string(); return HandleResponseEvent( - kOnHeadersReceived, info, std::move(callback), + ResponseEvent::kOnHeadersReceived, info, std::move(callback), std::make_pair(override_response_headers, status_line), request); } void WebRequest::OnSendHeaders(extensions::WebRequestInfo* info, const network::ResourceRequest& request, const net::HttpRequestHeaders& headers) { - HandleSimpleEvent(kOnSendHeaders, info, request, headers); + HandleSimpleEvent(SimpleEvent::kOnSendHeaders, info, request, headers); } void WebRequest::OnBeforeRedirect(extensions::WebRequestInfo* info, const network::ResourceRequest& request, const GURL& new_location) { - HandleSimpleEvent(kOnBeforeRedirect, info, request, new_location); + HandleSimpleEvent(SimpleEvent::kOnBeforeRedirect, info, request, + new_location); } void WebRequest::OnResponseStarted(extensions::WebRequestInfo* info, const network::ResourceRequest& request) { - HandleSimpleEvent(kOnResponseStarted, info, request); + HandleSimpleEvent(SimpleEvent::kOnResponseStarted, info, request); } void WebRequest::OnErrorOccurred(extensions::WebRequestInfo* info, @@ -338,7 +344,7 @@ void WebRequest::OnErrorOccurred(extensions::WebRequestInfo* info, int net_error) { callbacks_.erase(info->id); - HandleSimpleEvent(kOnErrorOccurred, info, request, net_error); + HandleSimpleEvent(SimpleEvent::kOnErrorOccurred, info, request, net_error); } void WebRequest::OnCompleted(extensions::WebRequestInfo* info, @@ -346,7 +352,7 @@ void WebRequest::OnCompleted(extensions::WebRequestInfo* info, int net_error) { callbacks_.erase(info->id); - HandleSimpleEvent(kOnCompleted, info, request, net_error); + HandleSimpleEvent(SimpleEvent::kOnCompleted, info, request, net_error); } void WebRequest::OnRequestWillBeDestroyed(extensions::WebRequestInfo* info) { diff --git a/shell/browser/api/electron_api_web_request.h b/shell/browser/api/electron_api_web_request.h index c1f756223367..8b7b25bd1d7c 100644 --- a/shell/browser/api/electron_api_web_request.h +++ b/shell/browser/api/electron_api_web_request.h @@ -86,14 +86,14 @@ class WebRequest : public gin::Wrappable, public WebRequestAPI { WebRequest(v8::Isolate* isolate, content::BrowserContext* browser_context); ~WebRequest() override; - enum SimpleEvent { + enum class SimpleEvent { kOnSendHeaders, kOnBeforeRedirect, kOnResponseStarted, kOnCompleted, kOnErrorOccurred, }; - enum ResponseEvent { + enum class ResponseEvent { kOnBeforeRequest, kOnBeforeSendHeaders, kOnHeadersReceived,