Skip to content

Commit 00ed7c6

Browse files
author
Martin Konicek
committed
Pass the correct URL to Android WebView events
Summary: `WebView.getUrl()` doesn't return the correct value in WebView callbacks (e.g. `onPageFinished`). For example, when navigating to a URL, we report that loading finished, but still with the old URL. This diff fixes that. public Reviewed By: andreicoman11 Differential Revision: D2769597 fb-gh-sync-id: f14bdd405290469ac0a20d0fb89aa2a27d33e758
1 parent 6c16d29 commit 00ed7c6

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void onPageFinished(WebView webView, String url) {
9191
if (!mLastLoadFailed) {
9292
ReactWebView reactWebView = (ReactWebView) webView;
9393
reactWebView.callInjectedJavaScript();
94-
emitFinishEvent(webView);
94+
emitFinishEvent(webView, url);
9595
}
9696
}
9797

@@ -107,7 +107,7 @@ public void onPageStarted(WebView webView, String url, Bitmap favicon) {
107107
new TopLoadingStartEvent(
108108
webView.getId(),
109109
SystemClock.uptimeMillis(),
110-
createWebViewEvent(webView)));
110+
createWebViewEvent(webView, url)));
111111
}
112112

113113
@Override
@@ -120,11 +120,11 @@ public void onReceivedError(
120120
mLastLoadFailed = true;
121121

122122
// In case of an error JS side expect to get a finish event first, and then get an error event
123-
// Android WebView does it in the oposite way, so we need to simulate that behavior
124-
emitFinishEvent(webView);
123+
// Android WebView does it in the opposite way, so we need to simulate that behavior
124+
emitFinishEvent(webView, failingUrl);
125125

126126
ReactContext reactContext = (ReactContext) ((ReactWebView) webView).getContext();
127-
WritableMap eventData = createWebViewEvent(webView);
127+
WritableMap eventData = createWebViewEvent(webView, failingUrl);
128128
eventData.putDouble("code", errorCode);
129129
eventData.putString("description", description);
130130

@@ -145,10 +145,10 @@ public void doUpdateVisitedHistory(WebView webView, String url, boolean isReload
145145
new TopLoadingStartEvent(
146146
webView.getId(),
147147
SystemClock.uptimeMillis(),
148-
createWebViewEvent(webView)));
148+
createWebViewEvent(webView, url)));
149149
}
150150

151-
private void emitFinishEvent(WebView webView) {
151+
private void emitFinishEvent(WebView webView, String url) {
152152
ReactContext reactContext = (ReactContext) webView.getContext();
153153

154154
EventDispatcher eventDispatcher =
@@ -157,13 +157,15 @@ private void emitFinishEvent(WebView webView) {
157157
new TopLoadingFinishEvent(
158158
webView.getId(),
159159
SystemClock.uptimeMillis(),
160-
createWebViewEvent(webView)));
160+
createWebViewEvent(webView, url)));
161161
}
162162

163-
private WritableMap createWebViewEvent(WebView webView) {
163+
private WritableMap createWebViewEvent(WebView webView, String url) {
164164
WritableMap event = Arguments.createMap();
165165
event.putDouble("target", webView.getId());
166-
event.putString("url", webView.getUrl());
166+
// Don't use webView.getUrl() here, the URL isn't updated to the new value yet in callbacks
167+
// like onPageFinished
168+
event.putString("url", url);
167169
event.putBoolean("loading", !mLastLoadFailed && webView.getProgress() != 100);
168170
event.putString("title", webView.getTitle());
169171
event.putBoolean("canGoBack", webView.canGoBack());
@@ -279,7 +281,7 @@ public void setHtml(WebView view, @Nullable String html) {
279281
public void setUrl(WebView view, @Nullable String url) {
280282
// TODO(8495359): url and html are coupled as they both call loadUrl, therefore in case when
281283
// property url is removed in favor of property html being added in single transaction we may
282-
// end up in a state when blank url is loaded as it depends onthe oreder of update operations!
284+
// end up in a state when blank url is loaded as it depends on the order of update operations!
283285
if (url != null) {
284286
view.loadUrl(url);
285287
} else {

0 commit comments

Comments
 (0)