@@ -453,7 +453,7 @@ - (UIView*)webView
453453 return self.engineWebView ;
454454}
455455
456- #pragma mark WKScriptMessageHandler implementation
456+ #pragma mark - WKScriptMessageHandler implementation
457457
458458- (void )userContentController : (WKUserContentController *)userContentController didReceiveScriptMessage : (WKScriptMessage *)message
459459{
@@ -489,7 +489,7 @@ - (void)userContentController:(WKUserContentController*)userContentController di
489489 }
490490}
491491
492- #pragma mark WKNavigationDelegate implementation
492+ #pragma mark - WKNavigationDelegate implementation
493493
494494- (void )webView : (WKWebView *)webView didStartProvisionalNavigation : (WKNavigation *)navigation
495495{
@@ -537,45 +537,64 @@ - (BOOL)defaultResourcePolicyForURL:(NSURL*)url
537537 return NO ;
538538}
539539
540- - (void ) webView : (WKWebView *) webView decidePolicyForNavigationAction : (WKNavigationAction *) navigationAction decisionHandler : (void (^)(WKNavigationActionPolicy )) decisionHandler
540+ - (void )webView : (WKWebView *)webView decidePolicyForNavigationAction : (WKNavigationAction *) navigationAction decisionHandler : (void (^)(WKNavigationActionPolicy ))decisionHandler
541541{
542- NSURL * url = [navigationAction.request URL ];
543- CDVViewController* vc = (CDVViewController*)self.viewController ;
542+ CDVViewController *vc = (CDVViewController *)self.viewController ;
544543
545- /*
546- * Give plugins the chance to handle the url
547- */
548- BOOL anyPluginsResponded = NO ;
549- BOOL shouldAllowRequest = NO ;
544+ NSURLRequest *request = navigationAction.request ;
545+ CDVWebViewNavigationType navType = (CDVWebViewNavigationType)navigationAction.navigationType ;
546+ NSMutableDictionary *info = [NSMutableDictionary dictionary ];
547+ info[@" sourceFrame" ] = navigationAction.sourceFrame ;
548+ info[@" targetFrame" ] = navigationAction.targetFrame ;
549+ #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140500
550+ if (@available (iOS 14.5 , *)) {
551+ info[@" shouldPerformDownload" ] = [NSNumber numberWithBool: navigationAction.shouldPerformDownload];
552+ }
553+ #endif
550554
551- for (CDVPlugin *plugin in vc.enumerablePlugins ) {
552- SEL selector = NSSelectorFromString (@" shouldOverrideLoadWithRequest:navigationType:" );
553- if ([plugin respondsToSelector: selector]) {
554- anyPluginsResponded = YES ;
555- // https://issues.apache.org/jira/browse/CB-12497
556- int navType = (int )navigationAction.navigationType ;
557- shouldAllowRequest = (((BOOL (*)(id , SEL , id , int ))objc_msgSend)(plugin, selector, navigationAction.request , navType));
558- if (!shouldAllowRequest) {
559- break ;
555+ // Give plugins the chance to handle the url, as long as this WebViewEngine is still the WKNavigationDelegate.
556+ // This allows custom delegates to choose to call this method for `default` cordova behavior without querying all plugins.
557+ if (webView.navigationDelegate == self) {
558+ BOOL anyPluginsResponded = NO ;
559+ BOOL shouldAllowRequest = NO ;
560+
561+ for (CDVPlugin *plugin in vc.enumerablePlugins ) {
562+ if ([plugin respondsToSelector: @selector (shouldOverrideLoadWithRequest:navigationType:info: )] || [plugin respondsToSelector: @selector (shouldOverrideLoadWithRequest:navigationType: )]) {
563+ CDVPlugin <CDVPluginNavigationHandler> *navPlugin = (CDVPlugin <CDVPluginNavigationHandler> *)plugin;
564+ anyPluginsResponded = YES ;
565+
566+ if ([navPlugin respondsToSelector: @selector (shouldOverrideLoadWithRequest:navigationType:info: )]) {
567+ shouldAllowRequest = [navPlugin shouldOverrideLoadWithRequest: request navigationType: navType info: info];
568+ } else {
569+ #pragma clang diagnostic push
570+ #pragma clang diagnostic ignored "-Wdeprecated-declarations"
571+ shouldAllowRequest = [navPlugin shouldOverrideLoadWithRequest: request navigationType: navType];
572+ #pragma clang diagnostic pop
573+ }
574+
575+ if (!shouldAllowRequest) {
576+ break ;
577+ }
560578 }
561579 }
562- }
563-
564- if (anyPluginsResponded) {
565- return decisionHandler (shouldAllowRequest);
566- }
567580
568- /*
569- * Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview.
570- */
571- BOOL shouldAllowNavigation = [self defaultResourcePolicyForURL: url];
572- if (shouldAllowNavigation) {
573- return decisionHandler (YES );
581+ if (anyPluginsResponded) {
582+ return decisionHandler (shouldAllowRequest ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel );
583+ }
574584 } else {
575- [[NSNotificationCenter defaultCenter ] postNotification: [NSNotification notificationWithName: CDVPluginHandleOpenURLNotification object: url]];
585+ CDVPlugin <CDVPluginNavigationHandler> *intentAndNavFilter = (CDVPlugin <CDVPluginNavigationHandler> *)[vc getCommandInstance: @" IntentAndNavigationFilter" ];
586+ if (intentAndNavFilter) {
587+ BOOL shouldAllowRequest = [intentAndNavFilter shouldOverrideLoadWithRequest: request navigationType: navType info: info];
588+ return decisionHandler (shouldAllowRequest ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel );
589+ }
576590 }
577591
578- return decisionHandler (NO );
592+ // Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview.
593+ BOOL shouldAllowNavigation = [self defaultResourcePolicyForURL: request.URL];
594+ if (!shouldAllowNavigation) {
595+ [[NSNotificationCenter defaultCenter ] postNotification: [NSNotification notificationWithName: CDVPluginHandleOpenURLNotification object: request.URL userInfo: @{}]];
596+ }
597+ return decisionHandler (shouldAllowNavigation ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel );
579598}
580599
581600- (void )webView : (WKWebView *)webView didReceiveAuthenticationChallenge : (NSURLAuthenticationChallenge *)challenge completionHandler : (void (^)(NSURLSessionAuthChallengeDisposition , NSURLCredential * _Nullable))completionHandler
0 commit comments