[web] Support platform view creation params (flutter/engine#42255)

1. Wire platform views' `creation params` in the engine.
2. Move acceptable factory signatures to `dart:ui_web`.
3. Don't put any JS types in `dart:ui_web`'s interface.
    -  Use type casting to check at runtime instead.
4. Tests.

Part of https://github.com/flutter/flutter/issues/127030
This commit is contained in:
Mouad Debbar
2023-06-02 17:53:47 -04:00
committed by GitHub
parent 298dad2e13
commit 11cb3ce067
4 changed files with 110 additions and 28 deletions

View File

@@ -2,25 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
import '../browser_detection.dart';
import '../dom.dart';
import '../embedder.dart';
import '../util.dart';
import 'slots.dart';
/// A function which takes a unique `id` and some `params` and creates an HTML element.
///
/// This is made available to end-users through dart:ui in web.
typedef ParameterizedPlatformViewFactory = DomElement Function(
int viewId, {
Object? params,
});
/// A function which takes a unique `id` and creates an HTML element.
///
/// This is made available to end-users through dart:ui in web.
typedef PlatformViewFactory = DomElement Function(int viewId);
/// This class handles the lifecycle of Platform Views in the DOM of a Flutter Web App.
///
/// There are three important parts of Platform Views. This class manages two of
@@ -79,8 +68,13 @@ class PlatformViewManager {
/// `factoryFunction` needs to be a [PlatformViewFactory].
bool registerFactory(String viewType, Function factoryFunction,
{bool isVisible = true}) {
assert(factoryFunction is PlatformViewFactory ||
factoryFunction is ParameterizedPlatformViewFactory);
assert(
factoryFunction is ui_web.PlatformViewFactory ||
factoryFunction is ui_web.ParameterizedPlatformViewFactory,
'Factory signature is invalid. Expected either '
'{${ui_web.PlatformViewFactory}} or {${ui_web.ParameterizedPlatformViewFactory}} '
'but got: {${factoryFunction.runtimeType}}',
);
if (_factories.containsKey(viewType)) {
return false;
@@ -132,11 +126,11 @@ class PlatformViewManager {
final Function factoryFunction = _factories[viewType]!;
final DomElement content;
if (factoryFunction is ParameterizedPlatformViewFactory) {
content = factoryFunction(viewId, params: params);
if (factoryFunction is ui_web.ParameterizedPlatformViewFactory) {
content = factoryFunction(viewId, params: params) as DomElement;
} else {
factoryFunction as PlatformViewFactory;
content = factoryFunction(viewId);
factoryFunction as ui_web.PlatformViewFactory;
content = factoryFunction(viewId) as DomElement;
}
_ensureContentCorrectlySized(content, viewType);

View File

@@ -68,6 +68,7 @@ class PlatformViewMessageHandler {
final Map<dynamic, dynamic> args = methodCall.arguments as Map<dynamic, dynamic>;
final int viewId = args.readInt('id');
final String viewType = args.readString('viewType');
final Object? params = args['params'];
if (!_contentManager.knowsViewType(viewType)) {
callback(_codec.encodeErrorEnvelope(
@@ -89,11 +90,10 @@ class PlatformViewMessageHandler {
return;
}
// TODO(hterkelsen): How can users add extra `args` from the HtmlElementView widget?
final DomElement content = _contentManager.renderContent(
viewType,
viewId,
args,
params,
);
// For now, we don't need anything fancier. If needed, this can be converted

View File

@@ -4,21 +4,32 @@
import 'package:ui/src/engine.dart';
/// A function which takes a unique `id` and some `params` and creates an HTML
/// element.
typedef ParameterizedPlatformViewFactory = Object Function(
int viewId, {
Object? params,
});
/// A function which takes a unique `id` and creates an HTML element.
typedef PlatformViewFactory = Object Function(int viewId);
/// The platform view registry for this app.
final PlatformViewRegistry platformViewRegistry = PlatformViewRegistry();
/// A registry for factories that create platform views.
class PlatformViewRegistry {
/// Register [viewTypeId] as being creating by the given [viewFactory].
/// [viewFactory] can be any function that takes an integer and returns an
/// `HTMLElement` DOM object.
/// Register [viewType] as being created by the given [viewFactory].
///
/// [viewFactory] can be any function that takes an integer and optional
/// `params` and returns an `HTMLElement` DOM object.
bool registerViewFactory(
String viewTypeId,
Object Function(int viewId) viewFactory, {
String viewType,
Function viewFactory, {
bool isVisible = true,
}) {
return platformViewManager.registerFactory(
viewTypeId,
viewType,
viewFactory,
isVisible: isVisible,
);

View File

@@ -15,6 +15,8 @@ void main() {
const MethodCodec codec = StandardMethodCodec();
typedef PlatformViewFactoryCall = ({int viewId, Object? params});
void testMain() {
group('PlatformViewMessageHandler', () {
group('handlePlatformViewCall', () {
@@ -109,6 +111,80 @@ void testMain() {
reason:
'The response should be a success envelope, with null in it.');
});
test('passes creation params to the factory', () async {
final List<PlatformViewFactoryCall> factoryCalls = <PlatformViewFactoryCall>[];
contentManager.registerFactory(viewType, (int viewId, {Object? params}) {
factoryCalls.add((viewId: viewId, params: params));
return createDomHTMLDivElement();
});
final PlatformViewMessageHandler messageHandler = PlatformViewMessageHandler(
contentManager: contentManager,
);
final List<Completer<ByteData?>> completers = <Completer<ByteData?>>[];
completers.add(Completer<ByteData?>());
messageHandler.handlePlatformViewCall(
_getCreateMessage(viewType, 111),
completers.last.complete,
);
completers.add(Completer<ByteData?>());
messageHandler.handlePlatformViewCall(
_getCreateMessage(viewType, 222, <dynamic, dynamic>{'foo': 'bar'}),
completers.last.complete,
);
completers.add(Completer<ByteData?>());
messageHandler.handlePlatformViewCall(
_getCreateMessage(viewType, 333, 'foobar'),
completers.last.complete,
);
completers.add(Completer<ByteData?>());
messageHandler.handlePlatformViewCall(
_getCreateMessage(viewType, 444, <dynamic>[1, null, 'str']),
completers.last.complete,
);
final List<ByteData?> responses = await Future.wait(
completers.map((Completer<ByteData?> c) => c.future),
);
for (final ByteData? response in responses) {
expect(
codec.decodeEnvelope(response!),
isNull,
reason: 'The response should be a success envelope, with null in it.',
);
}
expect(factoryCalls, hasLength(4));
expect(factoryCalls[0].viewId, 111);
expect(factoryCalls[0].params, isNull);
expect(factoryCalls[1].viewId, 222);
expect(factoryCalls[1].params, <dynamic, dynamic>{'foo': 'bar'});
expect(factoryCalls[2].viewId, 333);
expect(factoryCalls[2].params, 'foobar');
expect(factoryCalls[3].viewId, 444);
expect(factoryCalls[3].params, <dynamic>[1, null, 'str']);
});
test('fails if the factory returns a non-DOM object', () async {
contentManager.registerFactory(viewType, (int viewId) {
// Return an object that's not a DOM element.
return Object();
});
final PlatformViewMessageHandler messageHandler =
PlatformViewMessageHandler(contentManager: contentManager);
final ByteData? message = _getCreateMessage(viewType, viewId);
expect(() {
messageHandler.handlePlatformViewCall(message, (_) {});
}, throwsA(isA<TypeError>()));
});
});
group('"dispose" message', () {
@@ -162,12 +238,13 @@ class _FakePlatformViewManager extends PlatformViewManager {
}
}
ByteData? _getCreateMessage(String viewType, int viewId) {
ByteData? _getCreateMessage(String viewType, int viewId, [Object? params]) {
return codec.encodeMethodCall(MethodCall(
'create',
<String, dynamic>{
'id': viewId,
'viewType': viewType,
if (params != null) 'params': params,
},
));
}