1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_COMMON_JSON_PREF_STORE_H_ 6 #define CHROME_COMMON_JSON_PREF_STORE_H_ 7 #pragma once 8 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/file_path.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/observer_list.h" 15 #include "chrome/common/important_file_writer.h" 16 #include "chrome/common/persistent_pref_store.h" 17 18 namespace base { 19 class MessageLoopProxy; 20 } 21 22 class DictionaryValue; 23 class FilePath; 24 class Value; 25 26 // A writable PrefStore implementation that is used for user preferences. 27 class JsonPrefStore : public PersistentPrefStore, 28 public ImportantFileWriter::DataSerializer { 29 public: 30 class Delegate { 31 public: 32 virtual void OnPrefsRead(PrefReadError error, bool no_dir) = 0; 33 #ifdef ANDROID 34 virtual ~Delegate() {}; 35 #endif 36 }; 37 38 // |file_message_loop_proxy| is the MessageLoopProxy for a thread on which 39 // file I/O can be done. 40 JsonPrefStore(const FilePath& pref_filename, 41 base::MessageLoopProxy* file_message_loop_proxy); 42 virtual ~JsonPrefStore(); 43 44 // PrefStore overrides: 45 virtual ReadResult GetValue(const std::string& key, 46 const Value** result) const; 47 virtual void AddObserver(PrefStore::Observer* observer); 48 virtual void RemoveObserver(PrefStore::Observer* observer); 49 50 // PersistentPrefStore overrides: 51 virtual ReadResult GetMutableValue(const std::string& key, Value** result); 52 virtual void SetValue(const std::string& key, Value* value); 53 virtual void SetValueSilently(const std::string& key, Value* value); 54 virtual void RemoveValue(const std::string& key); 55 virtual bool ReadOnly() const; 56 virtual PrefReadError ReadPrefs(); 57 // todo(altimofeev): move it to the PersistentPrefStore inteface. 58 void ReadPrefs(Delegate* delegate); 59 virtual bool WritePrefs(); 60 virtual void ScheduleWritePrefs(); 61 virtual void CommitPendingWrite(); 62 virtual void ReportValueChanged(const std::string& key); 63 64 // This method is called after JSON file has been read. Method takes 65 // ownership of the |value| pointer. 66 void OnFileRead(Value* value_owned, PrefReadError error, bool no_dir); 67 68 private: 69 // ImportantFileWriter::DataSerializer overrides: 70 virtual bool SerializeData(std::string* output); 71 72 FilePath path_; 73 74 scoped_ptr<DictionaryValue> prefs_; 75 76 bool read_only_; 77 78 // Helper for safely writing pref data. 79 ImportantFileWriter writer_; 80 81 ObserverList<PrefStore::Observer, true> observers_; 82 83 Delegate* delegate_; 84 85 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); 86 }; 87 88 #endif // CHROME_COMMON_JSON_PREF_STORE_H_ 89