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 JoystickScrollProvider* native_object = | |
| 41 new JoystickScrollProvider(env, obj, web_contents); | |
| 42 return reinterpret_cast<intptr_t>(native_object); | |
| 43 } | |
| 44 | |
| 45 JoystickScrollProvider::JoystickScrollProvider(JNIEnv* env, | |
| 46 const JavaRef<jobject>& obj, | |
| 47 WebContents* web_contents) | |
| 48 : java_ref_(env, obj), | |
| 49 web_contents_(static_cast<WebContentsImpl*>(web_contents)) { | |
| 50 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.
| |
| 51 web_contents_->SetUserData(kJoystickScrollUserDataKey, new UserData(this)); | |
| 52 } | |
| 53 | |
| 54 JoystickScrollProvider::~JoystickScrollProvider() { | |
| 55 JNIEnv* env = AttachCurrentThread(); | |
| 56 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); | |
| 57 java_ref_.reset(); | |
| 58 if (!j_obj.is_null()) { | |
| 59 Java_JoystickScrollProvider_onNativeObjectDestroyed( | |
| 60 env, j_obj, reinterpret_cast<intptr_t>(this)); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void JoystickScrollProvider::ScrollBy( | |
| 65 JNIEnv* env, | |
| 66 const base::android::JavaParamRef<jobject>& obj, | |
| 67 jlong time_ms, | |
| 68 jfloat dx_dip, | |
| 69 jfloat dy_dip) { | |
| 70 if (!web_contents_) | |
| 71 return; | |
| 72 | |
| 73 RenderWidgetHostViewAndroid* rwhv = static_cast<RenderWidgetHostViewAndroid*>( | |
| 74 web_contents_->GetRenderWidgetHostView()); | |
| 75 if (!rwhv) | |
| 76 return; | |
| 77 | |
| 78 if (!dx_dip && !dy_dip) | |
| 79 return; | |
| 80 | |
| 81 blink::WebMouseWheelEvent event = WebMouseWheelEventBuilder::Build( | |
| 82 dx_dip, dy_dip, 1.0, time_ms / 1000.0, 0, 0); | |
| 83 | |
| 84 rwhv->SendMouseWheelEvent(event); | |
| 85 } | |
| 86 | |
| 87 bool RegisterJoystickScrollProvider(JNIEnv* env) { | |
| 88 return RegisterNativesImpl(env); | |
| 89 } | |
| 90 | |
| 91 } // namespace content | |
| OLD | NEW |