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.
5 #ifndef COMPONENTS_SYNC_DRIVER_SYNC_PREFS_H_
6 #define COMPONENTS_SYNC_DRIVER_SYNC_PREFS_H_
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"
20 namespace user_prefs
{
21 class PrefRegistrySyncable
;
24 namespace sync_driver
{
26 class SyncPrefObserver
{
28 // Called whenever the pref that controls whether sync is managed
30 virtual void OnSyncManagedPrefChange(bool is_sync_managed
) = 0;
33 virtual ~SyncPrefObserver();
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.
40 // TODO(akalin): Some classes still read the prefs directly. Consider
41 // passing down a pointer to SyncPrefs to them. A list of files:
43 // profile_sync_service_startup_unittest.cc
44 // profile_sync_service.cc
46 // sync_setup_wizard.cc
47 // sync_setup_wizard_unittest.cc
48 // two_client_preferences_sync_test.cc
49 class SyncPrefs
: NON_EXPORTED_BASE(public base::NonThreadSafe
),
50 public base::SupportsWeakPtr
<SyncPrefs
> {
52 // |pref_service| may not be NULL.
53 // Does not take ownership of |pref_service|.
54 explicit SyncPrefs(PrefService
* pref_service
);
61 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
63 void AddSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
);
64 void RemoveSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
);
66 // Clears important sync preferences.
67 void ClearPreferences();
69 // Getters and setters for global sync prefs.
71 bool HasSyncSetupCompleted() const;
72 void SetSyncSetupCompleted();
74 bool SyncHasAuthError() const;
75 void SetSyncAuthError(bool error
);
77 bool IsSyncRequested() const;
78 void SetSyncRequested(bool is_requested
);
80 base::Time
GetLastSyncedTime() const;
81 void SetLastSyncedTime(base::Time time
);
83 base::Time
GetLastPollTime() const;
84 void SetLastPollTime(base::Time time
);
86 bool HasKeepEverythingSynced() const;
87 void SetKeepEverythingSynced(bool keep_everything_synced
);
89 // The returned set is guaranteed to be a subset of
90 // |registered_types|. Returns |registered_types| directly if
91 // HasKeepEverythingSynced() is true.
92 syncer::ModelTypeSet
GetPreferredDataTypes(
93 syncer::ModelTypeSet registered_types
) const;
94 // |preferred_types| should be a subset of |registered_types|. All
95 // types in |preferred_types| are marked preferred, and all types in
96 // |registered_types| \ |preferred_types| are marked not preferred.
97 // Changes are still made to the prefs even if
98 // HasKeepEverythingSynced() is true, but won't be visible until
99 // SetKeepEverythingSynced(false) is called.
100 void SetPreferredDataTypes(syncer::ModelTypeSet registered_types
,
101 syncer::ModelTypeSet preferred_types
);
103 // This pref is set outside of sync.
104 bool IsManaged() const;
106 // Use this encryption bootstrap token if we're using an explicit passphrase.
107 std::string
GetEncryptionBootstrapToken() const;
108 void SetEncryptionBootstrapToken(const std::string
& token
);
110 // Use this keystore bootstrap token if we're not using an explicit
112 std::string
GetKeystoreEncryptionBootstrapToken() const;
113 void SetKeystoreEncryptionBootstrapToken(const std::string
& token
);
115 // Use this for the unique machine tag used for session sync.
116 std::string
GetSyncSessionsGUID() const;
117 void SetSyncSessionsGUID(const std::string
& guid
);
119 // Maps |data_type| to its corresponding preference name.
120 static const char* GetPrefNameForDataType(syncer::ModelType data_type
);
122 #if defined(OS_CHROMEOS)
123 // Use this spare bootstrap token only when setting up sync for the first
125 std::string
GetSpareBootstrapToken() const;
126 void SetSpareBootstrapToken(const std::string
& token
);
129 // Get/Set number of rollback attempts allowed.
130 virtual int GetRemainingRollbackTries() const;
131 virtual void SetRemainingRollbackTries(int times
);
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();
139 // Out of band sync passphrase prompt getter/setter.
140 bool IsPassphrasePrompted() const;
141 void SetPassphrasePrompted(bool value
);
144 void SetManagedForTest(bool is_managed
);
146 // Get/Set number of memory warnings received.
147 int GetMemoryPressureWarningCount() const;
148 void SetMemoryPressureWarningCount(int value
);
150 // Check if the previous shutdown was clean.
151 bool DidSyncShutdownCleanly() const;
153 // Set whetherthe last shutdown was clean.
154 void SetCleanShutdown(bool value
);
157 void RegisterPrefGroups();
159 static void RegisterDataTypePreferredPref(
160 user_prefs::PrefRegistrySyncable
* prefs
,
161 syncer::ModelType type
,
163 bool GetDataTypePreferred(syncer::ModelType type
) const;
164 void SetDataTypePreferred(syncer::ModelType type
, bool is_preferred
);
166 // Returns a ModelTypeSet based on |types| expanded to include pref groups
167 // (see |pref_groups_|), but as a subset of |registered_types|.
168 syncer::ModelTypeSet
ResolvePrefGroups(syncer::ModelTypeSet registered_types
,
169 syncer::ModelTypeSet types
) const;
171 void OnSyncManagedPrefChanged();
174 PrefService
* const pref_service_
;
176 base::ObserverList
<SyncPrefObserver
> sync_pref_observers_
;
178 // The preference that controls whether sync is under control by
179 // configuration management.
180 BooleanPrefMember pref_sync_managed_
;
182 // Groups of prefs that always have the same value as a "master" pref.
183 // For example, the APPS group has {APP_NOTIFICATIONS, APP_SETTINGS}
184 // (as well as APPS, but that is implied), so
185 // pref_groups_[syncer::APPS] = { syncer::APP_NOTIFICATIONS,
186 // syncer::APP_SETTINGS }
187 // pref_groups_[syncer::EXTENSIONS] = { syncer::EXTENSION_SETTINGS }
189 typedef std::map
<syncer::ModelType
, syncer::ModelTypeSet
> PrefGroupsMap
;
190 PrefGroupsMap pref_groups_
;
192 DISALLOW_COPY_AND_ASSIGN(SyncPrefs
);
195 } // namespace sync_driver
197 #endif // COMPONENTS_SYNC_DRIVER_SYNC_PREFS_H_