Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(790)

Side by Side Diff: content/browser/android/context_selection_client.cc

Issue 2740103006: Implement SmartText selection. (Closed)
Patch Set: Do not invalidate the menu and always recreate it; use Android resource id for Assist Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "content/public/browser/render_frame_host.h"
11 #include "jni/ContextSelectionClient_jni.h"
12
13 using base::android::AttachCurrentThread;
14 using base::android::ConvertUTF16ToJavaString;
15 using base::android::JavaParamRef;
16 using base::android::ScopedJavaLocalRef;
17
18 namespace content {
19
20 namespace {
21 const void* const kContextSelectionClientUDKey = &kContextSelectionClientUDKey;
22 }
23
24 // This class deletes ContextSelectionClient when WebContents is destroyed.
25 class ContextSelectionClient::UserData : public base::SupportsUserData::Data {
26 public:
27 explicit UserData(ContextSelectionClient* client) : client_(client) {}
28
29 private:
30 std::unique_ptr<ContextSelectionClient> client_;
31
32 DISALLOW_IMPLICIT_CONSTRUCTORS(UserData);
33 };
34
35 jlong Init(JNIEnv* env,
36 const JavaParamRef<jobject>& obj,
37 const JavaParamRef<jobject>& jweb_contents) {
38 WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents);
39 CHECK(web_contents)
40 << "A ContextSelectionClient should be created with a valid WebContents.";
41
42 return reinterpret_cast<intptr_t>(
43 new ContextSelectionClient(env, obj, web_contents));
44 }
45
46 ContextSelectionClient::ContextSelectionClient(
47 JNIEnv* env,
48 const base::android::JavaRef<jobject>& obj,
49 WebContents* web_contents)
50 : java_ref_(env, obj),
51 web_contents_(web_contents),
52 weak_ptr_factory_(this) {
53 DCHECK(!web_contents_->GetUserData(kContextSelectionClientUDKey));
54 web_contents_->SetUserData(kContextSelectionClientUDKey, new UserData(this));
55 }
56
57 ContextSelectionClient::~ContextSelectionClient() {
58 JNIEnv* env = base::android::AttachCurrentThread();
59 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
60 java_ref_.reset();
61 if (!j_obj.is_null()) {
62 Java_ContextSelectionClient_onNativeSideDestroyed(
63 env, j_obj, reinterpret_cast<intptr_t>(this));
64 }
65 }
66
67 void ContextSelectionClient::RequestSurroundingText(
68 JNIEnv* env,
69 const JavaParamRef<jobject>& obj,
70 int callback_data) {
71 RenderFrameHost* focused_frame = web_contents_->GetFocusedFrame();
72 if (!focused_frame) {
73 OnSurroundingTextReceived(callback_data, base::string16(), 0, 0);
74 return;
75 }
76
77 constexpr int EXTRA_CHARS = 100;
78 focused_frame->RequestTextSurroundingSelection(
79 base::Bind(&ContextSelectionClient::OnSurroundingTextReceived,
80 weak_ptr_factory_.GetWeakPtr(), callback_data),
81 EXTRA_CHARS);
82 }
83
84 void ContextSelectionClient::CancelAllRequests(
85 JNIEnv* env,
86 const base::android::JavaParamRef<jobject>& obj) {
87 weak_ptr_factory_.InvalidateWeakPtrs();
88 }
89
90 void ContextSelectionClient::OnSurroundingTextReceived(
91 int callback_data,
92 const base::string16& text,
93 int start,
94 int end) {
95 JNIEnv* env = AttachCurrentThread();
96 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
97 if (!obj.is_null()) {
98 ScopedJavaLocalRef<jstring> j_text = ConvertUTF16ToJavaString(env, text);
99 Java_ContextSelectionClient_onSurroundingTextReceived(
100 env, obj, callback_data, j_text, start, end);
101 }
102 }
103
104 bool RegisterContextSelectionClient(JNIEnv* env) {
105 return RegisterNativesImpl(env);
106 }
107
108 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698