OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/most_visited_sites.h" | 5 #include "chrome/browser/android/most_visited_sites.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
11 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
13 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
21 #include "chrome/browser/chrome_notification_types.h" | 21 #include "chrome/browser/chrome_notification_types.h" |
22 #include "chrome/browser/history/top_sites.h" | 22 #include "chrome/browser/history/top_sites.h" |
| 23 #include "chrome/browser/history/top_sites_factory.h" |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/profiles/profile_android.h" | 25 #include "chrome/browser/profiles/profile_android.h" |
25 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 26 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
26 #include "chrome/browser/search/suggestions/suggestions_source.h" | 27 #include "chrome/browser/search/suggestions/suggestions_source.h" |
27 #include "chrome/browser/sync/profile_sync_service.h" | 28 #include "chrome/browser/sync/profile_sync_service.h" |
28 #include "chrome/browser/sync/profile_sync_service_factory.h" | 29 #include "chrome/browser/sync/profile_sync_service_factory.h" |
29 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 30 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
30 #include "components/suggestions/suggestions_service.h" | 31 #include "components/suggestions/suggestions_service.h" |
31 #include "components/suggestions/suggestions_utils.h" | 32 #include "components/suggestions/suggestions_utils.h" |
32 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 226 |
226 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, | 227 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, |
227 jobject obj, | 228 jobject obj, |
228 jobject j_observer, | 229 jobject j_observer, |
229 jint num_sites) { | 230 jint num_sites) { |
230 observer_.Reset(env, j_observer); | 231 observer_.Reset(env, j_observer); |
231 num_sites_ = num_sites; | 232 num_sites_ = num_sites; |
232 | 233 |
233 QueryMostVisitedURLs(); | 234 QueryMostVisitedURLs(); |
234 | 235 |
235 history::TopSites* top_sites = profile_->GetTopSites(); | 236 scoped_refptr<history::TopSites> top_sites = |
| 237 TopSitesFactory::GetForProfile(profile_); |
236 if (top_sites) { | 238 if (top_sites) { |
237 // TopSites updates itself after a delay. To ensure up-to-date results, | 239 // TopSites updates itself after a delay. To ensure up-to-date results, |
238 // force an update now. | 240 // force an update now. |
239 top_sites->SyncWithHistory(); | 241 top_sites->SyncWithHistory(); |
240 | 242 |
241 // Register for notification when TopSites changes so that we can update | 243 // Register for notification when TopSites changes so that we can update |
242 // ourself. | 244 // ourself. |
243 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | 245 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, |
244 content::Source<history::TopSites>(top_sites)); | 246 content::Source<history::TopSites>(top_sites.get())); |
245 } | 247 } |
246 } | 248 } |
247 | 249 |
248 // Called from the UI Thread. | 250 // Called from the UI Thread. |
249 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, | 251 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, |
250 jobject obj, | 252 jobject obj, |
251 jstring url, | 253 jstring url, |
252 jobject j_callback_obj) { | 254 jobject j_callback_obj) { |
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
254 ScopedJavaGlobalRef<jobject>* j_callback = | 256 ScopedJavaGlobalRef<jobject>* j_callback = |
255 new ScopedJavaGlobalRef<jobject>(); | 257 new ScopedJavaGlobalRef<jobject>(); |
256 j_callback->Reset(env, j_callback_obj); | 258 j_callback->Reset(env, j_callback_obj); |
257 | 259 |
258 std::string url_string = ConvertJavaStringToUTF8(env, url); | 260 std::string url_string = ConvertJavaStringToUTF8(env, url); |
259 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); | 261 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
260 | 262 |
261 // If the Suggestions service is enabled and in use, create a callback to | 263 // If the Suggestions service is enabled and in use, create a callback to |
262 // fetch a server thumbnail from it, in case the local thumbnail is not found. | 264 // fetch a server thumbnail from it, in case the local thumbnail is not found. |
263 SuggestionsService* suggestions_service = | 265 SuggestionsService* suggestions_service = |
264 SuggestionsServiceFactory::GetForProfile(profile_); | 266 SuggestionsServiceFactory::GetForProfile(profile_); |
265 bool use_suggestions_service = suggestions_service && | 267 bool use_suggestions_service = suggestions_service && |
266 mv_source_ == SUGGESTIONS_SERVICE; | 268 mv_source_ == SUGGESTIONS_SERVICE; |
267 base::Closure lookup_failed_callback = use_suggestions_service ? | 269 base::Closure lookup_failed_callback = use_suggestions_service ? |
268 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, | 270 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, |
269 weak_ptr_factory_.GetWeakPtr(), | 271 weak_ptr_factory_.GetWeakPtr(), |
270 suggestions_service, url_string, | 272 suggestions_service, url_string, |
271 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : | 273 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : |
272 base::Closure(); | 274 base::Closure(); |
273 LookupSuccessCallback lookup_success_callback = | 275 LookupSuccessCallback lookup_success_callback = |
274 base::Bind(&MostVisitedSites::OnObtainedThumbnail, | 276 base::Bind(&MostVisitedSites::OnObtainedThumbnail, |
275 weak_ptr_factory_.GetWeakPtr()); | 277 weak_ptr_factory_.GetWeakPtr()); |
276 | 278 |
277 BrowserThread::PostTask( | 279 BrowserThread::PostTask( |
278 BrowserThread::DB, FROM_HERE, | 280 BrowserThread::DB, FROM_HERE, |
279 base::Bind( | 281 base::Bind(&GetUrlThumbnailTask, url_string, top_sites, |
280 &GetUrlThumbnailTask, url_string, top_sites, | 282 base::Owned(j_callback), lookup_success_callback, |
281 base::Owned(j_callback), lookup_success_callback, | 283 lookup_failed_callback)); |
282 lookup_failed_callback)); | |
283 } | 284 } |
284 | 285 |
285 void MostVisitedSites::BlacklistUrl(JNIEnv* env, | 286 void MostVisitedSites::BlacklistUrl(JNIEnv* env, |
286 jobject obj, | 287 jobject obj, |
287 jstring j_url) { | 288 jstring j_url) { |
288 std::string url = ConvertJavaStringToUTF8(env, j_url); | 289 std::string url = ConvertJavaStringToUTF8(env, j_url); |
289 | 290 |
290 switch (mv_source_) { | 291 switch (mv_source_) { |
291 case TOP_SITES: { | 292 case TOP_SITES: { |
292 TopSites* top_sites = profile_->GetTopSites(); | 293 scoped_refptr<TopSites> top_sites = |
| 294 TopSitesFactory::GetForProfile(profile_); |
293 DCHECK(top_sites); | 295 DCHECK(top_sites); |
294 top_sites->AddBlacklistedURL(GURL(url)); | 296 top_sites->AddBlacklistedURL(GURL(url)); |
295 break; | 297 break; |
296 } | 298 } |
297 | 299 |
298 case SUGGESTIONS_SERVICE: { | 300 case SUGGESTIONS_SERVICE: { |
299 SuggestionsService* suggestions_service = | 301 SuggestionsService* suggestions_service = |
300 SuggestionsServiceFactory::GetForProfile(profile_); | 302 SuggestionsServiceFactory::GetForProfile(profile_); |
301 DCHECK(suggestions_service); | 303 DCHECK(suggestions_service); |
302 suggestions_service->BlacklistURL( | 304 suggestions_service->BlacklistURL( |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 base::Bind( | 372 base::Bind( |
371 &MostVisitedSites::OnSuggestionsProfileAvailable, | 373 &MostVisitedSites::OnSuggestionsProfileAvailable, |
372 weak_ptr_factory_.GetWeakPtr(), | 374 weak_ptr_factory_.GetWeakPtr(), |
373 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); | 375 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); |
374 } else { | 376 } else { |
375 InitiateTopSitesQuery(); | 377 InitiateTopSitesQuery(); |
376 } | 378 } |
377 } | 379 } |
378 | 380 |
379 void MostVisitedSites::InitiateTopSitesQuery() { | 381 void MostVisitedSites::InitiateTopSitesQuery() { |
380 TopSites* top_sites = profile_->GetTopSites(); | 382 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); |
381 if (!top_sites) | 383 if (!top_sites) |
382 return; | 384 return; |
383 | 385 |
384 top_sites->GetMostVisitedURLs( | 386 top_sites->GetMostVisitedURLs( |
385 base::Bind( | 387 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, |
386 &MostVisitedSites::OnMostVisitedURLsAvailable, | 388 weak_ptr_factory_.GetWeakPtr(), |
387 weak_ptr_factory_.GetWeakPtr(), | 389 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), |
388 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), | 390 num_sites_), |
389 num_sites_), | |
390 false); | 391 false); |
391 } | 392 } |
392 | 393 |
393 void MostVisitedSites::OnMostVisitedURLsAvailable( | 394 void MostVisitedSites::OnMostVisitedURLsAvailable( |
394 ScopedJavaGlobalRef<jobject>* j_observer, | 395 ScopedJavaGlobalRef<jobject>* j_observer, |
395 int num_sites, | 396 int num_sites, |
396 const history::MostVisitedURLList& visited_list) { | 397 const history::MostVisitedURLList& visited_list) { |
397 std::vector<base::string16> titles; | 398 std::vector<base::string16> titles; |
398 std::vector<std::string> urls; | 399 std::vector<std::string> urls; |
399 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites); | 400 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 num_empty_thumbs_ = 0; | 528 num_empty_thumbs_ = 0; |
528 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); | 529 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); |
529 num_server_thumbs_ = 0; | 530 num_server_thumbs_ = 0; |
530 } | 531 } |
531 | 532 |
532 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 533 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
533 MostVisitedSites* most_visited_sites = | 534 MostVisitedSites* most_visited_sites = |
534 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 535 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
535 return reinterpret_cast<intptr_t>(most_visited_sites); | 536 return reinterpret_cast<intptr_t>(most_visited_sites); |
536 } | 537 } |
OLD | NEW |