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

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

Issue 2740103006: Implement SmartText selection. (Closed)
Patch Set: Made ~ContextSelectionClient() public and inlined SelectionPopupControler.unhideActionMode() Created 3 years, 8 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
« no previous file with comments | « content/browser/android/context_selection_client.h ('k') | content/public/android/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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 // This class deletes ContextSelectionClient when WebContents is destroyed.
26 class UserData : public base::SupportsUserData::Data {
27 public:
28 explicit UserData(ContextSelectionClient* client) : client_(client) {}
29
30 private:
31 std::unique_ptr<ContextSelectionClient> client_;
32
33 DISALLOW_IMPLICIT_CONSTRUCTORS(UserData);
34 };
35 }
36
37 jlong Init(JNIEnv* env,
38 const JavaParamRef<jobject>& obj,
39 const JavaParamRef<jobject>& jweb_contents) {
40 WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents);
41 CHECK(web_contents)
42 << "A ContextSelectionClient should be created with a valid WebContents.";
43
44 return reinterpret_cast<intptr_t>(
45 new ContextSelectionClient(env, obj, web_contents));
46 }
47
48 ContextSelectionClient::ContextSelectionClient(
49 JNIEnv* env,
50 const base::android::JavaRef<jobject>& obj,
51 WebContents* web_contents)
52 : java_ref_(env, obj),
53 web_contents_(web_contents),
54 weak_ptr_factory_(this) {
55 DCHECK(!web_contents_->GetUserData(kContextSelectionClientUDKey));
56 web_contents_->SetUserData(kContextSelectionClientUDKey, new UserData(this));
57 }
58
59 ContextSelectionClient::~ContextSelectionClient() {
60 JNIEnv* env = base::android::AttachCurrentThread();
61 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
62 if (!j_obj.is_null()) {
63 Java_ContextSelectionClient_onNativeSideDestroyed(
64 env, j_obj, reinterpret_cast<intptr_t>(this));
65 }
66 }
67
68 void ContextSelectionClient::RequestSurroundingText(
69 JNIEnv* env,
70 const JavaParamRef<jobject>& obj,
71 int num_extra_characters,
72 int callback_data) {
73 RenderFrameHost* focused_frame = web_contents_->GetFocusedFrame();
74 if (!focused_frame) {
75 OnSurroundingTextReceived(callback_data, base::string16(), 0, 0);
76 return;
77 }
78
79 focused_frame->RequestTextSurroundingSelection(
80 base::Bind(&ContextSelectionClient::OnSurroundingTextReceived,
81 weak_ptr_factory_.GetWeakPtr(), callback_data),
82 num_extra_characters);
83 }
84
85 void ContextSelectionClient::CancelAllRequests(
86 JNIEnv* env,
87 const base::android::JavaParamRef<jobject>& obj) {
88 weak_ptr_factory_.InvalidateWeakPtrs();
89 }
90
91 void ContextSelectionClient::OnSurroundingTextReceived(
92 int callback_data,
93 const base::string16& text,
94 int start,
95 int end) {
96 JNIEnv* env = AttachCurrentThread();
97 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
98 if (!obj.is_null()) {
99 ScopedJavaLocalRef<jstring> j_text = ConvertUTF16ToJavaString(env, text);
100 Java_ContextSelectionClient_onSurroundingTextReceived(
101 env, obj, callback_data, j_text, start, end);
102 }
103 }
104
105 bool RegisterContextSelectionClient(JNIEnv* env) {
106 return RegisterNativesImpl(env);
107 }
108
109 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/android/context_selection_client.h ('k') | content/public/android/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698