Skip to content

Commit

Permalink
fix(browser): added remaining fields in WebSourceResponse patch (#6479)
Browse files Browse the repository at this point in the history
* added remaining fields in WebSourceResposne patch

* updated patch to provide reasonPhrase if empty

* updated to remove build code

---------

Co-authored-by: Cal Leung <cleun007@gmail.com>
  • Loading branch information
sethkfman and Cal-L authored Jul 25, 2023
1 parent 71340aa commit 35992d2
Showing 1 changed file with 47 additions and 36 deletions.
83 changes: 47 additions & 36 deletions patches/react-native-webview+11.13.0.patch
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ index 0000000..f4a6af9
+ }
+}
diff --git a/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java b/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
index f743bbc..ede5475 100644
index f743bbc..406a7d4 100644
--- a/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
+++ b/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
@@ -5,6 +5,7 @@ import android.annotation.TargetApi;
Expand Down Expand Up @@ -128,7 +128,7 @@ index f743bbc..ede5475 100644
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
@@ -88,18 +96,54 @@ import com.reactnativecommunity.webview.events.TopRenderProcessGoneEvent;
@@ -88,18 +96,55 @@ import com.reactnativecommunity.webview.events.TopRenderProcessGoneEvent;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -162,6 +162,7 @@ index f743bbc..ede5475 100644
+import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

+import okhttp3.Headers;
+import okhttp3.MediaType;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
Expand All @@ -183,7 +184,7 @@ index f743bbc..ede5475 100644
/**
* Manages instances of {@link WebView}
* <p>
@@ -137,13 +181,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -137,13 +182,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
public static final int COMMAND_LOAD_URL = 7;
public static final int COMMAND_FOCUS = 8;

Expand All @@ -204,7 +205,7 @@ index f743bbc..ede5475 100644
protected static final String HTML_MIME_TYPE = "text/html";
protected static final String JAVASCRIPT_INTERFACE = "ReactNativeWebView";
protected static final String HTTP_METHOD_POST = "POST";
@@ -155,13 +205,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -155,13 +206,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {

protected RNCWebChromeClient mWebChromeClient = null;
protected boolean mAllowsFullscreenVideo = false;
Expand All @@ -229,15 +230,15 @@ index f743bbc..ede5475 100644
};
}

@@ -182,6 +238,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -182,6 +239,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected WebView createViewInstance(ThemedReactContext reactContext) {
RNCWebView webView = createRNCWebViewInstance(reactContext);
+ deviceUserAgent = webView.getSettings().getUserAgentString();
setupWebChromeClient(reactContext, webView);
reactContext.addLifecycleEventListener(webView);
mWebViewConfig.configWebView(webView);
@@ -209,47 +266,161 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -209,47 +267,171 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
}

webView.setDownloadListener(new DownloadListener() {
Expand Down Expand Up @@ -415,7 +416,17 @@ index f743bbc..ede5475 100644
+ is = new InputStreamWithInjectedJS(is, reactWebView.injectedJSBeforeContentLoaded, charset);
+ }
+
+ return new WebResourceResponse("text/html", charset.name(), is);
+ Headers headers = response.headers();
+ Map<String, String> headerMap = new HashMap<String,String>();
+ for (String headerName : headers.names()) {
+ String headerValue = headers.get(headerName);
+ headerMap.put(headerName, headerValue);
+ }
+
+ //Response message is generally empty but response is valid. Populate a string of the response code if nothing exists
+ String reasonPhrase = response.message().isEmpty() ? Integer.toString(response.code()) : response.message();
+
+ return new WebResourceResponse("text/html", charset.name(), response.code(), reasonPhrase, headerMap, is);
+ } catch (IOException e) {
+ return null;
+ }
Expand All @@ -424,7 +435,7 @@ index f743bbc..ede5475 100644
@ReactProp(name = "javaScriptEnabled")
public void setJavaScriptEnabled(WebView view, boolean enabled) {
view.getSettings().setJavaScriptEnabled(enabled);
@@ -285,13 +456,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -285,13 +467,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
if (enabled) {
Context ctx = view.getContext();
if (ctx != null) {
Expand All @@ -438,7 +449,7 @@ index f743bbc..ede5475 100644
}
}

@@ -327,12 +495,12 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -327,12 +506,12 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
public void setLayerType(WebView view, String layerTypeString) {
int layerType = View.LAYER_TYPE_NONE;
switch (layerTypeString) {
Expand All @@ -457,7 +468,7 @@ index f743bbc..ede5475 100644
}
view.setLayerType(layerType, null);
}
@@ -387,9 +555,9 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -387,9 +566,9 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@ReactProp(name = "userAgent")
public void setUserAgent(WebView view, @Nullable String userAgent) {
if (userAgent != null) {
Expand All @@ -469,7 +480,7 @@ index f743bbc..ede5475 100644
}
this.setUserAgentString(view);
}
@@ -399,19 +567,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -399,19 +578,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
if(applicationName != null) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
String defaultUserAgent = WebSettings.getDefaultUserAgent(view.getContext());
Expand All @@ -495,15 +506,15 @@ index f743bbc..ede5475 100644
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// handle unsets of `userAgent` prop as long as device is >= API 17
view.getSettings().setUserAgentString(WebSettings.getDefaultUserAgent(view.getContext()));
@@ -490,7 +658,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -490,7 +669,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {

// Disable caching
view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
- view.getSettings().setAppCacheEnabled(false);
view.clearHistory();
view.clearCache(true);

@@ -842,13 +1009,116 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -842,13 +1020,116 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
}
}

Expand Down Expand Up @@ -588,7 +599,7 @@ index f743bbc..ede5475 100644
+ return nextByte;
+ }
+ }
+
+ if (!headWasFound) {
+ int nextByte = pageIS.read();
+ contentBuffer.append((char) nextByte);
Expand All @@ -609,7 +620,7 @@ index f743bbc..ede5475 100644
+ }
+
+ protected class RNCWebViewClient extends WebViewClient {
+
+ protected static final String REACT_CLASS = "RNCWebViewClient";
protected boolean mLastLoadFailed = false;
protected @Nullable
Expand All @@ -621,7 +632,7 @@ index f743bbc..ede5475 100644

public void setIgnoreErrFailedForThisURL(@Nullable String url) {
ignoreErrFailedForThisURL = url;
@@ -857,24 +1127,26 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -857,24 +1138,26 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@Override
public void onPageFinished(WebView webView, String url) {
super.onPageFinished(webView, url);
Expand Down Expand Up @@ -653,7 +664,7 @@ index f743bbc..ede5475 100644
((RNCWebView) webView).dispatchEvent(
webView,
new TopLoadingStartEvent(
@@ -882,6 +1154,20 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -882,6 +1165,20 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
createWebViewEvent(webView, url)));
}

Expand All @@ -674,15 +685,15 @@ index f743bbc..ede5475 100644
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final RNCWebView rncWebView = (RNCWebView) view;
@@ -891,7 +1177,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -891,7 +1188,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
final Pair<Integer, AtomicReference<ShouldOverrideCallbackState>> lock = RNCWebViewModule.shouldOverrideUrlLoadingLock.getNewLock();
final int lockIdentifier = lock.first;
final AtomicReference<ShouldOverrideCallbackState> lockObject = lock.second;
-
final WritableMap event = createWebViewEvent(view, url);
event.putInt("lockIdentifier", lockIdentifier);
rncWebView.sendDirectMessage("onShouldStartLoadWithRequest", event);
@@ -919,6 +1204,17 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -919,6 +1215,17 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
RNCWebViewModule.shouldOverrideUrlLoadingLock.removeLock(lockIdentifier);

return shouldOverride;
Expand All @@ -700,7 +711,7 @@ index f743bbc..ede5475 100644
} else {
FLog.w(TAG, "Couldn't use blocking synchronous call for onShouldStartLoadWithRequest due to debugging or missing Catalyst instance, falling back to old event-and-load.");
progressChangedFilter.setWaitingForCommandLoadUrl(true);
@@ -934,67 +1230,86 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -934,67 +1241,86 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Expand Down Expand Up @@ -795,7 +806,8 @@ index f743bbc..ede5475 100644
+ int code = error.getPrimaryError();
+ String description = "";
+ String descriptionPrefix = "SSL error: ";
+

- description = descriptionPrefix + description;
+ // https://developer.android.com/reference/android/net/http/SslError.html
+ switch (code) {
+ case SslError.SSL_DATE_INVALID:
Expand All @@ -821,15 +833,14 @@ index f743bbc..ede5475 100644
+ break;
+ }

- description = descriptionPrefix + description;
+ description = descriptionPrefix + description;

- this.onReceivedError(
- webView,
- code,
- description,
- failingUrl
- );
+ description = descriptionPrefix + description;
+
+ this.onReceivedError(
+ webView,
+ code,
Expand All @@ -839,7 +850,7 @@ index f743bbc..ede5475 100644
}

@Override
@@ -1005,9 +1320,9 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1005,9 +1331,9 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
String failingUrl) {

if (ignoreErrFailedForThisURL != null
Expand All @@ -852,7 +863,7 @@ index f743bbc..ede5475 100644

// This is a workaround for a bug in the WebView.
// See these chromium issues for more context:
@@ -1056,36 +1371,36 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1056,36 +1382,36 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@TargetApi(Build.VERSION_CODES.O)
@Override
public boolean onRenderProcessGone(WebView webView, RenderProcessGoneDetail detail) {
Expand Down Expand Up @@ -913,15 +924,15 @@ index f743bbc..ede5475 100644
}

protected void emitFinishEvent(WebView webView, String url) {
@@ -1138,6 +1453,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1138,6 +1464,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {

protected View mVideoView;
protected WebChromeClient.CustomViewCallback mCustomViewCallback;
+ protected boolean blockJsDuringLoading = true; //This boolean block JS prompts and alerts from displaying during loading

/*
* - Permissions -
@@ -1217,19 +1533,44 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1217,19 +1544,44 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
ArrayList<String> requestedAndroidPermissions = new ArrayList<>();
for (String requestedResource : request.getResources()) {
String androidPermission = null;
Expand Down Expand Up @@ -967,7 +978,7 @@ index f743bbc..ede5475 100644
} else {
requestedAndroidPermissions.add(androidPermission);
}
@@ -1238,8 +1579,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1238,8 +1590,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {

// If all the permissions are already granted, send the response to the WebView synchronously
if (requestedAndroidPermissions.isEmpty()) {
Expand All @@ -981,7 +992,7 @@ index f743bbc..ede5475 100644
return;
}

@@ -1250,6 +1594,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1250,6 +1605,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
requestPermissions(requestedAndroidPermissions);
}

Expand All @@ -992,7 +1003,7 @@ index f743bbc..ede5475 100644

@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
@@ -1402,6 +1750,15 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1402,6 +1761,15 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
}
}

Expand All @@ -1008,7 +1019,7 @@ index f743bbc..ede5475 100644
@Override
public void onHostPause() { }

@@ -1447,6 +1804,13 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1447,6 +1815,13 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
protected boolean nestedScrollEnabled = false;
protected ProgressChangedFilter progressChangedFilter;

Expand All @@ -1022,7 +1033,7 @@ index f743bbc..ede5475 100644
/**
* WebView must be created with an context of the current activity
* <p>
@@ -1475,6 +1839,42 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1475,6 +1850,42 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
this.nestedScrollEnabled = nestedScrollEnabled;
}

Expand Down Expand Up @@ -1065,7 +1076,7 @@ index f743bbc..ede5475 100644
@Override
public void onHostResume() {
// do nothing
@@ -1533,6 +1933,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1533,6 +1944,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
}
}

Expand All @@ -1074,7 +1085,7 @@ index f743bbc..ede5475 100644
public @Nullable
RNCWebViewClient getRNCWebViewClient() {
return mRNCWebViewClient;
@@ -1609,8 +2011,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1609,8 +2022,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {

public void callInjectedJavaScriptBeforeContentLoaded() {
if (getSettings().getJavaScriptEnabled() &&
Expand All @@ -1085,7 +1096,7 @@ index f743bbc..ede5475 100644
evaluateJavascriptWithFallback("(function() {\n" + injectedJSBeforeContentLoaded + ";\n})();");
}
}
@@ -1672,16 +2074,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -1672,16 +2085,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {

if (mOnScrollDispatchHelper.onScrollChanged(x, y)) {
ScrollEvent event = ScrollEvent.obtain(
Expand Down

0 comments on commit 35992d2

Please sign in to comment.