Unverified Commit 6e84a142 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Revert "Simplify the flutter_web_plugins plugin registration API. (#70337)" (#70505)

This reverts commit 8749d955.
parent d5b1cc5b
...@@ -132,7 +132,7 @@ import '$generatedImport'; ...@@ -132,7 +132,7 @@ import '$generatedImport';
import '$mainImport' as entrypoint; import '$mainImport' as entrypoint;
Future<void> main() async { Future<void> main() async {
registerPlugins(webPluginRegistrar); registerPlugins(webPluginRegistry);
await ui.webOnlyInitializePlatform(); await ui.webOnlyInitializePlatform();
entrypoint.main(); entrypoint.main();
} }
......
...@@ -691,7 +691,7 @@ class _ResidentWebRunner extends ResidentWebRunner { ...@@ -691,7 +691,7 @@ class _ResidentWebRunner extends ResidentWebRunner {
'typedef _NullaryFunction = dynamic Function();', 'typedef _NullaryFunction = dynamic Function();',
'Future<void> main() async {', 'Future<void> main() async {',
if (hasWebPlugins) if (hasWebPlugins)
' registerPlugins(webPluginRegistrar);', ' registerPlugins(webPluginRegistry);',
' await ui.webOnlyInitializePlatform();', ' await ui.webOnlyInitializePlatform();',
' if (entrypoint.main is _UnaryFunction) {', ' if (entrypoint.main is _UnaryFunction) {',
' return (entrypoint.main as _UnaryFunction)(<String>[]);', ' return (entrypoint.main as _UnaryFunction)(<String>[]);',
......
...@@ -779,7 +779,8 @@ const String _dartPluginRegistryTemplate = ''' ...@@ -779,7 +779,8 @@ const String _dartPluginRegistryTemplate = '''
// Generated file. Do not edit. // Generated file. Do not edit.
// //
import 'dart:ui' as ui; // ignore: unused_import
import 'dart:ui';
{{#plugins}} {{#plugins}}
import 'package:{{name}}/{{file}}'; import 'package:{{name}}/{{file}}';
...@@ -788,15 +789,11 @@ import 'package:{{name}}/{{file}}'; ...@@ -788,15 +789,11 @@ import 'package:{{name}}/{{file}}';
import 'package:flutter_web_plugins/flutter_web_plugins.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart';
// ignore: public_member_api_docs // ignore: public_member_api_docs
void registerPlugins(Registrar registrar) { void registerPlugins(PluginRegistry registry) {
{{#plugins}} {{#plugins}}
{{class}}.registerWith(registrar); {{class}}.registerWith(registry.registrarFor({{class}}));
{{/plugins}} {{/plugins}}
// The function below is only defined in the web version of dart:ui, registry.registerMessageHandler();
// but the analyzer uses the VM version, so we have to explicitly
// tell the analyzer to ignore it here.
// ignore: undefined_function
ui.webOnlySetPluginHandler(registrar.handleFrameworkMessage);
} }
'''; ''';
......
...@@ -14,7 +14,7 @@ class {{pluginDartClass}}Web { ...@@ -14,7 +14,7 @@ class {{pluginDartClass}}Web {
final MethodChannel channel = MethodChannel( final MethodChannel channel = MethodChannel(
'{{projectName}}', '{{projectName}}',
const StandardMethodCodec(), const StandardMethodCodec(),
registrar, registrar.messenger,
); );
final pluginInstance = {{pluginDartClass}}Web(); final pluginInstance = {{pluginDartClass}}Web();
......
...@@ -83,7 +83,7 @@ void main() { ...@@ -83,7 +83,7 @@ void main() {
// Plugins // Plugins
expect(generated, contains("import 'package:foo/generated_plugin_registrant.dart';")); expect(generated, contains("import 'package:foo/generated_plugin_registrant.dart';"));
expect(generated, contains('registerPlugins(webPluginRegistrar);')); expect(generated, contains('registerPlugins(webPluginRegistry);'));
// Main // Main
expect(generated, contains('entrypoint.main();')); expect(generated, contains('entrypoint.main();'));
...@@ -185,7 +185,7 @@ void main() { ...@@ -185,7 +185,7 @@ void main() {
// Plugins // Plugins
expect(generated, contains("import 'package:foo/generated_plugin_registrant.dart';")); expect(generated, contains("import 'package:foo/generated_plugin_registrant.dart';"));
expect(generated, contains('registerPlugins(webPluginRegistrar);')); expect(generated, contains('registerPlugins(webPluginRegistry);'));
// Main // Main
expect(generated, contains('entrypoint.main();')); expect(generated, contains('entrypoint.main();'));
...@@ -208,7 +208,7 @@ void main() { ...@@ -208,7 +208,7 @@ void main() {
// Plugins // Plugins
expect(generated, isNot(contains("import 'package:foo/generated_plugin_registrant.dart';"))); expect(generated, isNot(contains("import 'package:foo/generated_plugin_registrant.dart';")));
expect(generated, isNot(contains('registerPlugins(webPluginRegistrar);'))); expect(generated, isNot(contains('registerPlugins(webPluginRegistry);')));
// Main // Main
expect(generated, contains('entrypoint.main();')); expect(generated, contains('entrypoint.main();'));
})); }));
...@@ -253,7 +253,7 @@ void main() { ...@@ -253,7 +253,7 @@ void main() {
// Plugins // Plugins
expect(generated, isNot(contains("import 'package:foo/generated_plugin_registrant.dart';"))); expect(generated, isNot(contains("import 'package:foo/generated_plugin_registrant.dart';")));
expect(generated, isNot(contains('registerPlugins(webPluginRegistrar);'))); expect(generated, isNot(contains('registerPlugins(webPluginRegistry);')));
// Main // Main
expect(generated, contains('entrypoint.main();')); expect(generated, contains('entrypoint.main();'));
......
...@@ -15,8 +15,6 @@ ...@@ -15,8 +15,6 @@
/// describing how the `url_launcher` package was created using [flutter_web_plugins]. /// describing how the `url_launcher` package was created using [flutter_web_plugins].
library flutter_web_plugins; library flutter_web_plugins;
export 'src/navigation/js_url_strategy.dart';
export 'src/navigation/url_strategy.dart'; export 'src/navigation/url_strategy.dart';
export 'src/navigation/utils.dart';
export 'src/plugin_event_channel.dart'; export 'src/plugin_event_channel.dart';
export 'src/plugin_registry.dart'; export 'src/plugin_registry.dart';
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
import 'dart:html'; import 'dart:html';
// TODO(mdebbar): Use the `URI` class instead?
final AnchorElement _urlParsingNode = AnchorElement(); final AnchorElement _urlParsingNode = AnchorElement();
/// Extracts the pathname part of a full [url]. /// Extracts the pathname part of a full [url].
......
...@@ -8,106 +8,93 @@ import 'dart:ui' as ui; ...@@ -8,106 +8,93 @@ import 'dart:ui' as ui;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
/// A registrar for Flutter plugins implemented in Dart. // TODO(hterkelsen): Why is this _MessageHandler duplicated here?
/// typedef _MessageHandler = Future<ByteData?>? Function(ByteData?);
/// Plugins for the web platform are implemented in Dart and are
/// registered with this class by code generated by the `flutter` tool /// This class registers web platform plugins.
/// 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 /// An instance of this class is available as [webPluginRegistry].
/// class MyPlugin { class PluginRegistry {
/// static void registerWith(Registrar registrar) { /// Creates a plugin registry.
/// final MethodChannel channel = MethodChannel( ///
/// 'com.my_plugin/my_plugin', /// The argument selects the [BinaryMessenger] to use. An
/// const StandardMethodCodec(), /// appropriate value would be [pluginBinaryMessenger].
/// registrar, // the registrar is used as the BinaryMessenger PluginRegistry(this._binaryMessenger);
/// );
/// final MyPlugin instance = MyPlugin(); final BinaryMessenger _binaryMessenger;
/// channel.setMethodCallHandler(instance.handleMethodCall);
/// } /// 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
class Registrar extends BinaryMessenger { /// platform messages are forwarded to the registry, where they can be
/// Creates a [Registrar]. /// correctly dispatched to one of the registered plugins.
/// ///
/// The argument is ignored. To create a test [Registrar] with custom behavior, /// Code generated by the `flutter` tool automatically calls this method
/// subclass the [Registrar] class and override methods as appropriate. /// for the global [webPluginRegistry] at startup.
Registrar([
@Deprecated(
'This argument is ignored. '
'This feature was deprecated after v1.24.0-7.0.pre.'
)
BinaryMessenger? binaryMessenger,
]);
/// Registers the registrar's message handler
/// ([handlePlatformMessage]) with the engine, so that plugin
/// messages are correctly dispatched to the relevant registered
/// plugin.
/// ///
/// Only one handler can be registered at a time. Calling this /// Only one [PluginRegistry] can be registered at a time. Calling this
/// method a second time silently unregisters any /// method a second time silently unregisters the first [PluginRegistry]
/// previously-registered handler and replaces it with the handler /// and replaces it with the new one.
/// from this object.
/// ///
/// This method uses a function called `webOnlySetPluginHandler` in /// This method uses a function called `webOnlySetPluginHandler` in
/// the [dart:ui] library. That function is only available when /// the [dart:ui] library. That function is only available when
/// compiling for the web. /// 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() { void registerMessageHandler() {
// The function below is only defined in the Web dart:ui. // The function below is only defined in the Web dart:ui.
// ignore: undefined_function // ignore: undefined_function
ui.webOnlySetPluginHandler(handleFrameworkMessage); ui.webOnlySetPluginHandler(_binaryMessenger.handlePlatformMessage);
} }
}
/// Receives a platform message from the framework. /// A registrar for a particular plugin.
/// ///
/// This method has been replaced with the more clearly-named [handleFrameworkMessage]. /// Gives access to a [BinaryMessenger] which has been configured to receive
@Deprecated( /// platform messages from the framework side.
'Use handleFrameworkMessage instead. ' class Registrar {
'This feature was deprecated after v1.24.0-7.0.pre.' /// Creates a registrar with the given [BinaryMessenger].
) Registrar(this.messenger);
@override
Future<void> handlePlatformMessage( /// A [BinaryMessenger] configured to receive platform messages from the
String channel, /// framework side.
ByteData? data,
ui.PlatformMessageResponseCallback? callback,
) => handleFrameworkMessage(channel, data, callback);
/// Message handler for web plugins.
///
/// 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 /// Use this [BinaryMessenger] when creating platform channels in order for
/// autogenerated by the `flutter` tool when the application is /// them to receive messages from the platform side. For example:
/// compiled, if any web plugins are used. The code in question is
/// the following:
/// ///
/// ```dart /// ```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, String channel,
ByteData? data, ByteData? data,
ui.PlatformMessageResponseCallback? callback, ui.PlatformMessageResponseCallback? callback,
...@@ -132,15 +119,6 @@ class Registrar extends BinaryMessenger { ...@@ -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. /// Sends a platform message from the platform side back to the framework.
@override @override
Future<ByteData> send(String channel, ByteData? message) { Future<ByteData> send(String channel, ByteData? message) {
...@@ -189,45 +167,8 @@ class Registrar extends BinaryMessenger { ...@@ -189,45 +167,8 @@ class Registrar extends BinaryMessenger {
} }
} }
/// This class was previously separate from [Registrar] but was merged into it /// The default [BinaryMessenger] for Flutter web plugins.
/// as part of a simplification of the web plugins API. ///
@Deprecated( /// This is the value used for [webPluginRegistry]'s [PluginRegistry]
'Use Registrar instead. ' /// constructor argument.
'This feature was deprecated after v1.24.0-7.0.pre.' final BinaryMessenger pluginBinaryMessenger = _PlatformBinaryMessenger();
)
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;
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