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/logging.h" | |
11 #include "content/public/browser/render_frame_host.h" | |
12 #include "jni/ContextSelectionClient_jni.h" | |
13 | |
14 using base::android::AttachCurrentThread; | |
15 using base::android::ConvertUTF16ToJavaString; | |
16 using base::android::JavaParamRef; | |
17 using base::android::ScopedJavaLocalRef; | |
18 | |
19 namespace content { | |
20 | |
21 namespace { | |
22 const void* const kContextSelectionClientUDKey = &kContextSelectionClientUDKey; | |
23 } | |
24 | |
25 // This class deletes ContextSelectionClient when WebContents is destroyed. | |
26 class ContextSelectionClient::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 jlong Init(JNIEnv* env, | |
37 const JavaParamRef<jobject>& obj, | |
38 const JavaParamRef<jobject>& jweb_contents) { | |
39 WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents); | |
40 CHECK(web_contents) | |
41 << "A ContextSelectionClient should be created with a valid WebContents."; | |
42 | |
43 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.
| |
44 } | |
45 | |
46 ContextSelectionClient::ContextSelectionClient( | |
47 JNIEnv* env, | |
48 const base::android::JavaRef<jobject>& obj, | |
49 WebContents* web_contents) | |
50 : java_ref_(env, obj), web_contents_(web_contents) { | |
51 DVLOG(1) << __func__; | |
boliu
2017/03/11 00:37:51
.
Tima Vaisburd
2017/03/20 05:06:04
Removed all DVLOGs in this file.
| |
52 | |
53 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.
| |
54 } | |
55 | |
56 ContextSelectionClient::~ContextSelectionClient() { | |
57 DVLOG(1) << __func__; | |
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 DVLOG(1) << __func__; | |
72 | |
73 RenderFrameHost* focused_frame = web_contents_->GetFocusedFrame(); | |
74 if (!focused_frame) { | |
75 OnSurroundingTextReceived(callback_data, base::string16(), 0, 0); | |
76 return; | |
77 } | |
78 | |
79 const int EXTRA_CHARS = 100; | |
boliu
2017/03/11 00:37:51
constexpr
Tima Vaisburd
2017/03/20 05:06:04
Done.
| |
80 focused_frame->RequestTextSurroundingSelection( | |
81 base::Bind(&ContextSelectionClient::OnSurroundingTextReceived, | |
82 AsWeakPtr(), callback_data), | |
83 EXTRA_CHARS); | |
84 } | |
85 | |
86 void ContextSelectionClient::CancelAllRequests( | |
87 JNIEnv* env, | |
88 const base::android::JavaParamRef<jobject>& obj) { | |
89 DVLOG(1) << __func__; | |
90 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
| |
91 } | |
92 | |
93 void ContextSelectionClient::OnSurroundingTextReceived( | |
94 int callback_data, | |
95 const base::string16& text, | |
96 int start, | |
97 int end) { | |
98 DVLOG(1) << __func__; | |
99 | |
100 JNIEnv* env = AttachCurrentThread(); | |
101 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
102 if (!obj.is_null()) { | |
103 ScopedJavaLocalRef<jstring> j_text = ConvertUTF16ToJavaString(env, text); | |
104 Java_ContextSelectionClient_onSurroundingTextReceived( | |
105 env, obj, callback_data, j_text, start, end); | |
106 } | |
107 } | |
108 | |
109 bool RegisterContextSelectionClient(JNIEnv* env) { | |
110 return RegisterNativesImpl(env); | |
111 } | |
112 | |
113 } // namespace content | |
OLD | NEW |