From 9006c0044c6657976951ebb5fb1ede4dde0e2d35 Mon Sep 17 00:00:00 2001 From: Joshua Quick Date: Thu, 23 Jul 2020 22:11:26 -0700 Subject: [PATCH] feat: add WebView blockedURLs property - Deprecated WebView "blacklistedURLs" property in favor of new "blockedURLs" property. - Deprecated WebView "blacklisturl" event in favor of new "blockedurl" event. Fixes TIMOB-28030 --- .../ti/modules/titanium/ui/WebViewProxy.java | 3 +- .../ui/widget/webview/TiWebViewClient.java | 30 ++++++++++--- .../java/org/appcelerator/titanium/TiC.java | 14 +++++- apidoc/Titanium/UI/WebView.yml | 30 +++++++++++++ iphone/Classes/TiUIWebView.h | 1 + iphone/Classes/TiUIWebView.m | 44 +++++++++++++++---- 6 files changed, 106 insertions(+), 16 deletions(-) diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/WebViewProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/WebViewProxy.java index 24b11988f94..9f8a4c222c8 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/WebViewProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/WebViewProxy.java @@ -31,7 +31,8 @@ @Kroll.proxy(creatableInModule = UIModule.class, propertyAccessors = { - TiC.PROPERTY_BLACKLISTED_URLS, + TiC.PROPERTY_BLACKLISTED_URLS, // DEPRECATED: Superseded by PROPERTY_BLOCKED_URLS. + TiC.PROPERTY_BLOCKED_URLS, TiC.PROPERTY_DATA, TiC.PROPERTY_ON_CREATE_WINDOW, TiC.PROPERTY_SCALES_PAGE_TO_FIT, diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebViewClient.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebViewClient.java index bcb5410fd98..d3590ea6e74 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebViewClient.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebViewClient.java @@ -137,15 +137,35 @@ public boolean shouldOverrideUrlLoading(final WebView view, String url) } } } - if (proxy.hasProperty(TiC.PROPERTY_BLACKLISTED_URLS)) { - String[] blacklistedSites = - TiConvert.toStringArray((Object[]) proxy.getProperty(TiC.PROPERTY_BLACKLISTED_URLS)); - for (String site : blacklistedSites) { + + // Do not load the URL if it's on the proxy's black-list. + // DEPRECATED: Superseded by PROPERTY_BLOCKED_URLS down below. + Object value = proxy.getProperty(TiC.PROPERTY_BLACKLISTED_URLS); + if ((value != null) && value.getClass().isArray()) { + String message + = "Property '" + TiC.PROPERTY_BLACKLISTED_URLS + "' is deprecated. Use '" + + TiC.PROPERTY_BLOCKED_URLS + "' instead."; + Log.w(TAG, message); + for (String site : TiConvert.toStringArray((Object[]) value)) { if (url.equalsIgnoreCase(site) || (url.indexOf(site) > -1)) { KrollDict data = new KrollDict(); data.put("url", url); data.put("message", "Webview did not load blacklisted url."); - proxy.fireEvent(TiC.PROPERTY_BLACKLIST_URL, data); + proxy.fireEvent(TiC.EVENT_BLACKLIST_URL, data); + return true; + } + } + } + + // Do not load the URL if it's on the proxy's block-list. + value = proxy.getProperty(TiC.PROPERTY_BLOCKED_URLS); + if ((value != null) && value.getClass().isArray()) { + for (String site : TiConvert.toStringArray((Object[]) value)) { + if (url.equalsIgnoreCase(site) || (url.indexOf(site) > -1)) { + KrollDict data = new KrollDict(); + data.put("url", url); + data.put("message", "Webview did not load blocked url."); + proxy.fireEvent(TiC.EVENT_BLOCKED_URL, data); return true; } } diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiC.java b/android/titanium/src/java/org/appcelerator/titanium/TiC.java index 3e69636f727..f471536dca4 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/TiC.java +++ b/android/titanium/src/java/org/appcelerator/titanium/TiC.java @@ -85,6 +85,16 @@ public class TiC */ public static final String EVENT_BATTERY = "battery"; + /** + * @module.api + */ + public static final String EVENT_BLACKLIST_URL = "blacklisturl"; + + /** + * @module.api + */ + public static final String EVENT_BLOCKED_URL = "blockedurl"; + /** * @module.api */ @@ -1110,12 +1120,12 @@ public class TiC /** * @module.api */ - public static final String PROPERTY_BLACKLIST_URL = "blacklisturl"; + public static final String PROPERTY_BLACKLISTED_URLS = "blacklistedURLs"; /** * @module.api */ - public static final String PROPERTY_BLACKLISTED_URLS = "blacklistedURLs"; + public static final String PROPERTY_BLOCKED_URLS = "blockedURLs"; /** * @module.api diff --git a/apidoc/Titanium/UI/WebView.yml b/apidoc/Titanium/UI/WebView.yml index dd740fd109d..42b6deac507 100644 --- a/apidoc/Titanium/UI/WebView.yml +++ b/apidoc/Titanium/UI/WebView.yml @@ -556,6 +556,21 @@ events: - name: url summary: The URL of the web document that is stopped. type: String + deprecated: + since: "9.2.0" + notes: Use the `blockedurl` event instead. + + - name: blockedurl + summary: Fired when a URL has been blocked from loading. + description: | + This event is fired when the end-user attempts to navigate to a website matching a URL in + the property. + platforms: [android, iphone, ipad] + since: {android: "9.2.0", iphone: "9.2.0", ipad: "9.2.0"} + properties: + - name: url + summary: The URL of the web document that has been blocked from loading. + type: String - name: message summary: Fired when a script message is received from a webpage. @@ -650,6 +665,21 @@ properties: since: {android: "5.4.0", iphone: "6.1.0", ipad: "6.1.0"} platforms: [android, iphone, ipad] availability: creation + deprecated: + since: "9.2.0" + notes: Use the property instead. + + - name: blockedURLs + summary: | + An array of url strings to be blocked. + description: | + An array of url strings to be blocked from loading. This will stop the webview from going to urls + listed in this array. Note that this only applies to the links tapped on by the end-user. + The first website that is loaded will not be stopped, even if it is listed in the blocklist. + type: Array + since: {android: "9.2.0", iphone: "9.2.0", ipad: "9.2.0"} + platforms: [android, iphone, ipad] + availability: creation - name: data summary: Web content to load. diff --git a/iphone/Classes/TiUIWebView.h b/iphone/Classes/TiUIWebView.h index e99ed8cabfe..9db6e5230c8 100644 --- a/iphone/Classes/TiUIWebView.h +++ b/iphone/Classes/TiUIWebView.h @@ -23,6 +23,7 @@ BOOL _willHandleTouches; NSArray *_blacklistedURLs; + NSArray *_blockedURLs; NSURL *_currentURL; UIActivityIndicatorView *_loadingIndicator; BOOL _isViewDetached; diff --git a/iphone/Classes/TiUIWebView.m b/iphone/Classes/TiUIWebView.m index 7785662c30c..9798c3e9f55 100644 --- a/iphone/Classes/TiUIWebView.m +++ b/iphone/Classes/TiUIWebView.m @@ -245,15 +245,26 @@ - (void)setData_:(id)value - (void)setBlacklistedURLs_:(id)blacklistedURLs { - ENSURE_TYPE(blacklistedURLs, NSArray); + DEPRECATED_REPLACED(@"UI.WebView.blacklistedURLs", @"9.2.0", @"UI.WebView.blockedURLs"); - for (id blacklistedURL in blacklistedURLs) { - ENSURE_TYPE(blacklistedURL, NSString); + ENSURE_TYPE(blacklistedURLs, NSArray); + for (id nextURL in blacklistedURLs) { + ENSURE_TYPE(nextURL, NSString); } _blacklistedURLs = blacklistedURLs; } +- (void)setBlockedURLs_:(id)blockedURLs +{ + ENSURE_TYPE(blockedURLs, NSArray); + for (id nextURL in blockedURLs) { + ENSURE_TYPE(nextURL, NSString); + } + + _blockedURLs = blockedURLs; +} + - (void)setHtml_:(id)args { ignoreNextRequest = YES; @@ -1010,12 +1021,12 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WK } NSArray *allowedURLSchemes = [[self proxy] valueForKey:@"allowedURLSchemes"]; - // Handle blacklisted URL's + // Do not load the URL if it's black-listed. + // DEPRECATED: Should use "blockedURLs" property with a "blockedurl" event listener instead. + NSString *urlCandidate = navigationAction.request.URL.absoluteString; if (_blacklistedURLs != nil && _blacklistedURLs.count > 0) { - NSString *urlCandidate = navigationAction.request.URL.absoluteString; - - for (NSString *blackListedURL in _blacklistedURLs) { - if ([urlCandidate rangeOfString:blackListedURL options:NSCaseInsensitiveSearch].location != NSNotFound) { + for (NSString *blockedURL in _blacklistedURLs) { + if ([urlCandidate rangeOfString:blockedURL options:NSCaseInsensitiveSearch].location != NSNotFound) { if ([[self proxy] _hasListeners:@"blacklisturl"]) { [[self proxy] fireEvent:@"blacklisturl" withObject:@{ @@ -1023,7 +1034,24 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WK @"message" : @"Webview did not load blacklisted url." }]; } + decisionHandler(WKNavigationActionPolicyCancel); + [self _cleanupLoadingIndicator]; + return; + } + } + } + // Do not load the URL if it's on the block-list. + if (_blockedURLs != nil && _blockedURLs.count > 0) { + for (NSString *blockedURL in _blockedURLs) { + if ([urlCandidate rangeOfString:blockedURL options:NSCaseInsensitiveSearch].location != NSNotFound) { + if ([[self proxy] _hasListeners:@"blockedurl"]) { + [[self proxy] fireEvent:@"blockedurl" + withObject:@{ + @"url" : urlCandidate, + @"message" : @"Webview did not load blocked url." + }]; + } decisionHandler(WKNavigationActionPolicyCancel); [self _cleanupLoadingIndicator]; return;