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 ...@@ -133,21 +133,24 @@ public class CastContentWindowAndroid implements CastWebContentsComponent.OnComp
} }
@Override @Override
public boolean consumeGesture(int gestureType) { public void consumeGesture(int gestureType,
CastWebContentsComponent.GestureHandledCallback handledGestureCallback) {
if (DEBUG) Log.d(TAG, "consumeGesture type=" + gestureType); if (DEBUG) Log.d(TAG, "consumeGesture type=" + gestureType);
if (mNativeCastContentWindowAndroid != 0) { if (mNativeCastContentWindowAndroid != 0) {
return CastContentWindowAndroidJni.get().consumeGesture( CastContentWindowAndroidJni.get().consumeGesture(mNativeCastContentWindowAndroid,
mNativeCastContentWindowAndroid, CastContentWindowAndroid.this, gestureType); CastContentWindowAndroid.this, gestureType, handledGestureCallback);
return;
} }
return false; handledGestureCallback.invoke(false);
} }
@NativeMethods @NativeMethods
interface Natives { interface Natives {
void onActivityStopped( void onActivityStopped(
long nativeCastContentWindowAndroid, CastContentWindowAndroid caller); long nativeCastContentWindowAndroid, CastContentWindowAndroid caller);
boolean consumeGesture(long nativeCastContentWindowAndroid, CastContentWindowAndroid caller, void consumeGesture(long nativeCastContentWindowAndroid, CastContentWindowAndroid caller,
int gestureType); int gestureType,
CastWebContentsComponent.GestureHandledCallback handledGestureCallback);
void onVisibilityChange(long nativeCastContentWindowAndroid, void onVisibilityChange(long nativeCastContentWindowAndroid,
CastContentWindowAndroid caller, int visibilityType); CastContentWindowAndroid caller, int visibilityType);
String getId(long nativeCastContentWindowAndroid, CastContentWindowAndroid caller); String getId(long nativeCastContentWindowAndroid, CastContentWindowAndroid caller);
......
...@@ -32,12 +32,19 @@ public class CastWebContentsComponent { ...@@ -32,12 +32,19 @@ public class CastWebContentsComponent {
*/ */
public interface OnComponentClosedHandler { void onComponentClosed(); } 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. * Callback interface for when UI events occur.
*/ */
public interface SurfaceEventHandler { public interface SurfaceEventHandler {
void onVisibilityChange(int visibilityType); void onVisibilityChange(int visibilityType);
boolean consumeGesture(int gestureType); void consumeGesture(int gestureType, GestureHandledCallback handledGestureCallback);
} }
/** /**
...@@ -226,15 +233,17 @@ public class CastWebContentsComponent { ...@@ -226,15 +233,17 @@ public class CastWebContentsComponent {
+ "; gesture=" + gestureType); + "; gesture=" + gestureType);
} }
if (mSurfaceEventHandler != null) { if (mSurfaceEventHandler != null) {
if (mSurfaceEventHandler.consumeGesture(gestureType)) { mSurfaceEventHandler.consumeGesture(gestureType, (handled) -> {
if (DEBUG) Log.d(TAG, "send gesture consumed instance=" + mSessionId); if (handled) {
sendIntentSync(CastWebContentsIntentUtils.gestureConsumed( if (DEBUG) Log.d(TAG, "send gesture consumed instance=" + mSessionId);
mSessionId, gestureType, true)); sendIntentSync(CastWebContentsIntentUtils.gestureConsumed(
} else { mSessionId, gestureType, true));
if (DEBUG) Log.d(TAG, "send gesture NOT consumed instance=" + mSessionId); } else {
sendIntentSync(CastWebContentsIntentUtils.gestureConsumed( if (DEBUG) Log.d(TAG, "send gesture NOT consumed instance=" + mSessionId);
mSessionId, gestureType, false)); sendIntentSync(CastWebContentsIntentUtils.gestureConsumed(
} mSessionId, gestureType, false));
}
});
} else { } else {
sendIntentSync( sendIntentSync(
CastWebContentsIntentUtils.gestureConsumed(mSessionId, gestureType, false)); CastWebContentsIntentUtils.gestureConsumed(mSessionId, gestureType, false));
......
...@@ -38,6 +38,35 @@ base::android::ScopedJavaLocalRef<jobject> CreateJavaWindow( ...@@ -38,6 +38,35 @@ base::android::ScopedJavaLocalRef<jobject> CreateJavaWindow(
constexpr char kContextInteractionId[] = "interactionId"; constexpr char kContextInteractionId[] = "interactionId";
constexpr char kContextConversationId[] = "conversationId"; 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 } // namespace
CastContentWindowAndroid::CastContentWindowAndroid( CastContentWindowAndroid::CastContentWindowAndroid(
...@@ -132,14 +161,20 @@ void CastContentWindowAndroid::RequestMoveOut() { ...@@ -132,14 +161,20 @@ void CastContentWindowAndroid::RequestMoveOut() {
Java_CastContentWindowAndroid_requestMoveOut(env, java_window_); Java_CastContentWindowAndroid_requestMoveOut(env, java_window_);
} }
bool CastContentWindowAndroid::ConsumeGesture( void CastContentWindowAndroid::ConsumeGesture(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller, const base::android::JavaParamRef<jobject>& jcaller,
int gesture_type) { int gesture_type,
const base::android::JavaParamRef<jobject>& callback) {
GestureConsumedCallbackWrapper wrapper(env, callback);
if (delegate_) { 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( void CastContentWindowAndroid::OnVisibilityChange(
......
...@@ -42,9 +42,11 @@ class CastContentWindowAndroid : public CastContentWindow { ...@@ -42,9 +42,11 @@ class CastContentWindowAndroid : public CastContentWindow {
// Called through JNI. // Called through JNI.
void OnActivityStopped(JNIEnv* env, void OnActivityStopped(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller); const base::android::JavaParamRef<jobject>& jcaller);
bool ConsumeGesture(JNIEnv* env, void ConsumeGesture(
const base::android::JavaParamRef<jobject>& jcaller, JNIEnv* env,
int gesture_type); const base::android::JavaParamRef<jobject>& jcaller,
int gesture_type,
const base::android::JavaParamRef<jobject>& handled_callback);
void OnVisibilityChange(JNIEnv* env, void OnVisibilityChange(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller, const base::android::JavaParamRef<jobject>& jcaller,
int visibility_type); int visibility_type);
......
...@@ -220,14 +220,13 @@ public class CastWebContentsComponentTest { ...@@ -220,14 +220,13 @@ public class CastWebContentsComponentTest {
public void testOnGestureCallback() { public void testOnGestureCallback() {
CastWebContentsComponent.SurfaceEventHandler callback = CastWebContentsComponent.SurfaceEventHandler callback =
Mockito.mock(CastWebContentsComponent.SurfaceEventHandler.class); Mockito.mock(CastWebContentsComponent.SurfaceEventHandler.class);
CastWebContentsComponent component = CastWebContentsComponent component =
new CastWebContentsComponent(SESSION_ID, null, callback, false, false, false, true); new CastWebContentsComponent(SESSION_ID, null, callback, false, false, false, true);
component.start(mStartParams); component.start(mStartParams);
CastWebContentsComponent.onGesture(SESSION_ID, 1); CastWebContentsComponent.onGesture(SESSION_ID, 1);
component.stop(mActivity); component.stop(mActivity);
verify(callback).consumeGesture(1); verify(callback).consumeGesture(eq(1), any());
} }
@Test @Test
......
...@@ -96,7 +96,7 @@ void CastContentGestureHandler::HandleSideSwipe( ...@@ -96,7 +96,7 @@ void CastContentGestureHandler::HandleSideSwipe(
delegate_->CancelGesture(GestureType::GO_BACK, touch_location); delegate_->CancelGesture(GestureType::GO_BACK, touch_location);
return; return;
} }
delegate_->ConsumeGesture(gesture_type); delegate_->ConsumeGesture(gesture_type, base::DoNothing());
DVLOG(1) << "gesture complete, elapsed time: " DVLOG(1) << "gesture complete, elapsed time: "
<< current_swipe_time_.Elapsed().InMilliseconds() << "ms"; << current_swipe_time_.Elapsed().InMilliseconds() << "ms";
break; break;
...@@ -108,7 +108,7 @@ void CastContentGestureHandler::HandleTapDownGesture( ...@@ -108,7 +108,7 @@ void CastContentGestureHandler::HandleTapDownGesture(
if (!delegate_ || !delegate_->CanHandleGesture(GestureType::TAP_DOWN)) { if (!delegate_ || !delegate_->CanHandleGesture(GestureType::TAP_DOWN)) {
return; return;
} }
delegate_->ConsumeGesture(GestureType::TAP_DOWN); delegate_->ConsumeGesture(GestureType::TAP_DOWN, base::DoNothing());
} }
void CastContentGestureHandler::HandleTapGesture( void CastContentGestureHandler::HandleTapGesture(
...@@ -116,7 +116,7 @@ void CastContentGestureHandler::HandleTapGesture( ...@@ -116,7 +116,7 @@ void CastContentGestureHandler::HandleTapGesture(
if (!delegate_ || !delegate_->CanHandleGesture(GestureType::TAP)) { if (!delegate_ || !delegate_->CanHandleGesture(GestureType::TAP)) {
return; return;
} }
delegate_->ConsumeGesture(GestureType::TAP); delegate_->ConsumeGesture(GestureType::TAP, base::DoNothing());
} }
} // namespace chromecast } // namespace chromecast
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/callback_forward.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/observer_list.h" #include "base/observer_list.h"
...@@ -100,6 +101,8 @@ class CastContentWindow { ...@@ -100,6 +101,8 @@ class CastContentWindow {
// Notify window destruction. // Notify window destruction.
virtual void OnWindowDestroyed() {} virtual void OnWindowDestroyed() {}
using GestureHandledCallback = base::OnceCallback<void(bool)>;
// Check to see if the gesture can be handled by the delegate. This is // Check to see if the gesture can be handled by the delegate. This is
// called prior to ConsumeGesture(). // called prior to ConsumeGesture().
virtual bool CanHandleGesture(GestureType gesture_type) = 0; virtual bool CanHandleGesture(GestureType gesture_type) = 0;
...@@ -113,9 +116,10 @@ class CastContentWindow { ...@@ -113,9 +116,10 @@ class CastContentWindow {
virtual void CancelGesture(GestureType gesture_type, virtual void CancelGesture(GestureType gesture_type,
const gfx::Point& touch_location) {} const gfx::Point& touch_location) {}
// Consume and handle a completed UI gesture. Returns whether the gesture // Consume and handle a completed UI gesture. Invokes the callback with a
// was handled or not. // boolean indicating whether the gesture was handled or not.
virtual bool ConsumeGesture(GestureType gesture_type) = 0; virtual void ConsumeGesture(GestureType gesture_type,
GestureHandledCallback handled_callback) = 0;
// Notify visibility change for this window. // Notify visibility change for this window.
virtual void OnVisibilityChange(VisibilityType visibility_type) {} virtual void OnVisibilityChange(VisibilityType visibility_type) {}
......
...@@ -93,8 +93,10 @@ bool CastServiceSimple::CanHandleGesture(GestureType gesture_type) { ...@@ -93,8 +93,10 @@ bool CastServiceSimple::CanHandleGesture(GestureType gesture_type) {
return false; return false;
} }
bool CastServiceSimple::ConsumeGesture(GestureType gesture_type) { void CastServiceSimple::ConsumeGesture(
return false; GestureType gesture_type,
GestureHandledCallback handled_callback) {
std::move(handled_callback).Run(false);
} }
void CastServiceSimple::OnVisibilityChange(VisibilityType visibility_type) {} void CastServiceSimple::OnVisibilityChange(VisibilityType visibility_type) {}
......
...@@ -39,7 +39,8 @@ class CastServiceSimple : public CastService, public CastWebView::Delegate { ...@@ -39,7 +39,8 @@ class CastServiceSimple : public CastService, public CastWebView::Delegate {
// CastContentWindow::Delegate implementation: // CastContentWindow::Delegate implementation:
void OnWindowDestroyed() override; void OnWindowDestroyed() override;
bool CanHandleGesture(GestureType gesture_type) 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; void OnVisibilityChange(VisibilityType visibility_type) override;
std::string GetId() override; std::string GetId() override;
......
...@@ -94,8 +94,9 @@ bool CastBrowserTest::CanHandleGesture(GestureType gesture_type) { ...@@ -94,8 +94,9 @@ bool CastBrowserTest::CanHandleGesture(GestureType gesture_type) {
return false; return false;
} }
bool CastBrowserTest::ConsumeGesture(GestureType gesture_type) { void CastBrowserTest::ConsumeGesture(GestureType gesture_type,
return false; GestureHandledCallback handled_callback) {
std::move(handled_callback).Run(false);
} }
std::string CastBrowserTest::GetId() { std::string CastBrowserTest::GetId() {
......
...@@ -51,7 +51,8 @@ class CastBrowserTest : public content::BrowserTestBase, ...@@ -51,7 +51,8 @@ class CastBrowserTest : public content::BrowserTestBase,
void OnWindowDestroyed() override; void OnWindowDestroyed() override;
void OnVisibilityChange(VisibilityType visibility_type) override; void OnVisibilityChange(VisibilityType visibility_type) override;
bool CanHandleGesture(GestureType gesture_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::string GetId() override;
std::unique_ptr<CastWebViewFactory> web_view_factory_; 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