Skip to content

[macos] Add BasicMessageChannel #162

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 4 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
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
70 changes: 70 additions & 0 deletions library/macos/FLEBasicMessageChannel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>

#import "FLEBinaryMessenger.h"
#import "FLEMessageCodec.h"

/**
* A message response callback. Used for sending a message's reply back to the Flutter engine.
* The reply must be serializable by the codec used to encode the message.
*/
typedef void (^FLEMessageReply)(id _Nullable reply);

/**
* A handler for receiving a message. Implementations should asynchronously call |callback|
* exactly once with the reply to the message (or nil if there is none).
*/
typedef void (^FLEMessageHandler)(id _Nullable message, FLEMessageReply _Nonnull callback);

/**
* A channel for communicating with the Flutter engine by sending asynchronous messages.
*/
@interface FLEBasicMessageChannel : NSObject

// TODO: support +messageChannelWithName:binaryMessenger: once the standard codec is supported
// (Issue #67).

/**
* Returns a new channel that sends and receives messages on the channel named |name|, encoded
* with |codec| and dispatched via |messenger|.
*/
+ (nonnull instancetype)messageChannelWithName:(nonnull NSString *)name
binaryMessenger:(nonnull NSObject<FLEBinaryMessenger> *)messenger
codec:(nonnull NSObject<FLEMessageCodec> *)codec;

/**
* Initializes a channel to send and receive messages on the channel named |name|, encoded
* with |codec| and dispatched via |messenger|.
*/
- (nonnull instancetype)initWithName:(nonnull NSString *)name
binaryMessenger:(nonnull NSObject<FLEBinaryMessenger> *)messenger
codec:(nonnull NSObject<FLEMessageCodec> *)codec;

/**
* Sends a message to the Flutter engine on this channel.
*
* The message must be encodable using this channel's codec.
*/
- (void)sendMessage:(nullable id)message;

// TODO: Support sendMessage:result: once
// https://github.com/flutter/flutter/issues/18852 is resolved

/**
* Registers a handler that should be called any time a message is received on this channel.
*/
- (void)setMessageHandler:(nullable FLEMessageHandler)handler;
@end
62 changes: 62 additions & 0 deletions library/macos/FLEBasicMessageChannel.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "FLEBasicMessageChannel.h"

@implementation FLEBasicMessageChannel {
NSString *_name;
__weak id<FLEBinaryMessenger> _messenger;
id<FLEMessageCodec> _codec;
}
+ (instancetype)messageChannelWithName:(NSString *)name
binaryMessenger:(NSObject<FLEBinaryMessenger> *)messenger
codec:(NSObject<FLEMessageCodec> *)codec {
return [[[self class] alloc] initWithName:name binaryMessenger:messenger codec:codec];
}

- (instancetype)initWithName:(NSString *)name
binaryMessenger:(NSObject<FLEBinaryMessenger> *)messenger
codec:(NSObject<FLEMessageCodec> *)codec {
self = [super init];
if (self) {
_name = [name copy];
_messenger = messenger;
_codec = codec;
}
return self;
}

- (void)sendMessage:(id)message {
[_messenger sendOnChannel:_name message:[_codec encode:message]];
}

- (void)setMessageHandler:(FLEMessageHandler)handler {
if (!handler) {
[_messenger setMessageHandlerOnChannel:_name binaryMessageHandler:nil];
return;
}

// Don't capture the channel in the callback, since that makes lifetimes harder to reason about.
id<FLEMessageCodec> codec = _codec;

FLEBinaryMessageHandler messageHandler = ^(NSData *message, FLEBinaryReply callback) {
handler([codec decode:message], ^(id reply) {
callback([codec encode:reply]);
});
};

[_messenger setMessageHandlerOnChannel:_name binaryMessageHandler:messageHandler];
}

@end
24 changes: 24 additions & 0 deletions library/macos/FLEJSONMessageCodec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>

#import "FLEMessageCodec.h"

/**
* A codec that uses JSON as the encoding format. Messages using this codec must be serializable
* to JSON.
*/
@interface FLEJSONMessageCodec : NSObject <FLEMessageCodec>
@end
67 changes: 67 additions & 0 deletions library/macos/FLEJSONMessageCodec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "FLEJSONMessageCodec.h"

@implementation FLEJSONMessageCodec

+ (instancetype)sharedInstance {
static FLEJSONMessageCodec *sharedInstance;
if (!sharedInstance) {
sharedInstance = [[FLEJSONMessageCodec alloc] init];
}
return sharedInstance;
}

- (NSData *)encode:(id)message {
if (!message) {
return nil;
}

NSData *encoding;
NSError *error = nil;
if ([message isKindOfClass:[NSArray class]] || [message isKindOfClass:[NSDictionary class]]) {
encoding = [NSJSONSerialization dataWithJSONObject:message options:0 error:&error];
} else {
// Wrap then unwrap non-collection objects; see FlutterCodecs.mm in the Flutter engine.
encoding = [NSJSONSerialization dataWithJSONObject:@[ message ] options:0 error:&error];
const NSUInteger kJSONArrayStartLength = 1;
const NSUInteger kJSONArrayEndLength = 1;
encoding = [encoding subdataWithRange:NSMakeRange(kJSONArrayStartLength,
encoding.length - (kJSONArrayStartLength +
kJSONArrayEndLength))];
}
if (error) {
NSLog(@"Error: Failed to create JSON message for %@: %@", message, error.debugDescription);
}
return encoding;
}

// See FlutterCodecs.mm in the Flutter engine for implementation notes.
- (id)decode:(NSData *)messageData {
if (!messageData) {
return nil;
}

NSError *error = nil;
id message = [NSJSONSerialization JSONObjectWithData:messageData
options:NSJSONReadingAllowFragments
error:&error];
if (error) {
NSLog(@"Error: Failed to decode JSON message: %@", error.debugDescription);
}
return message;
}

@end
24 changes: 24 additions & 0 deletions library/macos/FLEJSONMethodCodec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>

#import "FLEMethodCodec.h"

/**
* A codec that uses JSON as the encoding format. Method arguments and error details for plugins
* using this codec must be serializable to JSON.
*/
@interface FLEJSONMethodCodec : NSObject <FLEMethodCodec>
@end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "FLECodecs.h"
#import "FLEJSONMethodCodec.h"

// Keys for JSON-encoded method calls.
static NSString *const kMessageMethodKey = @"method";
Expand Down Expand Up @@ -70,7 +70,7 @@ - (FLEMethodCall *)decodeMethodCall:(NSData *)methodData {
return [[FLEMethodCall alloc] initWithMethodName:method arguments:arguments];
}

- (NSData *)encodeSuccessEnvelope:(id _Nullable)result {
- (NSData *)encodeSuccessEnvelope:(id)result {
return SerializeAsJSON(@[ result ?: [NSNull null] ]);
}

Expand Down
38 changes: 38 additions & 0 deletions library/macos/FLEMessageCodec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>

/**
* Translates between a binary message and higher-level message objects.
*/
@protocol FLEMessageCodec

/**
* Returns the shared instance of the codec.
*/
+ (nonnull instancetype)sharedInstance;

/**
* Returns a binary encoding of the given |message|, or nil if the message cannot be
* serialized by this codec.
*/
- (nullable NSData*)encode:(nullable id)message;

/**
* Returns the decoded mesasge, or nil if it cannot be decoded.
*/
- (nullable id)decode:(nullable NSData*)messageData;

@end
43 changes: 43 additions & 0 deletions library/macos/FLEMethodCall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>

/**
* An object encapsulating a method call from Flutter.
*/
@interface FLEMethodCall : NSObject

/**
* Initializes an FLEMethodCall. If |arguments| is provided, it must be serializable by the codec
* used to encode the call.
*/
- (nonnull instancetype)initWithMethodName:(nonnull NSString *)name
arguments:(nullable id)arguments NS_DESIGNATED_INITIALIZER;

- (nonnull instancetype)init NS_UNAVAILABLE;

/**
* The name of the method being called.
*/
@property(readonly, nonatomic, nonnull) NSString *methodName;

/**
* The arguments to the method being called, if any.
*
* This object must be serializable by the codec used to encode the call.
*/
@property(readonly, nonatomic, nullable) id arguments;

@end
28 changes: 28 additions & 0 deletions library/macos/FLEMethodCall.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "FLEMethodCall.h"

@implementation FLEMethodCall

- (instancetype)initWithMethodName:(NSString *)name arguments:(id)arguments {
self = [super init];
if (self) {
_methodName = name;
_arguments = arguments;
}
return self;
}

@end
Loading