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 reinterpret_cast<intptr_t>( | |
44 new ContextSelectionClient(env, obj, web_contents)); | |
45 } | |
46 | |
47 ContextSelectionClient::ContextSelectionClient( | |
48 JNIEnv* env, | |
49 const base::android::JavaRef<jobject>& obj, | |
50 WebContents* web_contents) | |
51 : java_ref_(env, obj), | |
52 web_contents_(web_contents), | |
53 weak_ptr_factory_(this) { | |
54 DVLOG(1) << __func__; | |
boliu
2017/03/20 20:27:26
can be you be more thorough about removing debug l
Tima Vaisburd
2017/03/21 02:07:01
Oops, and in several places.. My bad.
| |
55 | |
56 DCHECK(!web_contents_->GetUserData(kContextSelectionClientUDKey)); | |
57 web_contents_->SetUserData(kContextSelectionClientUDKey, new UserData(this)); | |
58 } | |
59 | |
60 ContextSelectionClient::~ContextSelectionClient() { | |
61 JNIEnv* env = base::android::AttachCurrentThread(); | |
62 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); | |
63 java_ref_.reset(); | |
64 if (!j_obj.is_null()) { | |
65 Java_ContextSelectionClient_onNativeSideDestroyed( | |
66 env, j_obj, reinterpret_cast<intptr_t>(this)); | |
67 } | |
68 } | |
69 | |
70 void ContextSelectionClient::RequestSurroundingText( | |
71 JNIEnv* env, | |
72 const JavaParamRef<jobject>& obj, | |
73 int callback_data) { | |
74 RenderFrameHost* focused_frame = web_contents_->GetFocusedFrame(); | |
75 if (!focused_frame) { | |
76 OnSurroundingTextReceived(callback_data, base::string16(), 0, 0); | |
77 return; | |
78 } | |
79 | |
80 constexpr int EXTRA_CHARS = 100; | |
81 focused_frame->RequestTextSurroundingSelection( | |
82 base::Bind(&ContextSelectionClient::OnSurroundingTextReceived, | |
83 weak_ptr_factory_.GetWeakPtr(), callback_data), | |
84 EXTRA_CHARS); | |
85 } | |
86 | |
87 void ContextSelectionClient::CancelAllRequests( | |
88 JNIEnv* env, | |
89 const base::android::JavaParamRef<jobject>& obj) { | |
90 weak_ptr_factory_.InvalidateWeakPtrs(); | |
91 } | |
92 | |
93 void ContextSelectionClient::OnSurroundingTextReceived( | |
94 int callback_data, | |
95 const base::string16& text, | |
96 int start, | |
97 int end) { | |
98 JNIEnv* env = AttachCurrentThread(); | |
99 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
100 if (!obj.is_null()) { | |
101 ScopedJavaLocalRef<jstring> j_text = ConvertUTF16ToJavaString(env, text); | |
102 Java_ContextSelectionClient_onSurroundingTextReceived( | |
103 env, obj, callback_data, j_text, start, end); | |
104 } | |
105 } | |
106 | |
107 bool RegisterContextSelectionClient(JNIEnv* env) { | |
108 return RegisterNativesImpl(env); | |
109 } | |
110 | |
111 } // namespace content | |
OLD | NEW |