OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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_POLICY_RESOURCE_CACHE_H_ | |
6 #define CHROME_BROWSER_POLICY_RESOURCE_CACHE_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/threading/non_thread_safe.h" | |
15 | |
16 namespace base { | |
17 class FilePath; | |
18 } | |
19 | |
20 namespace leveldb { | |
21 class DB; | |
22 } | |
23 | |
24 namespace policy { | |
25 | |
26 // Manages storage of data at a given path. The data is keyed by a key and | |
27 // a subkey, and can be queried by (key, subkey) or (key) lookups. | |
28 // The contents of the cache have to be manually cleared using Delete() or | |
29 // PurgeOtherSubkeys(). | |
30 // Instances of this class can be created on any thread, but from then on must | |
31 // be always used from the same thread, and it must support file I/O. | |
32 class ResourceCache : public base::NonThreadSafe { | |
33 public: | |
34 explicit ResourceCache(const base::FilePath& cache_path); | |
35 ~ResourceCache(); | |
36 | |
37 // Returns true if the underlying database was opened, and false otherwise. | |
38 // When this returns false then all the other operations will fail. | |
39 bool IsOpen() const { return db_; } | |
40 | |
41 // Stores |data| under (key, subkey). Returns true if the store suceeded, and | |
42 // false otherwise. | |
43 bool Store(const std::string& key, | |
44 const std::string& subkey, | |
45 const std::string& data); | |
46 | |
47 // Loads the contents of (key, subkey) into |data| and returns true. Returns | |
48 // false if (key, subkey) isn't found or if there is a problem reading the | |
49 // data. | |
50 bool Load(const std::string& key, | |
51 const std::string& subkey, | |
52 std::string* data); | |
53 | |
54 // Loads all the subkeys of |key| into |contents|. | |
55 void LoadAllSubkeys(const std::string& key, | |
56 std::map<std::string, std::string>* contents); | |
57 | |
58 // Deletes (key, subkey). | |
59 void Delete(const std::string& key, const std::string& subkey); | |
60 | |
61 // Deletes all the subkeys of |key| not in |subkeys_to_keep|. | |
62 void PurgeOtherSubkeys(const std::string& key, | |
63 const std::set<std::string>& subkeys_to_keep); | |
64 | |
65 private: | |
66 std::string GetStringWithPrefix(const std::string& s); | |
67 std::string CreatePathPrefix(const std::string& key); | |
68 std::string CreatePath(const std::string& key, const std::string& subkey); | |
69 std::string GetSubkey(const std::string& path); | |
70 | |
71 scoped_ptr<leveldb::DB> db_; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(ResourceCache); | |
74 }; | |
75 | |
76 } // namespace policy | |
77 | |
78 #endif // CHROME_BROWSER_POLICY_RESOURCE_CACHE_H_ | |
OLD | NEW |