Index: chrome/browser/android/most_visited_sites.cc |
diff --git a/chrome/browser/android/most_visited_sites.cc b/chrome/browser/android/most_visited_sites.cc |
index e13597ca0f14de5acfd9574152ff3e1ef2cf2604..6a16759b29c019a0ca42efb34c84d70585f16dda 100644 |
--- a/chrome/browser/android/most_visited_sites.cc |
+++ b/chrome/browser/android/most_visited_sites.cc |
@@ -24,8 +24,11 @@ |
#include "chrome/browser/profiles/profile_android.h" |
#include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
#include "chrome/browser/search/suggestions/suggestions_source.h" |
+#include "chrome/browser/sync/profile_sync_service.h" |
+#include "chrome/browser/sync/profile_sync_service_factory.h" |
#include "chrome/browser/thumbnails/thumbnail_list_source.h" |
#include "components/suggestions/suggestions_service.h" |
+#include "components/suggestions/suggestions_utils.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/notification_source.h" |
#include "content/public/browser/url_data_source.h" |
@@ -46,6 +49,7 @@ using suggestions::ChromeSuggestion; |
using suggestions::SuggestionsProfile; |
using suggestions::SuggestionsService; |
using suggestions::SuggestionsServiceFactory; |
+using suggestions::SyncState; |
namespace { |
@@ -171,6 +175,18 @@ void LogHistogramEvent(const std::string& histogram, int position, |
counter->Add(position); |
} |
+// Return the current SyncState for use with the SuggestionsService. |
+SyncState GetSyncState(Profile* profile) { |
+ ProfileSyncService* sync = |
+ ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
+ if (!sync) |
+ return SyncState::SYNC_OR_HISTORY_SYNC_DISABLED; |
+ return suggestions::GetSyncState( |
+ sync->IsSyncEnabledAndLoggedIn(), |
+ sync->sync_initialized(), |
+ sync->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES)); |
+} |
+ |
} // namespace |
MostVisitedSites::MostVisitedSites(Profile* profile) |
@@ -182,6 +198,14 @@ MostVisitedSites::MostVisitedSites(Profile* profile) |
content::URLDataSource::Add(profile_, |
new suggestions::SuggestionsSource(profile_)); |
content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); |
+ |
+ // If the sync service is not initialized, register this class as an observer. |
+ // On startup, this code may run when sync is not initialized, which is |
+ // relevant to the SuggestionsService (see OnStateChanged). |
+ ProfileSyncService* profile_sync_service = |
+ ProfileSyncServiceFactory::GetForProfile(profile_); |
+ if (profile_sync_service && !profile_sync_service->sync_initialized()) |
+ profile_sync_service->AddObserver(this); |
} |
MostVisitedSites::~MostVisitedSites() { |
@@ -230,11 +254,13 @@ void MostVisitedSites::GetURLThumbnail(JNIEnv* env, |
std::string url_string = ConvertJavaStringToUTF8(env, url); |
scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); |
- // If the Suggestions service is enabled, create a callback to fetch a |
- // server thumbnail from it, in case the local thumbnail is not found. |
+ // If the Suggestions service is enabled and in use, create a callback to |
+ // fetch a server thumbnail from it, in case the local thumbnail is not found. |
SuggestionsService* suggestions_service = |
SuggestionsServiceFactory::GetForProfile(profile_); |
- base::Closure lookup_failed_callback = suggestions_service ? |
+ bool use_suggestions_service = suggestions_service && |
+ mv_source_ == SUGGESTIONS_SERVICE; |
+ base::Closure lookup_failed_callback = use_suggestions_service ? |
base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, |
weak_ptr_factory_.GetWeakPtr(), |
suggestions_service, url_string, |
@@ -317,6 +343,27 @@ void MostVisitedSites::Observe(int type, |
} |
} |
+void MostVisitedSites::OnStateChanged() { |
+ ProfileSyncService* sync = |
+ ProfileSyncServiceFactory::GetForProfile(profile_); |
+ // We only care about the sync initialization event. |
+ if (sync && sync->sync_initialized()) { |
+ sync->RemoveObserver(this); |
+ SuggestionsService* suggestions_service = |
+ SuggestionsServiceFactory::GetForProfile(profile_); |
+ if (suggestions_service) { |
+ // It's important to refresh or trash the cache and the page, depending on |
+ // the state of sync, which is now initialized. |
+ suggestions_service->FetchSuggestionsData( |
+ GetSyncState(profile_), |
+ base::Bind( |
+ &MostVisitedSites::OnSuggestionsProfileAvailable, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); |
+ } |
+ } |
+} |
+ |
// static |
bool MostVisitedSites::Register(JNIEnv* env) { |
return RegisterNativesImpl(env); |
@@ -328,6 +375,7 @@ void MostVisitedSites::QueryMostVisitedURLs() { |
if (suggestions_service) { |
// Suggestions service is enabled, initiate a query. |
suggestions_service->FetchSuggestionsData( |
+ GetSyncState(profile_), |
base::Bind( |
&MostVisitedSites::OnSuggestionsProfileAvailable, |
weak_ptr_factory_.GetWeakPtr(), |