 Chromium Code Reviews
 Chromium Code Reviews Issue 2548363007:
  Emulate Android joystick scroll with synthetic mouse wheel event  (Closed)
    
  
    Issue 2548363007:
  Emulate Android joystick scroll with synthetic mouse wheel event  (Closed) 
  | Index: content/browser/android/joystick_scroll_provider.cc | 
| diff --git a/content/browser/android/joystick_scroll_provider.cc b/content/browser/android/joystick_scroll_provider.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..399b1b93c029dffea30e33ce206f4830610957d9 | 
| --- /dev/null | 
| +++ b/content/browser/android/joystick_scroll_provider.cc | 
| @@ -0,0 +1,91 @@ | 
| +// Copyright 2016 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/joystick_scroll_provider.h" | 
| + | 
| +#include "base/supports_user_data.h" | 
| +#include "content/browser/renderer_host/input/web_input_event_builders_android.h" | 
| +#include "content/browser/renderer_host/render_widget_host_view_android.h" | 
| +#include "content/browser/web_contents/web_contents_impl.h" | 
| +#include "jni/JoystickScrollProvider_jni.h" | 
| + | 
| +using base::android::AttachCurrentThread; | 
| +using base::android::JavaRef; | 
| +using base::android::ScopedJavaLocalRef; | 
| + | 
| +namespace content { | 
| + | 
| +const void* const kJoystickScrollUserDataKey = &kJoystickScrollUserDataKey; | 
| + | 
| +// A helper class to attach JoystickScrollProvider to the WebContents. | 
| +class JoystickScrollProvider::UserData : public base::SupportsUserData::Data { | 
| + public: | 
| + explicit UserData(JoystickScrollProvider* rep) : rep_(rep) {} | 
| + | 
| + private: | 
| + std::unique_ptr<JoystickScrollProvider> rep_; | 
| + | 
| + DISALLOW_IMPLICIT_CONSTRUCTORS(UserData); | 
| +}; | 
| + | 
| +jlong Init(JNIEnv* env, | 
| + const base::android::JavaParamRef<jobject>& obj, | 
| + const base::android::JavaParamRef<jobject>& jweb_contents) { | 
| + WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | 
| + WebContents::FromJavaWebContents(jweb_contents)); | 
| + CHECK(web_contents) | 
| + << "A JoystickScrollProvider should be created with a valid WebContents."; | 
| + | 
| + JoystickScrollProvider* native_object = | 
| + new JoystickScrollProvider(env, obj, web_contents); | 
| + return reinterpret_cast<intptr_t>(native_object); | 
| +} | 
| + | 
| +JoystickScrollProvider::JoystickScrollProvider(JNIEnv* env, | 
| + const JavaRef<jobject>& obj, | 
| + WebContents* web_contents) | 
| + : java_ref_(env, obj), | 
| + web_contents_(static_cast<WebContentsImpl*>(web_contents)) { | 
| + DCHECK(!web_contents_->GetUserData(kJoystickScrollUserDataKey)); | 
| 
boliu
2016/12/07 06:06:43
move this DCHECK to Init
conceptually, this is to
 
Tima Vaisburd
2016/12/08 00:35:47
Done.
 | 
| + web_contents_->SetUserData(kJoystickScrollUserDataKey, new UserData(this)); | 
| +} | 
| + | 
| +JoystickScrollProvider::~JoystickScrollProvider() { | 
| + JNIEnv* env = AttachCurrentThread(); | 
| + ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); | 
| + java_ref_.reset(); | 
| + if (!j_obj.is_null()) { | 
| + Java_JoystickScrollProvider_onNativeObjectDestroyed( | 
| + env, j_obj, reinterpret_cast<intptr_t>(this)); | 
| + } | 
| +} | 
| + | 
| +void JoystickScrollProvider::ScrollBy( | 
| + JNIEnv* env, | 
| + const base::android::JavaParamRef<jobject>& obj, | 
| + jlong time_ms, | 
| + jfloat dx_dip, | 
| + jfloat dy_dip) { | 
| + if (!web_contents_) | 
| + return; | 
| + | 
| + RenderWidgetHostViewAndroid* rwhv = static_cast<RenderWidgetHostViewAndroid*>( | 
| + web_contents_->GetRenderWidgetHostView()); | 
| + if (!rwhv) | 
| + return; | 
| + | 
| + if (!dx_dip && !dy_dip) | 
| + return; | 
| + | 
| + blink::WebMouseWheelEvent event = WebMouseWheelEventBuilder::Build( | 
| + dx_dip, dy_dip, 1.0, time_ms / 1000.0, 0, 0); | 
| + | 
| + rwhv->SendMouseWheelEvent(event); | 
| +} | 
| + | 
| +bool RegisterJoystickScrollProvider(JNIEnv* env) { | 
| + return RegisterNativesImpl(env); | 
| +} | 
| + | 
| +} // namespace content |