• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_USER_PREFS_PREF_REGISTRY_SYNCABLE_H_
6 #define COMPONENTS_USER_PREFS_PREF_REGISTRY_SYNCABLE_H_
7 
8 #include <set>
9 #include <string>
10 
11 #include "base/callback.h"
12 #include "base/prefs/pref_registry.h"
13 #include "components/user_prefs/user_prefs_export.h"
14 
15 namespace base {
16 class DictionaryValue;
17 class FilePath;
18 class ListValue;
19 class Value;
20 }
21 
22 namespace user_prefs {
23 
24 // A PrefRegistry that forces users to choose whether each registered
25 // preference is syncable or not.
26 //
27 // Classes or components that want to register such preferences should
28 // define a static function named RegisterUserPrefs that takes a
29 // PrefRegistrySyncable*, and the top-level application using the
30 // class or embedding the component should call this function at an
31 // appropriate time before the PrefService for these preferences is
32 // constructed. See e.g. chrome/browser/prefs/browser_prefs.cc which
33 // does this for Chrome.
34 class USER_PREFS_EXPORT PrefRegistrySyncable : public PrefRegistry {
35  public:
36   // Enum used when registering preferences to determine if it should
37   // be synced or not. Syncable priority preferences are preferences that are
38   // never encrypted and are synced before other datatypes. Because they're
39   // never encrypted, on first sync, they can be synced down before the user
40   // is prompted for a passphrase.
41   enum PrefSyncStatus {
42     UNSYNCABLE_PREF,
43     SYNCABLE_PREF,
44     SYNCABLE_PRIORITY_PREF,
45   };
46 
47   typedef
48       base::Callback<void(const char* path, const PrefSyncStatus sync_status)>
49           SyncableRegistrationCallback;
50 
51   PrefRegistrySyncable();
52 
53   typedef std::map<std::string, PrefSyncStatus> PrefToStatus;
54 
55   // Retrieve the set of syncable preferences currently registered.
56   const PrefToStatus& syncable_preferences() const;
57 
58   // Exactly one callback can be set for the event of a syncable
59   // preference being registered. It will be fired after the
60   // registration has occurred.
61   //
62   // Calling this method after a callback has already been set will
63   // make the object forget the previous callback and use the new one
64   // instead.
65   void SetSyncableRegistrationCallback(const SyncableRegistrationCallback& cb);
66 
67   void RegisterBooleanPref(const char* path,
68                            bool default_value,
69                            PrefSyncStatus sync_status);
70   void RegisterIntegerPref(const char* path,
71                            int default_value,
72                            PrefSyncStatus sync_status);
73   void RegisterDoublePref(const char* path,
74                           double default_value,
75                           PrefSyncStatus sync_status);
76   void RegisterStringPref(const char* path,
77                           const std::string& default_value,
78                           PrefSyncStatus sync_status);
79   void RegisterFilePathPref(const char* path,
80                             const base::FilePath& default_value,
81                             PrefSyncStatus sync_status);
82   void RegisterListPref(const char* path,
83                         PrefSyncStatus sync_status);
84   void RegisterDictionaryPref(const char* path,
85                               PrefSyncStatus sync_status);
86   void RegisterListPref(const char* path,
87                         base::ListValue* default_value,
88                         PrefSyncStatus sync_status);
89   void RegisterDictionaryPref(const char* path,
90                               base::DictionaryValue* default_value,
91                               PrefSyncStatus sync_status);
92   void RegisterLocalizedBooleanPref(const char* path,
93                                     int locale_default_message_id,
94                                     PrefSyncStatus sync_status);
95   void RegisterLocalizedIntegerPref(const char* path,
96                                     int locale_default_message_id,
97                                     PrefSyncStatus sync_status);
98   void RegisterLocalizedDoublePref(const char* path,
99                                    int locale_default_message_id,
100                                    PrefSyncStatus sync_status);
101   void RegisterLocalizedStringPref(const char* path,
102                                    int locale_default_message_id,
103                                    PrefSyncStatus sync_status);
104   void RegisterInt64Pref(const char* path,
105                          int64 default_value,
106                          PrefSyncStatus sync_status);
107   void RegisterUint64Pref(const char* path,
108                           uint64 default_value,
109                           PrefSyncStatus sync_status);
110 
111   // Returns a new PrefRegistrySyncable that uses the same defaults
112   // store.
113   scoped_refptr<PrefRegistrySyncable> ForkForIncognito();
114 
115  private:
116   virtual ~PrefRegistrySyncable();
117 
118   void RegisterSyncablePreference(const char* path,
119                                   base::Value* default_value,
120                                   PrefSyncStatus sync_status);
121 
122   SyncableRegistrationCallback callback_;
123 
124   // Contains the names of all registered preferences that are syncable.
125   PrefToStatus syncable_preferences_;
126 
127   DISALLOW_COPY_AND_ASSIGN(PrefRegistrySyncable);
128 };
129 
130 }  // namespace user_prefs
131 
132 #endif  // COMPONENTS_USER_PREFS_PREF_REGISTRY_SYNCABLE_H_
133