Componentize HistoryURLProvider/ScoredHistoryMatch.
[chromium-blink-merge.git] / chrome / browser / prefs / leveldb_pref_store.h
bloba48da81f2b96e3117865d170c4dd864cfa7ab6bf
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/containers/hash_tables.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/scoped_ptr.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/thread_task_runner_handle.h"
20 #include "base/timer/timer.h"
22 namespace base {
23 class DictionaryValue;
24 class SequencedTaskRunner;
25 class Value;
28 namespace leveldb {
29 class DB;
32 // A writable PrefStore implementation that is used for user preferences.
33 class LevelDBPrefStore : public PersistentPrefStore {
34 public:
35 // |sequenced_task_runner| is must be a shutdown-blocking task runner, ideally
36 // created by GetTaskRunnerForFile() method above.
37 LevelDBPrefStore(const base::FilePath& pref_filename,
38 base::SequencedTaskRunner* sequenced_task_runner);
40 // PrefStore overrides:
41 bool GetValue(const std::string& key,
42 const base::Value** result) const override;
43 void AddObserver(PrefStore::Observer* observer) override;
44 void RemoveObserver(PrefStore::Observer* observer) override;
45 bool HasObservers() const override;
46 bool IsInitializationComplete() const override;
48 // PersistentPrefStore overrides:
49 bool GetMutableValue(const std::string& key, base::Value** result) override;
50 // Takes ownership of value.
51 void SetValue(const std::string& key,
52 base::Value* value,
53 uint32 flags) override;
54 void SetValueSilently(const std::string& key,
55 base::Value* value,
56 uint32 flags) override;
57 void RemoveValue(const std::string& key, uint32 flags) override;
58 bool ReadOnly() const override;
59 PrefReadError GetReadError() const override;
60 PrefReadError ReadPrefs() override;
61 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
62 void CommitPendingWrite() override;
63 void SchedulePendingLossyWrites() override;
64 void ReportValueChanged(const std::string& key, uint32 flags) override;
66 private:
67 struct ReadingResults;
68 class FileThreadSerializer;
70 ~LevelDBPrefStore() override;
72 static scoped_ptr<ReadingResults> DoReading(const base::FilePath& path);
73 static void OpenDB(const base::FilePath& path,
74 ReadingResults* reading_results);
75 void OnStorageRead(scoped_ptr<ReadingResults> reading_results);
77 void PersistFromUIThread();
78 void RemoveFromUIThread(const std::string& key);
79 void ScheduleWrite();
81 void SetValueInternal(const std::string& key,
82 base::Value* value,
83 bool notify);
84 void NotifyObservers(const std::string& key);
85 void MarkForInsertion(const std::string& key, const std::string& value);
86 void MarkForDeletion(const std::string& key);
88 base::FilePath path_;
90 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
91 const scoped_refptr<base::SequencedTaskRunner> original_task_runner_;
93 PrefValueMap prefs_;
95 bool read_only_;
97 base::ObserverList<PrefStore::Observer, true> observers_;
99 scoped_ptr<ReadErrorDelegate> error_delegate_;
101 bool initialized_;
102 PrefReadError read_error_;
104 // This object is created on the UI thread right after preferences are loaded
105 // from disk. A message to delete it is sent to the FILE thread by
106 // ~LevelDBPrefStore.
107 scoped_ptr<FileThreadSerializer> serializer_;
109 // Changes are accumulated in |keys_to_delete_| and |keys_to_set_| and are
110 // stored in the database according to |timer_|.
111 base::hash_set<std::string> keys_to_delete_;
112 base::hash_map<std::string, std::string> keys_to_set_;
113 base::OneShotTimer<LevelDBPrefStore> timer_;
115 base::WeakPtrFactory<LevelDBPrefStore> weak_ptr_factory_;
117 DISALLOW_COPY_AND_ASSIGN(LevelDBPrefStore);
120 #endif // CHROME_BROWSER_PREFS_LEVELDB_PREF_STORE_H_