Chromium Code Reviews| 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 // A helper class to attach JoystickScrollProvider to the WebContents. | |
| 22 class JoystickScrollProvider::UserData : public base::SupportsUserData::Data { | |
| 23 public: | |
| 24 explicit UserData(JoystickScrollProvider* rep) : rep_(rep) {} | |
| 25 | |
| 26 private: | |
| 27 std::unique_ptr<JoystickScrollProvider> rep_; | |
| 28 | |
| 29 DISALLOW_IMPLICIT_CONSTRUCTORS(UserData); | |
| 30 }; | |
| 31 | |
| 32 jlong Init(JNIEnv* env, | |
| 33 const base::android::JavaParamRef<jobject>& obj, | |
| 34 const base::android::JavaParamRef<jobject>& jweb_contents) { | |
| 35 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | |
| 36 WebContents::FromJavaWebContents(jweb_contents)); | |
| 37 CHECK(web_contents) | |
| 38 << "A JoystickScrollProvider should be created with a valid WebContents."; | |
| 39 | |
| 40 DCHECK(!web_contents->GetUserData(kJoystickScrollUserDataKey)) | |
| 41 << "WebContents already has JoystickScrollProvider attached"; | |
| 42 | |
| 43 JoystickScrollProvider* native_object = | |
| 44 new JoystickScrollProvider(env, obj, web_contents); | |
| 45 return reinterpret_cast<intptr_t>(native_object); | |
| 46 } | |
| 47 | |
| 48 JoystickScrollProvider::JoystickScrollProvider(JNIEnv* env, | |
| 49 const JavaRef<jobject>& obj, | |
| 50 WebContentsImpl* web_contents) | |
| 51 : java_ref_(env, obj), web_contents_(web_contents) { | |
| 52 web_contents_->SetUserData(kJoystickScrollUserDataKey, new UserData(this)); | |
| 53 } | |
| 54 | |
| 55 JoystickScrollProvider::~JoystickScrollProvider() { | |
| 56 JNIEnv* env = AttachCurrentThread(); | |
| 57 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); | |
| 58 java_ref_.reset(); | |
| 59 if (!j_obj.is_null()) { | |
| 60 Java_JoystickScrollProvider_onNativeObjectDestroyed( | |
| 61 env, j_obj, reinterpret_cast<intptr_t>(this)); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 void JoystickScrollProvider::ScrollBy( | |
| 66 JNIEnv* env, | |
| 67 const base::android::JavaParamRef<jobject>& obj, | |
| 68 jlong time_ms, | |
| 69 jfloat dx_dip, | |
| 70 jfloat dy_dip) { | |
| 71 if (!web_contents_) | |
| 72 return; | |
| 73 | |
| 74 RenderWidgetHostViewAndroid* rwhv = static_cast<RenderWidgetHostViewAndroid*>( | |
| 75 web_contents_->GetRenderWidgetHostView()); | |
| 76 if (!rwhv) | |
| 77 return; | |
| 78 | |
| 79 if (!dx_dip && !dy_dip) | |
| 80 return; | |
| 81 | |
| 82 blink::WebMouseWheelEvent event = WebMouseWheelEventBuilder::Build( | |
| 83 dx_dip, dy_dip, 1.0, time_ms / 1000.0, 0, 0); | |
|
boliu
2016/12/08 06:03:09
too much implicit casting here, long/float/double,
Tima Vaisburd
2016/12/08 20:32:19
Declared MILLISECONDS_IN_SECOND for 1000.0
| |
| 84 | |
| 85 rwhv->SendMouseWheelEvent(event); | |
| 86 } | |
| 87 | |
| 88 bool RegisterJoystickScrollProvider(JNIEnv* env) { | |
| 89 return RegisterNativesImpl(env); | |
| 90 } | |
| 91 | |
| 92 } // namespace content | |
| OLD | NEW |