172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 206741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch// Use of this source code is governed by a BSD-style license that can be 306741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch// found in the LICENSE file. 406741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 506741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch#ifndef CHROME_COMMON_PREF_STORE_H_ 606741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch#define CHROME_COMMON_PREF_STORE_H_ 73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once 806741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen#include <string> 1021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 1121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen#include "base/basictypes.h" 12ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/ref_counted.h" 1321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 14731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickclass Value; 1506741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 1606741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch// This is an abstract interface for reading and writing from/to a persistent 1721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// preference store, used by PrefService. An implementation using a JSON file 1821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// can be found in JsonPrefStore, while an implementation without any backing 1921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// store for testing can be found in TestingPrefStore. Furthermore, there is 2021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// CommandLinePrefStore, which bridges command line options to preferences and 2121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// ConfigurationPolicyPrefStore, which is used for hooking up configuration 2221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// policy with the preference subsystem. 2372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenclass PrefStore : public base::RefCounted<PrefStore> { 2406741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch public: 2521d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // Observer interface for monitoring PrefStore. 2621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen class Observer { 2721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen public: 2821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen virtual ~Observer() {} 2921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 3021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // Called when the value for the given |key| in the store changes. 3121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen virtual void OnPrefValueChanged(const std::string& key) = 0; 3221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // Notification about the PrefStore being fully initialized. 3321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen virtual void OnInitializationCompleted() = 0; 3406741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch }; 3506741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 3621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // Return values for GetValue(). 3721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen enum ReadResult { 3821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // Value found and returned. 3921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen READ_OK, 4021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // No value present, but skip other pref stores and use default. 4121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen READ_USE_DEFAULT, 4221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // No value present. 4321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen READ_NO_VALUE, 4421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen }; 4506741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 4621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen PrefStore() {} 4706741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 4821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // Add and remove observers. 4921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen virtual void AddObserver(Observer* observer) {} 5021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen virtual void RemoveObserver(Observer* observer) {} 5106741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 5221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // Whether the store has completed all asynchronous initialization. 5321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen virtual bool IsInitializationComplete() const; 5406741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 5521d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // Get the value for a given preference |key| and stores it in |result|. 5621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // |result| is only modified if the return value is READ_OK. Ownership of the 5721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // |result| value remains with the PrefStore. 58ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen virtual ReadResult GetValue(const std::string& key, 59ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const Value** result) const = 0; 6006741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 6172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen protected: 6272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen friend class base::RefCounted<PrefStore>; 6372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen virtual ~PrefStore() {} 6472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 6572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen private: 6621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen DISALLOW_COPY_AND_ASSIGN(PrefStore); 6706741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch}; 6806741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 6906741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch#endif // CHROME_COMMON_PREF_STORE_H_ 70