Repository navigation
feat(windows): add DevTools, accelerator key and download settings - #44
Merged
Merged
Conversation
WebView2 enables DevTools, browser accelerator keys and downloads by default, and WindowsWebViewController had no way to turn any of them off: F12 always opened DevTools and a page could always save files to the user's Downloads folder. Add three controller methods, each backed by a WebView2 setting: - setInspectable(bool): ICoreWebView2Settings::AreDevToolsEnabled; named after WebKitWebViewController.setInspectable. - setBrowserAcceleratorKeysEnabled(bool): ICoreWebView2Settings3::AreBrowserAcceleratorKeysEnabled. - setDownloadsEnabled(bool): cancels DownloadStarting before a download operation exists, so nothing is written and no download event is sent. Defaults are unchanged. The requested values are kept by the controller and re-applied when a failed initialization is retried, because the retried WebView2 starts from the WebView2 defaults again.
Start one download with downloads allowed as a control, then the same download with downloads disabled, and check the user's Downloads folder. Without the control, a page that never started a download would pass too.
Collaborator
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
WebView2 turns on DevTools, browser accelerator keys and downloads by default, and
WindowsWebViewControllerhas no way to turn any of them off:DownloadStartingis marked handled and the download goes ahead, with no way to refuse it from Dart.That is fine for a general-purpose browser view, but it blocks apps that show content they do not fully control: kiosks, embedded help, previews of user-supplied HTML. On iOS/macOS the same need is already covered by
WebKitWebViewController.setInspectable.Fix
Three methods on
WindowsWebViewController, each backed by a WebView2 setting:setInspectable(bool)ICoreWebView2Settings::put_AreDevToolsEnabled— named afterWebKitWebViewController.setInspectablesetBrowserAcceleratorKeysEnabled(bool)ICoreWebView2Settings3::put_AreBrowserAcceleratorKeysEnabledsetDownloadsEnabled(bool)DownloadStarting→put_Cancel(TRUE)before a download operation exists, so nothing is written and no download event is sentDefaults are unchanged. Each method goes through the existing Pigeon host API (
setZoomControlEnabledserved as the template), and the channels are normalized withtool/normalize_pigeon_namespaces.py.The controller keeps the requested values and applies them again when a failed initialization is retried (the widget's Refresh), because the new WebView2 starts from the WebView2 defaults. A small private
_WebView2Settingalso records what the current native WebView already has, so a value is sent once even when the setter and the initialization race.setBrowserAcceleratorKeysEnabledneedsICoreWebView2Settings3. On a runtime without it the call throws aPlatformException(not_supported) rather than failing silently.Tests
webview_all_windows: unit tests for all three settings forwarding to the host API, and for their re-application after a failed-then-refreshed initialization.examples/platform: an integration test with a real WebView2. It starts one download with downloads allowed, as a control, then the same download with downloads disabled, and checks the user's Downloads folder. Without the control, a page that never started a download would pass too. The test removes both files.SetDownloadsEnabledpatched to ignore its argument, it fails with "A download started while downloads were disabled."Verification
Windows 11 Pro 25H2 (26200), WebView2 Runtime 153.0, Flutter 3.47.0, VS 2022 Build Tools 17.14:
flutter build windows --debuginexamples/platformbuilds with no warnings.flutter test integration_test/webview_flutter_test.dart -d windows:All tests passed!(29 passed, 3 skipped), new test included.flutter testinwebview_all_windows: 53 tests pass.flutter analyze --fatal-infosinwebview_all_windowsandexamples/platform:No issues found!dart format --set-exit-if-changedon the changed files is clean, andtool/normalize_pigeon_namespaces.py --check allpasses.Docs:
platforms/windows.md(EN/ZH) controller API table and the Windows section ofguides/debugging.md. I did not touchCHANGELOG.md, since release notes come with the release.