Skip to content

Conversation

sophiebremer
Copy link

@sophiebremer sophiebremer commented Aug 16, 2025

This PR adds an implementation for WebWebViewController.runJavaScript and changes the way WebWebViewController.loadHtmlString injects HTML code while also adding support for the baseUrl option.

The following issues are fixed:

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

google-cla bot commented Aug 16, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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 the runJavaScript method for the web implementation of WebView and refactors loadHtmlString to better support the baseUrl parameter. The changes are generally good, but there are a few areas for improvement regarding event listener management and robustness against race conditions. I've identified a potential memory leak in loadHtmlString and a high-severity issue in _getIFrameDocument that could lead to an infinite loop. I've provided suggestions to address these issues by using one-time event listeners and replacing polling with a more robust event-based approach.

Comment on lines 89 to 94
_webWebViewParams.iFrame.addEventListener(
'load',
() {
_webWebViewParams.iFrame.contentDocument?.write(html.toJS);
loading.complete();
}.toJS);

Choose a reason for hiding this comment

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

medium

The load event listener is added every time loadHtmlString is called but is never removed. This can lead to a memory leak if this method is called multiple times on the same controller, as multiple listeners will be attached.

To ensure the listener is only called once per loadHtmlString call, you can use the once option in addEventListener. This will automatically remove the listener after it has been invoked.

    _webWebViewParams.iFrame.addEventListener(
      'load',
      (web.Event _) {
        _webWebViewParams.iFrame.contentDocument?.write(html.toJS);
        loading.complete();
      }.toJS,
      AddEventListenerOptions(once: true),
    );

@sophiebremer sophiebremer changed the title Add WebWebViewController.runJavaScript [webview_flutter_web] Add WebWebViewController.runJavaScript Aug 16, 2025
@stuartmorgan-g
Copy link
Collaborator

From triage: @sophiebremer Are you planning on updating this PR to have graceful fallback to a clear error when run against anything other than a same-origin iframe, instead of crashing and/or hanging?

@sophiebremer
Copy link
Author

@stuartmorgan-g I will add the StateError.

@sophiebremer sophiebremer reopened this Sep 24, 2025
@fluttergithubbot
Copy link

An existing Git SHA, c4b4afff81d047217d0cdb6bc60f2b9e4ddbe2ce, was detected, and no actions were taken.

To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with --force) that already was pushed before, push a blank commit (git commit --allow-empty -m "Trigger Build") or rebase to continue.

@fluttergithubbot
Copy link

An existing Git SHA, c49686007cf4697e49dd561dc8554aa87449e9f6, was detected, and no actions were taken.

To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with --force) that already was pushed before, push a blank commit (git commit --allow-empty -m "Trigger Build") or rebase to continue.

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.

3 participants