Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[webview_flutter] Adds support to listen to url changes #3313

Merged
merged 20 commits into from
Apr 18, 2023

Conversation

bparrishMines
Copy link
Contributor

@bparrishMines bparrishMines commented Feb 27, 2023

Transfer of flutter/plugins#7113 (All the comments of that PR have been addressed in this one)

This PR follows the procedure for changing federated plugins: https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins

This is the aggregate solution for flutter/flutter#27729 and is NOT intended to be submitted. This will be split into PRs for each package after approval of this PR. All split PRs have been merged.

This adds the PlatformNavigationDelegate.setOnUrlChange(UrlChange) method to track url changes.

Android adds support for this with the WebViewClient.doUpdateVisitedHistory

iOS adds support by observing the value WKWebView.URL. Since the WKWebView.URL would pass back an NSURL to the observeValue callback, the observeValue callback would need to distinguish the difference when returning an int or an identifier. So the ObjectOrIdentifier data class was created to handle this. Alternatives considered:

  • Convert NSURL to an NSString. This would work for this situation, but wouldn't work for a class that can't be easily converted to a String.
  • Create a separate observeValue callback method for returning an identifier. Potentially named observeIdentifier. This doesn't work for returning a list of Objects.

Fixes flutter/flutter#27729

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

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

error:(FlutterError *_Nullable __autoreleasing *_Nonnull)error {
NSURL *instance = [self urlForIdentifier:identifier error:error];
if (*error) {
return nil;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we handle the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The pigeon host API is calling this method, so the error should be passed back to Dart. The error in *error could be set by the method above.

@@ -1673,46 +1707,10 @@ @implementation FWFNSObjectFlutterApiCodecReader
- (nullable id)readValueOfType:(UInt8)type {
switch (type) {
case 128:
return [FWFNSErrorData fromList:[self readValue]];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are removed because the class method observeValue now returns an ObjectOrIdentifier. When it returned an Object, all of these were included as possible return values.

@bparrishMines bparrishMines changed the title Url change tres [webview_flutter] Adds support to listen to url changes Feb 28, 2023
Copy link
Contributor

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

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

iOS looks good to me

Copy link
Contributor

@stuartmorgan stuartmorgan left a comment

Choose a reason for hiding this comment

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

(Transferring my high-level LGTM from the other PR to keep GitHub status up to date with reality.)

NavigationDelegate.fromPlatform(
this.platform, {
this.onNavigationRequest,
this.onPageStarted,
this.onPageFinished,
this.onProgress,
this.onWebResourceError,
void Function(UrlChange change)? onUrlChange,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was not added as a field of the clas because there is a chance that the field can be different from the actual value. For example, a user may set this value and then call NavigationDelegate.platform.setOnUrlChange:

final NavigationDelegate delegate = NavigationDelegate(onUrlChange: (_) {});
delegate.platform.setUrlChange((_) {});

This is true for all the other callbacks, but changing them would be a breaking change.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is true for all the other callbacks, but changing them would be a breaking change.

Couldn't we replace the final fields with getters, and have the getters return the platform version of the field, to preserve API compatibility while removing the potential for them to become out of sync?

(Doesn't need to be in this PR.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mvanbeusekom suggested something similar by making the callback method setters in PlatformNavigationDelegate fields instead of methods. I decided against it initially because using a method allowed us to throw an UnimplementedError for unsupported callback methods. But we could add getters to the interface as a mix of both solutions.

This wouldn't be possible in this PR since it is something we would have to add to the platform interface first anyways.

Copy link
Contributor

@stuartmorgan stuartmorgan left a comment

Choose a reason for hiding this comment

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

LGTM!

NavigationDelegate.fromPlatform(
this.platform, {
this.onNavigationRequest,
this.onPageStarted,
this.onPageFinished,
this.onProgress,
this.onWebResourceError,
void Function(UrlChange change)? onUrlChange,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is true for all the other callbacks, but changing them would be a breaking change.

Couldn't we replace the final fields with getters, and have the getters return the platform version of the field, to preserve API compatibility while removing the potential for them to become out of sync?

(Doesn't need to be in this PR.)

@bparrishMines bparrishMines added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 18, 2023
@auto-submit auto-submit bot merged commit 9342f5e into flutter:main Apr 18, 2023
@bparrishMines bparrishMines deleted the url_change_tres branch April 18, 2023 19:20
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 19, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 19, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 19, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Apr 19, 2023
flutter/packages@faf53fb...88591be

2023-04-19 [email protected] Roll Flutter from 42fb0b2 to 3476b96 (20 revisions) (flutter/packages#3760)
2023-04-19 49699333+dependabot[bot]@users.noreply.github.com Bump cirrusci/flutter from `794fbbc` to `d99b1ba` in /.ci (flutter/packages#3724)
2023-04-18 [email protected] [webview_flutter] Adds support to listen to url changes  (flutter/packages#3313)
2023-04-18 [email protected] Roll Flutter from 15cb1f8 to 42fb0b2 (19 revisions) (flutter/packages#3756)
2023-04-18 49699333+dependabot[bot]@users.noreply.github.com [webview]: Bump com.android.tools.build:gradle from 7.2.2 to 8.0.0 in /packages/webview_flutter/webview_flutter_android/android (flutter/packages#3739)
2023-04-18 [email protected] [local_auth] Convert Android to Pigeon (flutter/packages#3748)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
nploi pushed a commit to nploi/packages that referenced this pull request Jul 16, 2023
Transfer of flutter/plugins#7113 (All the comments of that PR have been addressed in this one)

This PR follows the procedure for changing federated plugins: https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins

~~This is the aggregate solution for flutter/flutter#27729 and is NOT intended to be submitted. This will be split into PRs for each package after approval of this PR.~~ All split PRs have been merged.

This adds the `PlatformNavigationDelegate.setOnUrlChange(UrlChange)` method to track url changes.

Android adds support for this with the [WebViewClient.doUpdateVisitedHistory](https://developer.android.com/reference/android/webkit/WebViewClient#doUpdateVisitedHistory(android.webkit.WebView,%20java.lang.String,%20boolean))

iOS adds support by observing the value [WKWebView.URL](https://developer.apple.com/documentation/webkit/wkwebview/1415005-url?language=objc). Since the WKWebView.URL would pass back an [NSURL](https://developer.apple.com/documentation/foundation/nsurl?language=objc) to the [observeValue](https://developer.apple.com/documentation/objectivec/nsobject/1416553-observevalueforkeypath?language=objc) callback, the `observeValue` callback would need to distinguish the difference when returning an `int` or an identifier. So the `ObjectOrIdentifier` data class was created to handle this. Alternatives considered:

* Convert `NSURL `to an `NSString`. This would work for this situation, but wouldn't work for a class that can't be easily converted to a `String`.
* Create a separate `observeValue `callback method for returning an identifier. Potentially named `observeIdentifier`. This doesn't work for returning a list of Objects.

Fixes flutter/flutter#27729
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: webview_flutter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WebView for Flutter - support listen and reacting to URL changes
3 participants