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

[webview_flutter_wkwebview] Implementation of Objective-C WKWebView Host Apis #5341

Merged
merged 22 commits into from
Apr 30, 2022

Conversation

bparrishMines
Copy link
Contributor

@bparrishMines bparrishMines commented Apr 22, 2022

Implementation of a few of the Host API methods of WKWebView. I wanted to get early feedback on the method names and the organization of the code.

NOTE 2400 lines of the code is generated by pigeon, so it's not as large as it seems!

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.

@bparrishMines bparrishMines requested a review from cyanglaz as a code owner April 22, 2022 21:49
@github-actions github-actions bot added p: webview_flutter Edits files for a webview_flutter plugin platform-ios labels Apr 22, 2022
@bparrishMines bparrishMines changed the title implementation of host apis [webview_flutter_wkwebview] Implementation of Objective-C WKWebView Host Apis Apr 23, 2022
@lllyyy

This comment was marked as off-topic.

*
* @return An NSURLRequest or nil if data could not be converted.
*/
id FWFConvertURLRequestData(FWFNSUrlRequestData* data);
Copy link
Contributor

Choose a reason for hiding this comment

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

I generally suggest following Apple's Obj-C-ish C function naming convention of naming the returned type for things like this (e.g., NSRectFromCGRect rather than ConvertCGRect), so FWFNSURLRequestFromRequestData.

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.

I did a full sweep of the generated names and left a bunch of suggestions. Sorry to be bikeshedding on this, it's just tricky since we're still figuring out how to navigate the intersection of the instance manager and Pigeon-generated API ordering constraints.

Jenn and/or Chris should definitely weigh in on my suggestions before you update everything, to avoid you having to rename things over and over.

@@ -376,6 +389,7 @@ abstract class WKWebViewHostApi {

void loadFlutterAsset(int instanceId, String key);

@ObjCSelector('webViewWithInstanceIdCanGoBack:')
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I missed this before but was reminded by a recent discussion in a code cleanup issue: the ObjC name for all of these should actually be ...WithInstanceIdentifier.... Id is incorrect for ObjC, and while ID is okay, Apple uses Identifier in their APIs and we should be consistent with that.

But I was also thinking as I looked at these that there's not much potential for confusion about what identifier we're referring to in the context of this class IMO, so we could probably just use Identifier instead of InstanceIdentifier everywhere. (The param can still be named instanceId which will make things even more obvious at the declaration point.)

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 was considering using identifier as well, but I noticed this made the param names inconsistent. I'm fine with either, though. I'll wait for feedback from @jmagman / @cyanglaz as well.

Copy link
Member

Choose a reason for hiding this comment

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

Identifier sgtm

Copy link
Contributor

Choose a reason for hiding this comment

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

Identifier +1

@@ -364,10 +375,12 @@ abstract class WKWebViewHostApi {

void setNavigationDelegate(int instanceId, int? navigationDelegateInstanceId);

@ObjCSelector('urlForWebViewWithInstanceId:')
Copy link
Contributor

Choose a reason for hiding this comment

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

As much as it pains me, the correct naming here is URLFor... even though it's the start of the method. See https://developer.apple.com/documentation/webkit/wkwebview/1415005-url?language=objc for instance.

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe getUrlForWebViewWithInstanceId:

Copy link
Contributor

@stuartmorgan stuartmorgan Apr 29, 2022

Choose a reason for hiding this comment

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

As Maurice mentioned in another comment where I forgot, ObjC getters shouldn't actually start with get.

@implementation FWFWebView
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
// Prevents the contentInsets to be adjusted by iOS and gives control to Flutter.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: change "to be" to "from being".

if (!urlRequest) {
*error = [FlutterError errorWithCode:@"CreateNSURLRequestFailure"
message:@"Failed instantiating an NSURLRequest."
details:[NSString stringWithFormat:@"Url was: '%@'", request.url]];
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: "URL was:"

return self;
}

- (FWFWebView *)getWebViewInstance:(NSNumber *)instanceId {
Copy link
Contributor

Choose a reason for hiding this comment

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

getWebViewWithIdentifier:? (see comment in other file about why "identifier")

@throw [NSException exceptionWithName:@"UnsupportedException" reason:nil userInfo:nil];
}

- (void)reloadInstanceId:(nonnull NSNumber *)instanceId
Copy link
Contributor

Choose a reason for hiding this comment

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

reloadWebViewWithIdentifier:

@throw [NSException exceptionWithName:@"UnsupportedException" reason:nil userInfo:nil];
}

- (void)setAllowsBackForwardNavigationGesturesInstanceId:(nonnull NSNumber *)instanceId
Copy link
Contributor

Choose a reason for hiding this comment

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

The setters are all awkward ordering-wise. setBackForwardForWebViewWithIdentifier:isAllowed:error:?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer to leave NavigationGestures there, I need a lot context to know what a BackForward mean.
The name becomes very long.

Is navigationGesture not enough? Are there other navigation gestures besides backforward navigation gesture?If not maybe:
setNavigationGesturesForWebViewWithIdentifier:isAllowed:error:

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer to leave NavigationGestures there, I need a lot context to know what a BackForward mean.

Yes, I'm not sure why I dropped that. My name is clearly wrong since it doesn't disable actually going back or forward, just the gestures.

Having a long name on a method that's only ever going to be called by generated code (and tests) doesn't seem like aa big deal. That's definitely not the longest signature I've ever seen :)

@throw [NSException exceptionWithName:@"UnsupportedException" reason:nil userInfo:nil];
}

- (void)setNavigationDelegateInstanceId:(nonnull NSNumber *)instanceId
Copy link
Contributor

Choose a reason for hiding this comment

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

setNavigationDelegateForWebViewWithIdentifier:delegateIdentifier:error:?

@throw [NSException exceptionWithName:@"UnsupportedException" reason:nil userInfo:nil];
}

- (void)setUIDelegateInstanceId:(nonnull NSNumber *)instanceId
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, setUIDelegate{FWVWI}:delegateIdentifier:error:?

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

Jenn and/or Chris should definitely weigh in on my suggestions before you update everything, to avoid you having to rename things over and over.

@stuartmorgan's name suggestions LGTM

@@ -376,6 +389,7 @@ abstract class WKWebViewHostApi {

void loadFlutterAsset(int instanceId, String key);

@ObjCSelector('webViewWithInstanceIdCanGoBack:')
Copy link
Member

Choose a reason for hiding this comment

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

Identifier sgtm

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.

I left a bunch more naming bikeshedding on classes I apparently didn't look at last time. I don't want to beat this PR into the ground though, and I think the patterns are mostly established now and can be applied (with mixed success depending on the name) to all the methods I flagged without me needing to re-review. If there are one or two that we want to adjust later we can do it as we go now that we have a reasonable baseline.

error:(FlutterError *_Nullable *_Nonnull)error;
- (void)createDefaultDataStoreInstanceId:(NSNumber *)instanceId
error:(FlutterError *_Nullable *_Nonnull)error;
- (void)removeDataOfTypesInstanceId:(NSNumber *)instanceId
Copy link
Contributor

Choose a reason for hiding this comment

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

Apparently I didn't look at every class, sorry 😬

removeDataFromDataStoreWithIdentifier:ofTypes:secondsModifiedSinceEpoch:completion:


@protocol FWFUIViewHostApi
/// @return `nil` only when `error != nil`.
- (nullable NSArray<NSNumber *> *)getContentOffsetInstanceId:(NSNumber *)instanceId
Copy link
Contributor

Choose a reason for hiding this comment

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

contentOffsetForViewWithIdentifier:

/// The codec used by FWFWKPreferencesHostApi.
NSObject<FlutterMessageCodec> *FWFWKPreferencesHostApiGetCodec(void);

@protocol FWFWKPreferencesHostApi
Copy link
Contributor

Choose a reason for hiding this comment

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

Same issues for these.

NSObject<FlutterMessageCodec> *FWFWKNavigationDelegateHostApiGetCodec(void);

@protocol FWFWKNavigationDelegateHostApi
- (void)createInstanceId:(NSNumber *)instanceId error:(FlutterError *_Nullable *_Nonnull)error;
Copy link
Contributor

Choose a reason for hiding this comment

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

createWithIdentifier:error:

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.

5 participants