Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix re-registering js bridges
  • Loading branch information
meliksahcakirr committed Nov 14, 2024
commit f4c0269c746c07274b17de82d34eba68998d2d52
Original file line number Diff line number Diff line change
Expand Up @@ -742,12 +742,16 @@ window.addEventListener("error", function(e) {
_javaScriptChannelParams.keys.forEach(
_webView.configuration.userContentController.removeScriptMessageHandler,
);

_javaScriptChannelParams.remove(removedJavaScriptChannel);
final Map<String, WebKitJavaScriptChannelParams> remainingChannelParams =
Map<String, WebKitJavaScriptChannelParams>.from(
_javaScriptChannelParams,
);
remainingChannelParams.remove(removedJavaScriptChannel);
_javaScriptChannelParams.clear();

await Future.wait(<Future<void>>[
for (final JavaScriptChannelParams params
in _javaScriptChannelParams.values)
in remainingChannelParams.values)
addJavaScriptChannel(params),
// Zoom is disabled with a WKUserScript, so this adds it back if it was
// removed above.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,72 @@ void main() {
verifyNoMoreInteractions(mockUserContentController);
});

test('removeJavaScriptChannel multiple times', () async {
final WebKitProxy webKitProxy = WebKitProxy(
createScriptMessageHandler: ({
required void Function(
WKUserContentController userContentController,
WKScriptMessage message,
) didReceiveScriptMessage,
}) {
return WKScriptMessageHandler.detached(
didReceiveScriptMessage: didReceiveScriptMessage,
);
},
);

final WebKitJavaScriptChannelParams javaScriptChannelParams1 =
WebKitJavaScriptChannelParams(
name: 'name1',
onMessageReceived: (JavaScriptMessage message) {},
webKitProxy: webKitProxy,
);

final WebKitJavaScriptChannelParams javaScriptChannelParams2 =
WebKitJavaScriptChannelParams(
name: 'name2',
onMessageReceived: (JavaScriptMessage message) {},
webKitProxy: webKitProxy,
);

final MockWKUserContentController mockUserContentController =
MockWKUserContentController();

final WebKitWebViewController controller = createControllerWithMocks(
mockUserContentController: mockUserContentController,
);

await controller.addJavaScriptChannel(javaScriptChannelParams1);
await controller.addJavaScriptChannel(javaScriptChannelParams2);
reset(mockUserContentController);

await controller.removeJavaScriptChannel('name1');

verify(mockUserContentController.removeAllUserScripts());
verify(mockUserContentController.removeScriptMessageHandler('name1'));
verify(mockUserContentController.removeScriptMessageHandler('name2'));

verify(mockUserContentController.addScriptMessageHandler(
argThat(isA<WKScriptMessageHandler>()),
'name2',
));

final WKUserScript userScript =
verify(mockUserContentController.addUserScript(captureAny))
.captured
.single as WKUserScript;
expect(userScript.source, 'window.name2 = webkit.messageHandlers.name2;');
expect(
userScript.injectionTime,
WKUserScriptInjectionTime.atDocumentStart,
);

await controller.removeJavaScriptChannel('name2');
verify(mockUserContentController.removeAllUserScripts());
verify(mockUserContentController.removeScriptMessageHandler('name2'));
verifyNoMoreInteractions(mockUserContentController);
});

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