Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android][WebView] onShouldStartLoadWithRequest callback #6478

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Libraries/Components/WebView/WebView.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var WebViewState = keyMirror({
ERROR: null,
});

type Event = Object;

/**
* Renders a native WebView.
*/
Expand All @@ -51,6 +53,7 @@ var WebView = React.createClass({
onNavigationStateChange: PropTypes.func,
startInLoadingState: PropTypes.bool, // force WebView to show loadingView on first load
style: View.propTypes.style,
onShouldStartLoadWithRequest: PropTypes.func,

html: deprecatedPropType(
PropTypes.string,
Expand Down Expand Up @@ -195,6 +198,15 @@ var WebView = React.createClass({
console.warn('WebView: `source.body` is not supported when using GET.');
}

var onShouldOverrideUrlLoading = this.props.onShouldStartLoadWithRequest
&& ((event: Event) => {
var shouldOverride = !this.props.onShouldStartLoadWithRequest(event.nativeEvent);
UIManager.dispatchViewManagerCommandSync(
this.getWebViewHandle(),
UIManager.RCTWebView.Commands.shouldOverrideWithResult,
[shouldOverride]);
});

var webView =
<RCTWebView
ref={RCT_WEBVIEW_REF}
Expand All @@ -212,6 +224,7 @@ var WebView = React.createClass({
onLoadingFinish={this.onLoadingFinish}
onLoadingError={this.onLoadingError}
testID={this.props.testID}
onShouldOverrideUrlLoading={onShouldOverrideUrlLoading}
/>;

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,7 @@ public void onCancel() {
}
}

public void dispatchCommand(int reactTag, int commandId, @Nullable ReadableArray args) {
UiThreadUtil.assertOnUiThread();
private void dispatchCommandCommon(int reactTag, int commandId, @Nullable ReadableArray args) {
View view = mTagsToViews.get(reactTag);
if (view == null) {
throw new IllegalViewOperationException("Trying to send command to a non-existing view " +
Expand All @@ -597,6 +596,16 @@ public void dispatchCommand(int reactTag, int commandId, @Nullable ReadableArray
viewManager.receiveCommand(view, commandId, args);
}

public void dispatchCommandSync(int reactTag, int commandId, @Nullable ReadableArray args) {
dispatchCommandCommon(reactTag, commandId, args);
}

public void dispatchCommand(int reactTag, int commandId, @Nullable ReadableArray args) {
UiThreadUtil.assertOnUiThread();
dispatchCommandCommon(reactTag, commandId, args);
}


/**
* Show a {@link PopupMenu}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,11 @@ public void dispatchViewManagerCommand(int reactTag, int commandId, ReadableArra
mOperationsQueue.enqueueDispatchCommand(reactTag, commandId, commandArgs);
}

public void dispatchViewManagerCommandSync(int reactTag, int commandId, ReadableArray commandArgs) {
assertViewExists(reactTag, "dispatchViewManagerCommandSync");
mOperationsQueue.executeDispatchCommand(reactTag, commandId, commandArgs);
}

/**
* Show a PopupMenu.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ public void dispatchViewManagerCommand(int reactTag, int commandId, ReadableArra
mUIImplementation.dispatchViewManagerCommand(reactTag, commandId, commandArgs);
}

@ReactMethod
public void dispatchViewManagerCommandSync(int reactTag, int commandId, ReadableArray commandArgs) {
mUIImplementation.dispatchViewManagerCommandSync(reactTag, commandId, commandArgs);
}

/**
* Show a PopupMenu.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ public DispatchCommandOperation(int tag, int command, @Nullable ReadableArray ar
public void execute() {
mNativeViewHierarchyManager.dispatchCommand(mTag, mCommand, mArgs);
}

public void executeSync() {
mNativeViewHierarchyManager.dispatchCommandSync(mTag, mCommand, mArgs);
}
}

private final class ShowPopupMenuOperation extends ViewOperation {
Expand Down Expand Up @@ -583,6 +587,14 @@ public void enqueueDispatchCommand(
mOperations.add(new DispatchCommandOperation(reactTag, commandId, commandArgs));
}

public void executeDispatchCommand(
int reactTag,
int commandId,
ReadableArray commandArgs) {
DispatchCommandOperation operation = new DispatchCommandOperation(reactTag, commandId, commandArgs);
operation.executeSync();
}

public void enqueueUpdateExtraData(int reactTag, Object extraData) {
mOperations.add(new UpdateViewExtraData(reactTag, extraData));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public boolean canCoalesce() {
return true;
}

public boolean isSync() {
return false;
}

/**
* Given two events, coalesce them into a single event that will be sent to JS instead of two
* separate events. By default, just chooses the one the is more recent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,32 @@ public EventDispatcher(ReactApplicationContext reactContext) {
public void dispatchEvent(Event event) {
Assertions.assertCondition(event.isInitialized(), "Dispatched event hasn't been initialized");
synchronized (mEventsStagingLock) {

if (event.isSync()) {
synchronized (mEventsToDispatchLock) {
addEventToEventsToDispatch(event);
mReactContext.runOnJSQueueThread(new Runnable() {
@Override
public void run() {
synchronized (mEventsToDispatchLock) {
for (int eventIdx = 0; eventIdx < mEventsToDispatchSize; eventIdx++) {
Event event = mEventsToDispatch[eventIdx];
if (event == null) {
continue;
}

event.dispatch(mRCTEventEmitter);
event.dispose();

}
clearEventsToDispatch();
}
}
});
}
return;
}

mEventStaging.add(event);
Systrace.startAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

import android.graphics.Bitmap;
import android.os.Build;
import android.os.ConditionVariable;
import android.text.TextUtils;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.facebook.catalyst.views.webview.events.TopLoadingErrorEvent;
import com.facebook.catalyst.views.webview.events.TopLoadingFinishEvent;
import com.facebook.catalyst.views.webview.events.TopLoadingStartEvent;
import com.facebook.react.views.webview.events.ShouldOverrideUrlLoadingEvent;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactContext;
Expand All @@ -40,6 +42,7 @@
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.common.MapBuilder;

/**
* Manages instances of {@link WebView}
Expand All @@ -53,6 +56,7 @@
* - topLoadingFinish
* - topLoadingStart
* - topLoadingError
* - topShouldOverrideUrlLoading
*
* Each event will carry the following properties:
* - target - view's react tag
Expand All @@ -74,6 +78,7 @@ public class ReactWebViewManager extends SimpleViewManager<WebView> {
public static final int COMMAND_GO_BACK = 1;
public static final int COMMAND_GO_FORWARD = 2;
public static final int COMMAND_RELOAD = 3;
public static final int COMMAND_SHOULD_OVERRIDE_WITH_RESULT = 4;

// Use `webView.loadUrl("about:blank")` to reliably reset the view
// state and release page resources (including any running JavaScript).
Expand Down Expand Up @@ -143,6 +148,23 @@ public void doUpdateVisitedHistory(WebView webView, String url, boolean isReload
createWebViewEvent(webView, url)));
}

@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
ReactWebView view = ((ReactWebView)webView);
view.mShouldOverrideUrlLoadingResult = false;
dispatchEvent(
webView,
new ShouldOverrideUrlLoadingEvent(
webView.getId(),
SystemClock.nanoTime(),
createWebViewEvent(webView, url)));

view.mShouldOverrideUrlLoadingConditionVariable.close();
view.mShouldOverrideUrlLoadingConditionVariable.block(250);

return view.mShouldOverrideUrlLoadingResult;
}

private void emitFinishEvent(WebView webView, String url) {
dispatchEvent(
webView,
Expand Down Expand Up @@ -180,6 +202,9 @@ private WritableMap createWebViewEvent(WebView webView, String url) {
private static class ReactWebView extends WebView implements LifecycleEventListener {
private @Nullable String injectedJS;

protected ConditionVariable mShouldOverrideUrlLoadingConditionVariable;
protected boolean mShouldOverrideUrlLoadingResult;

/**
* WebView must be created with an context of the current activity
*
Expand All @@ -189,6 +214,7 @@ private static class ReactWebView extends WebView implements LifecycleEventListe
*/
public ReactWebView(ThemedReactContext reactContext) {
super(reactContext);
mShouldOverrideUrlLoadingConditionVariable = new ConditionVariable();
}

@Override
Expand Down Expand Up @@ -222,6 +248,11 @@ private void cleanupCallbacksAndDestroy() {
setWebViewClient(null);
destroy();
}

private void shouldOverrideWithResult(ReadableArray args) {
this.mShouldOverrideUrlLoadingResult = args.getBoolean(0);
this.mShouldOverrideUrlLoadingConditionVariable.open();
}
}

public ReactWebViewManager() {
Expand Down Expand Up @@ -342,7 +373,8 @@ protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
return MapBuilder.of(
"goBack", COMMAND_GO_BACK,
"goForward", COMMAND_GO_FORWARD,
"reload", COMMAND_RELOAD);
"reload", COMMAND_RELOAD,
"shouldOverrideWithResult", COMMAND_SHOULD_OVERRIDE_WITH_RESULT);
}

@Override
Expand All @@ -357,6 +389,9 @@ public void receiveCommand(WebView root, int commandId, @Nullable ReadableArray
case COMMAND_RELOAD:
root.reload();
break;
case COMMAND_SHOULD_OVERRIDE_WITH_RESULT:
((ReactWebView) root).shouldOverrideWithResult(args);
break;
}
}

Expand All @@ -366,4 +401,11 @@ public void onDropViewInstance(WebView webView) {
((ThemedReactContext) webView.getContext()).removeLifecycleEventListener((ReactWebView) webView);
((ReactWebView) webView).cleanupCallbacksAndDestroy();
}

@Override
public @Nullable Map getExportedCustomDirectEventTypeConstants() {
return MapBuilder.builder()
.put("topShouldOverrideUrlLoading", MapBuilder.of("registrationName", "onShouldOverrideUrlLoading"))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

package com.facebook.react.views.webview.events;

import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.RCTEventEmitter;

/**
* Event emitted when loading has started
*/
public class ShouldOverrideUrlLoadingEvent extends Event<ShouldOverrideUrlLoadingEvent> {

public static final String EVENT_NAME = "topShouldOverrideUrlLoading";
private WritableMap mEventData;

public ShouldOverrideUrlLoadingEvent(int viewId, long timestampMs, WritableMap eventData) {
super(viewId, timestampMs);
mEventData = eventData;
}

@Override
public String getEventName() {
return EVENT_NAME;
}

@Override
public boolean canCoalesce() {
return false;
}

@Override
public boolean isSync() {
return true;
}

@Override
public short getCoalescingKey() {
// All events for a given view can be coalesced.
return 0;
}

@Override
public void dispatch(RCTEventEmitter rctEventEmitter) {
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), mEventData);
}
}