-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[webview_flutter] get/set a custom User Agent #1920
Conversation
- Added userAgent field to WebView - Added userAgent getter to WebViewController - Updated CreationParams and WebViewSettings - Added MethodChannel for 'getUserAgent'
- added updateUserAgent Method - added getUserAgent Method - userAgent gets set in flutter example
- added onGetUserAgent Method - added updateUserAgent Method - updated applySettings Method
- added tests for get/set User Agent - allow currentValue.userAgent/newValue.userAgent to be null in _clearUnchangedWebSettings
|
We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google. ℹ️ Googlers: Go here for more info. |
|
Updated emails for CLA. |
|
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
|
Thank you @LuisThein, we're already making use of this as we had the same requirement to change the user agent :) |
| _addIfNonNull('jsMode', settings.javascriptMode?.index); | ||
| _addIfNonNull('hasNavigationDelegate', settings.hasNavigationDelegate); | ||
| _addIfNonNull('debuggingEnabled', settings.debuggingEnabled); | ||
| _addIfNonNull('userAgent', settings.userAgent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if we rebuild a WebView that had an explicitly set userAgent and set the userAgent to null. IIUC the way this currently propagates will lead to preserving the previous user agent. I'd expect it to go back to the platform's default user agent.
We should add an test for this scenario as well (build with an explicitly set user agent, rebuild with user agent set to null)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I changed it so that the user agent can be set to null and the default platform user agent is used after a rebuild. Additionally I adapted the same logic for the FakePlatformWebView unit tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I think we should avoid updating the user agent each time the widget is rebuilt.
It seems like we're lacking the ability to express presence/absence of a setting that may be null. One way to do it would be to use some kind of an optional type.
I pushed 7976151 with a proposal on how to do that, let me know whether that makes sense to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that is a nice way to handle nullable settings and I agree that updating the user agent every rebuild is unnecessary.
- Updated Dartdoc comments - Changed tests to reuse one globalkey - Added test for rebuilding the webView with no user agent after setting the user agent
- Added comment to _webSettingsToMap regarding nullability of user agent - Changed handling of null user agent in FakePlatformWebView according to changes in WebviewMethodChannel
|
Added requested changes and updated branch. |
.../webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java
Show resolved
Hide resolved
|
@LuisThein I pushed a few changes to your branch. |
amirh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I pushed a few changes to the branch, let me know if these make sense to you.
- Renoved functions involved in getting the user agent - Updated tests to inject javascript to get the current user agent - updated changelog
.../webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java
Outdated
Show resolved
Hide resolved
.../webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java
Outdated
Show resolved
Hide resolved
.../webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java
Outdated
Show resolved
Hide resolved
amirh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
Based on this pull request #968 that was closed, I added the functionality to set a custom User Agent and get the current User Agent in the current WebView version.
Related Issues
flutter/flutter#28256
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]). This will ensure a smooth and quick review process.///).flutter analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?