Skip to content

Commit

Permalink
Pass the correct URL to Android WebView events
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Martin Konicek authored and facebook-github-bot-5 committed Dec 18, 2015
1 parent 85e8a46 commit b436943
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void onPageFinished(WebView webView, String url) {
if (!mLastLoadFailed) {
ReactWebView reactWebView = (ReactWebView) webView;
reactWebView.callInjectedJavaScript();
emitFinishEvent(webView);
emitFinishEvent(webView, url);
}
}

Expand All @@ -107,7 +107,7 @@ public void onPageStarted(WebView webView, String url, Bitmap favicon) {
new TopLoadingStartEvent(
webView.getId(),
SystemClock.uptimeMillis(),
createWebViewEvent(webView)));
createWebViewEvent(webView, url)));
}

@Override
Expand All @@ -120,11 +120,11 @@ public void onReceivedError(
mLastLoadFailed = true;

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

ReactContext reactContext = (ReactContext) ((ReactWebView) webView).getContext();
WritableMap eventData = createWebViewEvent(webView);
WritableMap eventData = createWebViewEvent(webView, failingUrl);
eventData.putDouble("code", errorCode);
eventData.putString("description", description);

Expand All @@ -145,10 +145,10 @@ public void doUpdateVisitedHistory(WebView webView, String url, boolean isReload
new TopLoadingStartEvent(
webView.getId(),
SystemClock.uptimeMillis(),
createWebViewEvent(webView)));
createWebViewEvent(webView, url)));
}

private void emitFinishEvent(WebView webView) {
private void emitFinishEvent(WebView webView, String url) {
ReactContext reactContext = (ReactContext) webView.getContext();

EventDispatcher eventDispatcher =
Expand All @@ -157,13 +157,15 @@ private void emitFinishEvent(WebView webView) {
new TopLoadingFinishEvent(
webView.getId(),
SystemClock.uptimeMillis(),
createWebViewEvent(webView)));
createWebViewEvent(webView, url)));
}

private WritableMap createWebViewEvent(WebView webView) {
private WritableMap createWebViewEvent(WebView webView, String url) {
WritableMap event = Arguments.createMap();
event.putDouble("target", webView.getId());
event.putString("url", webView.getUrl());
// Don't use webView.getUrl() here, the URL isn't updated to the new value yet in callbacks
// like onPageFinished
event.putString("url", url);
event.putBoolean("loading", !mLastLoadFailed && webView.getProgress() != 100);
event.putString("title", webView.getTitle());
event.putBoolean("canGoBack", webView.canGoBack());
Expand Down Expand Up @@ -279,7 +281,7 @@ public void setHtml(WebView view, @Nullable String html) {
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 onthe oreder of update operations!
// end up in a state when blank url is loaded as it depends on the order of update operations!
if (url != null) {
view.loadUrl(url);
} else {
Expand Down

0 comments on commit b436943

Please sign in to comment.