| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/android/joystick_scroll_provider.h" | |
| 6 | |
| 7 #include "base/supports_user_data.h" | |
| 8 #include "content/browser/renderer_host/input/web_input_event_builders_android.h
" | |
| 9 #include "content/browser/renderer_host/render_widget_host_view_android.h" | |
| 10 #include "content/browser/web_contents/web_contents_impl.h" | |
| 11 #include "jni/JoystickScrollProvider_jni.h" | |
| 12 | |
| 13 using base::android::AttachCurrentThread; | |
| 14 using base::android::JavaRef; | |
| 15 using base::android::ScopedJavaLocalRef; | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 const void* const kJoystickScrollUserDataKey = &kJoystickScrollUserDataKey; | |
| 20 | |
| 21 namespace { | |
| 22 const double MILLISECONDS_IN_SECOND = 1000.0; | |
| 23 } | |
| 24 | |
| 25 // A helper class to attach JoystickScrollProvider to the WebContents. | |
| 26 class JoystickScrollProvider::UserData : public base::SupportsUserData::Data { | |
| 27 public: | |
| 28 explicit UserData(JoystickScrollProvider* rep) : rep_(rep) {} | |
| 29 | |
| 30 private: | |
| 31 std::unique_ptr<JoystickScrollProvider> rep_; | |
| 32 | |
| 33 DISALLOW_IMPLICIT_CONSTRUCTORS(UserData); | |
| 34 }; | |
| 35 | |
| 36 jlong Init(JNIEnv* env, | |
| 37 const base::android::JavaParamRef<jobject>& obj, | |
| 38 const base::android::JavaParamRef<jobject>& jweb_contents) { | |
| 39 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | |
| 40 WebContents::FromJavaWebContents(jweb_contents)); | |
| 41 CHECK(web_contents) | |
| 42 << "A JoystickScrollProvider should be created with a valid WebContents."; | |
| 43 | |
| 44 DCHECK(!web_contents->GetUserData(kJoystickScrollUserDataKey)) | |
| 45 << "WebContents already has JoystickScrollProvider attached"; | |
| 46 | |
| 47 JoystickScrollProvider* native_object = | |
| 48 new JoystickScrollProvider(env, obj, web_contents); | |
| 49 return reinterpret_cast<intptr_t>(native_object); | |
| 50 } | |
| 51 | |
| 52 JoystickScrollProvider::JoystickScrollProvider(JNIEnv* env, | |
| 53 const JavaRef<jobject>& obj, | |
| 54 WebContentsImpl* web_contents) | |
| 55 : java_ref_(env, obj), web_contents_(web_contents) { | |
| 56 web_contents_->SetUserData(kJoystickScrollUserDataKey, new UserData(this)); | |
| 57 } | |
| 58 | |
| 59 JoystickScrollProvider::~JoystickScrollProvider() { | |
| 60 JNIEnv* env = AttachCurrentThread(); | |
| 61 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); | |
| 62 java_ref_.reset(); | |
| 63 if (!j_obj.is_null()) { | |
| 64 Java_JoystickScrollProvider_onNativeObjectDestroyed( | |
| 65 env, j_obj, reinterpret_cast<intptr_t>(this)); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void JoystickScrollProvider::ScrollBy( | |
| 70 JNIEnv* env, | |
| 71 const base::android::JavaParamRef<jobject>& obj, | |
| 72 jlong time_ms, | |
| 73 jfloat dx_dip, | |
| 74 jfloat dy_dip) { | |
| 75 if (!web_contents_) | |
| 76 return; | |
| 77 | |
| 78 RenderWidgetHostViewAndroid* rwhv = static_cast<RenderWidgetHostViewAndroid*>( | |
| 79 web_contents_->GetRenderWidgetHostView()); | |
| 80 if (!rwhv) | |
| 81 return; | |
| 82 | |
| 83 if (!dx_dip && !dy_dip) | |
| 84 return; | |
| 85 | |
| 86 // Revert the direction for syntheric mouse wheel event. | |
| 87 blink::WebMouseWheelEvent event = WebMouseWheelEventBuilder::Build( | |
| 88 -dx_dip, -dy_dip, 1.0, time_ms / MILLISECONDS_IN_SECOND, 0, 0); | |
| 89 | |
| 90 rwhv->SendMouseWheelEvent(event); | |
| 91 } | |
| 92 | |
| 93 bool RegisterJoystickScrollProvider(JNIEnv* env) { | |
| 94 return RegisterNativesImpl(env); | |
| 95 } | |
| 96 | |
| 97 } // namespace content | |
| OLD | NEW |