Commit 33b84dc5 authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

SuggestionsService: Use syncer::GetUploadToGoogleState

instead of hand-rolling its own version of it.

As a convenience for callers, GetUploadToGoogleState now also handles a
null SyncService (which maps to NOT_ACTIVE).

Bug: 824723
Change-Id: Ie81ddc0350ef128ab065c944ecb493f1b1a899ae
Reviewed-on: https://chromium-review.googlesource.com/975132
Commit-Queue: Marc Treib <treib@chromium.org>
Reviewed-by: default avatarMathieu Perreault <mathp@chromium.org>
Cr-Commit-Position: refs/heads/master@{#545091}
parent 5c942e8e
...@@ -127,7 +127,7 @@ SuggestionsServiceImpl::SuggestionsServiceImpl( ...@@ -127,7 +127,7 @@ SuggestionsServiceImpl::SuggestionsServiceImpl(
: identity_manager_(identity_manager), : identity_manager_(identity_manager),
sync_service_(sync_service), sync_service_(sync_service),
sync_service_observer_(this), sync_service_observer_(this),
sync_state_(INITIALIZED_ENABLED_HISTORY), history_sync_state_(syncer::UploadState::INITIALIZING),
url_request_context_(url_request_context), url_request_context_(url_request_context),
suggestions_store_(std::move(suggestions_store)), suggestions_store_(std::move(suggestions_store)),
thumbnail_manager_(std::move(thumbnail_manager)), thumbnail_manager_(std::move(thumbnail_manager)),
...@@ -152,7 +152,7 @@ SuggestionsServiceImpl::~SuggestionsServiceImpl() {} ...@@ -152,7 +152,7 @@ SuggestionsServiceImpl::~SuggestionsServiceImpl() {}
bool SuggestionsServiceImpl::FetchSuggestionsData() { bool SuggestionsServiceImpl::FetchSuggestionsData() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
// If sync state allows, issue a network request to refresh the suggestions. // If sync state allows, issue a network request to refresh the suggestions.
if (sync_state_ != INITIALIZED_ENABLED_HISTORY) { if (history_sync_state_ != syncer::UploadState::ACTIVE) {
return false; return false;
} }
IssueRequestIfNoneOngoing(BuildSuggestionsURL()); IssueRequestIfNoneOngoing(BuildSuggestionsURL());
...@@ -192,7 +192,7 @@ void SuggestionsServiceImpl::GetPageThumbnailWithURL( ...@@ -192,7 +192,7 @@ void SuggestionsServiceImpl::GetPageThumbnailWithURL(
bool SuggestionsServiceImpl::BlacklistURL(const GURL& candidate_url) { bool SuggestionsServiceImpl::BlacklistURL(const GURL& candidate_url) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
// TODO(treib): Do we need to check |sync_state_| here? // TODO(treib): Do we need to check |history_sync_state_| here?
if (!blacklist_store_->BlacklistUrl(candidate_url)) if (!blacklist_store_->BlacklistUrl(candidate_url))
return false; return false;
...@@ -211,7 +211,7 @@ bool SuggestionsServiceImpl::BlacklistURL(const GURL& candidate_url) { ...@@ -211,7 +211,7 @@ bool SuggestionsServiceImpl::BlacklistURL(const GURL& candidate_url) {
bool SuggestionsServiceImpl::UndoBlacklistURL(const GURL& url) { bool SuggestionsServiceImpl::UndoBlacklistURL(const GURL& url) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
// TODO(treib): Do we need to check |sync_state_| here? // TODO(treib): Do we need to check |history_sync_state_| here?
TimeDelta time_delta; TimeDelta time_delta;
if (blacklist_store_->GetTimeUntilURLReadyForUpload(url, &time_delta) && if (blacklist_store_->GetTimeUntilURLReadyForUpload(url, &time_delta) &&
...@@ -229,7 +229,7 @@ bool SuggestionsServiceImpl::UndoBlacklistURL(const GURL& url) { ...@@ -229,7 +229,7 @@ bool SuggestionsServiceImpl::UndoBlacklistURL(const GURL& url) {
void SuggestionsServiceImpl::ClearBlacklist() { void SuggestionsServiceImpl::ClearBlacklist() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
// TODO(treib): Do we need to check |sync_state_| here? // TODO(treib): Do we need to check |history_sync_state_| here?
blacklist_store_->ClearBlacklist(); blacklist_store_->ClearBlacklist();
callback_list_.Notify( callback_list_.Notify(
...@@ -308,55 +308,41 @@ GURL SuggestionsServiceImpl::BuildSuggestionsBlacklistClearURL() { ...@@ -308,55 +308,41 @@ GURL SuggestionsServiceImpl::BuildSuggestionsBlacklistClearURL() {
kDeviceType)); kDeviceType));
} }
SuggestionsServiceImpl::SyncState SuggestionsServiceImpl::ComputeSyncState()
const {
if (!sync_service_ || !sync_service_->CanSyncStart() ||
sync_service_->IsLocalSyncEnabled() ||
sync_service_->GetAuthError().IsPersistentError()) {
return SYNC_OR_HISTORY_SYNC_DISABLED;
}
if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) {
return NOT_INITIALIZED_ENABLED;
}
return sync_service_->GetActiveDataTypes().Has(
syncer::HISTORY_DELETE_DIRECTIVES)
? INITIALIZED_ENABLED_HISTORY
: SYNC_OR_HISTORY_SYNC_DISABLED;
}
SuggestionsServiceImpl::RefreshAction SuggestionsServiceImpl::RefreshAction
SuggestionsServiceImpl::RefreshSyncState() { SuggestionsServiceImpl::RefreshHistorySyncState() {
SyncState new_sync_state = ComputeSyncState(); syncer::UploadState new_sync_state = syncer::GetUploadToGoogleState(
if (sync_state_ == new_sync_state) { sync_service_, syncer::HISTORY_DELETE_DIRECTIVES);
if (history_sync_state_ == new_sync_state)
return NO_ACTION; return NO_ACTION;
}
SyncState old_sync_state = sync_state_; syncer::UploadState old_sync_state = history_sync_state_;
sync_state_ = new_sync_state; history_sync_state_ = new_sync_state;
switch (new_sync_state) { switch (new_sync_state) {
case NOT_INITIALIZED_ENABLED: case syncer::UploadState::INITIALIZING:
break; // In this state, we do not issue server requests, but we will serve from
case INITIALIZED_ENABLED_HISTORY: // cache if available -> no action required.
// If the user just signed in, we fetch suggestions, so that hopefully the return NO_ACTION;
// next NTP will already get them. case syncer::UploadState::ACTIVE:
if (old_sync_state == SYNC_OR_HISTORY_SYNC_DISABLED) { // If history sync was just enabled, immediately fetch suggestions, so
// that hopefully the next NTP will already get them.
if (old_sync_state == syncer::UploadState::NOT_ACTIVE)
return FETCH_SUGGESTIONS; return FETCH_SUGGESTIONS;
} // Otherwise, this just means sync initialization finished.
break; return NO_ACTION;
case SYNC_OR_HISTORY_SYNC_DISABLED: case syncer::UploadState::NOT_ACTIVE:
// If the user signed out (or disabled history sync), we have to clear // If the user signed out (or disabled history sync), we have to clear
// everything. // everything.
return CLEAR_SUGGESTIONS; return CLEAR_SUGGESTIONS;
} }
// Otherwise, there's nothing to do. NOTREACHED();
return NO_ACTION; return NO_ACTION;
} }
void SuggestionsServiceImpl::OnStateChanged(syncer::SyncService* sync) { void SuggestionsServiceImpl::OnStateChanged(syncer::SyncService* sync) {
DCHECK(sync_service_ == sync); DCHECK(sync_service_ == sync);
switch (RefreshSyncState()) { switch (RefreshHistorySyncState()) {
case NO_ACTION: case NO_ACTION:
break; break;
case CLEAR_SUGGESTIONS: case CLEAR_SUGGESTIONS:
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "components/suggestions/proto/suggestions.pb.h" #include "components/suggestions/proto/suggestions.pb.h"
#include "components/suggestions/suggestions_service.h" #include "components/suggestions/suggestions_service.h"
#include "components/sync/driver/sync_service_observer.h" #include "components/sync/driver/sync_service_observer.h"
#include "components/sync/driver/sync_service_utils.h"
#include "google_apis/gaia/google_service_auth_error.h" #include "google_apis/gaia/google_service_auth_error.h"
#include "net/base/backoff_entry.h" #include "net/base/backoff_entry.h"
#include "net/url_request/url_fetcher_delegate.h" #include "net/url_request/url_fetcher_delegate.h"
...@@ -111,7 +112,8 @@ class SuggestionsServiceImpl : public SuggestionsService, ...@@ -111,7 +112,8 @@ class SuggestionsServiceImpl : public SuggestionsService,
SYNC_OR_HISTORY_SYNC_DISABLED, SYNC_OR_HISTORY_SYNC_DISABLED,
}; };
// The action that should be taken as the result of a RefreshSyncState call. // The action that should be taken as the result of a RefreshHistorySyncState
// call.
enum RefreshAction { NO_ACTION, FETCH_SUGGESTIONS, CLEAR_SUGGESTIONS }; enum RefreshAction { NO_ACTION, FETCH_SUGGESTIONS, CLEAR_SUGGESTIONS };
// Helpers to build the various suggestions URLs. These are static members // Helpers to build the various suggestions URLs. These are static members
...@@ -122,12 +124,9 @@ class SuggestionsServiceImpl : public SuggestionsService, ...@@ -122,12 +124,9 @@ class SuggestionsServiceImpl : public SuggestionsService,
static GURL BuildSuggestionsBlacklistURL(const GURL& candidate_url); static GURL BuildSuggestionsBlacklistURL(const GURL& candidate_url);
static GURL BuildSuggestionsBlacklistClearURL(); static GURL BuildSuggestionsBlacklistClearURL();
// Computes the appropriate SyncState from |sync_service_|. // Re-computes |history_sync_state_| from the sync service. Returns the action
SyncState ComputeSyncState() const; // that should be taken in response.
RefreshAction RefreshHistorySyncState() WARN_UNUSED_RESULT;
// Re-computes |sync_state_| from the sync service. Returns the action that
// should be taken in response.
RefreshAction RefreshSyncState() WARN_UNUSED_RESULT;
// syncer::SyncServiceObserver implementation. // syncer::SyncServiceObserver implementation.
void OnStateChanged(syncer::SyncService* sync) override; void OnStateChanged(syncer::SyncService* sync) override;
...@@ -182,7 +181,8 @@ class SuggestionsServiceImpl : public SuggestionsService, ...@@ -182,7 +181,8 @@ class SuggestionsServiceImpl : public SuggestionsService,
ScopedObserver<syncer::SyncService, syncer::SyncServiceObserver> ScopedObserver<syncer::SyncService, syncer::SyncServiceObserver>
sync_service_observer_; sync_service_observer_;
SyncState sync_state_; // The state of history sync, i.e. are we uploading history data to Google?
syncer::UploadState history_sync_state_;
net::URLRequestContextGetter* url_request_context_; net::URLRequestContextGetter* url_request_context_;
......
...@@ -87,6 +87,9 @@ class MockSyncService : public syncer::FakeSyncService { ...@@ -87,6 +87,9 @@ class MockSyncService : public syncer::FakeSyncService {
MOCK_CONST_METHOD0(CanSyncStart, bool()); MOCK_CONST_METHOD0(CanSyncStart, bool());
MOCK_CONST_METHOD0(IsSyncActive, bool()); MOCK_CONST_METHOD0(IsSyncActive, bool());
MOCK_CONST_METHOD0(ConfigurationDone, bool()); MOCK_CONST_METHOD0(ConfigurationDone, bool());
MOCK_CONST_METHOD0(IsLocalSyncEnabled, bool());
MOCK_CONST_METHOD0(IsUsingSecondaryPassphrase, bool());
MOCK_CONST_METHOD0(GetPreferredDataTypes, syncer::ModelTypeSet());
MOCK_CONST_METHOD0(GetActiveDataTypes, syncer::ModelTypeSet()); MOCK_CONST_METHOD0(GetActiveDataTypes, syncer::ModelTypeSet());
}; };
...@@ -157,6 +160,16 @@ class SuggestionsServiceTest : public testing::Test { ...@@ -157,6 +160,16 @@ class SuggestionsServiceTest : public testing::Test {
EXPECT_CALL(*sync_service(), ConfigurationDone()) EXPECT_CALL(*sync_service(), ConfigurationDone())
.Times(AnyNumber()) .Times(AnyNumber())
.WillRepeatedly(Return(true)); .WillRepeatedly(Return(true));
EXPECT_CALL(*sync_service(), IsLocalSyncEnabled())
.Times(AnyNumber())
.WillRepeatedly(Return(false));
EXPECT_CALL(*sync_service(), IsUsingSecondaryPassphrase())
.Times(AnyNumber())
.WillRepeatedly(Return(false));
EXPECT_CALL(*sync_service(), GetPreferredDataTypes())
.Times(AnyNumber())
.WillRepeatedly(
Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES)));
EXPECT_CALL(*sync_service(), GetActiveDataTypes()) EXPECT_CALL(*sync_service(), GetActiveDataTypes())
.Times(AnyNumber()) .Times(AnyNumber())
.WillRepeatedly( .WillRepeatedly(
......
...@@ -23,7 +23,8 @@ UploadState GetUploadToGoogleState(const SyncService* sync_service, ...@@ -23,7 +23,8 @@ UploadState GetUploadToGoogleState(const SyncService* sync_service,
// Note: Before configuration is done, GetPreferredDataTypes returns // Note: Before configuration is done, GetPreferredDataTypes returns
// "everything" (i.e. the default setting). If a data type is missing there, // "everything" (i.e. the default setting). If a data type is missing there,
// it must be because the user explicitly disabled it. // it must be because the user explicitly disabled it.
if (!sync_service->CanSyncStart() || sync_service->IsLocalSyncEnabled() || if (!sync_service || !sync_service->CanSyncStart() ||
sync_service->IsLocalSyncEnabled() ||
!sync_service->GetPreferredDataTypes().Has(type) || !sync_service->GetPreferredDataTypes().Has(type) ||
sync_service->GetAuthError().IsPersistentError() || sync_service->GetAuthError().IsPersistentError() ||
sync_service->IsUsingSecondaryPassphrase()) { sync_service->IsUsingSecondaryPassphrase()) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment