Skip to content

Conversation

@rickhohler
Copy link

@rickhohler rickhohler commented Jan 29, 2026

Fixes flutter/flutter#180914

Description

This PR fixes a crash that occurs when setOnConsoleMessage is called multiple times. The WKUserContentController throws an exception if a script message handler with the same name is added more than once. This change ensures that any existing handler is removed before adding the new one.

Tests

  • Added testAddScriptMessageHandlerHandlesDuplicates to UserContentControllerProxyAPITests.swift.
  • Verified locally with xcodebuild (Exit Code 0).

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2 3

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces two separate and valuable improvements. The main change fixes a crash in webview_flutter_wkwebview when setOnConsoleMessage is called multiple times by correctly removing any existing script message handler before adding a new one. This is accompanied by a solid test case. The second change improves error handling in camera_avfoundation by checking the result of AVAssetWriter.startWriting() and propagating errors, also with a corresponding test. Both changes are correct and well-implemented. For future contributions, consider submitting unrelated changes in separate pull requests to improve review focus and maintain a clean commit history.

@rickhohler rickhohler force-pushed the fix/webview-console-crash branch from 6adc861 to 754ec75 Compare January 29, 2026 03:48
@stuartmorgan-g
Copy link
Collaborator

Please see this comment, which applies here as well.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft January 29, 2026 14:20
) throws {
// WKUserContentController will crash if a script message handler with the same name
// is added twice. We remove the existing one (if any) to ensure the new one replaces it.
pigeonInstance.removeScriptMessageHandler(forName: name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

As a high-level review, this is not consistent with the design of this plugin, which is to use intentionally minimal native wrappers, and implement as much logic as possible in Dart. We would not add remove logic to the add wrapper when removing can be done from Dart.

@rickhohler rickhohler marked this pull request as ready for review January 29, 2026 17:21
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request fixes a crash that occurs when setOnConsoleMessage is called multiple times by ensuring any existing script message handler is removed before adding a new one. The change is correct and is accompanied by a new test case that properly verifies the fix. I have one minor suggestion regarding code formatting consistency in the new test.

@rickhohler
Copy link
Author

rickhohler commented Jan 29, 2026

@stuartmorgan-g I've refactored the fix to be purely in Dart as requested. The native changes have been reverted, and the fix is now implemented in webkit_webview_controller.dart with a corresponding regression test. I also restored the PR checklist.

@stuartmorgan-g stuartmorgan-g requested review from bparrishMines and removed request for LouiseHsu January 29, 2026 17:50
@stuartmorgan-g
Copy link
Collaborator

  • Added testAddScriptMessageHandlerHandlesDuplicates to UserContentControllerProxyAPITests.swift.
  • Verified locally with xcodebuild (Exit Code 0).

The first entry here is no longer accurate, and it's not clear how the second is a test.

These changes don't appear to have been pushed to the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[webview_flutter] Calling setOnConsoleMessage multiple times throws

2 participants