Add headers prop to Android WebViews

Summary:
Related to [issue #5418](https://github.com/facebook/react-native/issues/5418)

This is a follow-up to [this previous pull request.](https://github.com/facebook/react-native/pull/5419)

~~Adds a new ReactProp 'urlWithHeaders' to Android WebViews that takes an object with a 'url' string and a 'headers' map.~~

[Update] Adds a new prop 'source' to Android WebViews
```
{
   html: string,
   url: string,
   headers: map<string, string>,
}
```

Update: resolves TODO 8495359
Closes https://github.com/facebook/react-native/pull/5494

Reviewed By: svcscm

Differential Revision: D2881313

Pulled By: nicklockwood

fb-gh-sync-id: 7cad8490d4932d0a7ef559165f3ec279d873c537
This commit is contained in:
Michael Tostenson
2016-02-01 10:05:26 -08:00
committed by facebook-github-bot-4
parent f04882f8e4
commit 80a2f5d50f
2 changed files with 55 additions and 33 deletions

View File

@@ -9,10 +9,6 @@
package com.facebook.react.views.webview;
import javax.annotation.Nullable;
import java.util.Map;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.SystemClock;
@@ -27,16 +23,23 @@ import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.uimanager.annotations.ReactProp;
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.Event;
import com.facebook.react.uimanager.events.EventDispatcher;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Manages instances of {@link WebView}
*
@@ -271,31 +274,28 @@ public class ReactWebViewManager extends SimpleViewManager<WebView> {
((ReactWebView) view).setInjectedJavaScript(injectedJavaScript);
}
@ReactProp(name = "html")
public void setHtml(WebView view, @Nullable String html) {
if (html != null) {
view.loadData(html, HTML_MIME_TYPE, HTML_ENCODING);
} else {
view.loadUrl(BLANK_URL);
}
}
@ReactProp(name = "url")
public void setUrl(WebView view, @Nullable String url) {
// TODO(8495359): url and html are coupled as they both call loadUrl, therefore in case when
// property url is removed in favor of property html being added in single transaction we may
// end up in a state when blank url is loaded as it depends on the order of update operations!
String currentUrl = view.getUrl();
if (currentUrl != null && currentUrl.equals(url)) {
// We are already loading it so no need to stomp over it and start again
return;
}
if (url != null) {
view.loadUrl(url);
} else {
view.loadUrl(BLANK_URL);
@ReactProp(name = "source")
public void setSource(WebView view, @Nullable ReadableMap source) {
if (source != null) {
if (source.hasKey("html")) {
view.loadData(source.getString("html"), HTML_MIME_TYPE, HTML_ENCODING);
return;
}
if (source.hasKey("uri")) {
HashMap<String, String> headerMap = new HashMap<>();
if (source.hasKey("headers")) {
ReadableMap headers = source.getMap("headers");
ReadableMapKeySetIterator iter = headers.keySetIterator();
while (iter.hasNextKey()) {
String key = iter.nextKey();
headerMap.put(key, headers.getString(key));
}
}
view.loadUrl(source.getString("uri"), headerMap);
return;
}
}
view.loadUrl(BLANK_URL);
}
@Override