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/context_selection_client.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/bind.h" |
| 10 #include "base/supports_user_data.h" |
| 11 #include "content/public/browser/render_frame_host.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "jni/ContextSelectionClient_jni.h" |
| 14 |
| 15 using base::android::AttachCurrentThread; |
| 16 using base::android::ConvertUTF16ToJavaString; |
| 17 using base::android::JavaParamRef; |
| 18 using base::android::ScopedJavaLocalRef; |
| 19 |
| 20 namespace content { |
| 21 |
| 22 namespace { |
| 23 const void* const kContextSelectionClientUDKey = &kContextSelectionClientUDKey; |
| 24 } |
| 25 |
| 26 // This class deletes ContextSelectionClient when WebContents is destroyed. |
| 27 class ContextSelectionClient::UserData : public base::SupportsUserData::Data { |
| 28 public: |
| 29 explicit UserData(ContextSelectionClient* client) : client_(client) {} |
| 30 |
| 31 private: |
| 32 struct Deleter { |
| 33 void operator()(ContextSelectionClient* client) { delete client; } |
| 34 }; |
| 35 std::unique_ptr<ContextSelectionClient, Deleter> client_; |
| 36 |
| 37 DISALLOW_IMPLICIT_CONSTRUCTORS(UserData); |
| 38 }; |
| 39 |
| 40 jlong Init(JNIEnv* env, |
| 41 const JavaParamRef<jobject>& obj, |
| 42 const JavaParamRef<jobject>& jweb_contents) { |
| 43 WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents); |
| 44 CHECK(web_contents) |
| 45 << "A ContextSelectionClient should be created with a valid WebContents."; |
| 46 |
| 47 return reinterpret_cast<intptr_t>( |
| 48 new ContextSelectionClient(env, obj, web_contents)); |
| 49 } |
| 50 |
| 51 ContextSelectionClient::ContextSelectionClient( |
| 52 JNIEnv* env, |
| 53 const base::android::JavaRef<jobject>& obj, |
| 54 WebContents* web_contents) |
| 55 : java_ref_(env, obj), |
| 56 web_contents_(web_contents), |
| 57 weak_ptr_factory_(this) { |
| 58 DCHECK(!web_contents_->GetUserData(kContextSelectionClientUDKey)); |
| 59 web_contents_->SetUserData(kContextSelectionClientUDKey, new UserData(this)); |
| 60 } |
| 61 |
| 62 ContextSelectionClient::~ContextSelectionClient() { |
| 63 JNIEnv* env = base::android::AttachCurrentThread(); |
| 64 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); |
| 65 if (!j_obj.is_null()) { |
| 66 Java_ContextSelectionClient_onNativeSideDestroyed( |
| 67 env, j_obj, reinterpret_cast<intptr_t>(this)); |
| 68 } |
| 69 } |
| 70 |
| 71 void ContextSelectionClient::RequestSurroundingText( |
| 72 JNIEnv* env, |
| 73 const JavaParamRef<jobject>& obj, |
| 74 int num_extra_characters, |
| 75 int callback_data) { |
| 76 RenderFrameHost* focused_frame = web_contents_->GetFocusedFrame(); |
| 77 if (!focused_frame) { |
| 78 OnSurroundingTextReceived(callback_data, base::string16(), 0, 0); |
| 79 return; |
| 80 } |
| 81 |
| 82 focused_frame->RequestTextSurroundingSelection( |
| 83 base::Bind(&ContextSelectionClient::OnSurroundingTextReceived, |
| 84 weak_ptr_factory_.GetWeakPtr(), callback_data), |
| 85 num_extra_characters); |
| 86 } |
| 87 |
| 88 void ContextSelectionClient::CancelAllRequests( |
| 89 JNIEnv* env, |
| 90 const base::android::JavaParamRef<jobject>& obj) { |
| 91 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 92 } |
| 93 |
| 94 void ContextSelectionClient::OnSurroundingTextReceived( |
| 95 int callback_data, |
| 96 const base::string16& text, |
| 97 int start, |
| 98 int end) { |
| 99 JNIEnv* env = AttachCurrentThread(); |
| 100 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 101 if (!obj.is_null()) { |
| 102 ScopedJavaLocalRef<jstring> j_text = ConvertUTF16ToJavaString(env, text); |
| 103 Java_ContextSelectionClient_onSurroundingTextReceived( |
| 104 env, obj, callback_data, j_text, start, end); |
| 105 } |
| 106 } |
| 107 |
| 108 bool RegisterContextSelectionClient(JNIEnv* env) { |
| 109 return RegisterNativesImpl(env); |
| 110 } |
| 111 |
| 112 } // namespace content |
OLD | NEW |