OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "chrome/browser/android/contextualsearch/search_action.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/android/jni_string.h" | |
10 #include "base/bind.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "chrome/browser/android/contextualsearch/contextual_search_context.h" | |
14 #include "content/public/browser/render_frame_host.h" | |
15 #include "content/public/browser/web_contents.h" | |
16 #include "jni/SearchAction_jni.h" | |
17 #include "url/gurl.h" | |
18 | |
19 using base::android::ConvertUTF8ToJavaString; | |
20 using base::android::JavaParamRef; | |
21 using base::android::ScopedJavaGlobalRef; | |
22 using content::WebContents; | |
23 | |
24 namespace { | |
25 // Context length is set to 1.5K characters for historical reasons: | |
26 // This provided the largest context that would fit in a GET request along with | |
27 // our other required parameters." | |
28 const int kSurroundingTextSize = 1536; | |
29 } | |
30 | |
31 SearchAction::SearchAction(JNIEnv* env, jobject obj) { | |
32 java_object_.Reset(env, obj); | |
33 } | |
34 | |
35 SearchAction::~SearchAction() { | |
36 // The Java object can be null in tests only, by calling the default | |
37 // constructor. | |
38 if (!java_object_.is_null()) { | |
39 JNIEnv* env = base::android::AttachCurrentThread(); | |
40 Java_SearchAction_clearNativePointer(env, java_object_.obj()); | |
41 } | |
42 } | |
43 | |
44 void SearchAction::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
45 delete this; | |
46 } | |
47 | |
48 void SearchAction::RequestSurroundingText( | |
49 JNIEnv* env, | |
50 const JavaParamRef<jobject>& obj, | |
51 const JavaParamRef<jobject>& j_web_contents) { | |
52 WebContents* web_contents = WebContents::FromJavaWebContents(j_web_contents); | |
53 DCHECK(web_contents); | |
54 | |
55 GURL url(web_contents->GetURL()); | |
56 std::string encoding(web_contents->GetEncoding()); | |
57 search_context_.reset(new ContextualSearchContext(true, url, encoding)); | |
58 | |
59 content::RenderFrameHost* focused_frame = web_contents->GetFocusedFrame(); | |
60 if (!focused_frame) { | |
61 OnSurroundingTextResponse(base::string16(), 0, 0); | |
62 } else { | |
63 focused_frame->RequestTextSurroundingSelection( | |
64 base::Bind(&SearchAction::OnSurroundingTextResponse, AsWeakPtr()), | |
65 kSurroundingTextSize); | |
66 } | |
67 } | |
68 | |
69 void SearchAction::OnSurroundingTextResponse( | |
70 const base::string16& surrounding_text, | |
71 int focus_start, | |
72 int focus_end) { | |
73 // Record the context, which can be accessed later on demand. | |
74 search_context_->surrounding_text = surrounding_text; | |
75 search_context_->start_offset = focus_start; | |
76 search_context_->end_offset = focus_end; | |
77 | |
78 // Notify Java. | |
79 JNIEnv* env = base::android::AttachCurrentThread(); | |
80 Java_SearchAction_onSurroundingTextReady(env, java_object_.obj()); | |
81 } | |
82 | |
83 std::string SearchAction::FindFocusedWord() { | |
84 DCHECK(search_context_); | |
85 | |
86 const base::string16& surrounding_text = search_context_->surrounding_text; | |
87 int focus_start = search_context_->start_offset; | |
88 int focus_end = search_context_->end_offset; | |
89 int surrounding_text_length = surrounding_text.length(); | |
90 | |
91 // First, we need to find the focused word boundaries. | |
92 int focused_word_start = focus_start; | |
93 int focused_word_end = focus_end; | |
94 | |
95 if (surrounding_text_length > 0 && | |
96 IsValidCharacter(surrounding_text[focused_word_start])) { | |
97 // Find focused word start (inclusive) | |
98 for (int i = focus_start; i >= 0; i--) { | |
99 focused_word_start = i; | |
100 | |
101 wchar_t character; | |
102 if (i > 0) { | |
103 character = surrounding_text[i - 1]; | |
104 | |
105 if (!IsValidCharacter(character)) | |
106 break; | |
107 } | |
108 } | |
109 | |
110 // Find focused word end (non inclusive) | |
111 for (int i = focus_end; i < surrounding_text_length; i++) { | |
112 wchar_t character = surrounding_text[i]; | |
113 | |
114 if (IsValidCharacter(character)) { | |
115 focused_word_end = i + 1; | |
116 } else { | |
117 focused_word_end = i; | |
118 break; | |
119 } | |
120 } | |
121 } | |
122 | |
123 return base::UTF16ToUTF8(surrounding_text.substr( | |
124 focused_word_start, focused_word_end - focused_word_start)); | |
125 } | |
126 | |
127 std::string SearchAction::GetSampleText(int sample_length) { | |
128 DCHECK(search_context_); | |
129 | |
130 const base::string16& surrounding_text = search_context_->surrounding_text; | |
131 int focus_start = search_context_->start_offset; | |
132 int focus_end = search_context_->end_offset; | |
133 int surrounding_text_length = surrounding_text.length(); | |
134 | |
135 //--------------------------------------------------------------------------- | |
136 // Cut the surrounding size so it can be sent to the Java side. | |
137 | |
138 // Start equally distributing the size around the focal point. | |
139 int focal_point_size = focus_end - focus_start; | |
140 int sample_margin = (sample_length - focal_point_size) / 2; | |
141 int sample_start = focus_start - sample_margin; | |
142 int sample_end = focus_end + sample_margin; | |
143 | |
144 // If the the start is out of bounds, compensate the end side. | |
145 if (sample_start < 0) { | |
146 sample_end -= sample_start; | |
147 sample_start = 0; | |
148 } | |
149 | |
150 // If the the end is out of bounds, compensate the start side. | |
151 if (sample_end > surrounding_text_length) { | |
152 int diff = sample_end - surrounding_text_length; | |
153 sample_end = surrounding_text_length; | |
154 sample_start -= diff; | |
155 } | |
156 | |
157 // Trim the start and end to make sure they are within bounds. | |
158 sample_start = std::max(0, sample_start); | |
159 sample_end = std::min(surrounding_text_length, sample_end); | |
160 return base::UTF16ToUTF8( | |
161 surrounding_text.substr(sample_start, sample_end - sample_start)); | |
162 } | |
163 | |
164 bool SearchAction::IsValidCharacter(int char_code) { | |
165 // See http://www.unicode.org/Public/UCD/latest/ucd/NamesList.txt | |
166 // See http://jrgraphix.net/research/unicode_blocks.php | |
167 | |
168 // TODO(donnd): should not include CJK characters! | |
169 // TODO(donnd): consider using regular expressions. | |
170 | |
171 if ((char_code >= 11264 && char_code <= 55295) || // Asian language symbols | |
172 (char_code >= 192 && char_code <= 8191) || // Letters in many languages | |
173 (char_code >= 97 && char_code <= 122) || // Lowercase letters | |
174 (char_code >= 65 && char_code <= 90) || // Uppercase letters | |
175 (char_code >= 48 && char_code <= 57)) { // Numbers | |
176 return true; | |
177 } | |
178 | |
179 return false; | |
180 } | |
181 | |
182 // Testing | |
183 | |
184 SearchAction::SearchAction() {} | |
185 | |
186 void SearchAction::SetContext(std::string surrounding_text, | |
187 int focus_start, | |
188 int focus_end) { | |
189 search_context_.reset(new ContextualSearchContext(false, GURL(), "")); | |
190 search_context_->surrounding_text = base::UTF8ToUTF16(surrounding_text); | |
191 search_context_->start_offset = focus_start; | |
192 search_context_->end_offset = focus_end; | |
193 } | |
194 | |
195 // Boilerplate | |
196 | |
197 bool RegisterSearchAction(JNIEnv* env) { | |
198 return RegisterNativesImpl(env); | |
199 } | |
200 | |
201 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
202 SearchAction* content = new SearchAction(env, obj); | |
203 return reinterpret_cast<intptr_t>(content); | |
204 } | |
OLD | NEW |