Commit db9fb69b authored by Adam Barth's avatar Adam Barth Committed by GitHub

Migrate HostMessages to PlatformMessages (#6409)

We'll eventually remove HostMessages because it's just a pass-through to
PlatformMessages, but that will be a separate step.
parent beb8afa4
...@@ -3,76 +3,25 @@ ...@@ -3,76 +3,25 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'package:mojo/core.dart' as core; import 'platform_messages.dart';
import 'package:flutter_services/platform/app_messages.dart' as mojom;
import 'shell.dart';
mojom.ApplicationMessagesProxy _initHostAppMessagesProxy() {
return shell.connectToViewAssociatedService(mojom.ApplicationMessages.connectToService);
}
final mojom.ApplicationMessagesProxy _hostAppMessagesProxy = _initHostAppMessagesProxy();
/// Signature for receiving [HostMessages].
typedef Future<String> HostMessageCallback(String message);
typedef Object _SendStringResponseFactory(String response);
class _ApplicationMessagesImpl extends mojom.ApplicationMessages {
final Map<String, HostMessageCallback> handlers = <String, HostMessageCallback>{};
_ApplicationMessagesImpl() {
shell.provideService(mojom.ApplicationMessages.serviceName,
(core.MojoMessagePipeEndpoint endpoint) {
mojom.ApplicationMessagesStub stub = new mojom.ApplicationMessagesStub.fromEndpoint(endpoint);
stub.impl = this;
}
);
}
@override
dynamic sendString(String messageName, String message, [_SendStringResponseFactory responseFactory]) {
HostMessageCallback callback = handlers[messageName];
if (callback == null)
return responseFactory(null);
return callback(message).then((String s) => responseFactory(s));
}
}
final _ApplicationMessagesImpl _appMessages = new _ApplicationMessagesImpl();
/// A service that can be implemented by the host application and the /// A service that can be implemented by the host application and the
/// Flutter framework to exchange application-specific messages. /// Flutter framework to exchange application-specific messages.
class HostMessages { class HostMessages {
/// Send a message to the host application. /// Send a message to the host application.
static Future<String> sendToHost(String messageName, [String message = '']) { static Future<String> sendToHost(String channel, [String message = '']) {
Completer<String> completer = new Completer<String>(); return PlatformMessages.sendString(channel, message);
_hostAppMessagesProxy.sendString(messageName, message, (String reply) {
completer.complete(reply);
});
return completer.future;
}
static dynamic _decode(String message) {
return message != null ? JSON.decode(message) : null;
} }
/// Sends a JSON-encoded message to the host application and JSON-decodes the response. /// Sends a JSON-encoded message to the host application and JSON-decodes the response.
static Future<dynamic> sendJSON(String messageName, [dynamic json]) async { static Future<dynamic> sendJSON(String channel, [dynamic json]) {
Completer<dynamic> completer = new Completer<dynamic>(); return PlatformMessages.sendJSON(channel, json);
_hostAppMessagesProxy.sendString(messageName, JSON.encode(json), (String reply) {
completer.complete(_decode(reply));
});
return completer.future;
} }
/// Register a callback for receiving messages from the host application. /// Register a callback for receiving messages from the host application.
static void addMessageHandler(String messageName, HostMessageCallback callback) { static void addMessageHandler(String channel, Future<String> callback(String message)) {
_appMessages.handlers[messageName] = callback; PlatformMessages.setStringMessageHandler(channel, callback);
} }
/// Register a callback for receiving JSON messages from the host application. /// Register a callback for receiving JSON messages from the host application.
...@@ -80,9 +29,7 @@ class HostMessages { ...@@ -80,9 +29,7 @@ class HostMessages {
/// Messages received from the host application are decoded as JSON before /// Messages received from the host application are decoded as JSON before
/// being passed to `callback`. The result of the callback is encoded as JSON /// being passed to `callback`. The result of the callback is encoded as JSON
/// before being returned to the host application. /// before being returned to the host application.
static void addJSONMessageHandler(String messageName, Future<dynamic> callback(dynamic json)) { static void addJSONMessageHandler(String channel, Future<dynamic> callback(dynamic json)) {
_appMessages.handlers[messageName] = (String message) async { PlatformMessages.setJSONMessageHandler(channel, callback);
return JSON.encode(await callback(_decode(message)));
};
} }
} }
...@@ -32,6 +32,8 @@ typedef Future<ByteData> _PlatformMessageHandler(ByteData message); ...@@ -32,6 +32,8 @@ typedef Future<ByteData> _PlatformMessageHandler(ByteData message);
/// Sends message to and receives messages from the underlying platform. /// Sends message to and receives messages from the underlying platform.
class PlatformMessages { class PlatformMessages {
PlatformMessages._();
/// Handlers for incoming platform messages. /// Handlers for incoming platform messages.
static final Map<String, _PlatformMessageHandler> _handlers = static final Map<String, _PlatformMessageHandler> _handlers =
<String, _PlatformMessageHandler>{}; <String, _PlatformMessageHandler>{};
...@@ -143,7 +145,7 @@ class PlatformMessages { ...@@ -143,7 +145,7 @@ class PlatformMessages {
/// To remove the mock handler, pass `null` as the `handler` argument. /// To remove the mock handler, pass `null` as the `handler` argument.
static void setMockBinaryMessageHandler(String channel, Future<ByteData> handler(ByteData message)) { static void setMockBinaryMessageHandler(String channel, Future<ByteData> handler(ByteData message)) {
if (handler == null) if (handler == null)
_mockHandlers.remove(handler); _mockHandlers.remove(channel);
else else
_mockHandlers[channel] = handler; _mockHandlers[channel] = handler;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment