15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef BASE_PREFS_PREF_VALUE_STORE_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define BASE_PREFS_PREF_VALUE_STORE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/callback.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/base_prefs_export.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/prefs/pref_store.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrefNotifier;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrefStore;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The PrefValueStore manages various sources of values for Preferences
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (e.g., configuration policies, extensions, and user settings). It returns
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the value of a Preference from the source with the highest priority, and
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// allows setting user-defined values for preferences that are not managed.
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Unless otherwise explicitly noted, all of the methods of this class must
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be called on the UI thread.
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class BASE_PREFS_EXPORT PrefValueStore {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef base::Callback<void(const std::string&)> PrefChangedCallback;
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In decreasing order of precedence:
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   |managed_prefs| contains all preferences from mandatory policies.
36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  //   |supervised_user_prefs| contains all preferences from supervised user
37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  //        settings, i.e. settings configured for a supervised user by their
38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  //        custodian.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   |extension_prefs| contains preference values set by extensions.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   |command_line_prefs| contains preference values set by command-line
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //        switches.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   |user_prefs| contains all user-set preference values.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   |recommended_prefs| contains all preferences from recommended policies.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   |default_prefs| contains application-default preference values. It must
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //        be non-null if any preferences are to be registered.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |pref_notifier| facilitates broadcasting preference change notifications
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to the world.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefValueStore(PrefStore* managed_prefs,
50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                 PrefStore* supervised_user_prefs,
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 PrefStore* extension_prefs,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 PrefStore* command_line_prefs,
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 PrefStore* user_prefs,
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 PrefStore* recommended_prefs,
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 PrefStore* default_prefs,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 PrefNotifier* pref_notifier);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~PrefValueStore();
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a clone of this PrefValueStore with PrefStores overwritten
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // by the parameters passed, if unequal NULL.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefValueStore* CloneAndSpecialize(PrefStore* managed_prefs,
62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                     PrefStore* supervised_user_prefs,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     PrefStore* extension_prefs,
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     PrefStore* command_line_prefs,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     PrefStore* user_prefs,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     PrefStore* recommended_prefs,
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     PrefStore* default_prefs,
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     PrefNotifier* pref_notifier);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A PrefValueStore can have exactly one callback that is directly
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // notified of preferences changing in the store. This does not
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // filter through the PrefNotifier mechanism, which may not forward
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // certain changes (e.g. unregistered prefs).
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void set_callback(const PrefChangedCallback& callback);
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the value for the given preference name that has the specified value
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // type. Values stored in a PrefStore that have the matching |name| but
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a non-matching |type| are silently skipped. Returns true if a valid value
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // was found in any of the available PrefStores. Most callers should use
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Preference::GetValue() instead of calling this method directly.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetValue(const std::string& name,
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                base::Value::Type type,
837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                const base::Value** out_value) const;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the recommended value for the given preference name that has the
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // specified value type. A value stored in the recommended PrefStore that has
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the matching |name| but a non-matching |type| is silently ignored. Returns
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // true if a valid value was found. Most callers should use
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Preference::GetRecommendedValue() instead of calling this method directly.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetRecommendedValue(const std::string& name,
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           base::Value::Type type,
927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                           const base::Value** out_value) const;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These methods return true if a preference with the given name is in the
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // indicated pref store, even if that value is currently being overridden by
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a higher-priority source.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueInManagedStore(const char* name) const;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueInExtensionStore(const char* name) const;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueInUserStore(const char* name) const;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These methods return true if a preference with the given name is actually
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // being controlled by the indicated pref store and not being overridden by
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a higher-priority source.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueFromExtensionStore(const char* name) const;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueFromUserStore(const char* name) const;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueFromRecommendedStore(const char* name) const;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueFromDefaultStore(const char* name) const;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check whether a Preference value is modifiable by the user, i.e. whether
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // there is no higher-priority source controlling it.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueUserModifiable(const char* name) const;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check whether a Preference value is modifiable by an extension, i.e.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // whether there is no higher-priority source controlling it.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueExtensionModifiable(const char* name) const;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the command line PrefStore with |command_line_prefs|.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateCommandLinePrefStore(PrefStore* command_line_prefs);
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // PrefStores must be listed here in order from highest to lowest priority.
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   MANAGED contains all managed preference values that are provided by
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //      mandatory policies (e.g. Windows Group Policy or cloud policy).
124424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  //   SUPERVISED_USER contains preferences that are valid for supervised users.
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   EXTENSION contains preference values set by extensions.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   COMMAND_LINE contains preference values set by command-line switches.
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   USER contains all user-set preference values.
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   RECOMMENDED contains all preferences that are provided by recommended
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //      policies.
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   DEFAULT contains all application default preference values.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum PrefStoreType {
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // INVALID_STORE is not associated with an actual PrefStore but used as
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // an invalid marker, e.g. as a return value.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INVALID_STORE = -1,
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MANAGED_STORE = 0,
136424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    SUPERVISED_USER_STORE,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXTENSION_STORE,
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    COMMAND_LINE_STORE,
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    USER_STORE,
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RECOMMENDED_STORE,
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DEFAULT_STORE,
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PREF_STORE_TYPE_MAX = DEFAULT_STORE
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Keeps a PrefStore reference on behalf of the PrefValueStore and monitors
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the PrefStore for changes, forwarding notifications to PrefValueStore. This
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // indirection is here for the sake of disambiguating notifications from the
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // individual PrefStores.
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class PrefStoreKeeper : public PrefStore::Observer {
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PrefStoreKeeper();
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~PrefStoreKeeper();
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Takes ownership of |pref_store|.
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void Initialize(PrefValueStore* store,
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    PrefStore* pref_store,
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    PrefStoreType type);
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PrefStore* store() { return pref_store_.get(); }
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const PrefStore* store() const { return pref_store_.get(); }
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // PrefStore::Observer implementation.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnPrefValueChanged(const std::string& key) OVERRIDE;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnInitializationCompleted(bool succeeded) OVERRIDE;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // PrefValueStore this keeper is part of.
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PrefValueStore* pref_value_store_;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The PrefStore managed by this keeper.
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_refptr<PrefStore> pref_store_;
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Type of the pref store.
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PrefStoreType type_;
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(PrefStoreKeeper);
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::map<std::string, base::Value::Type> PrefTypeMap;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class PrefValueStorePolicyRefreshTest;
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest, TestPolicyRefresh);
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest,
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           TestRefreshPolicyPrefsCompletion);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest,
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           TestConcurrentPolicyRefresh);
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the preference with the given name has a value in the
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // given PrefStoreType, of the same value type as the preference was
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // registered with.
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueInStore(const char* name, PrefStoreType store) const;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if a preference has an explicit value in any of the
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // stores in the range specified by |first_checked_store| and
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |last_checked_store|, even if that value is currently being
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // overridden by a higher-priority store.
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrefValueInStoreRange(const char* name,
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             PrefStoreType first_checked_store,
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             PrefStoreType last_checked_store) const;
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the pref store type identifying the source that controls the
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Preference identified by |name|. If none of the sources has a value,
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // INVALID_STORE is returned. In practice, the default PrefStore
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // should always have a value for any registered preferencem, so INVALID_STORE
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // indicates an error.
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefStoreType ControllingPrefStoreForPref(const char* name) const;
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get a value from the specified |store|.
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetValueFromStore(const char* name,
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         PrefStoreType store,
2117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                         const base::Value** out_value) const;
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get a value from the specified |store| if its |type| matches.
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetValueFromStoreWithType(const char* name,
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 base::Value::Type type,
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 PrefStoreType store,
2177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                 const base::Value** out_value) const;
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called upon changes in individual pref stores in order to determine whether
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the user-visible pref value has changed. Triggers the change notification
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if the effective value of the preference has changed, or if the store
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // controlling the pref has changed.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void NotifyPrefChanged(const char* path, PrefStoreType new_store);
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called from the PrefStoreKeeper implementation when a pref value for |key|
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // changed in the pref store for |type|.
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnPrefValueChanged(PrefStoreType type, const std::string& key);
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handle the event that the store for |type| has completed initialization.
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnInitializationCompleted(PrefStoreType type, bool succeeded);
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes a pref store keeper. Sets up a PrefStoreKeeper that will take
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ownership of the passed |pref_store|.
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InitPrefStore(PrefStoreType type, PrefStore* pref_store);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Checks whether initialization is completed and tells the notifier if that
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is the case.
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CheckInitializationCompleted();
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the PrefStore pointer for the given type. May return NULL if there is
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // no PrefStore for that type.
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefStore* GetPrefStore(PrefStoreType type) {
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return pref_stores_[type].store();
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const PrefStore* GetPrefStore(PrefStoreType type) const {
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return pref_stores_[type].store();
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Keeps the PrefStore references in order of precedence.
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefStoreKeeper pref_stores_[PREF_STORE_TYPE_MAX + 1];
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PrefChangedCallback pref_changed_callback_;
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Used for generating notifications. This is a weak reference,
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // since the notifier is owned by the corresponding PrefService.
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefNotifier* pref_notifier_;
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A mapping of preference names to their registered types.
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefTypeMap pref_types_;
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if not all of the PrefStores were initialized successfully.
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool initialization_failed_;
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PrefValueStore);
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // BASE_PREFS_PREF_VALUE_STORE_H_
268