Skip to content

Commit

Permalink
Expose AllowFileAccess property in WebView
Browse files Browse the repository at this point in the history
Summary: This diff adds a new property in ReactWebView to be able to configure allowFileAccess

Reviewed By: achen1

Differential Revision: D9789466

fbshipit-source-id: 39d042ac6ef69e44f006a4c4b0c2dd900f84dbc9
  • Loading branch information
mdvacca authored and facebook-github-bot committed Sep 12, 2018
1 parent 1d2808e commit 0c576ef
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 49 deletions.
7 changes: 7 additions & 0 deletions Libraries/Components/WebView/WebView.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ class WebView extends React.Component {
*/
scalesPageToFit: PropTypes.bool,

/**
* Sets whether the webview allow access to file system.
* @platform android
*/
allowFileAccess: PropTypes.bool,

/**
* Sets the user-agent for this WebView. The user-agent can also be set in native using
* WebViewConfig. This prop will overwrite that config.
Expand Down Expand Up @@ -317,6 +323,7 @@ class WebView extends React.Component {
style={webViewStyles}
source={resolveAssetSource(source)}
scalesPageToFit={this.props.scalesPageToFit}
allowFileAccess={this.props.allowFileAccess}
injectedJavaScript={this.props.injectedJavaScript}
userAgent={this.props.userAgent}
javaScriptEnabled={this.props.javaScriptEnabled}
Expand Down
1 change: 1 addition & 0 deletions RNTester/js/WebViewExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ exports.examples = [
backgroundColor: BGWASH,
height: 100,
}}
allowFileAccess={true}
originWhitelist={FILE_SYSTEM_ORIGIN_WHITE_LIST}
source={require('./helloworld.html')}
scalesPageToFit={true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,8 @@
package com.facebook.react.views.webview;

import android.annotation.TargetApi;
import android.content.Context;
import com.facebook.react.uimanager.UIManagerModule;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Picture;
Expand Down Expand Up @@ -53,6 +40,7 @@
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.events.ContentSizeChangeEvent;
import com.facebook.react.uimanager.events.Event;
Expand All @@ -62,10 +50,14 @@
import com.facebook.react.views.webview.events.TopLoadingStartEvent;
import com.facebook.react.views.webview.events.TopMessageEvent;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -140,10 +132,10 @@ public void onPageStarted(WebView webView, String url, Bitmap favicon) {
mLastLoadFailed = false;

dispatchEvent(
webView,
new TopLoadingStartEvent(
webView.getId(),
createWebViewEvent(webView, url)));
webView,
new TopLoadingStartEvent(
webView.getId(),
createWebViewEvent(webView, url)));
}

@Override
Expand All @@ -153,7 +145,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
// url blacklisting
if (mUrlPrefixesForDefaultIntent != null && mUrlPrefixesForDefaultIntent.size() > 0) {
ArrayList<Object> urlPrefixesForDefaultIntent =
mUrlPrefixesForDefaultIntent.toArrayList();
mUrlPrefixesForDefaultIntent.toArrayList();
for (Object urlPrefix : urlPrefixesForDefaultIntent) {
if (url.startsWith((String) urlPrefix)) {
launchIntent(view.getContext(), url);
Expand Down Expand Up @@ -196,10 +188,10 @@ private boolean shouldHandleURL(List<Pattern> originWhitelist, String url) {

@Override
public void onReceivedError(
WebView webView,
int errorCode,
String description,
String failingUrl) {
WebView webView,
int errorCode,
String description,
String failingUrl) {
super.onReceivedError(webView, errorCode, description, failingUrl);
mLastLoadFailed = true;

Expand All @@ -212,16 +204,16 @@ public void onReceivedError(
eventData.putString("description", description);

dispatchEvent(
webView,
new TopLoadingErrorEvent(webView.getId(), eventData));
webView,
new TopLoadingErrorEvent(webView.getId(), eventData));
}

protected void emitFinishEvent(WebView webView, String url) {
dispatchEvent(
webView,
new TopLoadingFinishEvent(
webView.getId(),
createWebViewEvent(webView, url)));
webView,
new TopLoadingFinishEvent(
webView.getId(),
createWebViewEvent(webView, url)));
}

protected WritableMap createWebViewEvent(WebView webView, String url) {
Expand Down Expand Up @@ -342,8 +334,8 @@ protected void evaluateJavascriptWithFallback(String script) {

public void callInjectedJavaScript() {
if (getSettings().getJavaScriptEnabled() &&
injectedJS != null &&
!TextUtils.isEmpty(injectedJS)) {
injectedJS != null &&
!TextUtils.isEmpty(injectedJS)) {
evaluateJavascriptWithFallback("(function() {\n" + injectedJS + ";\n})();");
}
}
Expand All @@ -366,9 +358,9 @@ public void onReceiveValue(String value) {
evaluateJavascriptWithFallback("(" +
"window.originalPostMessage = window.postMessage," +
"window.postMessage = function(data) {" +
BRIDGE_NAME + ".postMessage(String(data));" +
BRIDGE_NAME + ".postMessage(String(data));" +
"}" +
")");
")");
}
}

Expand Down Expand Up @@ -438,8 +430,8 @@ public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermiss

// Fixes broken full-screen modals/galleries due to body height being 0.
webView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));

setGeolocationEnabled(webView, false);
if (ReactBuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Expand Down Expand Up @@ -511,7 +503,7 @@ public void setSource(WebView view, @Nullable ReadableMap source) {
String html = source.getString("html");
if (source.hasKey("baseUrl")) {
view.loadDataWithBaseURL(
source.getString("baseUrl"), html, HTML_MIME_TYPE, HTML_ENCODING, null);
source.getString("baseUrl"), html, HTML_MIME_TYPE, HTML_ENCODING, null);
} else {
view.loadData(html, HTML_MIME_TYPE, HTML_ENCODING);
}
Expand Down Expand Up @@ -588,14 +580,21 @@ public void setMixedContentMode(WebView view, @Nullable String mixedContentMode)

@ReactProp(name = "urlPrefixesForDefaultIntent")
public void setUrlPrefixesForDefaultIntent(
WebView view,
@Nullable ReadableArray urlPrefixesForDefaultIntent) {
WebView view,
@Nullable ReadableArray urlPrefixesForDefaultIntent) {
ReactWebViewClient client = ((ReactWebView) view).getReactWebViewClient();
if (client != null && urlPrefixesForDefaultIntent != null) {
client.setUrlPrefixesForDefaultIntent(urlPrefixesForDefaultIntent);
}
}

@ReactProp(name = "allowFileAccess")
public void setAllowFileAccess(
WebView view,
@Nullable Boolean allowFileAccess) {
view.getSettings().setAllowFileAccess(allowFileAccess != null && allowFileAccess);
}

@ReactProp(name = "geolocationEnabled")
public void setGeolocationEnabled(
WebView view,
Expand Down Expand Up @@ -626,13 +625,13 @@ protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
@Override
public @Nullable Map<String, Integer> getCommandsMap() {
return MapBuilder.of(
"goBack", COMMAND_GO_BACK,
"goForward", COMMAND_GO_FORWARD,
"reload", COMMAND_RELOAD,
"stopLoading", COMMAND_STOP_LOADING,
"postMessage", COMMAND_POST_MESSAGE,
"injectJavaScript", COMMAND_INJECT_JAVASCRIPT
);
"goBack", COMMAND_GO_BACK,
"goForward", COMMAND_GO_FORWARD,
"reload", COMMAND_RELOAD,
"stopLoading", COMMAND_STOP_LOADING,
"postMessage", COMMAND_POST_MESSAGE,
"injectJavaScript", COMMAND_INJECT_JAVASCRIPT
);
}

@Override
Expand All @@ -659,13 +658,13 @@ public void receiveCommand(WebView root, int commandId, @Nullable ReadableArray
"var event;" +
"var data = " + eventInitDict.toString() + ";" +
"try {" +
"event = new MessageEvent('message', data);" +
"event = new MessageEvent('message', data);" +
"} catch (e) {" +
"event = document.createEvent('MessageEvent');" +
"event.initMessageEvent('message', true, true, data.data, data.origin, data.lastEventId, data.source);" +
"event = document.createEvent('MessageEvent');" +
"event.initMessageEvent('message', true, true, data.data, data.origin, data.lastEventId, data.source);" +
"}" +
"document.dispatchEvent(event);" +
"})();");
"})();");
} catch (JSONException e) {
throw new RuntimeException(e);
}
Expand Down

3 comments on commit 0c576ef

@maxammann
Copy link

Choose a reason for hiding this comment

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

I think this commit should already be in 0.57.0, right?
I can not get it file access.

@stevebartholomew
Copy link

@stevebartholomew stevebartholomew commented on 0c576ef Oct 12, 2018

Choose a reason for hiding this comment

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

@maxammann you need to be running on the 0.57.1+ to get this property.

@maxammann
Copy link

Choose a reason for hiding this comment

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

@stevebartholomew alright, thank you. Already switched to react-native-webview :)

Please sign in to comment.