| 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" | |
| 22 #include "chrome/browser/history/top_sites.h" | 21 #include "chrome/browser/history/top_sites.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/profiles/profile_android.h" | 23 #include "chrome/browser/profiles/profile_android.h" |
| 25 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 24 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 26 #include "chrome/browser/search/suggestions/suggestions_source.h" | 25 #include "chrome/browser/search/suggestions/suggestions_source.h" |
| 27 #include "chrome/browser/sync/profile_sync_service.h" | 26 #include "chrome/browser/sync/profile_sync_service.h" |
| 28 #include "chrome/browser/sync/profile_sync_service_factory.h" | 27 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 29 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 28 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
| 30 #include "components/suggestions/suggestions_service.h" | 29 #include "components/suggestions/suggestions_service.h" |
| 31 #include "components/suggestions/suggestions_utils.h" | 30 #include "components/suggestions/suggestions_utils.h" |
| 32 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/notification_source.h" | |
| 34 #include "content/public/browser/url_data_source.h" | 32 #include "content/public/browser/url_data_source.h" |
| 35 #include "jni/MostVisitedSites_jni.h" | 33 #include "jni/MostVisitedSites_jni.h" |
| 36 #include "third_party/skia/include/core/SkBitmap.h" | 34 #include "third_party/skia/include/core/SkBitmap.h" |
| 37 #include "ui/gfx/android/java_bitmap.h" | 35 #include "ui/gfx/android/java_bitmap.h" |
| 38 #include "ui/gfx/codec/jpeg_codec.h" | 36 #include "ui/gfx/codec/jpeg_codec.h" |
| 39 | 37 |
| 40 using base::android::AttachCurrentThread; | 38 using base::android::AttachCurrentThread; |
| 41 using base::android::ConvertUTF8ToJavaString; | 39 using base::android::ConvertUTF8ToJavaString; |
| 42 using base::android::ConvertJavaStringToUTF8; | 40 using base::android::ConvertJavaStringToUTF8; |
| 43 using base::android::ScopedJavaGlobalRef; | 41 using base::android::ScopedJavaGlobalRef; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 sync->IsSyncEnabledAndLoggedIn(), | 183 sync->IsSyncEnabledAndLoggedIn(), |
| 186 sync->SyncActive(), | 184 sync->SyncActive(), |
| 187 sync->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES)); | 185 sync->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES)); |
| 188 } | 186 } |
| 189 | 187 |
| 190 } // namespace | 188 } // namespace |
| 191 | 189 |
| 192 MostVisitedSites::MostVisitedSites(Profile* profile) | 190 MostVisitedSites::MostVisitedSites(Profile* profile) |
| 193 : profile_(profile), num_sites_(0), is_control_group_(false), | 191 : profile_(profile), num_sites_(0), is_control_group_(false), |
| 194 initial_load_done_(false), num_local_thumbs_(0), num_server_thumbs_(0), | 192 initial_load_done_(false), num_local_thumbs_(0), num_server_thumbs_(0), |
| 195 num_empty_thumbs_(0), weak_ptr_factory_(this) { | 193 num_empty_thumbs_(0), scoped_observer_(this), weak_ptr_factory_(this) { |
| 196 // Register the debugging page for the Suggestions Service and the thumbnails | 194 // Register the debugging page for the Suggestions Service and the thumbnails |
| 197 // debugging page. | 195 // debugging page. |
| 198 content::URLDataSource::Add(profile_, | 196 content::URLDataSource::Add(profile_, |
| 199 new suggestions::SuggestionsSource(profile_)); | 197 new suggestions::SuggestionsSource(profile_)); |
| 200 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); | 198 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); |
| 201 | 199 |
| 202 // Register this class as an observer to the sync service. It is important to | 200 // Register this class as an observer to the sync service. It is important to |
| 203 // be notified of changes in the sync state such as initialization, sync | 201 // be notified of changes in the sync state such as initialization, sync |
| 204 // being enabled or disabled, etc. | 202 // being enabled or disabled, etc. |
| 205 ProfileSyncService* profile_sync_service = | 203 ProfileSyncService* profile_sync_service = |
| (...skipping 25 matching lines...) Expand all Loading... |
| 231 num_sites_ = num_sites; | 229 num_sites_ = num_sites; |
| 232 | 230 |
| 233 QueryMostVisitedURLs(); | 231 QueryMostVisitedURLs(); |
| 234 | 232 |
| 235 history::TopSites* top_sites = profile_->GetTopSites(); | 233 history::TopSites* top_sites = profile_->GetTopSites(); |
| 236 if (top_sites) { | 234 if (top_sites) { |
| 237 // TopSites updates itself after a delay. To ensure up-to-date results, | 235 // TopSites updates itself after a delay. To ensure up-to-date results, |
| 238 // force an update now. | 236 // force an update now. |
| 239 top_sites->SyncWithHistory(); | 237 top_sites->SyncWithHistory(); |
| 240 | 238 |
| 241 // Register for notification when TopSites changes so that we can update | 239 // Register as TopSitesObserver so that we can update ourselves when the |
| 242 // ourself. | 240 // TopSites changes. |
| 243 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | 241 scoped_observer_.Add(top_sites); |
| 244 content::Source<history::TopSites>(top_sites)); | |
| 245 } | 242 } |
| 246 } | 243 } |
| 247 | 244 |
| 248 // Called from the UI Thread. | 245 // Called from the UI Thread. |
| 249 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, | 246 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, |
| 250 jobject obj, | 247 jobject obj, |
| 251 jstring url, | 248 jstring url, |
| 252 jobject j_callback_obj) { | 249 jobject j_callback_obj) { |
| 253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 254 ScopedJavaGlobalRef<jobject>* j_callback = | 251 ScopedJavaGlobalRef<jobject>* j_callback = |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 LogHistogramEvent(histogram, index, num_sites_); | 327 LogHistogramEvent(histogram, index, num_sites_); |
| 331 } else { | 328 } else { |
| 332 UMA_HISTOGRAM_SPARSE_SLOWLY(kOpenedItemServerHistogramName, index); | 329 UMA_HISTOGRAM_SPARSE_SLOWLY(kOpenedItemServerHistogramName, index); |
| 333 } | 330 } |
| 334 } | 331 } |
| 335 break; | 332 break; |
| 336 } | 333 } |
| 337 } | 334 } |
| 338 } | 335 } |
| 339 | 336 |
| 340 void MostVisitedSites::Observe(int type, | |
| 341 const content::NotificationSource& source, | |
| 342 const content::NotificationDetails& details) { | |
| 343 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | |
| 344 | |
| 345 if (mv_source_ == TOP_SITES) { | |
| 346 // The displayed suggestions are invalidated. | |
| 347 QueryMostVisitedURLs(); | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 void MostVisitedSites::OnStateChanged() { | 337 void MostVisitedSites::OnStateChanged() { |
| 352 // There have been changes to the sync state. This class cares about a few | 338 // There have been changes to the sync state. This class cares about a few |
| 353 // (just initialized, enabled/disabled or history sync state changed). Re-run | 339 // (just initialized, enabled/disabled or history sync state changed). Re-run |
| 354 // the query code which will use the proper state. | 340 // the query code which will use the proper state. |
| 355 QueryMostVisitedURLs(); | 341 QueryMostVisitedURLs(); |
| 356 } | 342 } |
| 357 | 343 |
| 358 // static | 344 // static |
| 359 bool MostVisitedSites::Register(JNIEnv* env) { | 345 bool MostVisitedSites::Register(JNIEnv* env) { |
| 360 return RegisterNativesImpl(env); | 346 return RegisterNativesImpl(env); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 void MostVisitedSites::RecordUMAMetrics() { | 508 void MostVisitedSites::RecordUMAMetrics() { |
| 523 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumLocalThumbnailTilesHistogramName, | 509 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumLocalThumbnailTilesHistogramName, |
| 524 num_local_thumbs_); | 510 num_local_thumbs_); |
| 525 num_local_thumbs_ = 0; | 511 num_local_thumbs_ = 0; |
| 526 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumEmptyTilesHistogramName, num_empty_thumbs_); | 512 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumEmptyTilesHistogramName, num_empty_thumbs_); |
| 527 num_empty_thumbs_ = 0; | 513 num_empty_thumbs_ = 0; |
| 528 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); | 514 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); |
| 529 num_server_thumbs_ = 0; | 515 num_server_thumbs_ = 0; |
| 530 } | 516 } |
| 531 | 517 |
| 518 void MostVisitedSites::TopSitesLoaded(history::TopSites* top_sites) { |
| 519 } |
| 520 |
| 521 void MostVisitedSites::TopSitesChanged(history::TopSites* top_sites) { |
| 522 if (mv_source_ == TOP_SITES) { |
| 523 // The displayed suggestions are invalidated. |
| 524 QueryMostVisitedURLs(); |
| 525 } |
| 526 } |
| 527 |
| 532 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 528 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
| 533 MostVisitedSites* most_visited_sites = | 529 MostVisitedSites* most_visited_sites = |
| 534 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 530 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
| 535 return reinterpret_cast<intptr_t>(most_visited_sites); | 531 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 536 } | 532 } |
| OLD | NEW |