Skip to content

Commit

Permalink
[Feature] Implement dynamic hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
bltnico committed Mar 22, 2017
1 parent 2a60323 commit cbaf108
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ public void onPageStarted(WebView webView, String url, Bitmap favicon) {

@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("gsoiso://")) {
ReadableArray[] hosts = ((BlockableWebView) webView).getAvailableHosts();
String host;

for (int i = 0; i < hosts.length; i++) {
host = hosts[i];

if (url.startsWith(host)) {
boolean shouldBlock = false;

ReadableMap[] policies = ((BlockableWebView) webView).getNavigationBlockingPolicies();
Expand Down Expand Up @@ -171,6 +177,9 @@ public boolean shouldOverrideUrlLoading(WebView webView, String url) {
}

return shouldBlock;
}

break;
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Expand Down Expand Up @@ -247,6 +256,10 @@ private static class BlockableWebView extends WebView implements LifecycleEventL
@Nullable
ReadableMap[] navigationBlockingPolicies;

private
@Nullable
ReadableArray[] availableHosts;

/**
* WebView must be created with an context of the current activity
*
Expand Down Expand Up @@ -306,6 +319,23 @@ public void setNavigationBlockingPolicies(@Nullable ReadableArray policies) {
navigationBlockingPolicies[i] = policies.getMap(i);
}
}

public ReadableArray[] getAvailableHosts() {
return availableHosts;
}

public void setAvailableHosts(@Nullable ReadableArray hosts) {
if (hosts == null || hosts.size() == 0) {
availableHosts = null;
return;
}

availableHosts = new ReadableArray[hosts.size()];

for (int i = 0; i < hosts.size(); i++) {
availableHosts[i] = hosts.getMap(i);
}
}
}

public BlockableWebViewManager() {
Expand Down Expand Up @@ -451,6 +481,11 @@ public void setNavigationBlockingPolicies(WebView view, @Nullable ReadableArray
((BlockableWebView) view).setNavigationBlockingPolicies(policies);
}

@ReactProp(name = "availableHosts")
public void setAvailableHosts(WebView view, @Nullable ReadableArray host) {
((BlockableWebView) view).setAvailableHosts(host);
}

@Override
protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
// Do not register default touch emitter and let WebView implementation handle touches
Expand Down
7 changes: 7 additions & 0 deletions blockableWebView.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,17 @@ class WebView extends React.Component {
* `navigationBlockingPolicies`.
*/
onNavigationBlocked: PropTypes.func,

/**
* Determines available hosts before webview load
*/
availableHosts: PropTypes.arrayOf(PropTypes.string),
};

static defaultProps = {
javaScriptEnabled : true,
scalesPageToFit: true,
availableHosts: ['http://', 'https://'],
};

state = {
Expand Down Expand Up @@ -234,6 +240,7 @@ class WebView extends React.Component {
mediaPlaybackRequiresUserAction={this.props.mediaPlaybackRequiresUserAction}
onNavigationBlocked={this.props.onNavigationBlocked}
navigationBlockingPolicies={this.props.navigationBlockingPolicies}
availableHosts={this.props.availableHosts}
/>;

return (
Expand Down

0 comments on commit cbaf108

Please sign in to comment.