OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/contextualsearch/contextual_search_manager.h" | 5 #include "chrome/browser/android/contextualsearch/contextual_search_manager.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h" | 13 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h" |
14 #include "chrome/browser/android/tab_android.h" | 14 #include "chrome/browser/android/tab_android.h" |
15 #include "chrome/browser/history/history_service_factory.h" | |
16 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
17 #include "chrome/browser/search_engines/template_url_service_factory.h" | 16 #include "chrome/browser/search_engines/template_url_service_factory.h" |
18 #include "chrome/browser/ui/android/window_android_helper.h" | 17 #include "chrome/browser/ui/android/window_android_helper.h" |
19 #include "components/history/core/browser/history_service.h" | |
20 #include "components/navigation_interception/intercept_navigation_delegate.h" | 18 #include "components/navigation_interception/intercept_navigation_delegate.h" |
21 #include "components/variations/variations_associated_data.h" | 19 #include "components/variations/variations_associated_data.h" |
22 #include "components/web_contents_delegate_android/web_contents_delegate_android
.h" | |
23 #include "content/public/browser/android/content_view_core.h" | 20 #include "content/public/browser/android/content_view_core.h" |
24 #include "content/public/browser/web_contents.h" | |
25 #include "jni/ContextualSearchManager_jni.h" | 21 #include "jni/ContextualSearchManager_jni.h" |
26 #include "net/url_request/url_fetcher_impl.h" | 22 #include "net/url_request/url_fetcher_impl.h" |
27 | 23 |
28 using content::ContentViewCore; | 24 using content::ContentViewCore; |
29 | 25 |
30 namespace { | |
31 | |
32 const int kHistoryDeletionWindowSeconds = 2; | |
33 | |
34 // Because we need a callback, this needs to exist. | |
35 void OnHistoryDeletionDone() { | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 // This class manages the native behavior of the Contextual Search feature. | 26 // This class manages the native behavior of the Contextual Search feature. |
41 // Instances of this class are owned by the Java ContextualSearchManager. | 27 // Instances of this class are owned by the Java ContextualSearchManager. |
42 // Most of the work is actually done in an associated delegate to this class: | 28 // Most of the work is actually done in an associated delegate to this class: |
43 // the ContextualSearchDelegate. | 29 // the ContextualSearchDelegate. |
44 ContextualSearchManager::ContextualSearchManager(JNIEnv* env, jobject obj) { | 30 ContextualSearchManager::ContextualSearchManager(JNIEnv* env, jobject obj) { |
45 java_manager_.Reset(env, obj); | 31 java_manager_.Reset(env, obj); |
46 Java_ContextualSearchManager_setNativeManager( | 32 Java_ContextualSearchManager_setNativeManager( |
47 env, obj, reinterpret_cast<intptr_t>(this)); | 33 env, obj, reinterpret_cast<intptr_t>(this)); |
48 Profile* profile = ProfileManager::GetActiveUserProfile(); | 34 Profile* profile = ProfileManager::GetActiveUserProfile(); |
49 delegate_.reset(new ContextualSearchDelegate( | 35 delegate_.reset(new ContextualSearchDelegate( |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 JNIEnv* env = base::android::AttachCurrentThread(); | 135 JNIEnv* env = base::android::AttachCurrentThread(); |
150 base::android::ScopedJavaLocalRef<jstring> j_encoding = | 136 base::android::ScopedJavaLocalRef<jstring> j_encoding = |
151 base::android::ConvertUTF8ToJavaString(env, encoding.c_str()); | 137 base::android::ConvertUTF8ToJavaString(env, encoding.c_str()); |
152 base::android::ScopedJavaLocalRef<jstring> j_surrounding_text = | 138 base::android::ScopedJavaLocalRef<jstring> j_surrounding_text = |
153 base::android::ConvertUTF16ToJavaString(env, surrounding_text.c_str()); | 139 base::android::ConvertUTF16ToJavaString(env, surrounding_text.c_str()); |
154 Java_ContextualSearchManager_onIcingSelectionAvailable( | 140 Java_ContextualSearchManager_onIcingSelectionAvailable( |
155 env, java_manager_.obj(), j_encoding.obj(), j_surrounding_text.obj(), | 141 env, java_manager_.obj(), j_encoding.obj(), j_surrounding_text.obj(), |
156 start_offset, end_offset); | 142 start_offset, end_offset); |
157 } | 143 } |
158 | 144 |
159 void ContextualSearchManager::RemoveLastSearchVisit( | |
160 JNIEnv* env, | |
161 jobject obj, | |
162 jstring search_url, | |
163 jlong search_start_time_ms) { | |
164 // The deletion window is from the time a search URL was put in history, up | |
165 // to a short amount of time later. | |
166 base::Time begin_time = base::Time::FromJsTime(search_start_time_ms); | |
167 base::Time end_time = begin_time + | |
168 base::TimeDelta::FromSeconds(kHistoryDeletionWindowSeconds); | |
169 | |
170 history::HistoryService* service = HistoryServiceFactory::GetForProfile( | |
171 ProfileManager::GetActiveUserProfile(), | |
172 ServiceAccessType::EXPLICIT_ACCESS); | |
173 if (service) { | |
174 // NOTE(mathp): We are only removing |search_url| from the local history | |
175 // because search results that are not promoted to a Tab do not make it to | |
176 // the web history, only local. | |
177 std::set<GURL> restrict_set; | |
178 restrict_set.insert( | |
179 GURL(base::android::ConvertJavaStringToUTF8(env, search_url))); | |
180 service->ExpireHistoryBetween( | |
181 restrict_set, | |
182 begin_time, | |
183 end_time, | |
184 base::Bind(&OnHistoryDeletionDone), | |
185 &history_task_tracker_); | |
186 } | |
187 } | |
188 | |
189 void ContextualSearchManager::SetWebContents(JNIEnv* env, | |
190 jobject obj, | |
191 jobject jcontent_view_core, | |
192 jobject jweb_contents_delegate) { | |
193 content::ContentViewCore* content_view_core = | |
194 content::ContentViewCore::GetNativeContentViewCore(env, | |
195 jcontent_view_core); | |
196 DCHECK(content_view_core); | |
197 DCHECK(content_view_core->GetWebContents()); | |
198 | |
199 // NOTE(pedrosimonetti): Takes ownership of the WebContents associated | |
200 // with the ContentViewCore. This is to make sure that the WebContens | |
201 // and the Compositor are in the same process. | |
202 // TODO(pedrosimonetti): Confirm with dtrainor@ if the comment above | |
203 // is accurate. | |
204 web_contents_.reset(content_view_core->GetWebContents()); | |
205 // TODO(pedrosimonetti): confirm if we need this after promoting it | |
206 // to a real tab. | |
207 TabAndroid::AttachTabHelpers(web_contents_.get()); | |
208 WindowAndroidHelper::FromWebContents(web_contents_.get()) | |
209 ->SetWindowAndroid(content_view_core->GetWindowAndroid()); | |
210 web_contents_delegate_.reset( | |
211 new web_contents_delegate_android::WebContentsDelegateAndroid( | |
212 env, jweb_contents_delegate)); | |
213 web_contents_->SetDelegate(web_contents_delegate_.get()); | |
214 } | |
215 | |
216 void ContextualSearchManager::DestroyWebContents(JNIEnv* env, jobject jobj) { | |
217 DCHECK(web_contents_.get()); | |
218 web_contents_.reset(); | |
219 // |web_contents_delegate_| may already be NULL at this point. | |
220 web_contents_delegate_.reset(); | |
221 } | |
222 | |
223 void ContextualSearchManager::ReleaseWebContents(JNIEnv* env, jobject jboj) { | |
224 DCHECK(web_contents_.get()); | |
225 web_contents_delegate_.reset(); | |
226 ignore_result(web_contents_.release()); | |
227 } | |
228 | |
229 void ContextualSearchManager::DestroyWebContentsFromContentViewCore( | |
230 JNIEnv* env, | |
231 jobject jobj, | |
232 jobject jcontent_view_core) { | |
233 content::ContentViewCore* content_view_core = | |
234 content::ContentViewCore::GetNativeContentViewCore(env, | |
235 jcontent_view_core); | |
236 DCHECK(content_view_core); | |
237 DCHECK(content_view_core->GetWebContents()); | |
238 | |
239 delete content_view_core->GetWebContents(); | |
240 } | |
241 | |
242 void ContextualSearchManager::SetInterceptNavigationDelegate( | |
243 JNIEnv* env, | |
244 jobject obj, | |
245 jobject delegate, | |
246 jobject jweb_contents) { | |
247 content::WebContents* web_contents = | |
248 content::WebContents::FromJavaWebContents(jweb_contents); | |
249 DCHECK(web_contents); | |
250 navigation_interception::InterceptNavigationDelegate::Associate( | |
251 web_contents, | |
252 make_scoped_ptr(new navigation_interception::InterceptNavigationDelegate( | |
253 env, delegate))); | |
254 } | |
255 | |
256 bool RegisterContextualSearchManager(JNIEnv* env) { | 145 bool RegisterContextualSearchManager(JNIEnv* env) { |
257 return RegisterNativesImpl(env); | 146 return RegisterNativesImpl(env); |
258 } | 147 } |
259 | 148 |
260 jlong Init(JNIEnv* env, jobject obj) { | 149 jlong Init(JNIEnv* env, jobject obj) { |
261 ContextualSearchManager* manager = new ContextualSearchManager(env, obj); | 150 ContextualSearchManager* manager = new ContextualSearchManager(env, obj); |
262 return reinterpret_cast<intptr_t>(manager); | 151 return reinterpret_cast<intptr_t>(manager); |
263 } | 152 } |
OLD | NEW |