Returning fetching back.
[chromium-blink-merge.git] / components / sync_driver / sync_prefs.h
bloba65f0b3ad6692332fb252465809cc272eff57e66
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"
17 class PrefService;
18 class ProfileIOData;
20 namespace user_prefs {
21 class PrefRegistrySyncable;
24 namespace sync_driver {
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;
32 protected:
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
45 // sync_setup_flow.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> {
51 public:
52 // |pref_service| may not be NULL.
53 // Does not take ownership of |pref_service|.
54 explicit SyncPrefs(PrefService* pref_service);
56 // For testing.
57 SyncPrefs();
59 virtual ~SyncPrefs();
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 IsStartSuppressed() const;
78 void SetStartSuppressed(bool is_suppressed);
80 base::Time GetLastSyncedTime() const;
81 void SetLastSyncedTime(base::Time time);
83 bool HasKeepEverythingSynced() const;
84 void SetKeepEverythingSynced(bool keep_everything_synced);
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);
100 // This pref is set outside of sync.
101 bool IsManaged() const;
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);
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);
112 // Use this for the unique machine tag used for session sync.
113 std::string GetSyncSessionsGUID() const;
114 void SetSyncSessionsGUID(const std::string& guid);
116 // Maps |data_type| to its corresponding preference name.
117 static const char* GetPrefNameForDataType(syncer::ModelType data_type);
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
126 // Get/Set number of rollback attempts allowed.
127 virtual int GetRemainingRollbackTries() const;
128 virtual void SetRemainingRollbackTries(int times);
130 // Get/set/clear first sync time of current user. Used to roll back browsing
131 // data later when user signs out.
132 base::Time GetFirstSyncTime() const;
133 void SetFirstSyncTime(base::Time time);
134 void ClearFirstSyncTime();
136 // For testing.
138 void SetManagedForTest(bool is_managed);
140 private:
141 void RegisterPrefGroups();
143 static void RegisterDataTypePreferredPref(
144 user_prefs::PrefRegistrySyncable* prefs,
145 syncer::ModelType type,
146 bool is_preferred);
147 bool GetDataTypePreferred(syncer::ModelType type) const;
148 void SetDataTypePreferred(syncer::ModelType type, bool is_preferred);
150 // Returns a ModelTypeSet based on |types| expanded to include pref groups
151 // (see |pref_groups_|), but as a subset of |registered_types|.
152 syncer::ModelTypeSet ResolvePrefGroups(syncer::ModelTypeSet registered_types,
153 syncer::ModelTypeSet types) const;
155 void OnSyncManagedPrefChanged();
157 // May be NULL.
158 PrefService* const pref_service_;
160 ObserverList<SyncPrefObserver> sync_pref_observers_;
162 // The preference that controls whether sync is under control by
163 // configuration management.
164 BooleanPrefMember pref_sync_managed_;
166 // Groups of prefs that always have the same value as a "master" pref.
167 // For example, the APPS group has {APP_NOTIFICATIONS, APP_SETTINGS}
168 // (as well as APPS, but that is implied), so
169 // pref_groups_[syncer::APPS] = { syncer::APP_NOTIFICATIONS,
170 // syncer::APP_SETTINGS }
171 // pref_groups_[syncer::EXTENSIONS] = { syncer::EXTENSION_SETTINGS }
172 // etc.
173 typedef std::map<syncer::ModelType, syncer::ModelTypeSet> PrefGroupsMap;
174 PrefGroupsMap pref_groups_;
176 DISALLOW_COPY_AND_ASSIGN(SyncPrefs);
179 } // namespace sync_driver
181 #endif // COMPONENTS_SYNC_DRIVER_SYNC_PREFS_H_