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)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This provides a way to access the application's current preferences.
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Chromium settings and storage represent user-selected preferences and
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// information and MUST not be extracted, overwritten or modified except
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// through Chromium defined APIs.
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef BASE_PREFS_PREF_SERVICE_H_
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define BASE_PREFS_PREF_SERVICE_H_
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <set>
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/callback.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/compiler_specific.h"
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/containers/hash_tables.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/ref_counted.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/observer_list.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/base_prefs_export.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/persistent_pref_store.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/threading/non_thread_safe.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/values.h"
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PrefNotifier;
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PrefNotifierImpl;
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PrefObserver;
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PrefRegistry;
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PrefValueStore;
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PrefStore;
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FilePath;
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace subtle {
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PrefMemberBase;
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ScopedUserPrefUpdateBase;
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Base class for PrefServices. You can use the base class to read and
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// interact with preferences, but not to register new preferences; for
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// that see e.g. PrefRegistrySimple.
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Settings and storage accessed through this class represent
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// user-selected preferences and information and MUST not be
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// extracted, overwritten or modified except through the defined APIs.
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class BASE_PREFS_EXPORT PrefService : public base::NonThreadSafe {
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum PrefInitializationStatus {
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    INITIALIZATION_STATUS_WAITING,
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    INITIALIZATION_STATUS_SUCCESS,
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE,
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    INITIALIZATION_STATUS_ERROR
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A helper class to store all the information associated with a preference.
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class BASE_PREFS_EXPORT Preference {
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   public:
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The type of the preference is determined by the type with which it is
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // registered. This type needs to be a boolean, integer, double, string,
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // dictionary (a branch), or list.  You shouldn't need to construct this on
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // your own; use the PrefService::Register*Pref methods instead.
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Preference(const PrefService* service,
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               const char* name,
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               base::Value::Type type);
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ~Preference() {}
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns the name of the Preference (i.e., the key, e.g.,
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // browser.window_placement).
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string name() const;
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns the registered type of the preference.
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::Value::Type GetType() const;
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns the value of the Preference, falling back to the registered
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // default value if no other has been set.
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::Value* GetValue() const;
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns the value recommended by the admin, if any.
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::Value* GetRecommendedValue() const;
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns true if the Preference is managed, i.e. set by an admin policy.
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Since managed prefs have the highest priority, this also indicates
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // whether the pref is actually being controlled by the policy setting.
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool IsManaged() const;
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns true if the Preference is recommended, i.e. set by an admin
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // policy but the user is allowed to change it.
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool IsRecommended() const;
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns true if the Preference has a value set by an extension, even if
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // that value is being overridden by a higher-priority source.
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool HasExtensionSetting() const;
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns true if the Preference has a user setting, even if that value is
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // being overridden by a higher-priority source.
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool HasUserSetting() const;
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns true if the Preference value is currently being controlled by an
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // extension, and not by any higher-priority source.
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool IsExtensionControlled() const;
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns true if the Preference value is currently being controlled by a
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // user setting, and not by any higher-priority source.
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool IsUserControlled() const;
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns true if the Preference is currently using its default value,
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // and has not been set by any higher-priority source (even with the same
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // value).
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool IsDefaultValue() const;
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns true if the user can change the Preference value, which is the
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // case if no higher-priority source than the user store controls the
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Preference.
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool IsUserModifiable() const;
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Returns true if an extension can change the Preference value, which is
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // the case if no higher-priority source than the extension store controls
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // the Preference.
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool IsExtensionModifiable() const;
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   private:
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    friend class PrefService;
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PrefValueStore* pref_value_store() const {
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return pref_service_->pref_value_store_.get();
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string name_;
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::Value::Type type_;
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Reference to the PrefService in which this pref was created.
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const PrefService* pref_service_;
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // You may wish to use PrefServiceFactory or one of its subclasses
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // for simplified construction.
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PrefService(
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PrefNotifierImpl* pref_notifier,
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PrefValueStore* pref_value_store,
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PersistentPrefStore* user_prefs,
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PrefRegistry* pref_registry,
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Callback<void(PersistentPrefStore::PrefReadError)>
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          read_error_callback,
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      bool async);
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~PrefService();
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Lands pending writes to disk. This should only be used if we need to save
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // immediately (basically, during shutdown).
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void CommitPendingWrite();
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the preference for the given preference name is available
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // and is managed.
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool IsManagedPreference(const char* pref_name) const;
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns |true| if a preference with the given name is available and its
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // value can be changed by the user.
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool IsUserModifiablePreference(const char* pref_name) const;
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Look up a preference.  Returns NULL if the preference is not
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // registered.
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const PrefService::Preference* FindPreference(const char* path) const;
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the path is valid and the value at the end of the path matches the type
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // specified, it will return the specified value.  Otherwise, the default
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // value (set when the pref was registered) will be returned.
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool GetBoolean(const char* path) const;
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int GetInteger(const char* path) const;
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  double GetDouble(const char* path) const;
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string GetString(const char* path) const;
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath GetFilePath(const char* path) const;
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the branch if it exists, or the registered default value otherwise.
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note that |path| must point to a registered preference. In that case, these
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // functions will never return NULL.
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::DictionaryValue* GetDictionary(
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const char* path) const;
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::ListValue* GetList(const char* path) const;
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Removes a user pref and restores the pref to its default value.
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void ClearPref(const char* path);
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the path is valid (i.e., registered), update the pref value in the user
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // prefs.
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // To set the value of dictionary or list values in the pref tree use
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set(), but to modify the value of a dictionary or list use either
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ListPrefUpdate or DictionaryPrefUpdate from scoped_user_pref_update.h.
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void Set(const char* path, const base::Value& value);
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetBoolean(const char* path, bool value);
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetInteger(const char* path, int value);
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetDouble(const char* path, double value);
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetString(const char* path, const std::string& value);
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetFilePath(const char* path, const base::FilePath& value);
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Int64 helper methods that actually store the given value as a string.
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note that if obtaining the named value via GetDictionary or GetList, the
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Value type will be TYPE_STRING.
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetInt64(const char* path, int64 value);
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int64 GetInt64(const char* path) const;
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // As above, but for unsigned values.
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetUint64(const char* path, uint64 value);
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  uint64 GetUint64(const char* path) const;
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the value of the given preference, from the user pref store. If
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the preference is not set in the user pref store, returns NULL.
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::Value* GetUserPrefValue(const char* path) const;
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Changes the default value for a preference. Takes ownership of |value|.
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Will cause a pref change notification to be fired if this causes
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the effective value to change.
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetDefaultPrefValue(const char* path, base::Value* value);
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the default value of the given preference. |path| must point to a
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // registered preference. In that case, will never return NULL.
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::Value* GetDefaultPrefValue(const char* path) const;
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if a value has been set for the specified path.
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // NOTE: this is NOT the same as FindPreference. In particular
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // FindPreference returns whether RegisterXXX has been invoked, where as
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // this checks if a value exists for the path.
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool HasPrefPath(const char* path) const;
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns a dictionary with effective preference values. The ownership
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is passed to the caller.
2320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> GetPreferenceValues() const;
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Returns a dictionary with effective preference values. Contrary to
2354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // GetPreferenceValues(), the paths of registered preferences are not split on
2364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // '.' characters. If a registered preference stores a dictionary, however,
2374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // the hierarchical structure inside the preference will be preserved.
2384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // For example, if "foo.bar" is a registered preference, the result could look
2394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // like this:
2404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //   {"foo.bar": {"a": {"b": true}}}.
2414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The ownership is passed to the caller.
2420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> GetPreferenceValuesWithoutPathExpansion()
2430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      const;
2444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ReadOnly() const;
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PrefInitializationStatus GetInitializationStatus() const;
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Tell our PrefValueStore to update itself to |command_line_store|.
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Takes ownership of the store.
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void UpdateCommandLinePrefStore(PrefStore* command_line_store);
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // We run the callback once, when initialization completes. The bool
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // parameter will be set to true for successful initialization,
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // false for unsuccessful.
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void AddPrefInitObserver(base::Callback<void(bool)> callback);
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the PrefRegistry object for this service. You should not
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // use this; the intent is for no registrations to take place after
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // PrefService has been constructed.
261a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  //
262a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Instead of using this method, the recommended approach is to
263a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // register all preferences for a class Xyz up front in a static
264a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Xyz::RegisterPrefs function, which gets invoked early in the
265a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // application's start-up, before a PrefService is created.
266a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  //
267a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // As an example, prefs registration in Chrome is triggered by the
268a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // functions chrome::RegisterPrefs (for global preferences) and
269a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // chrome::RegisterProfilePrefs (for user-specific preferences)
270a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // implemented in chrome/browser/prefs/browser_prefs.cc.
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PrefRegistry* DeprecatedGetPrefRegistry();
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The PrefNotifier handles registering and notifying preference observers.
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // It is created and owned by this PrefService. Subclasses may access it for
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // unit testing.
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<PrefNotifierImpl> pref_notifier_;
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The PrefValueStore provides prioritized preference values. It is owned by
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // this PrefService. Subclasses may access it for unit testing.
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<PrefValueStore> pref_value_store_;
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<PrefRegistry> pref_registry_;
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Pref Stores and profile that we passed to the PrefValueStore.
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<PersistentPrefStore> user_pref_store_;
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Callback to call when a read error occurs.
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_;
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Hash map expected to be fastest here since it minimises expensive
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // string comparisons. Order is unimportant, and deletions are rare.
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Confirmed on Android where this speeded Chrome startup by roughly 50ms
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers.
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef base::hash_map<std::string, Preference> PreferenceMap;
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Give access to ReportUserPrefChanged() and GetMutableUserPref().
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class subtle::ScopedUserPrefUpdateBase;
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Registration of pref change observers must be done using the
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // PrefChangeRegistrar, which is declared as a friend here to grant it
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // access to the otherwise protected members Add/RemovePrefObserver.
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // PrefMember registers for preferences changes notification directly to
3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // avoid the storage overhead of the registrar, so its base class must be
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // declared as a friend, too.
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class PrefChangeRegistrar;
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class subtle::PrefMemberBase;
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // These are protected so they can only be accessed by the friend
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // classes listed above.
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the pref at the given path changes, we call the observer's
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // OnPreferenceChanged method. Note that observers should not call
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // these methods directly but rather use a PrefChangeRegistrar to
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // make sure the observer gets cleaned up properly.
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Virtual for testing.
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void AddPrefObserver(const char* path, PrefObserver* obs);
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void RemovePrefObserver(const char* path, PrefObserver* obs);
3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sends notification of a changed preference. This needs to be called by
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed.
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void ReportUserPrefChanged(const std::string& key);
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the value for this pref path in the user pref store and informs the
3272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // PrefNotifier of the change.
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetUserPrefValue(const char* path, base::Value* new_value);
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Load preferences from storage, attempting to diagnose and handle errors.
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This should only be called from the constructor.
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void InitFromStorage(bool async);
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Used to set the value of dictionary or list values in the user pref store.
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This will create a dictionary or list if one does not exist in the user
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // pref store. This method returns NULL only if you're requesting an
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // unregistered pref or a non-dict/non-list pref.
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |path| must point to a registered preference of type |type|.
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Ownership of the returned value remains at the user pref store.
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::Value* GetMutableUserPref(const char* path,
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  base::Value::Type type);
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(),
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // it has been added for performance. If is faster because it does
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // not need to find or create a Preference object to get the
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // value (GetValue() calls back though the preference service to
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // actually get the value.).
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::Value* GetPreferenceValue(const std::string& path) const;
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Local cache of registered Preference objects. The pref_registry_
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is authoritative with respect to what the types and default values
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // of registered preferences are.
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  mutable PreferenceMap prefs_map_;
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PrefService);
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // BASE_PREFS_PREF_SERVICE_H_
360