pref_store.h revision 731df977c0511bca2206b5f333555b1205ff1f43
106741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch// Copyright (c) 2010 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 906741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdochclass DictionaryValue; 10731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickclass Value; 1106741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 1206741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch// This is an abstract interface for reading and writing from/to a persistent 1306741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch// preference store, used by |PrefService|. An implementation using a JSON file 1406741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch// can be found in |JsonPrefStore|, while an implementation without any backing 1506741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch// store (currently used for testing) can be found in |DummyPrefStore|. 1606741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdochclass PrefStore { 1706741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch public: 1806741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch // Unique integer code for each type of error so we can report them 1906741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch // distinctly in a histogram. 2006741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch // NOTE: Don't change the order here as it will change the server's meaning 2106741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch // of the histogram. 2206741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch enum PrefReadError { 2306741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch PREF_READ_ERROR_NONE = 0, 2406741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch PREF_READ_ERROR_JSON_PARSE, 2506741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch PREF_READ_ERROR_JSON_TYPE, 2606741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch PREF_READ_ERROR_ACCESS_DENIED, 2706741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch PREF_READ_ERROR_FILE_OTHER, 2806741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch PREF_READ_ERROR_FILE_LOCKED, 2906741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch PREF_READ_ERROR_NO_FILE, 3006741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch PREF_READ_ERROR_JSON_REPEAT, 313345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick PREF_READ_ERROR_OTHER, 323345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick PREF_READ_ERROR_FILE_NOT_SPECIFIED 3306741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch }; 3406741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 35731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // To require that the default value be used for a preference, a 36731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // PrefStore can set the value in its own prefs dictionary to the 37731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // sentinel Value returned by this function. 38731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // TODO(danno): Instead of having a sentinel value, pref stores 39731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // should return a richer set of information from the property 40731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // accessor methods to indicate that the default should be used. 41731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick static Value* CreateUseDefaultSentinelValue(); 42731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick 43731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // Returns true if a value is the special sentinel value created by 44731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // CreateUseDefaultSentinelValue. 45731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick static bool IsUseDefaultSentinelValue(Value* value); 46731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick 4706741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch virtual ~PrefStore() { } 4806741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 4906741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch // Whether the store is in a pseudo-read-only mode where changes are not 5006741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch // actually persisted to disk. This happens in some cases when there are 5106741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch // read errors during startup. 5206741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch virtual bool ReadOnly() { return true; } 5306741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 54731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // TODO(danno): PrefValueStore shouldn't allow direct access to the 55731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // DictionaryValue. Instead, it should have getters that return a 56731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // richer set of information for a pref, including if the store 57731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick // wants to return the default value for a preference. 5806741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch virtual DictionaryValue* prefs() = 0; 5906741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 6006741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch virtual PrefReadError ReadPrefs() = 0; 6106741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 6206741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch virtual bool WritePrefs() { return true; } 6306741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 6406741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch virtual void ScheduleWritePrefs() { } 6506741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch}; 6606741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch 6706741cbc25cd4227a9fba40dfd0273bfcc1a587aBen Murdoch#endif // CHROME_COMMON_PREF_STORE_H_ 68