Chromium Code Reviews| Index: content/browser/android/context_selection_client.cc |
| diff --git a/content/browser/android/context_selection_client.cc b/content/browser/android/context_selection_client.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d33e29ae5ea4a2ca487e2631a5108d1e1c623092 |
| --- /dev/null |
| +++ b/content/browser/android/context_selection_client.cc |
| @@ -0,0 +1,113 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/android/context_selection_client.h" |
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "content/public/browser/render_frame_host.h" |
| +#include "jni/ContextSelectionClient_jni.h" |
| + |
| +using base::android::AttachCurrentThread; |
| +using base::android::ConvertUTF16ToJavaString; |
| +using base::android::JavaParamRef; |
| +using base::android::ScopedJavaLocalRef; |
| + |
| +namespace content { |
| + |
| +namespace { |
| +const void* const kContextSelectionClientUDKey = &kContextSelectionClientUDKey; |
| +} |
| + |
| +// This class deletes ContextSelectionClient when WebContents is destroyed. |
| +class ContextSelectionClient::UserData : public base::SupportsUserData::Data { |
| + public: |
| + explicit UserData(ContextSelectionClient* client) : client_(client) {} |
| + |
| + private: |
| + std::unique_ptr<ContextSelectionClient> client_; |
| + |
| + DISALLOW_IMPLICIT_CONSTRUCTORS(UserData); |
| +}; |
| + |
| +jlong Init(JNIEnv* env, |
| + const JavaParamRef<jobject>& obj, |
| + const JavaParamRef<jobject>& jweb_contents) { |
| + WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents); |
| + CHECK(web_contents) |
| + << "A ContextSelectionClient should be created with a valid WebContents."; |
| + |
| + return (intptr_t)(new ContextSelectionClient(env, obj, web_contents)); |
|
boliu
2017/03/11 00:37:51
reinterpret_cast, c-style casts are banned by styl
Tima Vaisburd
2017/03/20 05:06:04
Done.
|
| +} |
| + |
| +ContextSelectionClient::ContextSelectionClient( |
| + JNIEnv* env, |
| + const base::android::JavaRef<jobject>& obj, |
| + WebContents* web_contents) |
| + : java_ref_(env, obj), web_contents_(web_contents) { |
| + DVLOG(1) << __func__; |
|
boliu
2017/03/11 00:37:51
.
Tima Vaisburd
2017/03/20 05:06:04
Removed all DVLOGs in this file.
|
| + |
| + web_contents_->SetUserData(kContextSelectionClientUDKey, new UserData(this)); |
|
boliu
2017/03/11 00:37:51
dcheck this isn't overwriting anything?
Tima Vaisburd
2017/03/20 05:06:04
Done.
|
| +} |
| + |
| +ContextSelectionClient::~ContextSelectionClient() { |
| + DVLOG(1) << __func__; |
| + JNIEnv* env = base::android::AttachCurrentThread(); |
| + ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); |
| + java_ref_.reset(); |
| + if (!j_obj.is_null()) { |
| + Java_ContextSelectionClient_onNativeSideDestroyed( |
| + env, j_obj, reinterpret_cast<intptr_t>(this)); |
| + } |
| +} |
| + |
| +void ContextSelectionClient::RequestSurroundingText( |
| + JNIEnv* env, |
| + const JavaParamRef<jobject>& obj, |
| + int callback_data) { |
| + DVLOG(1) << __func__; |
| + |
| + RenderFrameHost* focused_frame = web_contents_->GetFocusedFrame(); |
| + if (!focused_frame) { |
| + OnSurroundingTextReceived(callback_data, base::string16(), 0, 0); |
| + return; |
| + } |
| + |
| + const int EXTRA_CHARS = 100; |
|
boliu
2017/03/11 00:37:51
constexpr
Tima Vaisburd
2017/03/20 05:06:04
Done.
|
| + focused_frame->RequestTextSurroundingSelection( |
| + base::Bind(&ContextSelectionClient::OnSurroundingTextReceived, |
| + AsWeakPtr(), callback_data), |
| + EXTRA_CHARS); |
| +} |
| + |
| +void ContextSelectionClient::CancelAllRequests( |
| + JNIEnv* env, |
| + const base::android::JavaParamRef<jobject>& obj) { |
| + DVLOG(1) << __func__; |
| + NOTIMPLEMENTED(); |
|
boliu
2017/03/11 00:37:51
why unimplemented?
Tima Vaisburd
2017/03/20 05:06:04
Because I did not know how. Now with weak_ptr_fact
|
| +} |
| + |
| +void ContextSelectionClient::OnSurroundingTextReceived( |
| + int callback_data, |
| + const base::string16& text, |
| + int start, |
| + int end) { |
| + DVLOG(1) << __func__; |
| + |
| + JNIEnv* env = AttachCurrentThread(); |
| + ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| + if (!obj.is_null()) { |
| + ScopedJavaLocalRef<jstring> j_text = ConvertUTF16ToJavaString(env, text); |
| + Java_ContextSelectionClient_onSurroundingTextReceived( |
| + env, obj, callback_data, j_text, start, end); |
| + } |
| +} |
| + |
| +bool RegisterContextSelectionClient(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +} // namespace content |