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/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/profiles/profile_android.h" | 24 #include "chrome/browser/profiles/profile_android.h" |
25 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 25 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
26 #include "chrome/browser/search/suggestions/suggestions_source.h" | 26 #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_factory.h" |
27 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 29 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
28 #include "components/suggestions/suggestions_service.h" | 30 #include "components/suggestions/suggestions_service.h" |
| 31 #include "components/suggestions/suggestions_utils.h" |
29 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/browser/notification_source.h" | 33 #include "content/public/browser/notification_source.h" |
31 #include "content/public/browser/url_data_source.h" | 34 #include "content/public/browser/url_data_source.h" |
32 #include "jni/MostVisitedSites_jni.h" | 35 #include "jni/MostVisitedSites_jni.h" |
33 #include "third_party/skia/include/core/SkBitmap.h" | 36 #include "third_party/skia/include/core/SkBitmap.h" |
34 #include "ui/gfx/android/java_bitmap.h" | 37 #include "ui/gfx/android/java_bitmap.h" |
35 #include "ui/gfx/codec/jpeg_codec.h" | 38 #include "ui/gfx/codec/jpeg_codec.h" |
36 | 39 |
37 using base::android::AttachCurrentThread; | 40 using base::android::AttachCurrentThread; |
38 using base::android::ConvertUTF8ToJavaString; | 41 using base::android::ConvertUTF8ToJavaString; |
39 using base::android::ConvertJavaStringToUTF8; | 42 using base::android::ConvertJavaStringToUTF8; |
40 using base::android::ScopedJavaGlobalRef; | 43 using base::android::ScopedJavaGlobalRef; |
41 using base::android::ToJavaArrayOfStrings; | 44 using base::android::ToJavaArrayOfStrings; |
42 using base::android::CheckException; | 45 using base::android::CheckException; |
43 using content::BrowserThread; | 46 using content::BrowserThread; |
44 using history::TopSites; | 47 using history::TopSites; |
45 using suggestions::ChromeSuggestion; | 48 using suggestions::ChromeSuggestion; |
46 using suggestions::SuggestionsProfile; | 49 using suggestions::SuggestionsProfile; |
47 using suggestions::SuggestionsService; | 50 using suggestions::SuggestionsService; |
48 using suggestions::SuggestionsServiceFactory; | 51 using suggestions::SuggestionsServiceFactory; |
| 52 using suggestions::SyncState; |
49 | 53 |
50 namespace { | 54 namespace { |
51 | 55 |
52 // Total number of tiles displayed. | 56 // Total number of tiles displayed. |
53 const char kNumTilesHistogramName[] = "NewTabPage.NumberOfTiles"; | 57 const char kNumTilesHistogramName[] = "NewTabPage.NumberOfTiles"; |
54 // Tracking thumbnails. | 58 // Tracking thumbnails. |
55 const char kNumLocalThumbnailTilesHistogramName[] = | 59 const char kNumLocalThumbnailTilesHistogramName[] = |
56 "NewTabPage.NumberOfThumbnailTiles"; | 60 "NewTabPage.NumberOfThumbnailTiles"; |
57 const char kNumEmptyTilesHistogramName[] = "NewTabPage.NumberOfGrayTiles"; | 61 const char kNumEmptyTilesHistogramName[] = "NewTabPage.NumberOfGrayTiles"; |
58 const char kNumServerTilesHistogramName[] = "NewTabPage.NumberOfExternalTiles"; | 62 const char kNumServerTilesHistogramName[] = "NewTabPage.NumberOfExternalTiles"; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( | 168 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( |
165 histogram, | 169 histogram, |
166 1, | 170 1, |
167 num_sites, | 171 num_sites, |
168 num_sites + 1, | 172 num_sites + 1, |
169 base::Histogram::kUmaTargetedHistogramFlag); | 173 base::Histogram::kUmaTargetedHistogramFlag); |
170 if (counter) | 174 if (counter) |
171 counter->Add(position); | 175 counter->Add(position); |
172 } | 176 } |
173 | 177 |
| 178 // Return the current SyncState for use with the SuggestionsService. |
| 179 SyncState GetSyncState(Profile* profile) { |
| 180 ProfileSyncService* sync = |
| 181 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
| 182 if (!sync) |
| 183 return SyncState::SYNC_OR_HISTORY_SYNC_DISABLED; |
| 184 return suggestions::GetSyncState( |
| 185 sync->IsSyncEnabledAndLoggedIn(), |
| 186 sync->sync_initialized(), |
| 187 sync->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES)); |
| 188 } |
| 189 |
174 } // namespace | 190 } // namespace |
175 | 191 |
176 MostVisitedSites::MostVisitedSites(Profile* profile) | 192 MostVisitedSites::MostVisitedSites(Profile* profile) |
177 : profile_(profile), num_sites_(0), is_control_group_(false), | 193 : profile_(profile), num_sites_(0), is_control_group_(false), |
178 num_local_thumbs_(0), num_server_thumbs_(0), num_empty_thumbs_(0), | 194 num_local_thumbs_(0), num_server_thumbs_(0), num_empty_thumbs_(0), |
179 weak_ptr_factory_(this) { | 195 weak_ptr_factory_(this) { |
180 // Register the debugging page for the Suggestions Service and the thumbnails | 196 // Register the debugging page for the Suggestions Service and the thumbnails |
181 // debugging page. | 197 // debugging page. |
182 content::URLDataSource::Add(profile_, | 198 content::URLDataSource::Add(profile_, |
183 new suggestions::SuggestionsSource(profile_)); | 199 new suggestions::SuggestionsSource(profile_)); |
184 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); | 200 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); |
| 201 |
| 202 // 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 |
| 204 // being enabled or disabled, etc. |
| 205 ProfileSyncService* profile_sync_service = |
| 206 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 207 if (profile_sync_service) |
| 208 profile_sync_service->AddObserver(this); |
185 } | 209 } |
186 | 210 |
187 MostVisitedSites::~MostVisitedSites() { | 211 MostVisitedSites::~MostVisitedSites() { |
188 } | 212 } |
189 | 213 |
190 void MostVisitedSites::Destroy(JNIEnv* env, jobject obj) { | 214 void MostVisitedSites::Destroy(JNIEnv* env, jobject obj) { |
191 delete this; | 215 delete this; |
192 } | 216 } |
193 | 217 |
194 void MostVisitedSites::OnLoadingComplete(JNIEnv* env, jobject obj) { | 218 void MostVisitedSites::OnLoadingComplete(JNIEnv* env, jobject obj) { |
(...skipping 28 matching lines...) Expand all Loading... |
223 jstring url, | 247 jstring url, |
224 jobject j_callback_obj) { | 248 jobject j_callback_obj) { |
225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
226 ScopedJavaGlobalRef<jobject>* j_callback = | 250 ScopedJavaGlobalRef<jobject>* j_callback = |
227 new ScopedJavaGlobalRef<jobject>(); | 251 new ScopedJavaGlobalRef<jobject>(); |
228 j_callback->Reset(env, j_callback_obj); | 252 j_callback->Reset(env, j_callback_obj); |
229 | 253 |
230 std::string url_string = ConvertJavaStringToUTF8(env, url); | 254 std::string url_string = ConvertJavaStringToUTF8(env, url); |
231 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); | 255 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); |
232 | 256 |
233 // If the Suggestions service is enabled, create a callback to fetch a | 257 // If the Suggestions service is enabled and in use, create a callback to |
234 // server thumbnail from it, in case the local thumbnail is not found. | 258 // fetch a server thumbnail from it, in case the local thumbnail is not found. |
235 SuggestionsService* suggestions_service = | 259 SuggestionsService* suggestions_service = |
236 SuggestionsServiceFactory::GetForProfile(profile_); | 260 SuggestionsServiceFactory::GetForProfile(profile_); |
237 base::Closure lookup_failed_callback = suggestions_service ? | 261 bool use_suggestions_service = suggestions_service && |
| 262 mv_source_ == SUGGESTIONS_SERVICE; |
| 263 base::Closure lookup_failed_callback = use_suggestions_service ? |
238 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, | 264 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, |
239 weak_ptr_factory_.GetWeakPtr(), | 265 weak_ptr_factory_.GetWeakPtr(), |
240 suggestions_service, url_string, | 266 suggestions_service, url_string, |
241 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : | 267 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : |
242 base::Closure(); | 268 base::Closure(); |
243 LookupSuccessCallback lookup_success_callback = | 269 LookupSuccessCallback lookup_success_callback = |
244 base::Bind(&MostVisitedSites::OnObtainedThumbnail, | 270 base::Bind(&MostVisitedSites::OnObtainedThumbnail, |
245 weak_ptr_factory_.GetWeakPtr()); | 271 weak_ptr_factory_.GetWeakPtr()); |
246 | 272 |
247 BrowserThread::PostTask( | 273 BrowserThread::PostTask( |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 const content::NotificationSource& source, | 336 const content::NotificationSource& source, |
311 const content::NotificationDetails& details) { | 337 const content::NotificationDetails& details) { |
312 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | 338 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); |
313 | 339 |
314 if (mv_source_ == TOP_SITES) { | 340 if (mv_source_ == TOP_SITES) { |
315 // The displayed suggestions are invalidated. | 341 // The displayed suggestions are invalidated. |
316 QueryMostVisitedURLs(); | 342 QueryMostVisitedURLs(); |
317 } | 343 } |
318 } | 344 } |
319 | 345 |
| 346 void MostVisitedSites::OnStateChanged() { |
| 347 // There have been changes to the sync state. This class cares about a few |
| 348 // (just initialized, enabled/disabled or history sync state changed). Re-run |
| 349 // the query code which will use the proper state. |
| 350 QueryMostVisitedURLs(); |
| 351 } |
| 352 |
320 // static | 353 // static |
321 bool MostVisitedSites::Register(JNIEnv* env) { | 354 bool MostVisitedSites::Register(JNIEnv* env) { |
322 return RegisterNativesImpl(env); | 355 return RegisterNativesImpl(env); |
323 } | 356 } |
324 | 357 |
325 void MostVisitedSites::QueryMostVisitedURLs() { | 358 void MostVisitedSites::QueryMostVisitedURLs() { |
326 SuggestionsService* suggestions_service = | 359 SuggestionsService* suggestions_service = |
327 SuggestionsServiceFactory::GetForProfile(profile_); | 360 SuggestionsServiceFactory::GetForProfile(profile_); |
328 if (suggestions_service) { | 361 if (suggestions_service) { |
329 // Suggestions service is enabled, initiate a query. | 362 // Suggestions service is enabled, initiate a query. |
330 suggestions_service->FetchSuggestionsData( | 363 suggestions_service->FetchSuggestionsData( |
| 364 GetSyncState(profile_), |
331 base::Bind( | 365 base::Bind( |
332 &MostVisitedSites::OnSuggestionsProfileAvailable, | 366 &MostVisitedSites::OnSuggestionsProfileAvailable, |
333 weak_ptr_factory_.GetWeakPtr(), | 367 weak_ptr_factory_.GetWeakPtr(), |
334 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); | 368 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); |
335 } else { | 369 } else { |
336 InitiateTopSitesQuery(); | 370 InitiateTopSitesQuery(); |
337 } | 371 } |
338 } | 372 } |
339 | 373 |
340 void MostVisitedSites::InitiateTopSitesQuery() { | 374 void MostVisitedSites::InitiateTopSitesQuery() { |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 num_empty_thumbs_ = 0; | 512 num_empty_thumbs_ = 0; |
479 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); | 513 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); |
480 num_server_thumbs_ = 0; | 514 num_server_thumbs_ = 0; |
481 } | 515 } |
482 | 516 |
483 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 517 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
484 MostVisitedSites* most_visited_sites = | 518 MostVisitedSites* most_visited_sites = |
485 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 519 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
486 return reinterpret_cast<intptr_t>(most_visited_sites); | 520 return reinterpret_cast<intptr_t>(most_visited_sites); |
487 } | 521 } |
OLD | NEW |