Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter_web/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* Updates minimum Flutter version to 3.3.
* Aligns Dart and Flutter SDK constraints.

## 0.2.3

* Adds implementation for `WebWebViewController.addJavaScriptChannel` and `WebWebViewController.removeJavaScriptChannel`.

## 0.2.2+1

* Updates links for the merge of flutter/plugins into flutter/packages.
Expand Down
17 changes: 17 additions & 0 deletions packages/webview_flutter/webview_flutter_web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ The following functionality is currently available:

- `loadRequest`
- `loadHtmlString` (Without `baseUrl`)
- `addJavaScriptChannel`
- `removeJavaScriptChannel`

Nothing else is currently supported.

Expand All @@ -22,6 +24,21 @@ yet, so it currently requires extra setup to use:
Once the step above is complete, the APIs from `webview_flutter` listed
above can be used as normal on web.

### JavaScript channels

Contrary to the Android and iOS implementations, the `addJavaScriptChannel`
method does not create a named channel; the channel can only be accessed using
`window.parent`. In order to send a cross-platform message over a channel named
`flutterApp`, the following construction can be used in the web application:

```ts
if (window.flutterApp) {
Copy link
Member

Choose a reason for hiding this comment

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

What is window.flutterApp? Who's setting this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

webview_flutter is setting this on iOS and Android. If it's not available we assume we are on web and use the alternative.

window.flutterApp.postMessage(message);
} else if (window.parent) {
window.parent.postMessage(message, targetOrigin);
}
```

## Tests

Tests are contained in the `test` directory. You can run all tests from the root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:collection';
import 'dart:convert';
import 'dart:html' as html;

Expand Down Expand Up @@ -57,6 +58,10 @@ class WebWebViewController extends PlatformWebViewController {
WebWebViewControllerCreationParams get _webWebViewParams =>
params as WebWebViewControllerCreationParams;

/// Mapping between channel names and message event handlers.
HashMap<String, void Function(html.Event)> javascriptChannels =
HashMap<String, void Function(html.Event)>();

@override
Future<void> loadHtmlString(String html, {String? baseUrl}) async {
// ignore: unsafe_html
Expand Down Expand Up @@ -106,6 +111,31 @@ class WebWebViewController extends PlatformWebViewController {
encoding: encoding,
).toString();
}

@override
Future<void> addJavaScriptChannel(
JavaScriptChannelParams javaScriptChannelParams,
) async {
void handler(html.Event event) {
if (event is html.MessageEvent) {
javaScriptChannelParams.onMessageReceived(
JavaScriptMessage(message: event.data.toString()));
}
}

javascriptChannels[javaScriptChannelParams.name] = handler;
html.window.addEventListener('message', handler);
Copy link
Member

Choose a reason for hiding this comment

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

If a programmer registers multiple 'message' handlers (one per name), this means that every onMessageReceived registered will be called for every message received, right? I think this is potentially expensive. Also calling .toString() on the object that is being passed around is not great, you lose the ability to move around Transferable objects, which might be interesting to users of this API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If a programmer registers multiple 'message' handlers (one per name), this means that every onMessageReceived registered will be called for every message received, right? I think this is potentially expensive.

That is true. This can easily happen when you have multiple webviews in a single application. Since the webviews do not know anything from each other, we did not see another option. I think if you have a low amount of handlers (e.g. a handful), there is no performance hit.

Also calling .toString() on the object that is being passed around is not great, you lose the ability to move around Transferable objects, which might be interesting to users of this API.

On Android and iOS it is only possible to send string messages. We tried to mimic that API by limiting the allowed message types to string-values only. This way everything can be handled the same way in Flutter (i.e. all incoming messages are always string). If one wants to send complex messages one can always send a stringified object.

}

@override
Future<void> removeJavaScriptChannel(String javaScriptChannelName) async {
final void Function(html.Event)? handler =
javascriptChannels[javaScriptChannelName];

if (handler != null) {
html.window.removeEventListener('message', handler);
}
}
}

/// An implementation of [PlatformWebViewWidget] using Flutter the for Web API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_web
description: A Flutter plugin that provides a WebView widget on web.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 0.2.2+1
version: 0.2.3

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down