12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef COMPONENTS_PREF_REGISTRY_PREF_REGISTRY_SYNCABLE_H_
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define COMPONENTS_PREF_REGISTRY_PREF_REGISTRY_SYNCABLE_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <set>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/callback.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_registry.h"
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_export.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class DictionaryValue;
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FilePath;
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ListValue;
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Value;
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// TODO(tfarina): Change this namespace to pref_registry.
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace user_prefs {
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// A PrefRegistry that forces users to choose whether each registered
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// preference is syncable or not.
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Classes or components that want to register such preferences should
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// define a static function named RegisterUserPrefs that takes a
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// PrefRegistrySyncable*, and the top-level application using the
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// class or embedding the component should call this function at an
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// appropriate time before the PrefService for these preferences is
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// constructed. See e.g. chrome/browser/prefs/browser_prefs.cc which
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// does this for Chrome.
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class PREF_REGISTRY_EXPORT PrefRegistrySyncable : public PrefRegistry {
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Enum used when registering preferences to determine if it should
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // be synced or not. Syncable priority preferences are preferences that are
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // never encrypted and are synced before other datatypes. Because they're
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // never encrypted, on first sync, they can be synced down before the user
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // is prompted for a passphrase.
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum PrefSyncStatus {
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    UNSYNCABLE_PREF,
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SYNCABLE_PREF,
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SYNCABLE_PRIORITY_PREF,
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  typedef
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Callback<void(const char* path, const PrefSyncStatus sync_status)>
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          SyncableRegistrationCallback;
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PrefRegistrySyncable();
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  typedef std::map<std::string, PrefSyncStatus> PrefToStatus;
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Retrieve the set of syncable preferences currently registered.
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const PrefToStatus& syncable_preferences() const;
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Exactly one callback can be set for the event of a syncable
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // preference being registered. It will be fired after the
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // registration has occurred.
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Calling this method after a callback has already been set will
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // make the object forget the previous callback and use the new one
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // instead.
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetSyncableRegistrationCallback(const SyncableRegistrationCallback& cb);
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterBooleanPref(const char* path,
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           bool default_value,
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           PrefSyncStatus sync_status);
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterIntegerPref(const char* path,
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           int default_value,
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           PrefSyncStatus sync_status);
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterDoublePref(const char* path,
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          double default_value,
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          PrefSyncStatus sync_status);
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterStringPref(const char* path,
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          const std::string& default_value,
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          PrefSyncStatus sync_status);
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterFilePathPref(const char* path,
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            const base::FilePath& default_value,
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            PrefSyncStatus sync_status);
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterListPref(const char* path,
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        PrefSyncStatus sync_status);
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterDictionaryPref(const char* path,
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              PrefSyncStatus sync_status);
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterListPref(const char* path,
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        base::ListValue* default_value,
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        PrefSyncStatus sync_status);
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterDictionaryPref(const char* path,
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              base::DictionaryValue* default_value,
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              PrefSyncStatus sync_status);
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterLocalizedBooleanPref(const char* path,
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                    int locale_default_message_id,
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                    PrefSyncStatus sync_status);
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterLocalizedIntegerPref(const char* path,
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                    int locale_default_message_id,
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                    PrefSyncStatus sync_status);
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterLocalizedDoublePref(const char* path,
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   int locale_default_message_id,
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   PrefSyncStatus sync_status);
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterLocalizedStringPref(const char* path,
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   int locale_default_message_id,
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   PrefSyncStatus sync_status);
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterInt64Pref(const char* path,
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         int64 default_value,
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         PrefSyncStatus sync_status);
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterUint64Pref(const char* path,
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          uint64 default_value,
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          PrefSyncStatus sync_status);
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns a new PrefRegistrySyncable that uses the same defaults
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // store.
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<PrefRegistrySyncable> ForkForIncognito();
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~PrefRegistrySyncable();
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterSyncablePreference(const char* path,
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  base::Value* default_value,
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  PrefSyncStatus sync_status);
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SyncableRegistrationCallback callback_;
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Contains the names of all registered preferences that are syncable.
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PrefToStatus syncable_preferences_;
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PrefRegistrySyncable);
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace user_prefs
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif  // COMPONENTS_PREF_REGISTRY_PREF_REGISTRY_SYNCABLE_H_
134