OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| 6 #include "components/signin/core/browser/signin_manager.h" |
| 7 #include "components/sync_driver/sync_service.h" |
| 8 |
| 9 namespace ntp_snippets { |
| 10 |
| 11 NTPSnippetsStatusService::NTPSnippetsStatusService( |
| 12 SigninManagerBase* signin_manager, |
| 13 sync_driver::SyncService* sync_service) |
| 14 : disabled_reason_(DisabledReason::EXPLICITLY_DISABLED), |
| 15 signin_manager_(signin_manager), |
| 16 sync_service_(sync_service), |
| 17 sync_service_observer_(this) {} |
| 18 |
| 19 NTPSnippetsStatusService::~NTPSnippetsStatusService() {} |
| 20 |
| 21 void NTPSnippetsStatusService::Init( |
| 22 const DisabledReasonChangeCallback& callback) { |
| 23 DCHECK(disabled_reason_change_callback_.is_null()); |
| 24 |
| 25 disabled_reason_change_callback_ = callback; |
| 26 |
| 27 // Notify about the current state before registering the observer, to make |
| 28 // sure we don't get a double notification due to an undefined start state. |
| 29 disabled_reason_ = GetDisabledReasonFromDeps(); |
| 30 disabled_reason_change_callback_.Run(disabled_reason_); |
| 31 |
| 32 sync_service_observer_.Add(sync_service_); |
| 33 } |
| 34 |
| 35 void NTPSnippetsStatusService::OnStateChanged() { |
| 36 DisabledReason new_disabled_reason = GetDisabledReasonFromDeps(); |
| 37 |
| 38 if (new_disabled_reason == disabled_reason_) |
| 39 return; |
| 40 |
| 41 disabled_reason_ = new_disabled_reason; |
| 42 disabled_reason_change_callback_.Run(disabled_reason_); |
| 43 } |
| 44 |
| 45 DisabledReason NTPSnippetsStatusService::GetDisabledReasonFromDeps() const { |
| 46 if (!signin_manager_ || !signin_manager_->IsAuthenticated()) { |
| 47 DVLOG(1) << "[GetNewDisabledReason] Signed out"; |
| 48 return DisabledReason::SIGNED_OUT; |
| 49 } |
| 50 |
| 51 if (!sync_service_ || !sync_service_->CanSyncStart()) { |
| 52 DVLOG(1) << "[GetNewDisabledReason] Sync disabled"; |
| 53 return DisabledReason::SYNC_DISABLED; |
| 54 } |
| 55 |
| 56 // !IsSyncActive in cases where CanSyncStart is true hints at the backend not |
| 57 // being initialized. |
| 58 // ConfigurationDone() verifies that the sync service has properly loaded its |
| 59 // configuration and is aware of the different data types to sync. |
| 60 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) { |
| 61 DVLOG(1) << "[GetNewDisabledReason] Sync initialization is not complete."; |
| 62 return DisabledReason::HISTORY_SYNC_STATE_UNKNOWN; |
| 63 } |
| 64 |
| 65 if (sync_service_->IsEncryptEverythingEnabled()) { |
| 66 DVLOG(1) << "[GetNewDisabledReason] Encryption is enabled"; |
| 67 return DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED; |
| 68 } |
| 69 |
| 70 if (!sync_service_->GetActiveDataTypes().Has( |
| 71 syncer::HISTORY_DELETE_DIRECTIVES)) { |
| 72 DVLOG(1) << "[GetNewDisabledReason] History sync disabled"; |
| 73 return DisabledReason::HISTORY_SYNC_DISABLED; |
| 74 } |
| 75 |
| 76 DVLOG(1) << "[GetNewDisabledReason] Enabled"; |
| 77 return DisabledReason::NONE; |
| 78 } |
| 79 |
| 80 } // namespace ntp_snippets |
OLD | NEW |