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