|
|
|
@ -8,106 +8,93 @@ import 'dart:ui' as ui;
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
|
|
|
|
|
/// A registrar for Flutter plugins implemented in Dart.
|
|
|
|
|
///
|
|
|
|
|
/// Plugins for the web platform are implemented in Dart and are
|
|
|
|
|
/// registered with this class by code generated by the `flutter` tool
|
|
|
|
|
/// when compiling the application.
|
|
|
|
|
///
|
|
|
|
|
/// This class implements [BinaryMessenger] to route messages from the
|
|
|
|
|
/// framework to registered plugins.
|
|
|
|
|
///
|
|
|
|
|
/// Use this [BinaryMessenger] when creating platform channels in order for
|
|
|
|
|
/// them to receive messages from the platform side. For example:
|
|
|
|
|
///
|
|
|
|
|
/// ```dart
|
|
|
|
|
/// class MyPlugin {
|
|
|
|
|
/// static void registerWith(Registrar registrar) {
|
|
|
|
|
/// final MethodChannel channel = MethodChannel(
|
|
|
|
|
/// 'com.my_plugin/my_plugin',
|
|
|
|
|
/// const StandardMethodCodec(),
|
|
|
|
|
/// registrar, // the registrar is used as the BinaryMessenger
|
|
|
|
|
/// );
|
|
|
|
|
/// final MyPlugin instance = MyPlugin();
|
|
|
|
|
/// channel.setMethodCallHandler(instance.handleMethodCall);
|
|
|
|
|
/// }
|
|
|
|
|
/// // ...
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
class Registrar extends BinaryMessenger {
|
|
|
|
|
/// Creates a [Registrar].
|
|
|
|
|
///
|
|
|
|
|
/// The argument is ignored. To create a test [Registrar] with custom behavior,
|
|
|
|
|
/// subclass the [Registrar] class and override methods as appropriate.
|
|
|
|
|
Registrar([
|
|
|
|
|
@Deprecated(
|
|
|
|
|
'This argument is ignored. '
|
|
|
|
|
'This feature was deprecated after v1.24.0-7.0.pre.'
|
|
|
|
|
)
|
|
|
|
|
BinaryMessenger? binaryMessenger,
|
|
|
|
|
]);
|
|
|
|
|
// TODO(hterkelsen): Why is this _MessageHandler duplicated here?
|
|
|
|
|
typedef _MessageHandler = Future<ByteData?>? Function(ByteData?);
|
|
|
|
|
|
|
|
|
|
/// Registers the registrar's message handler
|
|
|
|
|
/// ([handlePlatformMessage]) with the engine, so that plugin
|
|
|
|
|
/// messages are correctly dispatched to the relevant registered
|
|
|
|
|
/// plugin.
|
|
|
|
|
/// This class registers web platform plugins.
|
|
|
|
|
///
|
|
|
|
|
/// An instance of this class is available as [webPluginRegistry].
|
|
|
|
|
class PluginRegistry {
|
|
|
|
|
/// Creates a plugin registry.
|
|
|
|
|
///
|
|
|
|
|
/// Only one handler can be registered at a time. Calling this
|
|
|
|
|
/// method a second time silently unregisters any
|
|
|
|
|
/// previously-registered handler and replaces it with the handler
|
|
|
|
|
/// from this object.
|
|
|
|
|
/// The argument selects the [BinaryMessenger] to use. An
|
|
|
|
|
/// appropriate value would be [pluginBinaryMessenger].
|
|
|
|
|
PluginRegistry(this._binaryMessenger);
|
|
|
|
|
|
|
|
|
|
final BinaryMessenger _binaryMessenger;
|
|
|
|
|
|
|
|
|
|
/// Creates a registrar for the given plugin implementation class.
|
|
|
|
|
Registrar registrarFor(Type key) => Registrar(_binaryMessenger);
|
|
|
|
|
|
|
|
|
|
/// Registers this plugin handler with the engine, so that unrecognized
|
|
|
|
|
/// platform messages are forwarded to the registry, where they can be
|
|
|
|
|
/// correctly dispatched to one of the registered plugins.
|
|
|
|
|
///
|
|
|
|
|
/// Code generated by the `flutter` tool automatically calls this method
|
|
|
|
|
/// for the global [webPluginRegistry] at startup.
|
|
|
|
|
///
|
|
|
|
|
/// Only one [PluginRegistry] can be registered at a time. Calling this
|
|
|
|
|
/// method a second time silently unregisters the first [PluginRegistry]
|
|
|
|
|
/// and replaces it with the new one.
|
|
|
|
|
///
|
|
|
|
|
/// This method uses a function called `webOnlySetPluginHandler` in
|
|
|
|
|
/// the [dart:ui] library. That function is only available when
|
|
|
|
|
/// compiling for the web.
|
|
|
|
|
@Deprecated(
|
|
|
|
|
'This method is no longer used by the tool; ui.webOnlySetPluginHandler is called directly. '
|
|
|
|
|
'This feature was deprecated after v1.24.0-7.0.pre.'
|
|
|
|
|
)
|
|
|
|
|
void registerMessageHandler() {
|
|
|
|
|
// The function below is only defined in the Web dart:ui.
|
|
|
|
|
// ignore: undefined_function
|
|
|
|
|
ui.webOnlySetPluginHandler(handleFrameworkMessage);
|
|
|
|
|
ui.webOnlySetPluginHandler(_binaryMessenger.handlePlatformMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Receives a platform message from the framework.
|
|
|
|
|
///
|
|
|
|
|
/// This method has been replaced with the more clearly-named [handleFrameworkMessage].
|
|
|
|
|
@Deprecated(
|
|
|
|
|
'Use handleFrameworkMessage instead. '
|
|
|
|
|
'This feature was deprecated after v1.24.0-7.0.pre.'
|
|
|
|
|
)
|
|
|
|
|
@override
|
|
|
|
|
Future<void> handlePlatformMessage(
|
|
|
|
|
String channel,
|
|
|
|
|
ByteData? data,
|
|
|
|
|
ui.PlatformMessageResponseCallback? callback,
|
|
|
|
|
) => handleFrameworkMessage(channel, data, callback);
|
|
|
|
|
/// A registrar for a particular plugin.
|
|
|
|
|
///
|
|
|
|
|
/// Gives access to a [BinaryMessenger] which has been configured to receive
|
|
|
|
|
/// platform messages from the framework side.
|
|
|
|
|
class Registrar {
|
|
|
|
|
/// Creates a registrar with the given [BinaryMessenger].
|
|
|
|
|
Registrar(this.messenger);
|
|
|
|
|
|
|
|
|
|
/// Message handler for web plugins.
|
|
|
|
|
/// A [BinaryMessenger] configured to receive platform messages from the
|
|
|
|
|
/// framework side.
|
|
|
|
|
///
|
|
|
|
|
/// This method is called when handling messages from the framework.
|
|
|
|
|
///
|
|
|
|
|
/// If a handler has been registered for the given `channel`, it is
|
|
|
|
|
/// invoked, and the value it returns is passed to `callback` (if that
|
|
|
|
|
/// is non-null). Then, the method's future is completed.
|
|
|
|
|
///
|
|
|
|
|
/// If no handler has been registered for that channel, then the
|
|
|
|
|
/// callback (if any) is invoked with null, then the method's future
|
|
|
|
|
/// is completed.
|
|
|
|
|
///
|
|
|
|
|
/// Messages are not buffered (unlike platform messages headed to
|
|
|
|
|
/// the framework, which are managed by [ChannelBuffers]).
|
|
|
|
|
///
|
|
|
|
|
/// This method is registered as the message handler by code
|
|
|
|
|
/// autogenerated by the `flutter` tool when the application is
|
|
|
|
|
/// compiled, if any web plugins are used. The code in question is
|
|
|
|
|
/// the following:
|
|
|
|
|
/// Use this [BinaryMessenger] when creating platform channels in order for
|
|
|
|
|
/// them to receive messages from the platform side. For example:
|
|
|
|
|
///
|
|
|
|
|
/// ```dart
|
|
|
|
|
/// ui.webOnlySetPluginHandler(webPluginRegistrar.handleFrameworkMessage);
|
|
|
|
|
/// class MyPlugin {
|
|
|
|
|
/// static void registerWith(Registrar registrar) {
|
|
|
|
|
/// final MethodChannel channel = MethodChannel(
|
|
|
|
|
/// 'com.my_plugin/my_plugin',
|
|
|
|
|
/// const StandardMethodCodec(),
|
|
|
|
|
/// registrar.messenger,
|
|
|
|
|
/// );
|
|
|
|
|
/// final MyPlugin instance = MyPlugin();
|
|
|
|
|
/// channel.setMethodCallHandler(instance.handleMethodCall);
|
|
|
|
|
/// }
|
|
|
|
|
/// // ...
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
Future<void> handleFrameworkMessage(
|
|
|
|
|
final BinaryMessenger messenger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The default plugin registry for the web.
|
|
|
|
|
///
|
|
|
|
|
/// Uses [pluginBinaryMessenger] as the [BinaryMessenger].
|
|
|
|
|
final PluginRegistry webPluginRegistry = PluginRegistry(pluginBinaryMessenger);
|
|
|
|
|
|
|
|
|
|
/// A [BinaryMessenger] which does the inverse of the default framework
|
|
|
|
|
/// messenger.
|
|
|
|
|
///
|
|
|
|
|
/// Instead of sending messages from the framework to the engine, this
|
|
|
|
|
/// receives messages from the framework and dispatches them to registered
|
|
|
|
|
/// plugins.
|
|
|
|
|
class _PlatformBinaryMessenger extends BinaryMessenger {
|
|
|
|
|
final Map<String, _MessageHandler> _handlers = <String, _MessageHandler>{};
|
|
|
|
|
|
|
|
|
|
/// Receives a platform message from the framework.
|
|
|
|
|
@override
|
|
|
|
|
Future<void> handlePlatformMessage(
|
|
|
|
|
String channel,
|
|
|
|
|
ByteData? data,
|
|
|
|
|
ui.PlatformMessageResponseCallback? callback,
|
|
|
|
@ -132,15 +119,6 @@ class Registrar extends BinaryMessenger {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `this`.
|
|
|
|
|
@Deprecated(
|
|
|
|
|
'This property is redundant. It returns the object on which it is called. '
|
|
|
|
|
'This feature was deprecated after v1.24.0-7.0.pre.'
|
|
|
|
|
)
|
|
|
|
|
BinaryMessenger get messenger => this;
|
|
|
|
|
|
|
|
|
|
final Map<String, MessageHandler> _handlers = <String, MessageHandler>{};
|
|
|
|
|
|
|
|
|
|
/// Sends a platform message from the platform side back to the framework.
|
|
|
|
|
@override
|
|
|
|
|
Future<ByteData> send(String channel, ByteData? message) {
|
|
|
|
@ -189,45 +167,8 @@ class Registrar extends BinaryMessenger {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This class was previously separate from [Registrar] but was merged into it
|
|
|
|
|
/// as part of a simplification of the web plugins API.
|
|
|
|
|
@Deprecated(
|
|
|
|
|
'Use Registrar instead. '
|
|
|
|
|
'This feature was deprecated after v1.24.0-7.0.pre.'
|
|
|
|
|
)
|
|
|
|
|
class PluginRegistry extends Registrar {
|
|
|
|
|
/// Creates a [Registrar].
|
|
|
|
|
///
|
|
|
|
|
/// The argument is ignored.
|
|
|
|
|
PluginRegistry([
|
|
|
|
|
@Deprecated(
|
|
|
|
|
'This argument is ignored. '
|
|
|
|
|
'This feature was deprecated after v1.24.0-7.0.pre.'
|
|
|
|
|
)
|
|
|
|
|
BinaryMessenger? binaryMessenger,
|
|
|
|
|
]) : super(); // ignore: avoid_unused_constructor_parameters
|
|
|
|
|
|
|
|
|
|
/// Returns `this`. The argument is ignored.
|
|
|
|
|
@Deprecated(
|
|
|
|
|
'This method is redundant. It returns the object on which it is called. '
|
|
|
|
|
'This feature was deprecated after v1.24.0-7.0.pre.'
|
|
|
|
|
)
|
|
|
|
|
Registrar registrarFor(Type key) => this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The default plugin registrar for the web.
|
|
|
|
|
final Registrar webPluginRegistrar = PluginRegistry();
|
|
|
|
|
|
|
|
|
|
/// A deprecated alias for [webPluginRegistrar].
|
|
|
|
|
@Deprecated(
|
|
|
|
|
'Use webPluginRegistrar instead. '
|
|
|
|
|
'This feature was deprecated after v1.24.0-7.0.pre.'
|
|
|
|
|
)
|
|
|
|
|
PluginRegistry get webPluginRegistry => webPluginRegistrar as PluginRegistry;
|
|
|
|
|
|
|
|
|
|
/// A deprecated alias for [webPluginRegistrar].
|
|
|
|
|
@Deprecated(
|
|
|
|
|
'Use webPluginRegistrar instead. '
|
|
|
|
|
'This feature was deprecated after v1.24.0-7.0.pre.'
|
|
|
|
|
)
|
|
|
|
|
BinaryMessenger get pluginBinaryMessenger => webPluginRegistrar;
|
|
|
|
|
/// The default [BinaryMessenger] for Flutter web plugins.
|
|
|
|
|
///
|
|
|
|
|
/// This is the value used for [webPluginRegistry]'s [PluginRegistry]
|
|
|
|
|
/// constructor argument.
|
|
|
|
|
final BinaryMessenger pluginBinaryMessenger = _PlatformBinaryMessenger();
|
|
|
|
|