| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/selection_popup_controller.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/android/scoped_java_ref.h" |
| 10 #include "content/browser/renderer_host/render_widget_host_view_android.h" |
| 11 #include "content/public/browser/web_contents.h" |
| 12 #include "jni/SelectionPopupController_jni.h" |
| 13 |
| 14 using base::android::AttachCurrentThread; |
| 15 using base::android::ConvertUTF8ToJavaString; |
| 16 using base::android::JavaParamRef; |
| 17 using base::android::ScopedJavaLocalRef; |
| 18 |
| 19 namespace content { |
| 20 |
| 21 void Init(JNIEnv* env, |
| 22 const JavaParamRef<jobject>& obj, |
| 23 const JavaParamRef<jobject>& jweb_contents) { |
| 24 WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents); |
| 25 DCHECK(web_contents); |
| 26 |
| 27 // Left unowned but will be destroyed when |WebContentsDestroyed| is called. |
| 28 new SelectionPopupController(env, obj, web_contents); |
| 29 } |
| 30 |
| 31 bool RegisterSelectionPopupController(JNIEnv* env) { |
| 32 return RegisterNativesImpl(env); |
| 33 } |
| 34 |
| 35 SelectionPopupController::SelectionPopupController( |
| 36 JNIEnv* env, |
| 37 const JavaParamRef<jobject>& obj, |
| 38 WebContents* web_contents) |
| 39 : RenderProcessConnector(web_contents) { |
| 40 java_obj_ = JavaObjectWeakGlobalRef(env, obj); |
| 41 } |
| 42 |
| 43 SelectionPopupController::~SelectionPopupController() { |
| 44 UpdateRenderProcessConnection(rwhva(), nullptr); |
| 45 } |
| 46 |
| 47 void SelectionPopupController::UpdateRenderProcessConnection( |
| 48 RenderWidgetHostViewAndroid* old_rwhva, |
| 49 RenderWidgetHostViewAndroid* new_rwhva) { |
| 50 if (old_rwhva) |
| 51 old_rwhva->set_selection_popup_controller(nullptr); |
| 52 if (new_rwhva) |
| 53 new_rwhva->set_selection_popup_controller(this); |
| 54 } |
| 55 |
| 56 void SelectionPopupController::OnSelectionEvent( |
| 57 ui::SelectionEventType event, |
| 58 const gfx::PointF& selection_anchor, |
| 59 const gfx::RectF& selection_rect) { |
| 60 JNIEnv* env = AttachCurrentThread(); |
| 61 ScopedJavaLocalRef<jobject> obj = java_obj_.get(env); |
| 62 if (obj.is_null()) |
| 63 return; |
| 64 |
| 65 Java_SelectionPopupController_onSelectionEvent( |
| 66 env, obj, event, selection_anchor.x(), selection_anchor.y(), |
| 67 selection_rect.x(), selection_rect.y(), selection_rect.right(), |
| 68 selection_rect.bottom()); |
| 69 } |
| 70 |
| 71 void SelectionPopupController::OnSelectionChanged(const std::string& text) { |
| 72 JNIEnv* env = AttachCurrentThread(); |
| 73 ScopedJavaLocalRef<jobject> obj = java_obj_.get(env); |
| 74 if (obj.is_null()) |
| 75 return; |
| 76 ScopedJavaLocalRef<jstring> jtext = ConvertUTF8ToJavaString(env, text); |
| 77 Java_SelectionPopupController_onSelectionChanged(env, obj, jtext); |
| 78 } |
| 79 |
| 80 } // namespace content |
| OLD | NEW |