refactor: convert more C++ enums to C++11 enum classes (#26850)

This commit is contained in:
Milan Burda 2020-12-08 05:39:33 +01:00 committed by GitHub
parent 3bc220db29
commit c9b813a1f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 38 deletions

View file

@ -26,7 +26,7 @@ namespace electron {
namespace api { namespace api {
#if defined(OS_MAC) #if defined(OS_MAC)
enum NotificationCenterKind { enum class NotificationCenterKind {
kNSDistributedNotificationCenter = 0, kNSDistributedNotificationCenter = 0,
kNSNotificationCenter, kNSNotificationCenter,
kNSWorkspaceNotificationCenter, kNSWorkspaceNotificationCenter,

View file

@ -140,12 +140,13 @@ void SystemPreferences::PostNotification(const std::string& name,
int SystemPreferences::SubscribeNotification( int SystemPreferences::SubscribeNotification(
const std::string& name, const std::string& name,
const NotificationCallback& callback) { const NotificationCallback& callback) {
return DoSubscribeNotification(name, callback, return DoSubscribeNotification(
kNSDistributedNotificationCenter); name, callback, NotificationCenterKind::kNSDistributedNotificationCenter);
} }
void SystemPreferences::UnsubscribeNotification(int request_id) { void SystemPreferences::UnsubscribeNotification(int request_id) {
DoUnsubscribeNotification(request_id, kNSDistributedNotificationCenter); DoUnsubscribeNotification(
request_id, NotificationCenterKind::kNSDistributedNotificationCenter);
} }
void SystemPreferences::PostLocalNotification(const std::string& name, void SystemPreferences::PostLocalNotification(const std::string& name,
@ -159,11 +160,13 @@ void SystemPreferences::PostLocalNotification(const std::string& name,
int SystemPreferences::SubscribeLocalNotification( int SystemPreferences::SubscribeLocalNotification(
const std::string& name, const std::string& name,
const NotificationCallback& callback) { const NotificationCallback& callback) {
return DoSubscribeNotification(name, callback, kNSNotificationCenter); return DoSubscribeNotification(name, callback,
NotificationCenterKind::kNSNotificationCenter);
} }
void SystemPreferences::UnsubscribeLocalNotification(int request_id) { void SystemPreferences::UnsubscribeLocalNotification(int request_id) {
DoUnsubscribeNotification(request_id, kNSNotificationCenter); DoUnsubscribeNotification(request_id,
NotificationCenterKind::kNSNotificationCenter);
} }
void SystemPreferences::PostWorkspaceNotification( void SystemPreferences::PostWorkspaceNotification(
@ -179,12 +182,13 @@ void SystemPreferences::PostWorkspaceNotification(
int SystemPreferences::SubscribeWorkspaceNotification( int SystemPreferences::SubscribeWorkspaceNotification(
const std::string& name, const std::string& name,
const NotificationCallback& callback) { const NotificationCallback& callback) {
return DoSubscribeNotification(name, callback, return DoSubscribeNotification(
kNSWorkspaceNotificationCenter); name, callback, NotificationCenterKind::kNSWorkspaceNotificationCenter);
} }
void SystemPreferences::UnsubscribeWorkspaceNotification(int request_id) { void SystemPreferences::UnsubscribeWorkspaceNotification(int request_id) {
DoUnsubscribeNotification(request_id, kNSWorkspaceNotificationCenter); DoUnsubscribeNotification(
request_id, NotificationCenterKind::kNSWorkspaceNotificationCenter);
} }
int SystemPreferences::DoSubscribeNotification( int SystemPreferences::DoSubscribeNotification(
@ -195,13 +199,13 @@ int SystemPreferences::DoSubscribeNotification(
__block NotificationCallback copied_callback = callback; __block NotificationCallback copied_callback = callback;
NSNotificationCenter* center; NSNotificationCenter* center;
switch (kind) { switch (kind) {
case kNSDistributedNotificationCenter: case NotificationCenterKind::kNSDistributedNotificationCenter:
center = [NSDistributedNotificationCenter defaultCenter]; center = [NSDistributedNotificationCenter defaultCenter];
break; break;
case kNSNotificationCenter: case NotificationCenterKind::kNSNotificationCenter:
center = [NSNotificationCenter defaultCenter]; center = [NSNotificationCenter defaultCenter];
break; break;
case kNSWorkspaceNotificationCenter: case NotificationCenterKind::kNSWorkspaceNotificationCenter:
center = [[NSWorkspace sharedWorkspace] notificationCenter]; center = [[NSWorkspace sharedWorkspace] notificationCenter];
break; break;
default: default:
@ -239,13 +243,13 @@ void SystemPreferences::DoUnsubscribeNotification(int request_id,
id observer = iter->second; id observer = iter->second;
NSNotificationCenter* center; NSNotificationCenter* center;
switch (kind) { switch (kind) {
case kNSDistributedNotificationCenter: case NotificationCenterKind::kNSDistributedNotificationCenter:
center = [NSDistributedNotificationCenter defaultCenter]; center = [NSDistributedNotificationCenter defaultCenter];
break; break;
case kNSNotificationCenter: case NotificationCenterKind::kNSNotificationCenter:
center = [NSNotificationCenter defaultCenter]; center = [NSNotificationCenter defaultCenter];
break; break;
case kNSWorkspaceNotificationCenter: case NotificationCenterKind::kNSWorkspaceNotificationCenter:
center = [[NSWorkspace sharedWorkspace] notificationCenter]; center = [[NSWorkspace sharedWorkspace] notificationCenter];
break; break;
default: default:

View file

@ -257,21 +257,26 @@ WebRequest::~WebRequest() {
gin::ObjectTemplateBuilder WebRequest::GetObjectTemplateBuilder( gin::ObjectTemplateBuilder WebRequest::GetObjectTemplateBuilder(
v8::Isolate* isolate) { v8::Isolate* isolate) {
return gin::Wrappable<WebRequest>::GetObjectTemplateBuilder(isolate) return gin::Wrappable<WebRequest>::GetObjectTemplateBuilder(isolate)
.SetMethod("onBeforeRequest", .SetMethod(
&WebRequest::SetResponseListener<kOnBeforeRequest>) "onBeforeRequest",
.SetMethod("onBeforeSendHeaders", &WebRequest::SetResponseListener<ResponseEvent::kOnBeforeRequest>)
&WebRequest::SetResponseListener<kOnBeforeSendHeaders>) .SetMethod(
.SetMethod("onHeadersReceived", "onBeforeSendHeaders",
&WebRequest::SetResponseListener<kOnHeadersReceived>) &WebRequest::SetResponseListener<ResponseEvent::kOnBeforeSendHeaders>)
.SetMethod(
"onHeadersReceived",
&WebRequest::SetResponseListener<ResponseEvent::kOnHeadersReceived>)
.SetMethod("onSendHeaders", .SetMethod("onSendHeaders",
&WebRequest::SetSimpleListener<kOnSendHeaders>) &WebRequest::SetSimpleListener<SimpleEvent::kOnSendHeaders>)
.SetMethod("onBeforeRedirect", .SetMethod("onBeforeRedirect",
&WebRequest::SetSimpleListener<kOnBeforeRedirect>) &WebRequest::SetSimpleListener<SimpleEvent::kOnBeforeRedirect>)
.SetMethod("onResponseStarted", .SetMethod(
&WebRequest::SetSimpleListener<kOnResponseStarted>) "onResponseStarted",
&WebRequest::SetSimpleListener<SimpleEvent::kOnResponseStarted>)
.SetMethod("onErrorOccurred", .SetMethod("onErrorOccurred",
&WebRequest::SetSimpleListener<kOnErrorOccurred>) &WebRequest::SetSimpleListener<SimpleEvent::kOnErrorOccurred>)
.SetMethod("onCompleted", &WebRequest::SetSimpleListener<kOnCompleted>); .SetMethod("onCompleted",
&WebRequest::SetSimpleListener<SimpleEvent::kOnCompleted>);
} }
const char* WebRequest::GetTypeName() { const char* WebRequest::GetTypeName() {
@ -286,8 +291,8 @@ int WebRequest::OnBeforeRequest(extensions::WebRequestInfo* info,
const network::ResourceRequest& request, const network::ResourceRequest& request,
net::CompletionOnceCallback callback, net::CompletionOnceCallback callback,
GURL* new_url) { GURL* new_url) {
return HandleResponseEvent(kOnBeforeRequest, info, std::move(callback), return HandleResponseEvent(ResponseEvent::kOnBeforeRequest, info,
new_url, request); std::move(callback), new_url, request);
} }
int WebRequest::OnBeforeSendHeaders(extensions::WebRequestInfo* info, int WebRequest::OnBeforeSendHeaders(extensions::WebRequestInfo* info,
@ -295,7 +300,7 @@ int WebRequest::OnBeforeSendHeaders(extensions::WebRequestInfo* info,
BeforeSendHeadersCallback callback, BeforeSendHeadersCallback callback,
net::HttpRequestHeaders* headers) { net::HttpRequestHeaders* headers) {
return HandleResponseEvent( return HandleResponseEvent(
kOnBeforeSendHeaders, info, ResponseEvent::kOnBeforeSendHeaders, info,
base::BindOnce(std::move(callback), std::set<std::string>(), base::BindOnce(std::move(callback), std::set<std::string>(),
std::set<std::string>()), std::set<std::string>()),
headers, request, *headers); headers, request, *headers);
@ -312,25 +317,26 @@ int WebRequest::OnHeadersReceived(
original_response_headers ? original_response_headers->GetStatusLine() original_response_headers ? original_response_headers->GetStatusLine()
: std::string(); : std::string();
return HandleResponseEvent( return HandleResponseEvent(
kOnHeadersReceived, info, std::move(callback), ResponseEvent::kOnHeadersReceived, info, std::move(callback),
std::make_pair(override_response_headers, status_line), request); std::make_pair(override_response_headers, status_line), request);
} }
void WebRequest::OnSendHeaders(extensions::WebRequestInfo* info, void WebRequest::OnSendHeaders(extensions::WebRequestInfo* info,
const network::ResourceRequest& request, const network::ResourceRequest& request,
const net::HttpRequestHeaders& headers) { const net::HttpRequestHeaders& headers) {
HandleSimpleEvent(kOnSendHeaders, info, request, headers); HandleSimpleEvent(SimpleEvent::kOnSendHeaders, info, request, headers);
} }
void WebRequest::OnBeforeRedirect(extensions::WebRequestInfo* info, void WebRequest::OnBeforeRedirect(extensions::WebRequestInfo* info,
const network::ResourceRequest& request, const network::ResourceRequest& request,
const GURL& new_location) { const GURL& new_location) {
HandleSimpleEvent(kOnBeforeRedirect, info, request, new_location); HandleSimpleEvent(SimpleEvent::kOnBeforeRedirect, info, request,
new_location);
} }
void WebRequest::OnResponseStarted(extensions::WebRequestInfo* info, void WebRequest::OnResponseStarted(extensions::WebRequestInfo* info,
const network::ResourceRequest& request) { const network::ResourceRequest& request) {
HandleSimpleEvent(kOnResponseStarted, info, request); HandleSimpleEvent(SimpleEvent::kOnResponseStarted, info, request);
} }
void WebRequest::OnErrorOccurred(extensions::WebRequestInfo* info, void WebRequest::OnErrorOccurred(extensions::WebRequestInfo* info,
@ -338,7 +344,7 @@ void WebRequest::OnErrorOccurred(extensions::WebRequestInfo* info,
int net_error) { int net_error) {
callbacks_.erase(info->id); callbacks_.erase(info->id);
HandleSimpleEvent(kOnErrorOccurred, info, request, net_error); HandleSimpleEvent(SimpleEvent::kOnErrorOccurred, info, request, net_error);
} }
void WebRequest::OnCompleted(extensions::WebRequestInfo* info, void WebRequest::OnCompleted(extensions::WebRequestInfo* info,
@ -346,7 +352,7 @@ void WebRequest::OnCompleted(extensions::WebRequestInfo* info,
int net_error) { int net_error) {
callbacks_.erase(info->id); callbacks_.erase(info->id);
HandleSimpleEvent(kOnCompleted, info, request, net_error); HandleSimpleEvent(SimpleEvent::kOnCompleted, info, request, net_error);
} }
void WebRequest::OnRequestWillBeDestroyed(extensions::WebRequestInfo* info) { void WebRequest::OnRequestWillBeDestroyed(extensions::WebRequestInfo* info) {

View file

@ -86,14 +86,14 @@ class WebRequest : public gin::Wrappable<WebRequest>, public WebRequestAPI {
WebRequest(v8::Isolate* isolate, content::BrowserContext* browser_context); WebRequest(v8::Isolate* isolate, content::BrowserContext* browser_context);
~WebRequest() override; ~WebRequest() override;
enum SimpleEvent { enum class SimpleEvent {
kOnSendHeaders, kOnSendHeaders,
kOnBeforeRedirect, kOnBeforeRedirect,
kOnResponseStarted, kOnResponseStarted,
kOnCompleted, kOnCompleted,
kOnErrorOccurred, kOnErrorOccurred,
}; };
enum ResponseEvent { enum class ResponseEvent {
kOnBeforeRequest, kOnBeforeRequest,
kOnBeforeSendHeaders, kOnBeforeSendHeaders,
kOnHeadersReceived, kOnHeadersReceived,