Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / prefs / leveldb_pref_store.h
blob0b6b052a4643ea6ab06bf972145057fe4382ba88
1 // Copyright 2014 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 CHROME_BROWSER_PREFS_LEVELDB_PREF_STORE_H_
6 #define CHROME_BROWSER_PREFS_LEVELDB_PREF_STORE_H_
8 #include <set>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/observer_list.h"
17 #include "base/prefs/persistent_pref_store.h"
18 #include "base/prefs/pref_value_map.h"
19 #include "base/timer/timer.h"
21 namespace base {
22 class DictionaryValue;
23 class SequencedTaskRunner;
24 class Value;
27 namespace leveldb {
28 class DB;
31 // A writable PrefStore implementation that is used for user preferences.
32 class LevelDBPrefStore : public PersistentPrefStore {
33 public:
34 // |sequenced_task_runner| is must be a shutdown-blocking task runner, ideally
35 // created by GetTaskRunnerForFile() method above.
36 LevelDBPrefStore(const base::FilePath& pref_filename,
37 base::SequencedTaskRunner* sequenced_task_runner);
39 // PrefStore overrides:
40 bool GetValue(const std::string& key,
41 const base::Value** result) const override;
42 void AddObserver(PrefStore::Observer* observer) override;
43 void RemoveObserver(PrefStore::Observer* observer) override;
44 bool HasObservers() const override;
45 bool IsInitializationComplete() const override;
47 // PersistentPrefStore overrides:
48 bool GetMutableValue(const std::string& key, base::Value** result) override;
49 // Takes ownership of value.
50 void SetValue(const std::string& key, base::Value* value) override;
51 void SetValueSilently(const std::string& key, base::Value* value) override;
52 void RemoveValue(const std::string& key) override;
53 bool ReadOnly() const override;
54 PrefReadError GetReadError() const override;
55 PrefReadError ReadPrefs() override;
56 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
57 void CommitPendingWrite() override;
58 void ReportValueChanged(const std::string& key) override;
60 private:
61 struct ReadingResults;
62 class FileThreadSerializer;
64 ~LevelDBPrefStore() override;
66 static scoped_ptr<ReadingResults> DoReading(const base::FilePath& path);
67 static void OpenDB(const base::FilePath& path,
68 ReadingResults* reading_results);
69 void OnStorageRead(scoped_ptr<ReadingResults> reading_results);
71 void PersistFromUIThread();
72 void RemoveFromUIThread(const std::string& key);
73 void ScheduleWrite();
75 void SetValueInternal(const std::string& key,
76 base::Value* value,
77 bool notify);
78 void NotifyObservers(const std::string& key);
79 void MarkForInsertion(const std::string& key, const std::string& value);
80 void MarkForDeletion(const std::string& key);
82 base::FilePath path_;
84 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
85 const scoped_refptr<base::SequencedTaskRunner> original_task_runner_;
87 PrefValueMap prefs_;
89 bool read_only_;
91 ObserverList<PrefStore::Observer, true> observers_;
93 scoped_ptr<ReadErrorDelegate> error_delegate_;
95 bool initialized_;
96 PrefReadError read_error_;
98 // This object is created on the UI thread right after preferences are loaded
99 // from disk. A message to delete it is sent to the FILE thread by
100 // ~LevelDBPrefStore.
101 scoped_ptr<FileThreadSerializer> serializer_;
103 // Changes are accumulated in |keys_to_delete_| and |keys_to_set_| and are
104 // stored in the database according to |timer_|.
105 std::set<std::string> keys_to_delete_;
106 std::map<std::string, std::string> keys_to_set_;
107 base::OneShotTimer<LevelDBPrefStore> timer_;
109 base::WeakPtrFactory<LevelDBPrefStore> weak_ptr_factory_;
111 DISALLOW_COPY_AND_ASSIGN(LevelDBPrefStore);
114 #endif // CHROME_BROWSER_PREFS_LEVELDB_PREF_STORE_H_