From 2354f982a11e14793891708648bbcc791bba0167 Mon Sep 17 00:00:00 2001 From: Francisco Magdaleno Arceo Date: Mon, 28 Jan 2019 14:54:31 -0800 Subject: [PATCH 1/2] [macOS] Use FLEMethodError on color panel plugin bad arguments --- .../color_panel/macos/FLEColorPanelPlugin.mm | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/plugins/color_panel/macos/FLEColorPanelPlugin.mm b/plugins/color_panel/macos/FLEColorPanelPlugin.mm index 7fd97e3f4..4c19b4294 100644 --- a/plugins/color_panel/macos/FLEColorPanelPlugin.mm +++ b/plugins/color_panel/macos/FLEColorPanelPlugin.mm @@ -24,15 +24,15 @@ @implementation FLEColorPanelPlugin { } + (void)registerWithRegistrar:(id)registrar { - FLEMethodChannel* channel = [FLEMethodChannel - methodChannelWithName:@(plugins_color_panel::kChannelName) - binaryMessenger:registrar.messenger - codec:[FLEJSONMethodCodec sharedInstance]]; - FLEColorPanelPlugin* instance = [[FLEColorPanelPlugin alloc] initWithChannel:channel]; + FLEMethodChannel *channel = + [FLEMethodChannel methodChannelWithName:@(plugins_color_panel::kChannelName) + binaryMessenger:registrar.messenger + codec:[FLEJSONMethodCodec sharedInstance]]; + FLEColorPanelPlugin *instance = [[FLEColorPanelPlugin alloc] initWithChannel:channel]; [registrar addMethodCallDelegate:instance channel:channel]; } -- (instancetype)initWithChannel:(FLEMethodChannel*)channel { +- (instancetype)initWithChannel:(FLEMethodChannel *)channel { self = [super init]; if (self) { _channel = channel; @@ -45,26 +45,30 @@ - (instancetype)initWithChannel:(FLEMethodChannel*)channel { * panel channel. */ - (void)handleMethodCall:(FLEMethodCall *)call result:(FLEMethodResult)result { - BOOL handled = YES; + BOOL methodImplemented = YES; if ([call.methodName isEqualToString:@(plugins_color_panel::kShowColorPanelMethod)]) { if ([call.arguments isKindOfClass:[NSDictionary class]]) { BOOL showAlpha = [[call.arguments valueForKey:@(plugins_color_panel::kColorPanelShowAlpha)] boolValue]; [self showColorPanelWithAlpha:showAlpha]; } else { - NSLog(@"Malformed call for %@. Expected an NSDictionary but got %@", - @(plugins_color_panel::kShowColorPanelMethod), - NSStringFromClass([call.arguments class])); - handled = NO; + NSString *errorString = + [NSString stringWithFormat:@"Malformed call for %@. Expected an NSDictionary but got %@", + @(plugins_color_panel::kShowColorPanelMethod), + NSStringFromClass([call.arguments class])]; + result([[FLEMethodError alloc] initWithCode:@"error" + message:@"Bad arguments" + details:errorString]); + return; } } else if ([call.methodName isEqualToString:@(plugins_color_panel::kHideColorPanelMethod)]) { [self hideColorPanel]; } else { - handled = NO; + methodImplemented = NO; } // Send an immediate empty success message for handled messages, since the actual color data // will be provided in follow-up messages. - result(handled ? nil : FLEMethodNotImplemented); + result(methodImplemented ? nil : FLEMethodNotImplemented); } /** @@ -114,7 +118,7 @@ - (void)selectedColorDidChange { NSColor *color = [NSColorPanel sharedColorPanel].color; NSDictionary *colorDictionary = [self dictionaryWithColor:color]; [_channel invokeMethod:@(plugins_color_panel::kColorSelectedCallbackMethod) - arguments:colorDictionary]; + arguments:colorDictionary]; } /** @@ -138,8 +142,7 @@ - (NSDictionary *)dictionaryWithColor:(NSColor *)color { - (void)windowWillClose:(NSNotification *)notification { [self removeColorPanelConnections]; - [_channel invokeMethod:@(plugins_color_panel::kClosedCallbackMethod) - arguments:nil]; + [_channel invokeMethod:@(plugins_color_panel::kClosedCallbackMethod) arguments:nil]; } @end From cdc1e757d73dec1766e633695f9e44d52df6cee8 Mon Sep 17 00:00:00 2001 From: Francisco Magdaleno Arceo Date: Mon, 28 Jan 2019 15:33:48 -0800 Subject: [PATCH 2/2] Address review comments --- .../color_panel/macos/FLEColorPanelPlugin.mm | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/plugins/color_panel/macos/FLEColorPanelPlugin.mm b/plugins/color_panel/macos/FLEColorPanelPlugin.mm index 4c19b4294..68df89039 100644 --- a/plugins/color_panel/macos/FLEColorPanelPlugin.mm +++ b/plugins/color_panel/macos/FLEColorPanelPlugin.mm @@ -45,7 +45,7 @@ - (instancetype)initWithChannel:(FLEMethodChannel *)channel { * panel channel. */ - (void)handleMethodCall:(FLEMethodCall *)call result:(FLEMethodResult)result { - BOOL methodImplemented = YES; + id methodResult = nil; if ([call.methodName isEqualToString:@(plugins_color_panel::kShowColorPanelMethod)]) { if ([call.arguments isKindOfClass:[NSDictionary class]]) { BOOL showAlpha = @@ -56,19 +56,18 @@ - (void)handleMethodCall:(FLEMethodCall *)call result:(FLEMethodResult)result { [NSString stringWithFormat:@"Malformed call for %@. Expected an NSDictionary but got %@", @(plugins_color_panel::kShowColorPanelMethod), NSStringFromClass([call.arguments class])]; - result([[FLEMethodError alloc] initWithCode:@"error" - message:@"Bad arguments" - details:errorString]); - return; + methodResult = [[FLEMethodError alloc] initWithCode:@"Bad arguments" + message:errorString + details:nil]; } } else if ([call.methodName isEqualToString:@(plugins_color_panel::kHideColorPanelMethod)]) { [self hideColorPanel]; } else { - methodImplemented = NO; + methodResult = FLEMethodNotImplemented; } - // Send an immediate empty success message for handled messages, since the actual color data - // will be provided in follow-up messages. - result(methodImplemented ? nil : FLEMethodNotImplemented); + // If no errors are generated, send an immediate empty success message for handled messages, since + // the actual color data will be provided in follow-up messages. + result(methodResult); } /**