Thanks to visit codestin.com
Credit goes to github.com

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

Adds support for JavaScript channels to the new iOS implementation of webview_flutter_wkwebview. #4782

Merged
merged 7 commits into from
Feb 11, 2022

Conversation

bparrishMines
Copy link
Contributor

@bparrishMines bparrishMines commented Feb 9, 2022

Adds support for JavaScript channels to the new iOS implementation.

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 Feb 9, 2022
@@ -140,18 +146,89 @@ class WebKitWebViewPlatformController extends WebViewPlatformController {
if (!requiresUserAction) WKAudiovisualMediaType.none,
};
}

@override
Future<void> addJavascriptChannels(Set<String> javascriptChannelNames) async {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

@override
Future<void> removeJavascriptChannels(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

}) : super(callbacksHandler) {
_setCreationParams(
creationParams,
configuration: configuration ?? WKWebViewConfiguration(),
).then((_) => _initializationCompleter.complete());
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 method doesn't actually wait for any Future and _initializationCompleter is not used. I missed this in the PR before this one.

}

final Completer<void> _initializationCompleter = Completer<void>();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

unused

///
/// This should only be used for testing purposes.
@visibleForTesting
class WebViewProxy {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Name change since this class constructs more than just WKWebView.

@@ -49,7 +59,6 @@ void main() {
CreationParams? creationParams,
bool hasNavigationDelegate = false,
bool hasProgressTracking = false,
bool useHybridComposition = false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

unused

@bparrishMines bparrishMines changed the title [WIP] Ios webview 2 Adds support for JavaScript channels to the new iOS implementation of webview_flutter_wkwebview. Feb 10, 2022
/// An interface for receiving messages from JavaScript code running in a webpage.
///
/// Wraps [WKScriptMessageHandler](https://developer.apple.com/documentation/webkit/wkscriptmessagehandler?language=objc)
class WKScriptMessageHandler {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is making this a class actually useful? A one-method protocol seems like it would be more simply represented by a function typedef. You could even name that typedef WKScriptMessageHandler.

Copy link
Contributor

Choose a reason for hiding this comment

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

(Especially since you've converted the protocol method to a lambda anyway.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this idea since it would make this simpler and easier to test. But my main concern would be that this protocol could eventually be updated with a new method, then we would have to convert it back to a class. Although this would be relatively trivial to do; ~50 line change.

So I wonder if you think it's better to play it safe and avoid larger changes in the future or use our intuition to convert one-method classes/protocols to typedefs. Java uses a lot of one method interfaces, so this policy could definitely apply to the Android code as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking that since it was entirely internal it wouldn't be a big deal if we had to convert it to a class (if it were public I'd definitely say we should keep this version). But I guess in cases where we did have to do that conversion later it would undermine your design goal of this approach of putting the language boundary as close to the OS APIs as possible in order to make updating it for small OS API changes a simple, mechanical change.

I guess it really becomes a gamble about whether we think new methods are likely or not. It's tricky since the pattern of using protocols for delegation, rather than blocks, is baked pretty deeply into ObjC norms, so the existence of a protocol isn't really an indication of whether the designers expect to add more methods.

I'll leave it to you to decide since I think there are good arguments both ways here, and the main deciding factor is unknowable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My preference is to play it safe so that it can remain easy to maintain for future maintainers and community developers. So I'll keep it as is for now.

_setWebViewConfiguration(
configuration,
allowsInlineMediaPlayback: params.webSettings?.allowsInlineMediaPlayback,
autoMediaPlaybackPolicy: params.autoMediaPlaybackPolicy,
);

webView = webViewProxy.createWebView(configuration);

addJavascriptChannels(params.javascriptChannelNames);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't we want to await this, and keep the surrounding method async?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good catch. I forgot I added this method. I changed the method to be async again. However, its only use is in the constructor so it won't be awaited.

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

final Set<String> remainingNames = _scriptMessageHandlers.keys.toSet();
_scriptMessageHandlers.clear();

return addJavascriptChannels(remainingNames);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Since this is a (Future) void function, wouldn't it be more idiomatic to await rather than return here?

@bparrishMines bparrishMines merged commit 36df4c5 into flutter:main Feb 11, 2022
@bparrishMines bparrishMines deleted the ios_webview_2 branch February 11, 2022 19:24
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 14, 2022
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