OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_VALUE_STORE_CACHING_VALUE_STORE_H_ | |
6 #define CHROME_BROWSER_VALUE_STORE_CACHING_VALUE_STORE_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/file_path.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/observer_list.h" | |
16 #include "base/threading/non_thread_safe.h" | |
17 #include "base/values.h" | |
18 | |
19 // Value store area with caching, backed by a LeveldbValueStore on the FILE | |
20 // thread. The key/value store is flat with no path expansion, meaning "foo" | |
21 // and "foo.bar" are completely separate keys. | |
22 class CachingValueStore | |
23 : public base::SupportsWeakPtr<CachingValueStore>, | |
24 public base::NonThreadSafe { | |
25 public: | |
26 class Observer { | |
27 public: | |
28 virtual ~Observer() {} | |
29 | |
30 // Notification that the Store is ready to use. | |
31 virtual void OnInitializationComplete() = 0; | |
32 }; | |
33 | |
34 explicit CachingValueStore(const FilePath& db_path); | |
35 ~CachingValueStore(); | |
36 | |
37 // Retrieves a value from the cache, returning true if value with the given | |
38 // key exists. The returned value is a reference to the value in the cache, | |
39 // i.e. no copies are made. | |
40 bool Get(const std::string& key, const base::Value** result); | |
41 | |
42 // Sets a value with the given key. Ownership of |value| is transferred to | |
43 // the store. | |
44 void Set(const std::string& key, base::Value* value); | |
45 | |
46 // Removes the value with the given key. | |
47 void Remove(const std::string& key); | |
48 | |
49 // Returns true if the store has finished initializing. | |
50 bool IsInitialized(); | |
51 | |
52 void AddObserver(Observer* observer); | |
53 void RemoveObserver(Observer* observer); | |
54 | |
55 private: | |
56 class Backend; | |
57 | |
58 // Called when our backend finishes reading the database. | |
59 void OnBackendReady(scoped_ptr<base::DictionaryValue> values); | |
60 | |
61 // A cache of the value store. This is always up-to-date, with changes | |
62 // persisted to disk as they are made. | |
63 scoped_ptr<base::DictionaryValue> cache_; | |
64 | |
65 // A helper class to manage lifetime of the backing ValueStore, which lives | |
66 // on the FILE thread. | |
67 scoped_refptr<Backend> backend_; | |
68 | |
69 ObserverList<Observer, true> observers_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(CachingValueStore); | |
72 }; | |
73 | |
74 #endif // CHROME_BROWSER_VALUE_STORE_CACHING_VALUE_STORE_H_ | |
OLD | NEW |