1 // Copyright (c) 2012 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 #ifndef COMPONENTS_SYNC_BASE_SYNC_PREFS_H_
6 #define COMPONENTS_SYNC_BASE_SYNC_PREFS_H_
7 
8 #include <stdint.h>
9 
10 #include <map>
11 #include <memory>
12 #include <string>
13 
14 #include "base/compiler_specific.h"
15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/sequence_checker.h"
19 #include "base/time/time.h"
20 #include "build/build_config.h"
21 #include "components/prefs/pref_member.h"
22 #include "components/sync/base/model_type.h"
23 #include "components/sync/base/user_selectable_type.h"
24 #include "components/sync/protocol/sync.pb.h"
25 
26 class PrefRegistrySimple;
27 class PrefService;
28 
29 namespace syncer {
30 
31 class SyncPrefObserver {
32  public:
33   virtual void OnSyncManagedPrefChange(bool is_sync_managed) = 0;
34   virtual void OnFirstSetupCompletePrefChange(bool is_first_setup_complete) = 0;
35   virtual void OnSyncRequestedPrefChange(bool is_sync_requested) = 0;
36   virtual void OnPreferredDataTypesPrefChange() = 0;
37 
38  protected:
39   virtual ~SyncPrefObserver();
40 };
41 
42 // Use this for crypto/passphrase-related parts of sync prefs.
43 class CryptoSyncPrefs {
44  public:
45   virtual ~CryptoSyncPrefs();
46 
47   // Use this encryption bootstrap token if we're using an explicit passphrase.
48   virtual std::string GetEncryptionBootstrapToken() const = 0;
49   virtual void SetEncryptionBootstrapToken(const std::string& token) = 0;
50 
51   // Use this keystore bootstrap token if we're not using an explicit
52   // passphrase.
53   virtual std::string GetKeystoreEncryptionBootstrapToken() const = 0;
54   virtual void SetKeystoreEncryptionBootstrapToken(
55       const std::string& token) = 0;
56 };
57 
58 // SyncPrefs is a helper class that manages getting, setting, and persisting
59 // global sync preferences. It is not thread-safe, and lives on the UI thread.
60 class SyncPrefs : public CryptoSyncPrefs,
61                   public base::SupportsWeakPtr<SyncPrefs> {
62  public:
63   // |pref_service| must not be null and must outlive this object.
64   explicit SyncPrefs(PrefService* pref_service);
65   ~SyncPrefs() override;
66 
67   static void RegisterProfilePrefs(PrefRegistrySimple* registry);
68 
69   void AddSyncPrefObserver(SyncPrefObserver* sync_pref_observer);
70   void RemoveSyncPrefObserver(SyncPrefObserver* sync_pref_observer);
71 
72   // Clears "bookkeeping" sync preferences, such as the last synced time,
73   // whether the last shutdown was clean, etc. Does *not* clear sync preferences
74   // which are directly user-controlled, such as the set of selected types.
75   void ClearLocalSyncTransportData();
76 
77   // Getters and setters for global sync prefs.
78 
79   // First-Setup-Complete is conceptually similar to the user's consent to
80   // enable sync-the-feature.
81   bool IsFirstSetupComplete() const;
82   void SetFirstSetupComplete();
83   void ClearFirstSetupComplete();
84 
85   bool IsSyncRequested() const;
86   void SetSyncRequested(bool is_requested);
87   void SetSyncRequestedIfNotSetExplicitly();
88 
89   base::Time GetLastSyncedTime() const;
90   void SetLastSyncedTime(base::Time time);
91 
92   base::Time GetLastPollTime() const;
93   void SetLastPollTime(base::Time time);
94 
95   base::TimeDelta GetPollInterval() const;
96   void SetPollInterval(base::TimeDelta interval);
97 
98   bool HasKeepEverythingSynced() const;
99 
100   // Returns UserSelectableTypeSet::All() if HasKeepEverythingSynced() is true.
101   UserSelectableTypeSet GetSelectedTypes() const;
102 
103   // Sets the selection state for all |registered_types| and "keep everything
104   // synced" flag.
105   // |keep_everything_synced| indicates that all current and future types
106   // should be synced. If this is set to true, then GetSelectedTypes() will
107   // always return UserSelectableTypeSet::All(), even if not all of them are
108   // registered or individually marked as selected.
109   // Changes are still made to the individual selectable type prefs even if
110   // |keep_everything_synced| is true, but won't be visible until it's set to
111   // false.
112   void SetSelectedTypes(bool keep_everything_synced,
113                         UserSelectableTypeSet registered_types,
114                         UserSelectableTypeSet selected_types);
115 
116 #if defined(OS_CHROMEOS)
117   // Chrome OS provides a separate settings UI surface for sync of OS types,
118   // including a separate "Sync All" toggle for OS types.
119   bool IsSyncAllOsTypesEnabled() const;
120   UserSelectableOsTypeSet GetSelectedOsTypes() const;
121   void SetSelectedOsTypes(bool sync_all_os_types,
122                           UserSelectableOsTypeSet registered_types,
123                           UserSelectableOsTypeSet selected_types);
124   bool IsOsSyncFeatureEnabled() const;
125   void SetOsSyncFeatureEnabled(bool enabled);
126 
127   // Maps |type| to its corresponding preference name. Returns nullptr if |type|
128   // isn't an OS type.
129   static const char* GetPrefNameForOsType(UserSelectableOsType type);
130 #endif
131 
132   // Whether Sync is forced off by enterprise policy. Note that this only covers
133   // one out of two types of policy, "browser" policy. The second kind, "cloud"
134   // policy, is handled directly in ProfileSyncService.
135   bool IsManaged() const;
136 
137   // The encryption bootstrap token is used for explicit passphrase users
138   // (usually custom passphrase) and represents a user-entered passphrase.
139   // Hence, it gets treated as user-controlled similarly to sync datatype
140   // selection settings (i.e. doesn't get cleared in
141   // ClearLocalSyncTransportData()).
142   std::string GetEncryptionBootstrapToken() const override;
143   void SetEncryptionBootstrapToken(const std::string& token) override;
144   void ClearEncryptionBootstrapToken();
145 
146   // Use this keystore bootstrap token if we're not using an explicit
147   // passphrase.
148   std::string GetKeystoreEncryptionBootstrapToken() const override;
149   void SetKeystoreEncryptionBootstrapToken(const std::string& token) override;
150 
151   // Maps |type| to its corresponding preference name.
152   static const char* GetPrefNameForType(UserSelectableType type);
153 
154   void SetGaiaId(const std::string& gaia_id);
155   std::string GetGaiaId() const;
156   void SetCacheGuid(const std::string& cache_guid);
157   std::string GetCacheGuid() const;
158   void SetBirthday(const std::string& birthday);
159   std::string GetBirthday() const;
160   void SetBagOfChips(const std::string& bag_of_chips);
161   std::string GetBagOfChips() const;
162 
163   // Out of band sync passphrase prompt getter/setter.
164   bool IsPassphrasePrompted() const;
165   void SetPassphrasePrompted(bool value);
166 
167 #if defined(OS_ANDROID)
168   // Sets a boolean pref representing that Sync should no longer respect whether
169   // Android master sync is enabled/disabled. It is set per-device and never
170   // gets cleared.
171   void SetDecoupledFromAndroidMasterSync();
172 
173   // Gets the value for the boolean pref representing whether Sync should no
174   // longer respect if Android master sync is enabled/disabled. Returns false
175   // until |SetDecoupledFromAndroidMasterSync()| is called.
176   bool GetDecoupledFromAndroidMasterSync();
177 #endif  // defined(OS_ANDROID)
178 
179   // For testing.
180   void SetManagedForTest(bool is_managed);
181 
182   // Get/set for the last known sync invalidation versions.
183   std::map<ModelType, int64_t> GetInvalidationVersions() const;
184   void UpdateInvalidationVersions(
185       const std::map<ModelType, int64_t>& invalidation_versions);
186 
187   // Will return the contents of the LastRunVersion preference. This may be an
188   // empty string if no version info was present, and is only valid at
189   // Sync startup time (after which the LastRunVersion preference will have been
190   // updated to the current version).
191   std::string GetLastRunVersion() const;
192   void SetLastRunVersion(const std::string& current_version);
193 
194   // Gets the local sync backend enabled state.
195   bool IsLocalSyncEnabled() const;
196 
197  private:
198   static void RegisterTypeSelectedPref(PrefRegistrySimple* prefs,
199                                        UserSelectableType type);
200 
201   void OnSyncManagedPrefChanged();
202   void OnFirstSetupCompletePrefChange();
203   void OnSyncRequestedPrefChange();
204 
205   // Never null.
206   PrefService* const pref_service_;
207 
208   base::ObserverList<SyncPrefObserver>::Unchecked sync_pref_observers_;
209 
210   // The preference that controls whether sync is under control by
211   // configuration management.
212   BooleanPrefMember pref_sync_managed_;
213 
214   BooleanPrefMember pref_first_setup_complete_;
215 
216   BooleanPrefMember pref_sync_requested_;
217 
218   bool local_sync_enabled_;
219 
220   SEQUENCE_CHECKER(sequence_checker_);
221 
222   DISALLOW_COPY_AND_ASSIGN(SyncPrefs);
223 };
224 
225 void MigrateSessionsToProxyTabsPrefs(PrefService* pref_service);
226 void ClearObsoleteUserTypePrefs(PrefService* pref_service);
227 void ClearObsoleteClearServerDataPrefs(PrefService* pref_service);
228 void ClearObsoleteAuthErrorPrefs(PrefService* pref_service);
229 void ClearObsoleteFirstSyncTime(PrefService* pref_service);
230 void ClearObsoleteSyncLongPollIntervalSeconds(PrefService* pref_service);
231 void MigrateSyncSuppressedPref(PrefService* pref_service);
232 
233 }  // namespace syncer
234 
235 #endif  // COMPONENTS_SYNC_BASE_SYNC_PREFS_H_
236