1 // Copyright (c) 2012 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.
5 #ifndef BASE_PREFS_JSON_PREF_STORE_H_
6 #define BASE_PREFS_JSON_PREF_STORE_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/files/important_file_writer.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop_proxy.h"
17 #include "base/observer_list.h"
18 #include "base/prefs/base_prefs_export.h"
19 #include "base/prefs/persistent_pref_store.h"
22 class DictionaryValue
;
24 class SequencedWorkerPool
;
25 class SequencedTaskRunner
;
30 // A writable PrefStore implementation that is used for user preferences.
31 class BASE_PREFS_EXPORT JsonPrefStore
32 : public PersistentPrefStore
,
33 public base::ImportantFileWriter::DataSerializer
{
35 // Returns instance of SequencedTaskRunner which guarantees that file
36 // operations on the same file will be executed in sequenced order.
37 static scoped_refptr
<base::SequencedTaskRunner
> GetTaskRunnerForFile(
38 const base::FilePath
& pref_filename
,
39 base::SequencedWorkerPool
* worker_pool
);
41 // |sequenced_task_runner| is must be a shutdown-blocking task runner, ideally
42 // created by GetTaskRunnerForFile() method above.
43 JsonPrefStore(const base::FilePath
& pref_filename
,
44 base::SequencedTaskRunner
* sequenced_task_runner
);
46 // PrefStore overrides:
47 virtual bool GetValue(const std::string
& key
,
48 const base::Value
** result
) const OVERRIDE
;
49 virtual void AddObserver(PrefStore::Observer
* observer
) OVERRIDE
;
50 virtual void RemoveObserver(PrefStore::Observer
* observer
) OVERRIDE
;
51 virtual size_t NumberOfObservers() const OVERRIDE
;
52 virtual bool IsInitializationComplete() const OVERRIDE
;
54 // PersistentPrefStore overrides:
55 virtual bool GetMutableValue(const std::string
& key
,
56 base::Value
** result
) OVERRIDE
;
57 virtual void SetValue(const std::string
& key
, base::Value
* value
) OVERRIDE
;
58 virtual void SetValueSilently(const std::string
& key
,
59 base::Value
* value
) OVERRIDE
;
60 virtual void RemoveValue(const std::string
& key
) OVERRIDE
;
61 virtual void MarkNeedsEmptyValue(const std::string
& key
) OVERRIDE
;
62 virtual bool ReadOnly() const OVERRIDE
;
63 virtual PrefReadError
GetReadError() const OVERRIDE
;
64 virtual PrefReadError
ReadPrefs() OVERRIDE
;
65 virtual void ReadPrefsAsync(ReadErrorDelegate
* error_delegate
) OVERRIDE
;
66 virtual void CommitPendingWrite() OVERRIDE
;
67 virtual void ReportValueChanged(const std::string
& key
) OVERRIDE
;
69 // This method is called after JSON file has been read. Method takes
70 // ownership of the |value| pointer. Note, this method is used with
71 // asynchronous file reading, so class exposes it only for the internal needs.
72 // (read: do not call it manually).
73 void OnFileRead(base::Value
* value_owned
, PrefReadError error
, bool no_dir
);
76 virtual ~JsonPrefStore();
78 // ImportantFileWriter::DataSerializer overrides:
79 virtual bool SerializeData(std::string
* output
) OVERRIDE
;
82 const scoped_refptr
<base::SequencedTaskRunner
> sequenced_task_runner_
;
84 scoped_ptr
<base::DictionaryValue
> prefs_
;
88 // Helper for safely writing pref data.
89 base::ImportantFileWriter writer_
;
91 ObserverList
<PrefStore::Observer
, true> observers_
;
93 scoped_ptr
<ReadErrorDelegate
> error_delegate_
;
96 PrefReadError read_error_
;
98 std::set
<std::string
> keys_need_empty_value_
;
100 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore
);
103 #endif // BASE_PREFS_JSON_PREF_STORE_H_