Skip to content

[webview_flutter_wkebview] Verify JavaScriptChannels have a unique name #5904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 17, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT
## 3.10.3

* Adds a check that throws an `ArgumentError` when `WebKitWebViewController.addJavaScriptChannel`
receives a `JavaScriptChannelParams` with a name that is not unique.
* Updates minimum iOS version to 12.0 and minimum Flutter version to 3.16.6.

## 3.10.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ class WebKitWebViewController extends PlatformWebViewController {
Future<void> addJavaScriptChannel(
JavaScriptChannelParams javaScriptChannelParams,
) {
final String channelName = javaScriptChannelParams.name;
Copy link
Member

Choose a reason for hiding this comment

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

Is somewhere already checking this isn't an empty string?

This parameter must be unique within the user content controller and must not be an empty string.

https://developer.apple.com/documentation/webkit/wkusercontentcontroller/1537172-addscriptmessagehandler

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 (_javaScriptChannelParams.containsKey(channelName)) {
throw ArgumentError(
'A JavaScriptChannel with name `$channelName` already exists.',
);
}

final WebKitJavaScriptChannelParams webKitParams =
javaScriptChannelParams is WebKitJavaScriptChannelParams
? javaScriptChannelParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_wkwebview
description: A Flutter plugin that provides a WebView widget based on Apple's WKWebView control.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_wkwebview
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 3.10.2
version: 3.10.3

environment:
sdk: ^3.2.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,45 @@ void main() {
);
});

test('addJavaScriptChannel requires channel with a unique name', () async {
final WebKitProxy webKitProxy = WebKitProxy(
createScriptMessageHandler: ({
required void Function(
WKUserContentController userContentController,
WKScriptMessage message,
) didReceiveScriptMessage,
}) {
return WKScriptMessageHandler.detached(
didReceiveScriptMessage: didReceiveScriptMessage,
);
},
);
final MockWKUserContentController mockUserContentController =
MockWKUserContentController();
final WebKitWebViewController controller = createControllerWithMocks(
mockUserContentController: mockUserContentController,
);

const String nonUniqueName = 'name';
final WebKitJavaScriptChannelParams javaScriptChannelParams =
WebKitJavaScriptChannelParams(
name: nonUniqueName,
onMessageReceived: (JavaScriptMessage message) {},
webKitProxy: webKitProxy,
);
await controller.addJavaScriptChannel(javaScriptChannelParams);

expect(
() => controller.addJavaScriptChannel(
JavaScriptChannelParams(
name: nonUniqueName,
onMessageReceived: (_) {},
),
),
throwsArgumentError,
);
});

test('removeJavaScriptChannel', () async {
final WebKitProxy webKitProxy = WebKitProxy(
createScriptMessageHandler: ({
Expand Down