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 if (disabled_reason_change_callback_.is_null()) | |
21 return; | |
22 | |
23 disabled_reason_change_callback_.Reset(); | |
Bernhard Bauer
2016/06/28 13:47:39
This isn't really necessary -- no one is going to
dgn
2016/06/28 16:38:57
Done.
| |
24 sync_service_observer_.Remove(sync_service_); | |
Bernhard Bauer
2016/06/28 13:47:39
? The point of ScopedObserver is that it will unre
dgn
2016/06/28 16:38:57
Moved over from NTPSnippetService but there it was
Bernhard Bauer
2016/06/28 16:47:25
Ah yes, ScopedObserver::Remove will actually remov
| |
25 } | |
26 | |
27 void NTPSnippetsStatusService::Init( | |
28 const DisabledReasonChangeCallback& callback) { | |
29 DCHECK(disabled_reason_change_callback_.is_null()); | |
30 | |
31 disabled_reason_change_callback_ = callback; | |
32 | |
33 // Notify about the current state before registering the observer, to make | |
34 // sure we don't get a double notification due to an undefined start state. | |
35 disabled_reason_ = GetDisabledReasonFromDeps(); | |
36 disabled_reason_change_callback_.Run(disabled_reason_); | |
37 | |
38 sync_service_observer_.Add(sync_service_); | |
39 } | |
40 | |
41 void NTPSnippetsStatusService::OnStateChanged() { | |
42 DisabledReason new_disabled_reason = GetDisabledReasonFromDeps(); | |
43 | |
44 if (new_disabled_reason == disabled_reason_) | |
45 return; | |
46 | |
47 disabled_reason_ = new_disabled_reason; | |
48 disabled_reason_change_callback_.Run(disabled_reason_); | |
49 } | |
50 | |
51 DisabledReason NTPSnippetsStatusService::GetDisabledReasonFromDeps() const { | |
52 if (!signin_manager_ || !signin_manager_->IsAuthenticated()) { | |
53 DVLOG(1) << "[GetNewDisabledReason] Signed out"; | |
54 return DisabledReason::SIGNED_OUT; | |
55 } | |
56 | |
57 if (!sync_service_ || !sync_service_->CanSyncStart()) { | |
58 DVLOG(1) << "[GetNewDisabledReason] Sync disabled"; | |
59 return DisabledReason::SYNC_DISABLED; | |
60 } | |
61 | |
62 // !IsSyncActive in cases where CanSyncStart is true hints at the backend not | |
63 // being initialized. | |
64 // ConfigurationDone() verifies that the sync service has properly loaded its | |
65 // configuration and is aware of the different data types to sync. | |
66 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) { | |
67 DVLOG(1) << "[GetNewDisabledReason] Sync initialization is not complete."; | |
68 return DisabledReason::HISTORY_SYNC_STATE_UNKNOWN; | |
69 } | |
70 | |
71 if (sync_service_->IsEncryptEverythingEnabled()) { | |
72 DVLOG(1) << "[GetNewDisabledReason] Encryption is enabled"; | |
73 return DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED; | |
74 } | |
75 | |
76 if (!sync_service_->GetActiveDataTypes().Has( | |
77 syncer::HISTORY_DELETE_DIRECTIVES)) { | |
78 DVLOG(1) << "[GetNewDisabledReason] History sync disabled"; | |
79 return DisabledReason::HISTORY_SYNC_DISABLED; | |
80 } | |
81 | |
82 DVLOG(1) << "[GetNewDisabledReason] Enabled"; | |
83 return DisabledReason::NONE; | |
84 } | |
85 | |
86 } // namespace ntp_snippets | |
OLD | NEW |