| 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/history/top_sites.h" | 21 #include "chrome/browser/history/top_sites.h" |
| 22 #include "chrome/browser/history/top_sites_factory.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/profiles/profile_android.h" | 24 #include "chrome/browser/profiles/profile_android.h" |
| 24 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 25 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 25 #include "chrome/browser/search/suggestions/suggestions_source.h" | 26 #include "chrome/browser/search/suggestions/suggestions_source.h" |
| 26 #include "chrome/browser/sync/profile_sync_service.h" | 27 #include "chrome/browser/sync/profile_sync_service.h" |
| 27 #include "chrome/browser/sync/profile_sync_service_factory.h" | 28 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 28 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 29 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
| 29 #include "components/suggestions/suggestions_service.h" | 30 #include "components/suggestions/suggestions_service.h" |
| 30 #include "components/suggestions/suggestions_utils.h" | 31 #include "components/suggestions/suggestions_utils.h" |
| 31 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 224 |
| 224 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, | 225 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, |
| 225 jobject obj, | 226 jobject obj, |
| 226 jobject j_observer, | 227 jobject j_observer, |
| 227 jint num_sites) { | 228 jint num_sites) { |
| 228 observer_.Reset(env, j_observer); | 229 observer_.Reset(env, j_observer); |
| 229 num_sites_ = num_sites; | 230 num_sites_ = num_sites; |
| 230 | 231 |
| 231 QueryMostVisitedURLs(); | 232 QueryMostVisitedURLs(); |
| 232 | 233 |
| 233 history::TopSites* top_sites = profile_->GetTopSites(); | 234 scoped_refptr<history::TopSites> top_sites = |
| 235 TopSitesFactory::GetForProfile(profile_); |
| 234 if (top_sites) { | 236 if (top_sites) { |
| 235 // TopSites updates itself after a delay. To ensure up-to-date results, | 237 // TopSites updates itself after a delay. To ensure up-to-date results, |
| 236 // force an update now. | 238 // force an update now. |
| 237 top_sites->SyncWithHistory(); | 239 top_sites->SyncWithHistory(); |
| 238 | 240 |
| 239 // Register as TopSitesObserver so that we can update ourselves when the | 241 // Register as TopSitesObserver so that we can update ourselves when the |
| 240 // TopSites changes. | 242 // TopSites changes. |
| 241 scoped_observer_.Add(top_sites); | 243 scoped_observer_.Add(top_sites.get()); |
| 242 } | 244 } |
| 243 } | 245 } |
| 244 | 246 |
| 245 // Called from the UI Thread. | 247 // Called from the UI Thread. |
| 246 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, | 248 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, |
| 247 jobject obj, | 249 jobject obj, |
| 248 jstring url, | 250 jstring url, |
| 249 jobject j_callback_obj) { | 251 jobject j_callback_obj) { |
| 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 251 ScopedJavaGlobalRef<jobject>* j_callback = | 253 ScopedJavaGlobalRef<jobject>* j_callback = |
| 252 new ScopedJavaGlobalRef<jobject>(); | 254 new ScopedJavaGlobalRef<jobject>(); |
| 253 j_callback->Reset(env, j_callback_obj); | 255 j_callback->Reset(env, j_callback_obj); |
| 254 | 256 |
| 255 std::string url_string = ConvertJavaStringToUTF8(env, url); | 257 std::string url_string = ConvertJavaStringToUTF8(env, url); |
| 256 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); | 258 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
| 257 | 259 |
| 258 // If the Suggestions service is enabled and in use, create a callback to | 260 // If the Suggestions service is enabled and in use, create a callback to |
| 259 // fetch a server thumbnail from it, in case the local thumbnail is not found. | 261 // fetch a server thumbnail from it, in case the local thumbnail is not found. |
| 260 SuggestionsService* suggestions_service = | 262 SuggestionsService* suggestions_service = |
| 261 SuggestionsServiceFactory::GetForProfile(profile_); | 263 SuggestionsServiceFactory::GetForProfile(profile_); |
| 262 bool use_suggestions_service = suggestions_service && | 264 bool use_suggestions_service = suggestions_service && |
| 263 mv_source_ == SUGGESTIONS_SERVICE; | 265 mv_source_ == SUGGESTIONS_SERVICE; |
| 264 base::Closure lookup_failed_callback = use_suggestions_service ? | 266 base::Closure lookup_failed_callback = use_suggestions_service ? |
| 265 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, | 267 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, |
| 266 weak_ptr_factory_.GetWeakPtr(), | 268 weak_ptr_factory_.GetWeakPtr(), |
| 267 suggestions_service, url_string, | 269 suggestions_service, url_string, |
| 268 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : | 270 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : |
| 269 base::Closure(); | 271 base::Closure(); |
| 270 LookupSuccessCallback lookup_success_callback = | 272 LookupSuccessCallback lookup_success_callback = |
| 271 base::Bind(&MostVisitedSites::OnObtainedThumbnail, | 273 base::Bind(&MostVisitedSites::OnObtainedThumbnail, |
| 272 weak_ptr_factory_.GetWeakPtr()); | 274 weak_ptr_factory_.GetWeakPtr()); |
| 273 | 275 |
| 274 BrowserThread::PostTask( | 276 BrowserThread::PostTask( |
| 275 BrowserThread::DB, FROM_HERE, | 277 BrowserThread::DB, FROM_HERE, |
| 276 base::Bind( | 278 base::Bind(&GetUrlThumbnailTask, url_string, top_sites, |
| 277 &GetUrlThumbnailTask, url_string, top_sites, | 279 base::Owned(j_callback), lookup_success_callback, |
| 278 base::Owned(j_callback), lookup_success_callback, | 280 lookup_failed_callback)); |
| 279 lookup_failed_callback)); | |
| 280 } | 281 } |
| 281 | 282 |
| 282 void MostVisitedSites::BlacklistUrl(JNIEnv* env, | 283 void MostVisitedSites::BlacklistUrl(JNIEnv* env, |
| 283 jobject obj, | 284 jobject obj, |
| 284 jstring j_url) { | 285 jstring j_url) { |
| 285 std::string url = ConvertJavaStringToUTF8(env, j_url); | 286 std::string url = ConvertJavaStringToUTF8(env, j_url); |
| 286 | 287 |
| 287 switch (mv_source_) { | 288 switch (mv_source_) { |
| 288 case TOP_SITES: { | 289 case TOP_SITES: { |
| 289 TopSites* top_sites = profile_->GetTopSites(); | 290 scoped_refptr<TopSites> top_sites = |
| 291 TopSitesFactory::GetForProfile(profile_); |
| 290 DCHECK(top_sites); | 292 DCHECK(top_sites); |
| 291 top_sites->AddBlacklistedURL(GURL(url)); | 293 top_sites->AddBlacklistedURL(GURL(url)); |
| 292 break; | 294 break; |
| 293 } | 295 } |
| 294 | 296 |
| 295 case SUGGESTIONS_SERVICE: { | 297 case SUGGESTIONS_SERVICE: { |
| 296 SuggestionsService* suggestions_service = | 298 SuggestionsService* suggestions_service = |
| 297 SuggestionsServiceFactory::GetForProfile(profile_); | 299 SuggestionsServiceFactory::GetForProfile(profile_); |
| 298 DCHECK(suggestions_service); | 300 DCHECK(suggestions_service); |
| 299 suggestions_service->BlacklistURL( | 301 suggestions_service->BlacklistURL( |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 base::Bind( | 358 base::Bind( |
| 357 &MostVisitedSites::OnSuggestionsProfileAvailable, | 359 &MostVisitedSites::OnSuggestionsProfileAvailable, |
| 358 weak_ptr_factory_.GetWeakPtr(), | 360 weak_ptr_factory_.GetWeakPtr(), |
| 359 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); | 361 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); |
| 360 } else { | 362 } else { |
| 361 InitiateTopSitesQuery(); | 363 InitiateTopSitesQuery(); |
| 362 } | 364 } |
| 363 } | 365 } |
| 364 | 366 |
| 365 void MostVisitedSites::InitiateTopSitesQuery() { | 367 void MostVisitedSites::InitiateTopSitesQuery() { |
| 366 TopSites* top_sites = profile_->GetTopSites(); | 368 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); |
| 367 if (!top_sites) | 369 if (!top_sites) |
| 368 return; | 370 return; |
| 369 | 371 |
| 370 top_sites->GetMostVisitedURLs( | 372 top_sites->GetMostVisitedURLs( |
| 371 base::Bind( | 373 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, |
| 372 &MostVisitedSites::OnMostVisitedURLsAvailable, | 374 weak_ptr_factory_.GetWeakPtr(), |
| 373 weak_ptr_factory_.GetWeakPtr(), | 375 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), |
| 374 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), | 376 num_sites_), |
| 375 num_sites_), | |
| 376 false); | 377 false); |
| 377 } | 378 } |
| 378 | 379 |
| 379 void MostVisitedSites::OnMostVisitedURLsAvailable( | 380 void MostVisitedSites::OnMostVisitedURLsAvailable( |
| 380 ScopedJavaGlobalRef<jobject>* j_observer, | 381 ScopedJavaGlobalRef<jobject>* j_observer, |
| 381 int num_sites, | 382 int num_sites, |
| 382 const history::MostVisitedURLList& visited_list) { | 383 const history::MostVisitedURLList& visited_list) { |
| 383 std::vector<base::string16> titles; | 384 std::vector<base::string16> titles; |
| 384 std::vector<std::string> urls; | 385 std::vector<std::string> urls; |
| 385 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites); | 386 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites); |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 // The displayed suggestions are invalidated. | 524 // The displayed suggestions are invalidated. |
| 524 QueryMostVisitedURLs(); | 525 QueryMostVisitedURLs(); |
| 525 } | 526 } |
| 526 } | 527 } |
| 527 | 528 |
| 528 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 529 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
| 529 MostVisitedSites* most_visited_sites = | 530 MostVisitedSites* most_visited_sites = |
| 530 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 531 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
| 531 return reinterpret_cast<intptr_t>(most_visited_sites); | 532 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 532 } | 533 } |
| OLD | NEW |