This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[webview_flutter_wkwebview] Implement Dart side of Flutter Apis #5933
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8a9cf2b
new pigeon code
bparrishMines fccd9d6
webkit flutter apis
bparrishMines adf9875
foundation flutter apis
bparrishMines 3a6f373
add to flutter apis
bparrishMines f9a197b
navigation tests
bparrishMines 5c05bbf
other webkit tests
bparrishMines d10c8a7
nsobject test
bparrishMines 4bea16a
silly late initialization
bparrishMines 37c1c54
fix webview parameter in uidelegate
bparrishMines eec92f9
name fix
bparrishMines c35b9e7
formatting
bparrishMines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
webkit flutter apis
- Loading branch information
commit fccd9d67b750b64a3b7b0332e08ed5ddc84ff7bd
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,16 @@ extension _NSHttpCookieConverter on NSHttpCookie { | |
} | ||
} | ||
|
||
extension _WKNavigationActionPolicyConverter on WKNavigationActionPolicy { | ||
WKNavigationActionPolicyEnumData toWKNavigationActionPolicyEnumData() { | ||
return WKNavigationActionPolicyEnumData( | ||
value: WKNavigationActionPolicyEnum.values.firstWhere( | ||
(WKNavigationActionPolicyEnum element) => element.name == name, | ||
), | ||
); | ||
} | ||
} | ||
|
||
extension _NSHttpCookiePropertyKeyConverter on NSHttpCookiePropertyKey { | ||
NSHttpCookiePropertyKeyEnumData toNSHttpCookiePropertyKeyEnumData() { | ||
late final NSHttpCookiePropertyKeyEnum value; | ||
|
@@ -154,6 +164,48 @@ Iterable<WKAudiovisualMediaTypeEnumData> _toWKAudiovisualMediaTypeEnumData( | |
}); | ||
} | ||
|
||
extension _NavigationActionDataConverter on WKNavigationActionData { | ||
WKNavigationAction toNavigationAction() { | ||
return WKNavigationAction( | ||
request: request.toNSUrlRequest(), | ||
targetFrame: targetFrame.toWKFrameInfo(), | ||
); | ||
} | ||
} | ||
|
||
extension _WKFrameInfoDataConverter on WKFrameInfoData { | ||
WKFrameInfo toWKFrameInfo() { | ||
return WKFrameInfo(isMainFrame: isMainFrame); | ||
} | ||
} | ||
|
||
extension _NSUrlRequestDataConverter on NSUrlRequestData { | ||
NSUrlRequest toNSUrlRequest() { | ||
return NSUrlRequest( | ||
url: url, | ||
httpBody: httpBody, | ||
httpMethod: httpMethod, | ||
allHttpHeaderFields: allHttpHeaderFields.cast(), | ||
); | ||
} | ||
} | ||
|
||
extension _WKNSErrorDataConverter on NSErrorData { | ||
NSError toNSError() { | ||
return NSError( | ||
domain: domain, | ||
code: code, | ||
localizedDescription: localiziedDescription, | ||
); | ||
} | ||
} | ||
|
||
extension _WKScriptMessageDataConverter on WKScriptMessageData { | ||
WKScriptMessage toWKScriptMessage() { | ||
return WKScriptMessage(name: name, body: body); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We really need the ability to use existing classes as Pigeon data classes :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. I think we could use the enums and data classes generated from pigeon if flutter/flutter#53191 was implemented. This would save us alot of code. |
||
|
||
extension _WKUserScriptConverter on WKUserScript { | ||
WKUserScriptData toWKUserScriptData() { | ||
return WKUserScriptData( | ||
|
@@ -282,6 +334,36 @@ class WKScriptMessageHandlerHostApiImpl extends WKScriptMessageHandlerHostApi { | |
} | ||
} | ||
|
||
/// Flutter api implementation for [WKScriptMessageHandler]. | ||
class WKScriptMessageHandlerFlutterApiImpl | ||
extends WKScriptMessageHandlerFlutterApi { | ||
/// Constructs a [WKScriptMessageHandlerFlutterApiImpl]. | ||
WKScriptMessageHandlerFlutterApiImpl({InstanceManager? instanceManager}) { | ||
this.instanceManager = instanceManager ?? NSObject.globalInstanceManager; | ||
} | ||
|
||
/// Maintains instances stored to communicate with native language objects. | ||
late final InstanceManager instanceManager; | ||
|
||
WKScriptMessageHandler _getHandler(int identifier) { | ||
return instanceManager.getInstanceWithWeakReference(identifier)!; | ||
} | ||
|
||
@override | ||
void didReceiveScriptMessage( | ||
int identifier, | ||
int userContentControllerIdentifier, | ||
WKScriptMessageData message, | ||
) { | ||
_getHandler(identifier).didReceiveScriptMessage( | ||
instanceManager.getInstanceWithWeakReference( | ||
userContentControllerIdentifier, | ||
)! as WKUserContentController, | ||
message.toWKScriptMessage(), | ||
); | ||
} | ||
} | ||
|
||
/// Host api implementation for [WKPreferences]. | ||
class WKPreferencesHostApiImpl extends WKPreferencesHostApi { | ||
/// Constructs a [WKPreferencesHostApiImpl]. | ||
|
@@ -492,6 +574,36 @@ class WKUIDelegateHostApiImpl extends WKUIDelegateHostApi { | |
} | ||
} | ||
|
||
/// Flutter api implementation for [WKUIDelegate]. | ||
class WKUIDelegateFlutterApiImpl extends WKUIDelegateFlutterApi { | ||
/// Constructs a [WKUIDelegateFlutterApiImpl]. | ||
WKUIDelegateFlutterApiImpl({InstanceManager? instanceManager}) { | ||
this.instanceManager = instanceManager ?? NSObject.globalInstanceManager; | ||
} | ||
|
||
/// Maintains instances stored to communicate with native language objects. | ||
late final InstanceManager instanceManager; | ||
|
||
WKUIDelegate _getDelegate(int identifier) { | ||
return instanceManager.getInstanceWithWeakReference(identifier)!; | ||
} | ||
|
||
@override | ||
void onCreateWebView( | ||
int identifier, | ||
int configurationIdentifier, | ||
WKNavigationActionData navigationAction, | ||
) { | ||
final void Function(WKWebViewConfiguration, WKNavigationAction)? function = | ||
_getDelegate(identifier).onCreateWebView; | ||
function?.call( | ||
instanceManager.getInstanceWithWeakReference(configurationIdentifier)! | ||
as WKWebViewConfiguration, | ||
navigationAction.toNavigationAction(), | ||
); | ||
} | ||
} | ||
|
||
/// Host api implementation for [WKNavigationDelegate]. | ||
@immutable | ||
class WKNavigationDelegateHostApiImpl extends WKNavigationDelegateHostApi { | ||
|
@@ -558,6 +670,89 @@ class WKNavigationDelegateFlutterApiImpl | |
url, | ||
); | ||
} | ||
|
||
@override | ||
Future<WKNavigationActionPolicyEnumData> decidePolicyForNavigationAction( | ||
int identifier, | ||
int webViewIdentifier, | ||
WKNavigationActionData navigationAction, | ||
) async { | ||
final Future<WKNavigationActionPolicy> Function( | ||
WKWebView, | ||
WKNavigationAction navigationAction, | ||
)? function = _getDelegate(identifier).decidePolicyForNavigationAction; | ||
|
||
if (function == null) { | ||
return WKNavigationActionPolicyEnumData( | ||
value: WKNavigationActionPolicyEnum.allow, | ||
); | ||
} | ||
|
||
final WKNavigationActionPolicy policy = await function( | ||
instanceManager.getInstanceWithWeakReference(webViewIdentifier)! | ||
as WKWebView, | ||
navigationAction.toNavigationAction(), | ||
); | ||
return policy.toWKNavigationActionPolicyEnumData(); | ||
} | ||
|
||
@override | ||
void didFailNavigation( | ||
int identifier, | ||
int webViewIdentifier, | ||
NSErrorData error, | ||
) { | ||
final void Function(WKWebView, NSError)? function = | ||
_getDelegate(identifier).didFailNavigation; | ||
function?.call( | ||
instanceManager.getInstanceWithWeakReference(webViewIdentifier)! | ||
as WKWebView, | ||
error.toNSError(), | ||
); | ||
} | ||
|
||
@override | ||
void didFailProvisionalNavigation( | ||
int identifier, | ||
int webViewIdentifier, | ||
NSErrorData error, | ||
) { | ||
final void Function(WKWebView, NSError)? function = | ||
_getDelegate(identifier).didFailProvisionalNavigation; | ||
function?.call( | ||
instanceManager.getInstanceWithWeakReference(webViewIdentifier)! | ||
as WKWebView, | ||
error.toNSError(), | ||
); | ||
} | ||
|
||
@override | ||
void didStartProvisionalNavigation( | ||
int identifier, | ||
int webViewIdentifier, | ||
String? url, | ||
) { | ||
final void Function(WKWebView, String?)? function = | ||
_getDelegate(identifier).didStartProvisionalNavigation; | ||
function?.call( | ||
instanceManager.getInstanceWithWeakReference(webViewIdentifier)! | ||
as WKWebView, | ||
url, | ||
); | ||
} | ||
|
||
@override | ||
void webViewWebContentProcessDidTerminate( | ||
int identifier, | ||
int webViewIdentifier, | ||
) { | ||
final void Function(WKWebView)? function = | ||
_getDelegate(identifier).webViewWebContentProcessDidTerminate; | ||
function?.call( | ||
instanceManager.getInstanceWithWeakReference(webViewIdentifier)! | ||
as WKWebView, | ||
); | ||
} | ||
} | ||
|
||
/// Host api implementation for [WKWebView]. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.