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 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_STATUS_SERVICE_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_STATUS_SERVICE_H_ |
6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_STATUS_SERVICE_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_STATUS_SERVICE_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/gtest_prod_util.h" | 9 #include "base/gtest_prod_util.h" |
10 #include "base/scoped_observer.h" | 10 #include "base/scoped_observer.h" |
11 #include "components/prefs/pref_change_registrar.h" | 11 #include "components/prefs/pref_change_registrar.h" |
12 #include "components/sync_driver/sync_service_observer.h" | 12 #include "components/signin/core/browser/signin_manager.h" |
13 | 13 |
14 class PrefRegistrySimple; | 14 class PrefRegistrySimple; |
15 class PrefService; | 15 class PrefService; |
16 class SigninManagerBase; | |
17 | |
18 namespace sync_driver { | |
19 class SyncService; | |
20 } | |
21 | 16 |
22 namespace ntp_snippets { | 17 namespace ntp_snippets { |
23 | 18 |
24 enum class DisabledReason : int { | 19 enum class DisabledReason : int { |
25 // Snippets are enabled | 20 // Snippets are enabled |
26 NONE, | 21 NONE, |
27 // Snippets have been disabled as part of the service configuration. | 22 // Snippets have been disabled as part of the service configuration. |
28 EXPLICITLY_DISABLED, | 23 EXPLICITLY_DISABLED, |
29 // The user is not signed in, and the service requires it to be enabled. | 24 // The user is not signed in, and the service requires it to be enabled. |
30 SIGNED_OUT, | 25 SIGNED_OUT, |
31 // Sync is not enabled, and the service requires it to be enabled. | |
32 SYNC_DISABLED, | |
33 // The service requires passphrase encryption to be disabled. | |
34 PASSPHRASE_ENCRYPTION_ENABLED, | |
35 // History sync is not enabled, and the service requires it to be enabled. | |
36 HISTORY_SYNC_DISABLED, | |
37 // The sync service is not completely initialized, and the status is unknown. | |
38 HISTORY_SYNC_STATE_UNKNOWN | |
39 }; | 26 }; |
40 | 27 |
41 // Aggregates data from sync and signin to notify the snippet service of | 28 // Aggregates data from preferences and signin to notify the snippet service of |
42 // relevant changes in their states. | 29 // relevant changes in their states. |
43 class NTPSnippetsStatusService : public sync_driver::SyncServiceObserver { | 30 class NTPSnippetsStatusService : public SigninManagerBase::Observer { |
44 public: | 31 public: |
45 typedef base::Callback<void(DisabledReason)> DisabledReasonChangeCallback; | 32 typedef base::Callback<void(DisabledReason)> DisabledReasonChangeCallback; |
46 | 33 |
47 NTPSnippetsStatusService(SigninManagerBase* signin_manager, | 34 NTPSnippetsStatusService(SigninManagerBase* signin_manager, |
48 sync_driver::SyncService* sync_service, | |
49 PrefService* pref_service); | 35 PrefService* pref_service); |
50 | 36 |
51 ~NTPSnippetsStatusService() override; | 37 ~NTPSnippetsStatusService() override; |
52 | 38 |
53 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 39 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
54 | 40 |
55 // Starts listening for changes from the dependencies. |callback| will be | 41 // Starts listening for changes from the dependencies. |callback| will be |
56 // called when a significant change in state is detected. | 42 // called when a significant change in state is detected. |
57 void Init(const DisabledReasonChangeCallback& callback); | 43 void Init(const DisabledReasonChangeCallback& callback); |
58 | 44 |
59 DisabledReason disabled_reason() const { return disabled_reason_; } | 45 DisabledReason disabled_reason() const { return disabled_reason_; } |
60 | 46 |
61 private: | 47 private: |
62 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsStatusServiceTest, | 48 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsStatusServiceTest, |
63 SyncStateCompatibility); | 49 SigninStateCompatibility); |
64 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsStatusServiceTest, DisabledViaPref); | 50 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsStatusServiceTest, DisabledViaPref); |
65 | 51 |
66 // sync_driver::SyncServiceObserver implementation. Also used as a callback | 52 // SigninManagerBase::Observer implementation |
67 // for the PrefChangeRegistrar. | 53 void GoogleSigninSucceeded(const std::string& account_id, |
68 void OnStateChanged() override; | 54 const std::string& username, |
| 55 const std::string& password) override; |
| 56 void GoogleSignedOut(const std::string& account_id, |
| 57 const std::string& username) override; |
| 58 |
| 59 // Callback for the PrefChangeRegistrar. |
| 60 void OnStateChanged(); |
69 | 61 |
70 DisabledReason GetDisabledReasonFromDeps() const; | 62 DisabledReason GetDisabledReasonFromDeps() const; |
71 | 63 |
72 DisabledReason disabled_reason_; | 64 DisabledReason disabled_reason_; |
73 DisabledReasonChangeCallback disabled_reason_change_callback_; | 65 DisabledReasonChangeCallback disabled_reason_change_callback_; |
74 | 66 |
75 SigninManagerBase* signin_manager_; | 67 SigninManagerBase* signin_manager_; |
76 sync_driver::SyncService* sync_service_; | |
77 PrefService* pref_service_; | 68 PrefService* pref_service_; |
78 | 69 |
79 PrefChangeRegistrar pref_change_registrar_; | 70 PrefChangeRegistrar pref_change_registrar_; |
80 | 71 |
81 // The observer for the SyncService. When the sync state changes, | 72 // The observer for the SigninManager. |
82 // SyncService will call |OnStateChanged|. | 73 ScopedObserver<SigninManagerBase, SigninManagerBase::Observer> |
83 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> | 74 signin_observer_; |
84 sync_service_observer_; | |
85 | 75 |
86 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsStatusService); | 76 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsStatusService); |
87 }; | 77 }; |
88 | 78 |
89 } // namespace ntp_snippets | 79 } // namespace ntp_snippets |
90 | 80 |
91 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_STATUS_SERVICE_H_ | 81 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_STATUS_SERVICE_H_ |
OLD | NEW |