Skip to content

[macOS] Use FLEMethodError on color panel plugin bad arguments #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions plugins/color_panel/macos/FLEColorPanelPlugin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ @implementation FLEColorPanelPlugin {
}

+ (void)registerWithRegistrar:(id<FLEPluginRegistrar>)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;
Expand All @@ -45,26 +45,29 @@ - (instancetype)initWithChannel:(FLEMethodChannel*)channel {
* panel channel.
*/
- (void)handleMethodCall:(FLEMethodCall *)call result:(FLEMethodResult)result {
BOOL handled = YES;
id methodResult = nil;
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])];
methodResult = [[FLEMethodError alloc] initWithCode:@"Bad arguments"
message:errorString
details:nil];
}
} else if ([call.methodName isEqualToString:@(plugins_color_panel::kHideColorPanelMethod)]) {
[self hideColorPanel];
} else {
handled = 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(handled ? 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);
}

/**
Expand Down Expand Up @@ -114,7 +117,7 @@ - (void)selectedColorDidChange {
NSColor *color = [NSColorPanel sharedColorPanel].color;
NSDictionary *colorDictionary = [self dictionaryWithColor:color];
[_channel invokeMethod:@(plugins_color_panel::kColorSelectedCallbackMethod)
arguments:colorDictionary];
arguments:colorDictionary];
}

/**
Expand All @@ -138,8 +141,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