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_DRIVER_SYNC_PREFS_H_
6 #define COMPONENTS_SYNC_DRIVER_SYNC_PREFS_H_
7
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/observer_list.h"
12 #include "base/prefs/pref_member.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "base/time/time.h"
15 #include "sync/internal_api/public/base/model_type.h"
16
17 class PrefService;
18 class ProfileIOData;
19
20 namespace user_prefs {
21 class PrefRegistrySyncable;
22 }
23
24 namespace sync_driver {
25
26 class SyncPrefObserver {
27 public:
28 // Called whenever the pref that controls whether sync is managed
29 // changes.
30 virtual void OnSyncManagedPrefChange(bool is_sync_managed) = 0;
31
32 protected:
33 virtual ~SyncPrefObserver();
34 };
35
36 // SyncPrefs is a helper class that manages getting, setting, and
37 // persisting global sync preferences. It is not thread-safe, and
38 // lives on the UI thread.
39 //
40 // TODO(akalin): Some classes still read the prefs directly. Consider
41 // passing down a pointer to SyncPrefs to them. A list of files:
42 //
43 // profile_sync_service_startup_unittest.cc
44 // profile_sync_service.cc
45 // sync_setup_flow.cc
46 // sync_setup_wizard.cc
47 // sync_setup_wizard_unittest.cc
48 // two_client_preferences_sync_test.cc
NON_EXPORTED_BASE(public base::NonThreadSafe)49 class SyncPrefs : NON_EXPORTED_BASE(public base::NonThreadSafe),
50 public base::SupportsWeakPtr<SyncPrefs> {
51 public:
52 // |pref_service| may not be NULL.
53 // Does not take ownership of |pref_service|.
54 explicit SyncPrefs(PrefService* pref_service);
55
56 // For testing.
57 SyncPrefs();
58
59 virtual ~SyncPrefs();
60
61 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
62
63 void AddSyncPrefObserver(SyncPrefObserver* sync_pref_observer);
64 void RemoveSyncPrefObserver(SyncPrefObserver* sync_pref_observer);
65
66 // Clears important sync preferences.
67 void ClearPreferences();
68
69 // Getters and setters for global sync prefs.
70
71 bool HasSyncSetupCompleted() const;
72 void SetSyncSetupCompleted();
73
74 bool SyncHasAuthError() const;
75 void SetSyncAuthError(bool error);
76
77 bool IsStartSuppressed() const;
78 void SetStartSuppressed(bool is_suppressed);
79
80 base::Time GetLastSyncedTime() const;
81 void SetLastSyncedTime(base::Time time);
82
83 bool HasKeepEverythingSynced() const;
84 void SetKeepEverythingSynced(bool keep_everything_synced);
85
86 // The returned set is guaranteed to be a subset of
87 // |registered_types|. Returns |registered_types| directly if
88 // HasKeepEverythingSynced() is true.
89 syncer::ModelTypeSet GetPreferredDataTypes(
90 syncer::ModelTypeSet registered_types) const;
91 // |preferred_types| should be a subset of |registered_types|. All
92 // types in |preferred_types| are marked preferred, and all types in
93 // |registered_types| \ |preferred_types| are marked not preferred.
94 // Changes are still made to the prefs even if
95 // HasKeepEverythingSynced() is true, but won't be visible until
96 // SetKeepEverythingSynced(false) is called.
97 void SetPreferredDataTypes(syncer::ModelTypeSet registered_types,
98 syncer::ModelTypeSet preferred_types);
99
100 // This pref is set outside of sync.
101 bool IsManaged() const;
102
103 // Use this encryption bootstrap token if we're using an explicit passphrase.
104 std::string GetEncryptionBootstrapToken() const;
105 void SetEncryptionBootstrapToken(const std::string& token);
106
107 // Use this keystore bootstrap token if we're not using an explicit
108 // passphrase.
109 std::string GetKeystoreEncryptionBootstrapToken() const;
110 void SetKeystoreEncryptionBootstrapToken(const std::string& token);
111
112 // Use this for the unique machine tag used for session sync.
113 std::string GetSyncSessionsGUID() const;
114 void SetSyncSessionsGUID(const std::string& guid);
115
116 // Maps |data_type| to its corresponding preference name.
117 static const char* GetPrefNameForDataType(syncer::ModelType data_type);
118
119 #if defined(OS_CHROMEOS)
120 // Use this spare bootstrap token only when setting up sync for the first
121 // time.
122 std::string GetSpareBootstrapToken() const;
123 void SetSpareBootstrapToken(const std::string& token);
124 #endif
125
126 // Merges the given set of types with the set of acknowledged types.
127 void AcknowledgeSyncedTypes(syncer::ModelTypeSet types);
128
129 // Get/Set number of rollback attempts allowed.
130 virtual int GetRemainingRollbackTries() const;
131 virtual void SetRemainingRollbackTries(int times);
132
133 // Get/set/clear first sync time of current user. Used to roll back browsing
134 // data later when user signs out.
135 base::Time GetFirstSyncTime() const;
136 void SetFirstSyncTime(base::Time time);
137 void ClearFirstSyncTime();
138
139 // For testing.
140
141 void SetManagedForTest(bool is_managed);
142 syncer::ModelTypeSet GetAcknowledgeSyncedTypesForTest() const;
143
144 private:
145 void RegisterPrefGroups();
146
147 static void RegisterDataTypePreferredPref(
148 user_prefs::PrefRegistrySyncable* prefs,
149 syncer::ModelType type,
150 bool is_preferred);
151 bool GetDataTypePreferred(syncer::ModelType type) const;
152 void SetDataTypePreferred(syncer::ModelType type, bool is_preferred);
153
154 // Returns a ModelTypeSet based on |types| expanded to include pref groups
155 // (see |pref_groups_|), but as a subset of |registered_types|.
156 syncer::ModelTypeSet ResolvePrefGroups(syncer::ModelTypeSet registered_types,
157 syncer::ModelTypeSet types) const;
158
159 void OnSyncManagedPrefChanged();
160
161 // May be NULL.
162 PrefService* const pref_service_;
163
164 ObserverList<SyncPrefObserver> sync_pref_observers_;
165
166 // The preference that controls whether sync is under control by
167 // configuration management.
168 BooleanPrefMember pref_sync_managed_;
169
170 // Groups of prefs that always have the same value as a "master" pref.
171 // For example, the APPS group has {APP_NOTIFICATIONS, APP_SETTINGS}
172 // (as well as APPS, but that is implied), so
173 // pref_groups_[syncer::APPS] = { syncer::APP_NOTIFICATIONS,
174 // syncer::APP_SETTINGS }
175 // pref_groups_[syncer::EXTENSIONS] = { syncer::EXTENSION_SETTINGS }
176 // etc.
177 typedef std::map<syncer::ModelType, syncer::ModelTypeSet> PrefGroupsMap;
178 PrefGroupsMap pref_groups_;
179
180 DISALLOW_COPY_AND_ASSIGN(SyncPrefs);
181 };
182
183 } // namespace sync_driver
184
185 #endif // COMPONENTS_SYNC_DRIVER_SYNC_PREFS_H_
186