Skip to content

Commit

Permalink
Update react-native-webview to 9.4.0 (#8489)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsapeta committed May 26, 2020
1 parent d43b6ed commit bd337bc
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 134 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@ Package-specific changes not released in any SDK will be added here just before
- Updated `react-native-reanimated` from `1.7.0` to `1.9.0`. ([#8424](https://github.com/expo/expo/pull/8424) by [@sjchmiela](https://github.com/sjchmiela))
- Updated `react-native-safe-area-context` from `0.7.3` to `2.0.1`. ([#8459](https://github.com/expo/expo/pull/8459) by [@brentvatne](https://github.com/brentvatne) and [#8479](https://github.com/expo/expo/pull/8479) by [@tsapeta](https://github.com/tsapeta))
- Updated `@react-native-community/datetimepicker` from `2.2.2` to `2.4.0`. ([#8476](https://github.com/expo/expo/pull/8476) by [@tsapeta](https://github.com/tsapeta))
- Updated `react-native-webview` from `8.1.1` to `9.4.0`. ([#8489](https://github.com/expo/expo/pull/8489) by [@tsapeta](https://github.com/tsapeta))
- Updated `react-native-svg` from `11.0.1` to `12.1.0`. ([#8491](https://github.com/expo/expo/pull/8491) by [@tsapeta](https://github.com/tsapeta))

### 🛠 Breaking changes
Expand Down
Expand Up @@ -41,12 +41,15 @@
import com.facebook.react.views.scroll.ScrollEventType;
import com.facebook.react.views.scroll.OnScrollDispatchHelper;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.CatalystInstance;
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.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.module.annotations.ReactModule;
Expand Down Expand Up @@ -194,6 +197,8 @@ protected WebView createViewInstance(ThemedReactContext reactContext) {

webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
webView.setIgnoreErrFailedForThisURL(url);

RNCWebViewModule module = getModule(reactContext);

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
Expand Down Expand Up @@ -395,6 +400,11 @@ public void setInjectedJavaScript(WebView view, @Nullable String injectedJavaScr
public void setMessagingEnabled(WebView view, boolean enabled) {
((RNCWebView) view).setMessagingEnabled(enabled);
}

@ReactProp(name = "messagingModuleName")
public void setMessagingModuleName(WebView view, String moduleName) {
((RNCWebView) view).setMessagingModuleName(moduleName);
}

@ReactProp(name = "incognito")
public void setIncognito(WebView view, boolean enabled) {
Expand Down Expand Up @@ -713,6 +723,11 @@ protected static class RNCWebViewClient extends WebViewClient {
protected boolean mLastLoadFailed = false;
protected @Nullable
ReadableArray mUrlPrefixesForDefaultIntent;
protected @Nullable String ignoreErrFailedForThisURL = null;

public void setIgnoreErrFailedForThisURL(@Nullable String url) {
ignoreErrFailedForThisURL = url;
}

@Override
public void onPageFinished(WebView webView, String url) {
Expand Down Expand Up @@ -764,6 +779,21 @@ public void onReceivedError(
int errorCode,
String description,
String failingUrl) {

if (ignoreErrFailedForThisURL != null
&& failingUrl.equals(ignoreErrFailedForThisURL)
&& errorCode == -1
&& description.equals("net::ERR_FAILED")) {

// This is a workaround for a bug in the WebView.
// See these chromium issues for more context:
// https://bugs.chromium.org/p/chromium/issues/detail?id=1023678
// https://bugs.chromium.org/p/chromium/issues/detail?id=1050635
// This entire commit should be reverted once this bug is resolved in chromium.
setIgnoreErrFailedForThisURL(null);
return;
}

super.onReceivedError(webView, errorCode, description, failingUrl);
mLastLoadFailed = true;

Expand Down Expand Up @@ -972,7 +1002,11 @@ protected static class RNCWebView extends WebView implements LifecycleEventListe
String injectedJS;
protected boolean messagingEnabled = false;
protected @Nullable
String messagingModuleName;
protected @Nullable
RNCWebViewClient mRNCWebViewClient;
protected @Nullable
CatalystInstance mCatalystInstance;
protected boolean sendContentSizeChangeEvents = false;
private OnScrollDispatchHelper mOnScrollDispatchHelper;
protected boolean hasScrollEvent = false;
Expand All @@ -987,6 +1021,10 @@ public RNCWebView(ThemedReactContext reactContext) {
super(reactContext);
}

public void setIgnoreErrFailedForThisURL(String url) {
mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
}

public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
}
Expand Down Expand Up @@ -1047,6 +1085,14 @@ protected RNCWebViewBridge createRNCWebViewBridge(RNCWebView webView) {
return new RNCWebViewBridge(webView);
}

protected void createCatalystInstance() {
ReactContext reactContext = (ReactContext) this.getContext();

if (reactContext != null) {
mCatalystInstance = reactContext.getCatalystInstance();
}
}

@SuppressLint("AddJavascriptInterface")
public void setMessagingEnabled(boolean enabled) {
if (messagingEnabled == enabled) {
Expand All @@ -1057,11 +1103,16 @@ public void setMessagingEnabled(boolean enabled) {

if (enabled) {
addJavascriptInterface(createRNCWebViewBridge(this), JAVASCRIPT_INTERFACE);
this.createCatalystInstance();
} else {
removeJavascriptInterface(JAVASCRIPT_INTERFACE);
}
}

public void setMessagingModuleName(String moduleName) {
messagingModuleName = moduleName;
}

protected void evaluateJavascriptWithFallback(String script) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
evaluateJavascript(script, null);
Expand All @@ -1085,6 +1136,9 @@ public void callInjectedJavaScript() {
}

public void onMessage(String message) {
ReactContext reactContext = (ReactContext) this.getContext();
RNCWebView mContext = this;

if (mRNCWebViewClient != null) {
WebView webView = this;
webView.post(new Runnable() {
Expand All @@ -1095,16 +1149,36 @@ public void run() {
}
WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
data.putString("data", message);
dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));

if (mCatalystInstance != null) {
mContext.sendDirectMessage(data);
} else {
dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
}
}
});
} else {
WritableMap eventData = Arguments.createMap();
eventData.putString("data", message);
dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));

if (mCatalystInstance != null) {
this.sendDirectMessage(eventData);
} else {
dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
}
}
}

protected void sendDirectMessage(WritableMap data) {
WritableNativeMap event = new WritableNativeMap();
event.putMap("nativeEvent", data);

WritableNativeArray params = new WritableNativeArray();
params.pushMap(event);

mCatalystInstance.callFunction(messagingModuleName, "onMessage", params);
}

protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);

Expand Down
2 changes: 1 addition & 1 deletion apps/native-component-list/package.json
Expand Up @@ -97,7 +97,7 @@
"react-native-svg": "12.1.0",
"react-native-view-shot": "^3.1.2",
"react-native-web": "^0.11.0",
"react-native-webview": "8.1.2",
"react-native-webview": "9.4.0",
"react-navigation": "4.1.0-alpha.1",
"react-navigation-header-buttons": "^1.2.1",
"react-navigation-material-bottom-tabs": "^1.1.0",
Expand Down
Expand Up @@ -11,5 +11,6 @@

+ (instancetype) sharedManager;
- (WKProcessPool *)sharedProcessPool;
- (WKProcessPool *)sharedProcessPoolForExperienceId:(NSString *)experienceId;

@end
Expand Up @@ -10,6 +10,7 @@

@interface RNCWKProcessPoolManager() {
WKProcessPool *_sharedProcessPool;
NSMutableDictionary<NSString *, WKProcessPool *> *_pools;
}
@end

Expand All @@ -25,12 +26,33 @@ + (id) sharedManager {
}
}

- (instancetype)init
{
if (self = [super init]) {
_pools = [NSMutableDictionary new];
}
return self;
}

- (WKProcessPool *)sharedProcessPool {
if (!_sharedProcessPool) {
_sharedProcessPool = [[WKProcessPool alloc] init];
}
return _sharedProcessPool;
}

- (WKProcessPool *)sharedProcessPoolForExperienceId:(NSString *)experienceId
{
if (!experienceId) {
return [self sharedProcessPool];
}

if (!_pools[experienceId]) {
_pools[experienceId] = [[WKProcessPool alloc] init];
}

return _pools[experienceId];
}

@end

Expand Up @@ -26,11 +26,14 @@

@interface RNCWebView : RCTView

@property (nonatomic, strong) NSString *experienceId;
@property (nonatomic, weak) id<RNCWebViewDelegate> _Nullable delegate;
@property (nonatomic, copy) NSDictionary * _Nullable source;
@property (nonatomic, assign) BOOL messagingEnabled;
@property (nonatomic, copy) NSString * _Nullable injectedJavaScript;
@property (nonatomic, copy) NSString * _Nullable injectedJavaScriptBeforeContentLoaded;
@property (nonatomic, assign) BOOL injectedJavaScriptForMainFrameOnly;
@property (nonatomic, assign) BOOL injectedJavaScriptBeforeContentLoadedForMainFrameOnly;
@property (nonatomic, assign) BOOL scrollEnabled;
@property (nonatomic, assign) BOOL sharedCookiesEnabled;
@property (nonatomic, assign) BOOL pagingEnabled;
Expand All @@ -57,6 +60,7 @@
@property (nonatomic, assign) BOOL showsHorizontalScrollIndicator;
@property (nonatomic, assign) BOOL showsVerticalScrollIndicator;
@property (nonatomic, assign) BOOL directionalLockEnabled;
@property (nonatomic, assign) BOOL ignoreSilentHardwareSwitch;
@property (nonatomic, copy) NSString * _Nullable allowingReadAccessToURL;

+ (void)setClientAuthenticationCredential:(nullable NSURLCredential*)credential;
Expand Down

0 comments on commit bd337bc

Please sign in to comment.