Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/web_ui/lib/src/engine/platform_dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {

case 'flutter/platform_views':
final MethodCall(:String method, :dynamic arguments) = standardCodec.decodeMethodCall(data);
final int? flutterViewId = tryFlutterViewId(arguments);
final int? flutterViewId = tryViewId(arguments);
if (flutterViewId == null) {
implicitView!.platformViewMessageHandler.handleLegacyPlatformViewCall(method, arguments, callback!);
return;
Expand Down
62 changes: 37 additions & 25 deletions lib/web_ui/lib/src/engine/platform_views/message_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PlatformViewMessageHandler {

/// Handle a `create` Platform View message.
///
/// This will attempt to render the `contents` and of a Platform View, if its
/// This will attempt to render the `contents` of a Platform View, if its
/// `viewType` has been registered previously.
///
/// (See [PlatformViewManager.registerFactory] for more details.)
Expand All @@ -63,36 +63,34 @@ class PlatformViewMessageHandler {
/// If all goes well, this function will `callback` with an empty success envelope.
/// In case of error, this will `callback` with an error envelope describing the error.
void _createPlatformView(
Map<dynamic, dynamic> arguments,
_PlatformMessageResponseCallback callback,
) {
final int viewId = arguments.readInt('id');
final String viewType = arguments.readString('viewType');
final Object? params = arguments['params'];

if (!_contentManager.knowsViewType(viewType)) {
_PlatformMessageResponseCallback callback, {
required int platformViewId,
required String platformViewType,
required Object? params,
}) {
if (!_contentManager.knowsViewType(platformViewType)) {
callback(_codec.encodeErrorEnvelope(
code: 'unregistered_view_type',
message: 'A HtmlElementView widget is trying to create a platform view '
'with an unregistered type: <$viewType>.',
'with an unregistered type: <$platformViewType>.',
details: 'If you are the author of the PlatformView, make sure '
'`registerViewFactory` is invoked.',
));
return;
}

if (_contentManager.knowsViewId(viewId)) {
if (_contentManager.knowsViewId(platformViewId)) {
callback(_codec.encodeErrorEnvelope(
code: 'recreating_view',
message: 'trying to create an already created view',
details: 'view id: $viewId',
details: 'view id: $platformViewId',
));
return;
}

final DomElement content = _contentManager.renderContent(
viewType,
viewId,
platformViewType,
platformViewId,
params,
);

Expand All @@ -105,28 +103,28 @@ class PlatformViewMessageHandler {
/// Handle a `dispose` Platform View message.
///
/// This will clear the cached information that the framework has about a given
/// `viewId`, through the [_contentManager].
/// `platformViewId`, through the [_contentManager].
///
/// Once that's done, the dispose call is delegated to the [_disposeHandler]
/// function, so the active rendering backend can dispose of whatever resources
/// it needed to get ahold of.
///
/// This function should always `callback` with an empty success envelope.
void _disposePlatformView(
int viewId,
_PlatformMessageResponseCallback callback,
) {
_PlatformMessageResponseCallback callback, {
required int platformViewId,
}) {
// The contentManager removes the slot and the contents from its internal
// cache, and the DOM.
_contentManager.clearPlatformView(viewId);
_contentManager.clearPlatformView(platformViewId);

callback(_codec.encodeSuccessEnvelope(null));
}

/// Handles legacy PlatformViewCalls that don't contain a `flutterViewId`.
/// Handles legacy PlatformViewCalls that don't contain a Flutter View ID.
///
/// This is transitional code to support the old platform view channel. As
/// soon as the framework code is updated to send the `flutterViewId`, this
/// soon as the framework code is updated to send the Flutter View ID, this
/// method can be removed.
void handleLegacyPlatformViewCall(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally forgot about this method. Good repurposing though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must've been looking at an individual commit from the PR 🙂 This method is new.

String method,
Expand All @@ -135,10 +133,16 @@ class PlatformViewMessageHandler {
) {
switch (method) {
case 'create':
_createPlatformView(arguments as Map<dynamic, dynamic>, callback);
arguments as Map<dynamic, dynamic>;
_createPlatformView(
callback,
platformViewId: arguments.readInt('id'),
platformViewType: arguments.readString('viewType'),
params: arguments['params'],
);
return;
case 'dispose':
_disposePlatformView(arguments as int, callback);
_disposePlatformView(callback, platformViewId: arguments as int);
return;
}
callback(null);
Expand All @@ -156,10 +160,18 @@ class PlatformViewMessageHandler {
) {
switch (method) {
case 'create':
_createPlatformView(arguments, callback);
_createPlatformView(
callback,
platformViewId: arguments.readInt('platformViewId'),
platformViewType: arguments.readString('platformViewType'),
params: arguments['params'],
);
return;
case 'dispose':
_disposePlatformView(arguments.readInt('id'), callback);
_disposePlatformView(
callback,
platformViewId: arguments.readInt('platformViewId'),
);
return;
}
callback(null);
Expand Down
8 changes: 4 additions & 4 deletions lib/web_ui/lib/src/engine/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -631,16 +631,16 @@ extension JsonExtensions on Map<dynamic, dynamic> {
/// Extracts view ID from the [MethodCall.arguments] map.
///
/// Throws if the view ID is not present or if [arguments] is not a map.
int readFlutterViewId(Object? arguments) {
return tryFlutterViewId(arguments)!;
int readViewId(Object? arguments) {
return tryViewId(arguments)!;
}

/// Extracts view ID from the [MethodCall.arguments] map.
///
/// Returns null if the view ID is not present or if [arguments] is not a map.
int? tryFlutterViewId(Object? arguments) {
int? tryViewId(Object? arguments) {
if (arguments is Map) {
return arguments.tryInt('flutterViewId');
return arguments.tryInt('viewId');
}
return null;
}
Expand Down
Loading