Automatically generated registrants for web plugins (#39628)
* WIP on web plugin registry * WIP on registering plugins * WIP on web plugin registration * Only generate `package:flutter_web_plugins` imports if plugins are defined * Add parsing test * Add documentation * Fix analyzer warnings * add license headers * Add tests for package:flutter_web_plugins * Run `flutter update-packages --force-upgrade` * Fix analyzer errors * Fix analyzer error in test * Update copyright and remove flutter SDK constraints * Enable tests since engine has rolled * add flutter_web_plugins tests to bots * Create an empty .packages file for WebFs test
This commit is contained in:
18
packages/flutter_web_plugins/BUILD.gn
Normal file
18
packages/flutter_web_plugins/BUILD.gn
Normal file
@@ -0,0 +1,18 @@
|
||||
# Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
import("//build/dart/dart_library.gni")
|
||||
|
||||
dart_library("flutter_web_plugins") {
|
||||
package_name = "flutter_web_plugins"
|
||||
|
||||
# Can be left empty as analysis is disabled.
|
||||
sources = []
|
||||
|
||||
disable_analysis = true
|
||||
|
||||
deps = [
|
||||
"../flutter",
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
export 'src/plugin_registry.dart';
|
||||
121
packages/flutter_web_plugins/lib/src/plugin_registry.dart
Normal file
121
packages/flutter_web_plugins/lib/src/plugin_registry.dart
Normal file
@@ -0,0 +1,121 @@
|
||||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
typedef _MessageHandler = Future<ByteData> Function(ByteData);
|
||||
|
||||
/// This class registers web platform plugins.
|
||||
class PluginRegistry {
|
||||
/// Creates a plugin registry.
|
||||
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.
|
||||
void registerMessageHandler() {
|
||||
// The function below is only defined in the Web dart:ui.
|
||||
// ignore: undefined_function
|
||||
ui.webOnlySetPluginHandler(_binaryMessenger.handlePlatformMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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);
|
||||
|
||||
/// A [BinaryMessenger] configured to receive platform messages from the
|
||||
/// framework side.
|
||||
///
|
||||
/// Use this [BinaryMessenger] when creating platform channels in order for
|
||||
/// them to receive messages from the platform side. For example:
|
||||
///
|
||||
///
|
||||
/// 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);
|
||||
/// }
|
||||
/// ...
|
||||
/// }
|
||||
final BinaryMessenger messenger;
|
||||
}
|
||||
|
||||
/// The default plugin registry for the web.
|
||||
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) async {
|
||||
ByteData response;
|
||||
try {
|
||||
final MessageHandler handler = _handlers[channel];
|
||||
if (handler != null) {
|
||||
response = await handler(data);
|
||||
}
|
||||
} catch (exception, stack) {
|
||||
FlutterError.reportError(FlutterErrorDetails(
|
||||
exception: exception,
|
||||
stack: stack,
|
||||
library: 'flutter web shell',
|
||||
context: ErrorDescription('during a plugin platform message call'),
|
||||
));
|
||||
} finally {
|
||||
callback(response);
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends a platform message from the platform side back to the framework.
|
||||
@override
|
||||
Future<ByteData> send(String channel, ByteData message) {
|
||||
throw FlutterError(
|
||||
'Cannot send messages from the platform side to the framework.');
|
||||
}
|
||||
|
||||
@override
|
||||
void setMessageHandler(
|
||||
String channel, Future<ByteData> Function(ByteData message) handler) {
|
||||
if (handler == null)
|
||||
_handlers.remove(channel);
|
||||
else
|
||||
_handlers[channel] = handler;
|
||||
}
|
||||
|
||||
@override
|
||||
void setMockMessageHandler(
|
||||
String channel, Future<ByteData> Function(ByteData message) handler) {
|
||||
throw FlutterError(
|
||||
'Setting mock handlers is not supported on the platform side.');
|
||||
}
|
||||
}
|
||||
|
||||
/// The default [BinaryMessenger] for Flutter Web plugins.
|
||||
final BinaryMessenger pluginBinaryMessenger = _PlatformBinaryMessenger();
|
||||
43
packages/flutter_web_plugins/pubspec.yaml
Normal file
43
packages/flutter_web_plugins/pubspec.yaml
Normal file
@@ -0,0 +1,43 @@
|
||||
name: flutter_web_plugins
|
||||
description: Library to register Flutter Web plugins
|
||||
author: Flutter Authors <flutter-dev@googlegroups.com>
|
||||
homepage: http://flutter.dev
|
||||
|
||||
environment:
|
||||
sdk: ">=2.0.0-dev.28.0 <3.0.0"
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
collection: 1.14.11 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
meta: 1.1.7 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
typed_data: 1.1.6 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
vector_math: 2.0.8 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
archive: 2.0.10 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
args: 1.5.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
async: 2.3.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
boolean_selector: 1.0.5 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
charcode: 1.1.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
convert: 2.1.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
crypto: 2.1.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
image: 2.1.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
matcher: 0.12.5 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
path: 1.6.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
pedantic: 1.8.0+1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
petitparser: 2.4.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
quiver: 2.0.5 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
source_span: 1.5.5 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
stack_trace: 1.9.3 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
stream_channel: 2.0.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
string_scanner: 1.0.5 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
term_glyph: 1.1.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
test_api: 0.2.5 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
xml: 3.5.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
|
||||
# PUBSPEC CHECKSUM: 5c57
|
||||
61
packages/flutter_web_plugins/test/plugin_registry_test.dart
Normal file
61
packages/flutter_web_plugins/test/plugin_registry_test.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
@TestOn('chrome') // Uses web-only Flutter SDK
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
|
||||
|
||||
class TestPlugin {
|
||||
static void registerWith(Registrar registrar) {
|
||||
final MethodChannel channel = MethodChannel(
|
||||
'test_plugin',
|
||||
const StandardMethodCodec(),
|
||||
registrar.messenger,
|
||||
);
|
||||
final TestPlugin testPlugin = TestPlugin();
|
||||
channel.setMethodCallHandler(testPlugin.handleMethodCall);
|
||||
}
|
||||
|
||||
static final List<String> calledMethods = <String>[];
|
||||
|
||||
Future<void> handleMethodCall(MethodCall call) async {
|
||||
calledMethods.add(call.method);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('Plugin Registry', () {
|
||||
setUp(() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
webPluginRegistry.registerMessageHandler();
|
||||
});
|
||||
|
||||
test('Can register a plugin', () {
|
||||
TestPlugin.calledMethods.clear();
|
||||
|
||||
final Registrar registrar = webPluginRegistry.registrarFor(TestPlugin);
|
||||
TestPlugin.registerWith(registrar);
|
||||
|
||||
const MethodChannel frameworkChannel =
|
||||
MethodChannel('test_plugin', StandardMethodCodec());
|
||||
frameworkChannel.invokeMethod<void>('test1');
|
||||
|
||||
expect(TestPlugin.calledMethods, <String>['test1']);
|
||||
});
|
||||
|
||||
test('Throws when trying to send a platform message to the framework', () {
|
||||
expect(() => pluginBinaryMessenger.send('test', ByteData(0)),
|
||||
throwsFlutterError);
|
||||
});
|
||||
|
||||
test('Throws when trying to set a mock handler', () {
|
||||
expect(
|
||||
() => pluginBinaryMessenger.setMockMessageHandler(
|
||||
'test', (ByteData data) async => ByteData(0)),
|
||||
throwsFlutterError);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user