Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: chrome/browser/android/most_visited_sites.cc

Issue 473123002: [Most Visited] Check for Sync state when using SuggestionsService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: clean Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
29 #include "content/public/browser/browser_thread.h" 31 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/notification_source.h" 32 #include "content/public/browser/notification_source.h"
31 #include "content/public/browser/url_data_source.h" 33 #include "content/public/browser/url_data_source.h"
32 #include "jni/MostVisitedSites_jni.h" 34 #include "jni/MostVisitedSites_jni.h"
33 #include "third_party/skia/include/core/SkBitmap.h" 35 #include "third_party/skia/include/core/SkBitmap.h"
34 #include "ui/gfx/android/java_bitmap.h" 36 #include "ui/gfx/android/java_bitmap.h"
35 #include "ui/gfx/codec/jpeg_codec.h" 37 #include "ui/gfx/codec/jpeg_codec.h"
36 38
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( 166 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
165 histogram, 167 histogram,
166 1, 168 1,
167 num_sites, 169 num_sites,
168 num_sites + 1, 170 num_sites + 1,
169 base::Histogram::kUmaTargetedHistogramFlag); 171 base::Histogram::kUmaTargetedHistogramFlag);
170 if (counter) 172 if (counter)
171 counter->Add(position); 173 counter->Add(position);
172 } 174 }
173 175
176 // Returns whether full history sync is enabled for this user.
177 bool IsHistorySyncEnabled(Profile* profile) {
178 ProfileSyncService* sync =
179 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
180 return sync &&
181 sync->sync_initialized() &&
182 sync->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES);
183 }
184
185 // Returns whether the sync service is initialized.
186 bool IsSyncInitialized(Profile* profile) {
187 ProfileSyncService* sync =
188 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
189 return sync && sync->sync_initialized();
190 }
191
174 } // namespace 192 } // namespace
175 193
176 MostVisitedSites::MostVisitedSites(Profile* profile) 194 MostVisitedSites::MostVisitedSites(Profile* profile)
177 : profile_(profile), num_sites_(0), is_control_group_(false), 195 : profile_(profile), num_sites_(0), sync_initialized_(false),
178 num_local_thumbs_(0), num_server_thumbs_(0), num_empty_thumbs_(0), 196 is_control_group_(false), num_local_thumbs_(0), num_server_thumbs_(0),
179 weak_ptr_factory_(this) { 197 num_empty_thumbs_(0), weak_ptr_factory_(this) {
180 // Register the debugging page for the Suggestions Service and the thumbnails 198 // Register the debugging page for the Suggestions Service and the thumbnails
181 // debugging page. 199 // debugging page.
182 content::URLDataSource::Add(profile_, 200 content::URLDataSource::Add(profile_,
183 new suggestions::SuggestionsSource(profile_)); 201 new suggestions::SuggestionsSource(profile_));
184 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); 202 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_));
203
204 // Save sync initialization state and if not initialized, register this class
205 // as an observer.
manzagop (departed) 2014/08/15 13:01:52 Perhaps explain why this is needed: ensuring the s
Mathieu 2014/08/20 14:21:32 Done.
206 ProfileSyncService* profile_sync_service =
manzagop (departed) 2014/08/15 13:01:52 I said yesterday I thought this would be null is t
Mathieu 2014/08/20 14:21:31 Done.
207 ProfileSyncServiceFactory::GetForProfile(profile_);
208 if (profile_sync_service) {
209 sync_initialized_ = profile_sync_service->sync_initialized();
manzagop (departed) 2014/08/15 13:01:52 sync_initialized_ doesn't need to be a class membe
Mathieu 2014/08/20 14:21:32 Done.
210 if (!sync_initialized_) {
211 profile_sync_service->AddObserver(this);
212 }
213 }
185 } 214 }
186 215
187 MostVisitedSites::~MostVisitedSites() { 216 MostVisitedSites::~MostVisitedSites() {
188 } 217 }
189 218
190 void MostVisitedSites::Destroy(JNIEnv* env, jobject obj) { 219 void MostVisitedSites::Destroy(JNIEnv* env, jobject obj) {
191 delete this; 220 delete this;
192 } 221 }
193 222
194 void MostVisitedSites::OnLoadingComplete(JNIEnv* env, jobject obj) { 223 void MostVisitedSites::OnLoadingComplete(JNIEnv* env, jobject obj) {
(...skipping 28 matching lines...) Expand all
223 jstring url, 252 jstring url,
224 jobject j_callback_obj) { 253 jobject j_callback_obj) {
225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
226 ScopedJavaGlobalRef<jobject>* j_callback = 255 ScopedJavaGlobalRef<jobject>* j_callback =
227 new ScopedJavaGlobalRef<jobject>(); 256 new ScopedJavaGlobalRef<jobject>();
228 j_callback->Reset(env, j_callback_obj); 257 j_callback->Reset(env, j_callback_obj);
229 258
230 std::string url_string = ConvertJavaStringToUTF8(env, url); 259 std::string url_string = ConvertJavaStringToUTF8(env, url);
231 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); 260 scoped_refptr<TopSites> top_sites(profile_->GetTopSites());
232 261
233 // If the Suggestions service is enabled, create a callback to fetch a 262 // 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. 263 // fetch a server thumbnail from it, in case the local thumbnail is not found.
235 SuggestionsService* suggestions_service = 264 SuggestionsService* suggestions_service =
236 SuggestionsServiceFactory::GetForProfile(profile_); 265 SuggestionsServiceFactory::GetForProfile(profile_);
237 base::Closure lookup_failed_callback = suggestions_service ? 266 bool use_suggestions_service = suggestions_service &&
267 mv_source_ == SUGGESTIONS_SERVICE;
268 base::Closure lookup_failed_callback = use_suggestions_service ?
238 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, 269 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread,
239 weak_ptr_factory_.GetWeakPtr(), 270 weak_ptr_factory_.GetWeakPtr(),
240 suggestions_service, url_string, 271 suggestions_service, url_string,
241 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : 272 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) :
242 base::Closure(); 273 base::Closure();
243 LookupSuccessCallback lookup_success_callback = 274 LookupSuccessCallback lookup_success_callback =
244 base::Bind(&MostVisitedSites::OnObtainedThumbnail, 275 base::Bind(&MostVisitedSites::OnObtainedThumbnail,
245 weak_ptr_factory_.GetWeakPtr()); 276 weak_ptr_factory_.GetWeakPtr());
246 277
247 BrowserThread::PostTask( 278 BrowserThread::PostTask(
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 const content::NotificationSource& source, 341 const content::NotificationSource& source,
311 const content::NotificationDetails& details) { 342 const content::NotificationDetails& details) {
312 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); 343 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED);
313 344
314 if (mv_source_ == TOP_SITES) { 345 if (mv_source_ == TOP_SITES) {
315 // The displayed suggestions are invalidated. 346 // The displayed suggestions are invalidated.
316 QueryMostVisitedURLs(); 347 QueryMostVisitedURLs();
317 } 348 }
318 } 349 }
319 350
351 void MostVisitedSites::OnStateChanged() {
352 ProfileSyncService* sync =
353 ProfileSyncServiceFactory::GetForProfile(profile_);
354 // We only care about the sync initialization event.
355 if (sync && sync->sync_initialized()) {
manzagop (departed) 2014/08/15 13:01:52 We should double check the sync service gets initi
Mathieu 2014/08/20 14:21:32 Done.
356 sync_initialized_ = true;
357 SuggestionsService* suggestions_service =
358 SuggestionsServiceFactory::GetForProfile(profile_);
359 if (suggestions_service) {
360 // This serves as a cache warm-up or a cache refresh, depending if the
361 // SuggestionsService query has happened at this point. Both are
362 // important.
363 suggestions_service->FetchSuggestionsData(
364 sync_initialized_,
365 IsHistorySyncEnabled(profile_),
366 base::Callback<void(const SuggestionsProfile&)>());
367 }
368 }
369 }
370
320 // static 371 // static
321 bool MostVisitedSites::Register(JNIEnv* env) { 372 bool MostVisitedSites::Register(JNIEnv* env) {
322 return RegisterNativesImpl(env); 373 return RegisterNativesImpl(env);
323 } 374 }
324 375
325 void MostVisitedSites::QueryMostVisitedURLs() { 376 void MostVisitedSites::QueryMostVisitedURLs() {
326 SuggestionsService* suggestions_service = 377 SuggestionsService* suggestions_service =
327 SuggestionsServiceFactory::GetForProfile(profile_); 378 SuggestionsServiceFactory::GetForProfile(profile_);
328 if (suggestions_service) { 379 if (suggestions_service) {
329 // Suggestions service is enabled, initiate a query. 380 // Suggestions service is enabled, initiate a query.
330 suggestions_service->FetchSuggestionsData( 381 suggestions_service->FetchSuggestionsData(
382 IsSyncInitialized(profile_),
383 IsHistorySyncEnabled(profile_),
331 base::Bind( 384 base::Bind(
332 &MostVisitedSites::OnSuggestionsProfileAvailable, 385 &MostVisitedSites::OnSuggestionsProfileAvailable,
333 weak_ptr_factory_.GetWeakPtr(), 386 weak_ptr_factory_.GetWeakPtr(),
334 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); 387 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_))));
335 } else { 388 } else {
336 InitiateTopSitesQuery(); 389 InitiateTopSitesQuery();
337 } 390 }
338 } 391 }
339 392
340 void MostVisitedSites::InitiateTopSitesQuery() { 393 void MostVisitedSites::InitiateTopSitesQuery() {
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 num_empty_thumbs_ = 0; 531 num_empty_thumbs_ = 0;
479 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); 532 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_);
480 num_server_thumbs_ = 0; 533 num_server_thumbs_ = 0;
481 } 534 }
482 535
483 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { 536 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) {
484 MostVisitedSites* most_visited_sites = 537 MostVisitedSites* most_visited_sites =
485 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); 538 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile));
486 return reinterpret_cast<intptr_t>(most_visited_sites); 539 return reinterpret_cast<intptr_t>(most_visited_sites);
487 } 540 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698