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 "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>( | |
Ted C
2017/03/29 17:43:50
since this is using UserData, what is the expected
Tima Vaisburd
2017/03/29 22:56:36
I wanted to allow only one, and I thought the DCHE
Ted C
2017/03/30 00:12:39
The API still seems quite weird. Having the java
Tima Vaisburd
2017/03/30 01:03:13
Acknowledged.
| |
43 new ContextSelectionClient(env, obj, web_contents)); | |
44 } | |
45 | |
46 ContextSelectionClient::ContextSelectionClient( | |
Ted C
2017/03/29 17:43:50
actually, this is only created via init, so should
Tima Vaisburd
2017/03/29 22:56:36
Init() is not a class member, it is a stand-alone
Ted C
2017/03/30 00:12:39
Acknowledged.
| |
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(); | |
Ted C
2017/03/29 17:43:50
do you need to do this? wouldn't this happen as p
Tima Vaisburd
2017/03/29 22:56:36
Removed.
| |
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; | |
Ted C
2017/03/29 17:43:50
should this be configurable w/ a upper bounds limi
Tima Vaisburd
2017/03/29 22:56:36
I changed to pass this value with every call to Re
| |
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 | |
OLD | NEW |