Chromium Code Reviews| Index: content/browser/android/event_forwarder_impl.cc |
| diff --git a/content/browser/android/event_forwarder_impl.cc b/content/browser/android/event_forwarder_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..11507a0f3c12a8ca5f83d8ef18bddc0fb18914e6 |
| --- /dev/null |
| +++ b/content/browser/android/event_forwarder_impl.cc |
| @@ -0,0 +1,123 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/android/event_forwarder_impl.h" |
| + |
| +#include "base/metrics/histogram_macros.h" |
| +#include "content/browser/renderer_host/render_widget_host_view_android.h" |
| +#include "content/browser/web_contents/web_contents_impl.h" |
| +#include "content/browser/web_contents/web_contents_view.h" |
| +#include "content/public/browser/interstitial_page.h" |
| +#include "jni/EventForwarderImpl_jni.h" |
| + |
| +using base::android::JavaParamRef; |
| +using ui::MotionEventAndroid; |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +void RecordToolTypeForActionDown(MotionEventAndroid& event) { |
| + MotionEventAndroid::Action action = event.GetAction(); |
| + if (action == MotionEventAndroid::ACTION_DOWN || |
| + action == MotionEventAndroid::ACTION_POINTER_DOWN || |
| + action == MotionEventAndroid::ACTION_BUTTON_PRESS) { |
| + UMA_HISTOGRAM_ENUMERATION("Event.AndroidActionDown.ToolType", |
| + event.GetToolType(0), |
| + MotionEventAndroid::TOOL_TYPE_LAST + 1); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +EventForwarderImpl::EventForwarderImpl(WebContentsImpl* web_contents) |
| + : web_contents_(web_contents) { |
| + dip_scale_ = GetViewAndroid()->GetDipScale(); |
| +} |
| + |
| +jboolean EventForwarderImpl::OnTouchEvent( |
| + JNIEnv* env, |
| + const JavaParamRef<jobject>& obj, |
| + const JavaParamRef<jobject>& motion_event, |
| + jlong time_ms, |
| + jint android_action, |
| + jint pointer_count, |
| + jint history_size, |
| + jint action_index, |
| + jfloat pos_x_0, |
| + jfloat pos_y_0, |
| + jfloat pos_x_1, |
| + jfloat pos_y_1, |
| + jint pointer_id_0, |
| + jint pointer_id_1, |
| + jfloat touch_major_0, |
| + jfloat touch_major_1, |
| + jfloat touch_minor_0, |
| + jfloat touch_minor_1, |
| + jfloat orientation_0, |
| + jfloat orientation_1, |
| + jfloat tilt_0, |
| + jfloat tilt_1, |
| + jfloat raw_pos_x, |
| + jfloat raw_pos_y, |
| + jint android_tool_type_0, |
| + jint android_tool_type_1, |
| + jint android_button_state, |
| + jint android_meta_state, |
| + jboolean is_touch_handle_event) { |
| + RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid(); |
| + // Avoid synthesizing a touch event if it cannot be forwarded. |
| + if (!rwhv) |
| + return false; |
| + |
| + MotionEventAndroid::Pointer pointer0( |
| + pointer_id_0, pos_x_0, pos_y_0, touch_major_0, touch_minor_0, |
| + orientation_0, tilt_0, android_tool_type_0); |
| + MotionEventAndroid::Pointer pointer1( |
| + pointer_id_1, pos_x_1, pos_y_1, touch_major_1, touch_minor_1, |
| + orientation_1, tilt_1, android_tool_type_1); |
| + MotionEventAndroid event(1.f / dip_scale_, env, motion_event, time_ms, |
| + android_action, pointer_count, history_size, |
| + action_index, android_button_state, |
| + android_meta_state, raw_pos_x - pos_x_0, |
| + raw_pos_y - pos_y_0, &pointer0, &pointer1); |
| + |
| + RecordToolTypeForActionDown(event); |
| + return is_touch_handle_event ? rwhv->OnTouchHandleEvent(event) |
|
boliu
2017/02/21 16:23:30
this isn't actually using the viewandroid tree to
Jinsuk Kim
2017/02/26 23:18:27
Done.
|
| + : rwhv->OnTouchEvent(event); |
| +} |
| + |
| +ui::ViewAndroid* EventForwarderImpl::GetViewAndroid() const { |
| + return web_contents_->GetView()->GetNativeView(); |
| +} |
| + |
| +RenderWidgetHostViewAndroid* |
| +EventForwarderImpl::GetRenderWidgetHostViewAndroid() const { |
| + RenderWidgetHostView* rwhv = nullptr; |
| + rwhv = web_contents_->GetRenderWidgetHostView(); |
| + if (web_contents_->ShowingInterstitialPage()) { |
| + rwhv = web_contents_->GetInterstitialPage() |
| + ->GetMainFrame() |
| + ->GetRenderViewHost() |
| + ->GetWidget() |
| + ->GetView(); |
| + } |
| + return static_cast<RenderWidgetHostViewAndroid*>(rwhv); |
| +} |
| + |
| +jlong Init(JNIEnv* env, |
| + const JavaParamRef<jobject>& obj, |
| + const JavaParamRef<jobject>& jweb_contents) { |
| + WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( |
| + WebContents::FromJavaWebContents(jweb_contents)); |
| + CHECK(web_contents) |
| + << "An EventFowarder should be created with a valid WebContents."; |
| + return reinterpret_cast<intptr_t>(new EventForwarderImpl(web_contents)); |
| +} |
| + |
| +bool RegisterEventForwarderImpl(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +} // namespace content |