| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "components/ntp_snippets/ntp_snippets_status_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| 6 | 6 |
| 7 #include "components/ntp_snippets/pref_names.h" | 7 #include "components/ntp_snippets/pref_names.h" |
| 8 #include "components/prefs/pref_registry_simple.h" | 8 #include "components/prefs/pref_registry_simple.h" |
| 9 #include "components/prefs/pref_service.h" | 9 #include "components/prefs/pref_service.h" |
| 10 #include "components/signin/core/browser/signin_manager.h" | |
| 11 #include "components/sync_driver/sync_service.h" | |
| 12 | 10 |
| 13 namespace ntp_snippets { | 11 namespace ntp_snippets { |
| 14 | 12 |
| 15 NTPSnippetsStatusService::NTPSnippetsStatusService( | 13 NTPSnippetsStatusService::NTPSnippetsStatusService( |
| 16 SigninManagerBase* signin_manager, | 14 SigninManagerBase* signin_manager, |
| 17 sync_driver::SyncService* sync_service, | |
| 18 PrefService* pref_service) | 15 PrefService* pref_service) |
| 19 : disabled_reason_(DisabledReason::EXPLICITLY_DISABLED), | 16 : disabled_reason_(DisabledReason::EXPLICITLY_DISABLED), |
| 20 signin_manager_(signin_manager), | 17 signin_manager_(signin_manager), |
| 21 sync_service_(sync_service), | |
| 22 pref_service_(pref_service), | 18 pref_service_(pref_service), |
| 23 sync_service_observer_(this) {} | 19 signin_observer_(this) {} |
| 24 | 20 |
| 25 NTPSnippetsStatusService::~NTPSnippetsStatusService() {} | 21 NTPSnippetsStatusService::~NTPSnippetsStatusService() {} |
| 26 | 22 |
| 27 // static | 23 // static |
| 28 void NTPSnippetsStatusService::RegisterProfilePrefs( | 24 void NTPSnippetsStatusService::RegisterProfilePrefs( |
| 29 PrefRegistrySimple* registry) { | 25 PrefRegistrySimple* registry) { |
| 30 registry->RegisterBooleanPref(prefs::kEnableSnippets, true); | 26 registry->RegisterBooleanPref(prefs::kEnableSnippets, true); |
| 31 } | 27 } |
| 32 | 28 |
| 33 void NTPSnippetsStatusService::Init( | 29 void NTPSnippetsStatusService::Init( |
| 34 const DisabledReasonChangeCallback& callback) { | 30 const DisabledReasonChangeCallback& callback) { |
| 35 DCHECK(disabled_reason_change_callback_.is_null()); | 31 DCHECK(disabled_reason_change_callback_.is_null()); |
| 36 | 32 |
| 37 disabled_reason_change_callback_ = callback; | 33 disabled_reason_change_callback_ = callback; |
| 38 | 34 |
| 39 // Notify about the current state before registering the observer, to make | 35 // Notify about the current state before registering the observer, to make |
| 40 // sure we don't get a double notification due to an undefined start state. | 36 // sure we don't get a double notification due to an undefined start state. |
| 41 disabled_reason_ = GetDisabledReasonFromDeps(); | 37 disabled_reason_ = GetDisabledReasonFromDeps(); |
| 42 disabled_reason_change_callback_.Run(disabled_reason_); | 38 disabled_reason_change_callback_.Run(disabled_reason_); |
| 43 | 39 |
| 44 sync_service_observer_.Add(sync_service_); | 40 signin_observer_.Add(signin_manager_); |
| 45 | 41 |
| 46 pref_change_registrar_.Init(pref_service_); | 42 pref_change_registrar_.Init(pref_service_); |
| 47 pref_change_registrar_.Add( | 43 pref_change_registrar_.Add( |
| 48 prefs::kEnableSnippets, | 44 prefs::kEnableSnippets, |
| 49 base::Bind(&NTPSnippetsStatusService::OnStateChanged, | 45 base::Bind(&NTPSnippetsStatusService::OnStateChanged, |
| 50 base::Unretained(this))); | 46 base::Unretained(this))); |
| 51 } | 47 } |
| 52 | 48 |
| 53 void NTPSnippetsStatusService::OnStateChanged() { | 49 void NTPSnippetsStatusService::OnStateChanged() { |
| 54 DisabledReason new_disabled_reason = GetDisabledReasonFromDeps(); | 50 DisabledReason new_disabled_reason = GetDisabledReasonFromDeps(); |
| 55 | 51 |
| 56 if (new_disabled_reason == disabled_reason_) | 52 if (new_disabled_reason == disabled_reason_) |
| 57 return; | 53 return; |
| 58 | 54 |
| 59 disabled_reason_ = new_disabled_reason; | 55 disabled_reason_ = new_disabled_reason; |
| 60 disabled_reason_change_callback_.Run(disabled_reason_); | 56 disabled_reason_change_callback_.Run(disabled_reason_); |
| 61 } | 57 } |
| 62 | 58 |
| 59 void NTPSnippetsStatusService::GoogleSigninSucceeded( |
| 60 const std::string& account_id, |
| 61 const std::string& username, |
| 62 const std::string& password) { |
| 63 OnStateChanged(); |
| 64 } |
| 65 |
| 66 void NTPSnippetsStatusService::GoogleSignedOut(const std::string& account_id, |
| 67 const std::string& username) { |
| 68 OnStateChanged(); |
| 69 } |
| 70 |
| 63 DisabledReason NTPSnippetsStatusService::GetDisabledReasonFromDeps() const { | 71 DisabledReason NTPSnippetsStatusService::GetDisabledReasonFromDeps() const { |
| 64 if (!pref_service_->GetBoolean(prefs::kEnableSnippets)) { | 72 if (!pref_service_->GetBoolean(prefs::kEnableSnippets)) { |
| 65 DVLOG(1) << "[GetNewDisabledReason] Disabled via pref"; | 73 DVLOG(1) << "[GetNewDisabledReason] Disabled via pref"; |
| 66 return DisabledReason::EXPLICITLY_DISABLED; | 74 return DisabledReason::EXPLICITLY_DISABLED; |
| 67 } | 75 } |
| 68 | 76 |
| 69 if (!signin_manager_ || !signin_manager_->IsAuthenticated()) { | 77 if (!signin_manager_ || !signin_manager_->IsAuthenticated()) { |
| 70 DVLOG(1) << "[GetNewDisabledReason] Signed out"; | 78 DVLOG(1) << "[GetNewDisabledReason] Signed out"; |
| 71 return DisabledReason::SIGNED_OUT; | 79 return DisabledReason::SIGNED_OUT; |
| 72 } | 80 } |
| 73 | 81 |
| 74 if (!sync_service_ || !sync_service_->CanSyncStart()) { | |
| 75 DVLOG(1) << "[GetNewDisabledReason] Sync disabled"; | |
| 76 return DisabledReason::SYNC_DISABLED; | |
| 77 } | |
| 78 | |
| 79 // !IsSyncActive in cases where CanSyncStart is true hints at the backend not | |
| 80 // being initialized. | |
| 81 // ConfigurationDone() verifies that the sync service has properly loaded its | |
| 82 // configuration and is aware of the different data types to sync. | |
| 83 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) { | |
| 84 DVLOG(1) << "[GetNewDisabledReason] Sync initialization is not complete."; | |
| 85 return DisabledReason::HISTORY_SYNC_STATE_UNKNOWN; | |
| 86 } | |
| 87 | |
| 88 if (sync_service_->IsEncryptEverythingEnabled()) { | |
| 89 DVLOG(1) << "[GetNewDisabledReason] Encryption is enabled"; | |
| 90 return DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED; | |
| 91 } | |
| 92 | |
| 93 if (!sync_service_->GetActiveDataTypes().Has( | |
| 94 syncer::HISTORY_DELETE_DIRECTIVES)) { | |
| 95 DVLOG(1) << "[GetNewDisabledReason] History sync disabled"; | |
| 96 return DisabledReason::HISTORY_SYNC_DISABLED; | |
| 97 } | |
| 98 | |
| 99 DVLOG(1) << "[GetNewDisabledReason] Enabled"; | 82 DVLOG(1) << "[GetNewDisabledReason] Enabled"; |
| 100 return DisabledReason::NONE; | 83 return DisabledReason::NONE; |
| 101 } | 84 } |
| 102 | 85 |
| 103 } // namespace ntp_snippets | 86 } // namespace ntp_snippets |
| OLD | NEW |