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