Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter_wkwebview] Implement WKNavigationDelegate.didFinishNavigation as a proof of concept for callback methods #5199

Merged
merged 8 commits into from
Apr 15, 2022

Conversation

bparrishMines
Copy link
Contributor

@bparrishMines bparrishMines commented Apr 7, 2022

This PR implements one of the callback methods for WKNavigationDelegate as a proof of concept for the rest of the callback methods in the library. An anonymous functions is used rather than making a callback on an overriden method. This provides the benefit of separating the WKNavigationDelegate instance from the callback function instances.

By separating them, the Dart WKNavigationDelegate can be disposed and still receive callback methods. Which prevents a race condition where the Dart WKNavigationDelegate is disposed at the same time a callback is made from Objective-C. To prevent this race condition on Android, the WebView tracks all the callback classes and handles disposing them. This implementation allows each callback class to manage its own callback methods.

No version change:
Part of flutter/flutter#93732 and doesn't make any changes to the current implementation.

No CHANGELOG change: Incremental unused code doesn't need to be noted in the CHANGELOG.

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/plugins repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin 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.

@github-actions github-actions bot added p: webview_flutter Edits files for a webview_flutter plugin platform-ios labels Apr 7, 2022
@bparrishMines bparrishMines changed the title callback method [webview_flutter_wkwebview] Implement WKNavigationDelegate.didFinishNavigation as a proof of concept Apr 11, 2022
@bparrishMines bparrishMines changed the title [webview_flutter_wkwebview] Implement WKNavigationDelegate.didFinishNavigation as a proof of concept [webview_flutter_wkwebview] Implement WKNavigationDelegate.didFinishNavigation as a proof of concept Apr 11, 2022
@bparrishMines bparrishMines changed the title [webview_flutter_wkwebview] Implement WKNavigationDelegate.didFinishNavigation as a proof of concept [webview_flutter_wkwebview] Implement WKNavigationDelegate.didFinishNavigation as a proof of concept for callback methods Apr 11, 2022

/// Mutable instance containing all Flutter Apis for the Foundation library.
///
/// This should only be changed for testing purposes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be better to enforce this with a private variable, a public getter, and a @visibleForTesting setter?

@visibleForTesting
late final FunctionFlutterApiImpl functionFlutterApi;

/// Ensures all the Flutter APIs have been setup to receive calls from native code.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: set up

class WebKitFlutterApis {
/// Constructs a [WebKitFlutterApis].
///
/// This should only be changed for testing purposes.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment doesn't make sense here, since the constructor doesn't change anything.


/// Mutable instance containing all Flutter Apis for WebKit.
///
/// This should only be changed for testing purposes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same note here as in the other file; this can be a getter+annotated setter

static WebKitFlutterApis instance = WebKitFlutterApis();

/// Sends binary data across the Flutter platform barrier.
final BinaryMessenger? binaryMessenger;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want this to be public?

@visibleForTesting
late final WKNavigationDelegateFlutterApiImpl navigationDelegateFlutterApi;

/// Ensures all the Flutter APIs have been setup to receive calls from native code.
Copy link
Contributor

Choose a reason for hiding this comment

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

set up

int? functionInstanceId;
if (didFinishNavigation != null) {
functionInstanceId = instanceManager.getInstanceId(didFinishNavigation);
functionInstanceId ??=
Copy link
Contributor

Choose a reason for hiding this comment

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

Why = then ??= rather than just functionInstanceId = ... ?? ...;?

) function =
instanceManager.getInstance(functionInstanceId)! as void Function(
WKWebView webView,
String? url,
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems worth typedefing this instead of repeating the signature twice.

Copy link
Contributor Author

@bparrishMines bparrishMines Apr 13, 2022

Choose a reason for hiding this comment

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

I actually realized that InstanceManager.getInstance uses generics to make this casting easier. So, I didn't actually need the as ....

@bparrishMines
Copy link
Contributor Author

@stuartmorgan This is ready for a second review

@stuartmorgan-g stuartmorgan-g self-requested a review April 14, 2022 18:46
Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

LGTM

@bparrishMines bparrishMines merged commit e3a184f into flutter:main Apr 15, 2022
@bparrishMines bparrishMines deleted the wkwebview_callback_method branch April 15, 2022 01:25
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 15, 2022
….didFinishNavigation` as a proof of concept for callback methods (flutter/plugins#5199)
LoPat-Airgram pushed a commit to mindcruiser/plugins that referenced this pull request Apr 19, 2022
* master: (153 commits)
  Roll Flutter from fd360c4 to ef5a6da (1 revision) (flutter#5298)
  [video_player_avfoundation] Applies the standardized transform for videos with different orientations (flutter#5069)
  Roll Flutter from 3752fb7 to fd360c4 (2 revisions) (flutter#5295)
  Roll Flutter from 3c4d7a1 to 3752fb7 (11 revisions) (flutter#5294)
  Add owners for Android implementations (flutter#5293)
  Roll Flutter from f4875ae to 3c4d7a1 (2 revisions) (flutter#5292)
  [video_player_web] Stop buffering when browser canPlayThrough. (flutter#5068)
  Roll Flutter from aa5d7b6 to f4875ae (1 revision) (flutter#5289)
  Roll Flutter from 44be0b8 to aa5d7b6 (12 revisions) (flutter#5286)
  Roll Flutter from ec8289c to 44be0b8 (2 revisions) (flutter#5284)
  Roll Flutter from 329ceae to ec8289c (26 revisions) (flutter#5283)
  [flutter_plugin_tools] Preserve Dart SDK version in all-plugins-app (flutter#5281)
  [webview_flutter_wkwebview] Implements the `HostApis` and methods for the `CookieManager`. (flutter#5244)
  [webview_flutter_wkwebview] Implement `WKNavigationDelegate.didFinishNavigation` as a proof of concept for callback methods (flutter#5199)
  Roll Flutter from 08e467d to 2b83332 (5 revisions) (flutter#5270)
  Roll Flutter from e2d1206 to 08e467d (1 revision) (flutter#5268)
  Roll Flutter from ff9d6e5 to e2d1206 (2 revisions) (flutter#5266)
  Roll Flutter from 0575932 to ff9d6e5 (1 revision) (flutter#5265)
  [local_auth] Refactor package to make use of new platform interface and native implementations (flutter#4701)
  Roll Flutter from cb968c5 to 0575932 (1 revision) (flutter#5263)
  ...
mauricioluz pushed a commit to mauricioluz/plugins that referenced this pull request Jan 26, 2023
…Navigation` as a proof of concept for callback methods (flutter#5199)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p: webview_flutter Edits files for a webview_flutter plugin platform-ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants