Index: components/ntp_snippets/ntp_snippets_service.cc |
diff --git a/components/ntp_snippets/ntp_snippets_service.cc b/components/ntp_snippets/ntp_snippets_service.cc |
index 040c1c82c926f186caa88dfb4c7ff668ed2a6d8f..454dd537aed56193e1e2dc9e027785b52e212868 100644 |
--- a/components/ntp_snippets/ntp_snippets_service.cc |
+++ b/components/ntp_snippets/ntp_snippets_service.cc |
@@ -26,6 +26,7 @@ |
#include "components/ntp_snippets/switches.h" |
#include "components/prefs/pref_registry_simple.h" |
#include "components/prefs/pref_service.h" |
+#include "components/signin/core/browser/signin_manager.h" |
#include "components/suggestions/proto/suggestions.pb.h" |
#include "components/sync_driver/sync_service.h" |
#include "components/variations/variations_associated_data.h" |
@@ -180,6 +181,7 @@ void Compact(NTPSnippet::PtrVector* snippets) { |
NTPSnippetsService::NTPSnippetsService( |
bool enabled, |
PrefService* pref_service, |
+ SigninManagerBase* signin_manager, |
sync_driver::SyncService* sync_service, |
SuggestionsService* suggestions_service, |
const std::string& application_language_code, |
@@ -188,8 +190,10 @@ NTPSnippetsService::NTPSnippetsService( |
std::unique_ptr<ImageFetcher> image_fetcher, |
std::unique_ptr<NTPSnippetsDatabase> database) |
: state_(State::NOT_INITED), |
- explicitly_disabled_(!enabled), |
+ disabled_reason_(enabled ? DisabledReason::NONE |
+ : DisabledReason::EXPLICITLY_DISABLED), |
pref_service_(pref_service), |
+ signin_manager_(signin_manager), |
sync_service_(sync_service), |
sync_service_observer_(this), |
suggestions_service_(suggestions_service), |
@@ -202,7 +206,8 @@ NTPSnippetsService::NTPSnippetsService( |
// TODO(dgn) should be removed after branch point (https:://crbug.com/617585). |
ClearDeprecatedPrefs(); |
- if (explicitly_disabled_) { |
+ if (disabled_reason_ == DisabledReason::EXPLICITLY_DISABLED) { |
+ // Don't even bother loading the database. |
EnterState(State::DISABLED); |
return; |
} |
@@ -343,34 +348,6 @@ void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
observers_.RemoveObserver(observer); |
} |
-DisabledReason NTPSnippetsService::GetDisabledReason() const { |
- if (explicitly_disabled_) |
- return DisabledReason::EXPLICITLY_DISABLED; |
- |
- if (!sync_service_ || !sync_service_->CanSyncStart()) { |
- DVLOG(1) << "[GetDisabledReason] Sync disabled"; |
- return DisabledReason::HISTORY_SYNC_DISABLED; |
- } |
- |
- // !IsSyncActive in cases where CanSyncStart is true hints at the backend not |
- // being initialized. |
- // ConfigurationDone() verifies that the sync service has properly loaded its |
- // configuration and is aware of the different data types to sync. |
- if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) { |
- DVLOG(1) << "[GetDisabledReason] Sync initialization is not complete."; |
- return DisabledReason::HISTORY_SYNC_STATE_UNKNOWN; |
- } |
- |
- if (!sync_service_->GetActiveDataTypes().Has( |
- syncer::HISTORY_DELETE_DIRECTIVES)) { |
- DVLOG(1) << "[GetDisabledReason] History sync disabled"; |
- return DisabledReason::HISTORY_SYNC_DISABLED; |
- } |
- |
- DVLOG(1) << "[GetDisabledReason] Enabled"; |
- return DisabledReason::NONE; |
-} |
- |
// static |
int NTPSnippetsService::GetMaxSnippetCountForTesting() { |
return kMaxSnippetCount; |
@@ -624,8 +601,6 @@ void NTPSnippetsService::EnterStateDisabled() { |
expiry_timer_.Stop(); |
RescheduleFetching(); |
- FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
- NTPSnippetsServiceDisabled()); |
} |
void NTPSnippetsService::EnterStateShutdown() { |
@@ -635,7 +610,8 @@ void NTPSnippetsService::EnterStateShutdown() { |
expiry_timer_.Stop(); |
suggestions_service_subscription_.reset(); |
- if (sync_service_) |
+ // When explicity disabled we don't register the observer. |
+ if (sync_service_ && disabled_reason_ != DisabledReason::EXPLICITLY_DISABLED) |
sync_service_observer_.Remove(sync_service_); |
} |
@@ -658,17 +634,62 @@ void NTPSnippetsService::FinishInitialization() { |
FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
NTPSnippetsServiceLoaded()); |
+} |
- // Start a fetch if we don't have any snippets yet, or a fetch was requested |
- // earlier. |
- if (ready() && (snippets_.empty() || fetch_after_load_)) { |
- fetch_after_load_ = false; |
- FetchSnippets(); |
+void NTPSnippetsService::UpdateDisabledReason() { |
+ DisabledReason new_disabled_reason = GetNewDisabledReason(); |
+ |
+ if (new_disabled_reason == disabled_reason_) |
+ return; |
+ |
+ disabled_reason_ = new_disabled_reason; |
+ FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
+ NTPSnippetsServiceDisabledReasonChanged(disabled_reason_)); |
+} |
+ |
+DisabledReason NTPSnippetsService::GetNewDisabledReason() const { |
+ if (disabled_reason_ == DisabledReason::EXPLICITLY_DISABLED) { |
+ DVLOG(1) << "[GetNewDisabledReason] Explicitly disabled"; |
+ return DisabledReason::EXPLICITLY_DISABLED; |
+ } |
+ |
+ if (!signin_manager_ || !signin_manager_->IsAuthenticated()) { |
+ DVLOG(1) << "[GetNewDisabledReason] Signed out"; |
+ return DisabledReason::SIGNED_OUT; |
} |
+ |
+ if (!sync_service_ || !sync_service_->CanSyncStart()) { |
+ DVLOG(1) << "[GetNewDisabledReason] Sync disabled"; |
+ return DisabledReason::SYNC_DISABLED; |
+ } |
+ |
+ // !IsSyncActive in cases where CanSyncStart is true hints at the backend not |
+ // being initialized. |
+ // ConfigurationDone() verifies that the sync service has properly loaded its |
+ // configuration and is aware of the different data types to sync. |
+ if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) { |
+ DVLOG(1) << "[GetNewDisabledReason] Sync initialization is not complete."; |
+ return DisabledReason::HISTORY_SYNC_STATE_UNKNOWN; |
+ } |
+ |
+ if (sync_service_->IsEncryptEverythingEnabled()) { |
+ DVLOG(1) << "[GetNewDisabledReason] Encryption is enabled"; |
+ return DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED; |
+ } |
+ |
+ if (!sync_service_->GetActiveDataTypes().Has( |
+ syncer::HISTORY_DELETE_DIRECTIVES)) { |
+ DVLOG(1) << "[GetNewDisabledReason] History sync disabled"; |
+ return DisabledReason::HISTORY_SYNC_DISABLED; |
+ } |
+ |
+ DVLOG(1) << "[GetNewDisabledReason] Enabled"; |
+ return DisabledReason::NONE; |
} |
NTPSnippetsService::State NTPSnippetsService::GetStateForDependenciesStatus() { |
- switch (GetDisabledReason()) { |
+ UpdateDisabledReason(); |
+ switch (disabled_reason_) { |
case DisabledReason::NONE: |
return State::READY; |
@@ -677,10 +698,13 @@ NTPSnippetsService::State NTPSnippetsService::GetStateForDependenciesStatus() { |
// state is and we just return the current one. If things change, |
// |OnStateChanged| will call this function again to update the state. |
DVLOG(1) << "Sync configuration incomplete, continuing based on the " |
- "current state."; |
+ "current state."; |
return state_; |
case DisabledReason::EXPLICITLY_DISABLED: |
+ case DisabledReason::SIGNED_OUT: |
+ case DisabledReason::SYNC_DISABLED: |
+ case DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED: |
case DisabledReason::HISTORY_SYNC_DISABLED: |
return State::DISABLED; |
} |
@@ -703,9 +727,7 @@ void NTPSnippetsService::EnterState(State state) { |
case State::READY: { |
DCHECK(state_ == State::NOT_INITED || state_ == State::DISABLED); |
- // If the service was previously disabled, we will need to start a fetch |
- // because otherwise there won't be any. |
- bool fetch_snippets = state_ == State::DISABLED || fetch_after_load_; |
+ bool fetch_snippets = snippets_.empty() || fetch_after_load_; |
DVLOG(1) << "Entering state: READY"; |
state_ = State::READY; |
fetch_after_load_ = false; |