Commit 5a5efe04 authored by Ryan Daum's avatar Ryan Daum Committed by Commit Bot

[chromecast] Make ConsumeGesture take a completion callback

Indication of back gesture completion from application comes over an
asynchronous mojo callback and its return value is currently dropped
on the floor in order to avoid blocking the browser process to wait.

This cl changes the ConsumeGesture call to take a completion callback
and along with corresponding internal changes wires the return value
all the way.

Bug: internal b/149740759
Test: manual and unit tests
Change-Id: Ib2e519326e019148a64966e5e46ee9df00d40755
Merge-With: eureka-internal/382879
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2132980Reviewed-by: default avatarDaniel Nicoara <dnicoara@chromium.org>
Commit-Queue: Ryan Daum <rdaum@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756780}
parent 82807c55
......@@ -133,21 +133,24 @@ public class CastContentWindowAndroid implements CastWebContentsComponent.OnComp
}
@Override
public boolean consumeGesture(int gestureType) {
public void consumeGesture(int gestureType,
CastWebContentsComponent.GestureHandledCallback handledGestureCallback) {
if (DEBUG) Log.d(TAG, "consumeGesture type=" + gestureType);
if (mNativeCastContentWindowAndroid != 0) {
return CastContentWindowAndroidJni.get().consumeGesture(
mNativeCastContentWindowAndroid, CastContentWindowAndroid.this, gestureType);
CastContentWindowAndroidJni.get().consumeGesture(mNativeCastContentWindowAndroid,
CastContentWindowAndroid.this, gestureType, handledGestureCallback);
return;
}
return false;
handledGestureCallback.invoke(false);
}
@NativeMethods
interface Natives {
void onActivityStopped(
long nativeCastContentWindowAndroid, CastContentWindowAndroid caller);
boolean consumeGesture(long nativeCastContentWindowAndroid, CastContentWindowAndroid caller,
int gestureType);
void consumeGesture(long nativeCastContentWindowAndroid, CastContentWindowAndroid caller,
int gestureType,
CastWebContentsComponent.GestureHandledCallback handledGestureCallback);
void onVisibilityChange(long nativeCastContentWindowAndroid,
CastContentWindowAndroid caller, int visibilityType);
String getId(long nativeCastContentWindowAndroid, CastContentWindowAndroid caller);
......
......@@ -32,12 +32,19 @@ public class CastWebContentsComponent {
*/
public interface OnComponentClosedHandler { void onComponentClosed(); }
/**
* Callback interface invoked to indicate whether a gesture has been handled.
*/
public interface GestureHandledCallback {
void invoke(boolean handled);
}
/**
* Callback interface for when UI events occur.
*/
public interface SurfaceEventHandler {
void onVisibilityChange(int visibilityType);
boolean consumeGesture(int gestureType);
void consumeGesture(int gestureType, GestureHandledCallback handledGestureCallback);
}
/**
......@@ -226,15 +233,17 @@ public class CastWebContentsComponent {
+ "; gesture=" + gestureType);
}
if (mSurfaceEventHandler != null) {
if (mSurfaceEventHandler.consumeGesture(gestureType)) {
if (DEBUG) Log.d(TAG, "send gesture consumed instance=" + mSessionId);
sendIntentSync(CastWebContentsIntentUtils.gestureConsumed(
mSessionId, gestureType, true));
} else {
if (DEBUG) Log.d(TAG, "send gesture NOT consumed instance=" + mSessionId);
sendIntentSync(CastWebContentsIntentUtils.gestureConsumed(
mSessionId, gestureType, false));
}
mSurfaceEventHandler.consumeGesture(gestureType, (handled) -> {
if (handled) {
if (DEBUG) Log.d(TAG, "send gesture consumed instance=" + mSessionId);
sendIntentSync(CastWebContentsIntentUtils.gestureConsumed(
mSessionId, gestureType, true));
} else {
if (DEBUG) Log.d(TAG, "send gesture NOT consumed instance=" + mSessionId);
sendIntentSync(CastWebContentsIntentUtils.gestureConsumed(
mSessionId, gestureType, false));
}
});
} else {
sendIntentSync(
CastWebContentsIntentUtils.gestureConsumed(mSessionId, gestureType, false));
......
......@@ -38,6 +38,35 @@ base::android::ScopedJavaLocalRef<jobject> CreateJavaWindow(
constexpr char kContextInteractionId[] = "interactionId";
constexpr char kContextConversationId[] = "conversationId";
// Wraps the JNI gesture consumption handled callback for invocation from C++.
class GestureConsumedCallbackWrapper {
public:
GestureConsumedCallbackWrapper(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& callback)
: env_(env), callback_(callback) {
class_ = base::android::ScopedJavaLocalRef<jclass>(
base::android::GetClass(env_,
"org.chromium.chromecast.shell."
"CastWebComponent.GestureHandledCallback"));
callback_method_id_ =
base::android::MethodID::Get<base::android::MethodID::TYPE_INSTANCE>(
env_, class_.obj(), "invoke", "(Z;)V");
}
void Invoke(bool handled) {
env_->CallObjectMethod(callback_.obj(), callback_method_id_, &handled);
}
private:
JNIEnv* env_;
const base::android::JavaParamRef<jobject>& callback_;
base::android::ScopedJavaLocalRef<jclass> class_;
jmethodID callback_method_id_;
DISALLOW_COPY_AND_ASSIGN(GestureConsumedCallbackWrapper);
};
} // namespace
CastContentWindowAndroid::CastContentWindowAndroid(
......@@ -132,14 +161,20 @@ void CastContentWindowAndroid::RequestMoveOut() {
Java_CastContentWindowAndroid_requestMoveOut(env, java_window_);
}
bool CastContentWindowAndroid::ConsumeGesture(
void CastContentWindowAndroid::ConsumeGesture(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
int gesture_type) {
int gesture_type,
const base::android::JavaParamRef<jobject>& callback) {
GestureConsumedCallbackWrapper wrapper(env, callback);
if (delegate_) {
return delegate_->ConsumeGesture(static_cast<GestureType>(gesture_type));
delegate_->ConsumeGesture(
static_cast<GestureType>(gesture_type),
base::BindOnce(&GestureConsumedCallbackWrapper::Invoke,
base::Unretained(&wrapper)));
return;
}
return false;
wrapper.Invoke(false);
}
void CastContentWindowAndroid::OnVisibilityChange(
......
......@@ -42,9 +42,11 @@ class CastContentWindowAndroid : public CastContentWindow {
// Called through JNI.
void OnActivityStopped(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller);
bool ConsumeGesture(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
int gesture_type);
void ConsumeGesture(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
int gesture_type,
const base::android::JavaParamRef<jobject>& handled_callback);
void OnVisibilityChange(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
int visibility_type);
......
......@@ -220,14 +220,13 @@ public class CastWebContentsComponentTest {
public void testOnGestureCallback() {
CastWebContentsComponent.SurfaceEventHandler callback =
Mockito.mock(CastWebContentsComponent.SurfaceEventHandler.class);
CastWebContentsComponent component =
new CastWebContentsComponent(SESSION_ID, null, callback, false, false, false, true);
component.start(mStartParams);
CastWebContentsComponent.onGesture(SESSION_ID, 1);
component.stop(mActivity);
verify(callback).consumeGesture(1);
verify(callback).consumeGesture(eq(1), any());
}
@Test
......
......@@ -96,7 +96,7 @@ void CastContentGestureHandler::HandleSideSwipe(
delegate_->CancelGesture(GestureType::GO_BACK, touch_location);
return;
}
delegate_->ConsumeGesture(gesture_type);
delegate_->ConsumeGesture(gesture_type, base::DoNothing());
DVLOG(1) << "gesture complete, elapsed time: "
<< current_swipe_time_.Elapsed().InMilliseconds() << "ms";
break;
......@@ -108,7 +108,7 @@ void CastContentGestureHandler::HandleTapDownGesture(
if (!delegate_ || !delegate_->CanHandleGesture(GestureType::TAP_DOWN)) {
return;
}
delegate_->ConsumeGesture(GestureType::TAP_DOWN);
delegate_->ConsumeGesture(GestureType::TAP_DOWN, base::DoNothing());
}
void CastContentGestureHandler::HandleTapGesture(
......@@ -116,7 +116,7 @@ void CastContentGestureHandler::HandleTapGesture(
if (!delegate_ || !delegate_->CanHandleGesture(GestureType::TAP)) {
return;
}
delegate_->ConsumeGesture(GestureType::TAP);
delegate_->ConsumeGesture(GestureType::TAP, base::DoNothing());
}
} // namespace chromecast
......@@ -8,6 +8,7 @@
#include <memory>
#include <string>
#include "base/callback_forward.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/observer_list.h"
......@@ -100,6 +101,8 @@ class CastContentWindow {
// Notify window destruction.
virtual void OnWindowDestroyed() {}
using GestureHandledCallback = base::OnceCallback<void(bool)>;
// Check to see if the gesture can be handled by the delegate. This is
// called prior to ConsumeGesture().
virtual bool CanHandleGesture(GestureType gesture_type) = 0;
......@@ -113,9 +116,10 @@ class CastContentWindow {
virtual void CancelGesture(GestureType gesture_type,
const gfx::Point& touch_location) {}
// Consume and handle a completed UI gesture. Returns whether the gesture
// was handled or not.
virtual bool ConsumeGesture(GestureType gesture_type) = 0;
// Consume and handle a completed UI gesture. Invokes the callback with a
// boolean indicating whether the gesture was handled or not.
virtual void ConsumeGesture(GestureType gesture_type,
GestureHandledCallback handled_callback) = 0;
// Notify visibility change for this window.
virtual void OnVisibilityChange(VisibilityType visibility_type) {}
......
......@@ -93,8 +93,10 @@ bool CastServiceSimple::CanHandleGesture(GestureType gesture_type) {
return false;
}
bool CastServiceSimple::ConsumeGesture(GestureType gesture_type) {
return false;
void CastServiceSimple::ConsumeGesture(
GestureType gesture_type,
GestureHandledCallback handled_callback) {
std::move(handled_callback).Run(false);
}
void CastServiceSimple::OnVisibilityChange(VisibilityType visibility_type) {}
......
......@@ -39,7 +39,8 @@ class CastServiceSimple : public CastService, public CastWebView::Delegate {
// CastContentWindow::Delegate implementation:
void OnWindowDestroyed() override;
bool CanHandleGesture(GestureType gesture_type) override;
bool ConsumeGesture(GestureType gesture_type) override;
void ConsumeGesture(GestureType gesture_type,
GestureHandledCallback handled_callback) override;
void OnVisibilityChange(VisibilityType visibility_type) override;
std::string GetId() override;
......
......@@ -94,8 +94,9 @@ bool CastBrowserTest::CanHandleGesture(GestureType gesture_type) {
return false;
}
bool CastBrowserTest::ConsumeGesture(GestureType gesture_type) {
return false;
void CastBrowserTest::ConsumeGesture(GestureType gesture_type,
GestureHandledCallback handled_callback) {
std::move(handled_callback).Run(false);
}
std::string CastBrowserTest::GetId() {
......
......@@ -51,7 +51,8 @@ class CastBrowserTest : public content::BrowserTestBase,
void OnWindowDestroyed() override;
void OnVisibilityChange(VisibilityType visibility_type) override;
bool CanHandleGesture(GestureType gesture_type) override;
bool ConsumeGesture(GestureType gesture_type) override;
void ConsumeGesture(GestureType gesture_type,
GestureHandledCallback handled_callback) override;
std::string GetId() override;
std::unique_ptr<CastWebViewFactory> web_view_factory_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment