@@ -21,35 +21,41 @@ typedef PlatformViewContentHandler = void Function(DomElement);
2121
2222/// This class handles incoming framework messages to create/dispose Platform Views.
2323///
24- /// (An instance of this class is connected to the `flutter/platform_views`
24+ /// (The instance of this class is connected to the `flutter/platform_views`
2525/// Platform Channel in the [EnginePlatformDispatcher] class.)
2626///
2727/// It uses a [PlatformViewManager] to handle the CRUD of the DOM of Platform Views.
2828/// This `contentManager` is shared across the engine, to perform
2929/// all operations related to platform views (registration, rendering, etc...),
3030/// regardless of the rendering backend.
3131///
32- /// When the `contents` of a Platform View are created, a [PlatformViewContentHandler]
33- /// function (passed from the outside) will decide where in the DOM to inject
34- /// said content.
32+ /// Platform views are injected into the DOM when needed by the correct instance
33+ /// of the active renderer.
3534///
36- /// The rendering/ compositing of Platform Views can create the other "half" of a
35+ /// The rendering and compositing of Platform Views can create the other "half" of a
3736/// Platform View: the `slot` , through the [createPlatformViewSlot] method.
3837///
3938/// When a Platform View is disposed of, it is removed from the cache (and DOM)
4039/// directly by the `contentManager` . The canvaskit rendering backend needs to do
4140/// some extra cleanup of its internal state, but it can do it automatically. See
42- /// [HtmlViewEmbedder.disposeViews]
41+ /// [HtmlViewEmbedder.disposeViews] .
4342class PlatformViewMessageHandler {
4443 PlatformViewMessageHandler ({
45- required DomElement platformViewsContainer,
46- PlatformViewManager ? contentManager,
47- }) : _contentManager = contentManager ?? PlatformViewManager .instance,
48- _platformViewsContainer = platformViewsContainer;
44+ required PlatformViewManager contentManager,
45+ }) : _contentManager = contentManager;
46+
47+ static const String channelName = 'flutter/platform_views' ;
48+
49+ /// The shared instance of PlatformViewMessageHandler.
50+ ///
51+ /// Unless configured differently, this connects to the shared instance of the
52+ /// [PlatformViewManager] .
53+ static PlatformViewMessageHandler instance = PlatformViewMessageHandler (
54+ contentManager: PlatformViewManager .instance,
55+ );
4956
5057 final MethodCodec _codec = const StandardMethodCodec ();
5158 final PlatformViewManager _contentManager;
52- final DomElement _platformViewsContainer;
5359
5460 /// Handle a `create` Platform View message.
5561 ///
@@ -58,10 +64,12 @@ class PlatformViewMessageHandler {
5864 ///
5965 /// (See [PlatformViewManager.registerFactory] for more details.)
6066 ///
61- /// The `contents` are inserted into the [_platformViewsContainer] .
62- ///
6367 /// If all goes well, this function will `callback` with an empty success envelope.
6468 /// In case of error, this will `callback` with an error envelope describing the error.
69+ ///
70+ /// The `callback` signals when the contents of a given [platformViewId] have
71+ /// been rendered. They're now accessible through `platformViewRegistry.getViewById`
72+ /// from `dart:ui_web` . **(Not the DOM!)**
6573 void _createPlatformView (
6674 _PlatformMessageResponseCallback callback, {
6775 required int platformViewId,
@@ -88,15 +96,12 @@ class PlatformViewMessageHandler {
8896 return ;
8997 }
9098
91- final DomElement content = _contentManager.renderContent (
99+ _contentManager.renderContent (
92100 platformViewType,
93101 platformViewId,
94102 params,
95103 );
96104
97- // For now, we don't need anything fancier. If needed, this can be converted
98- // to a PlatformViewStrategy class for each web-renderer backend?
99- _platformViewsContainer.append (content);
100105 callback (_codec.encodeSuccessEnvelope (null ));
101106 }
102107
@@ -126,7 +131,7 @@ class PlatformViewMessageHandler {
126131 /// This is transitional code to support the old platform view channel. As
127132 /// soon as the framework code is updated to send the Flutter View ID, this
128133 /// method can be removed.
129- void handleLegacyPlatformViewCall (
134+ void handlePlatformViewCall (
130135 String method,
131136 dynamic arguments,
132137 _PlatformMessageResponseCallback callback,
@@ -141,39 +146,11 @@ class PlatformViewMessageHandler {
141146 params: arguments['params' ],
142147 );
143148 return ;
149+ // TODO(web): Send `arguments` as a Map for `dispose` too!
144150 case 'dispose' :
145151 _disposePlatformView (callback, platformViewId: arguments as int );
146152 return ;
147153 }
148154 callback (null );
149155 }
150-
151- /// Handles a PlatformViewCall to the `flutter/platform_views` channel.
152- ///
153- /// This method handles two possible messages:
154- /// * `create` : See [_createPlatformView]
155- /// * `dispose` : See [_disposePlatformView]
156- void handlePlatformViewCall (
157- String method,
158- Map <dynamic , dynamic > arguments,
159- _PlatformMessageResponseCallback callback,
160- ) {
161- switch (method) {
162- case 'create' :
163- _createPlatformView (
164- callback,
165- platformViewId: arguments.readInt ('platformViewId' ),
166- platformViewType: arguments.readString ('platformViewType' ),
167- params: arguments['params' ],
168- );
169- return ;
170- case 'dispose' :
171- _disposePlatformView (
172- callback,
173- platformViewId: arguments.readInt ('platformViewId' ),
174- );
175- return ;
176- }
177- callback (null );
178- }
179156}
0 commit comments