| 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_CLOUD_RESOURCE_CACHE_H_ | |
| 6 #define CHROME_BROWSER_POLICY_CLOUD_RESOURCE_CACHE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/callback_forward.h" | |
| 14 #include "base/files/file_path.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 | |
| 17 namespace base { | |
| 18 class SequencedTaskRunner; | |
| 19 } | |
| 20 | |
| 21 namespace policy { | |
| 22 | |
| 23 // Manages storage of data at a given path. The data is keyed by a key and | |
| 24 // a subkey, and can be queried by (key, subkey) or (key) lookups. | |
| 25 // The contents of the cache have to be manually cleared using Delete() or | |
| 26 // Purge*(). | |
| 27 // The class can be instantiated on any thread but from then on, it must be | |
| 28 // accessed via the |task_runner| only. The |task_runner| must support file I/O. | |
| 29 class ResourceCache { | |
| 30 public: | |
| 31 explicit ResourceCache(const base::FilePath& cache_path, | |
| 32 scoped_refptr<base::SequencedTaskRunner> task_runner); | |
| 33 virtual ~ResourceCache(); | |
| 34 | |
| 35 // Stores |data| under (key, subkey). Returns true if the store suceeded, and | |
| 36 // false otherwise. | |
| 37 bool Store(const std::string& key, | |
| 38 const std::string& subkey, | |
| 39 const std::string& data); | |
| 40 | |
| 41 // Loads the contents of (key, subkey) into |data| and returns true. Returns | |
| 42 // false if (key, subkey) isn't found or if there is a problem reading the | |
| 43 // data. | |
| 44 bool Load(const std::string& key, | |
| 45 const std::string& subkey, | |
| 46 std::string* data); | |
| 47 | |
| 48 // Loads all the subkeys of |key| into |contents|. | |
| 49 void LoadAllSubkeys(const std::string& key, | |
| 50 std::map<std::string, std::string>* contents); | |
| 51 | |
| 52 // Deletes (key, subkey). | |
| 53 void Delete(const std::string& key, const std::string& subkey); | |
| 54 | |
| 55 // Deletes all the subkeys of |key|. | |
| 56 void Clear(const std::string& key); | |
| 57 | |
| 58 // Deletes the subkeys of |key| for which the |filter| returns true. | |
| 59 typedef base::Callback<bool(const std::string&)> SubkeyFilter; | |
| 60 void FilterSubkeys(const std::string& key, const SubkeyFilter& filter); | |
| 61 | |
| 62 // Deletes all keys not in |keys_to_keep|, along with their subkeys. | |
| 63 void PurgeOtherKeys(const std::set<std::string>& keys_to_keep); | |
| 64 | |
| 65 // Deletes all the subkeys of |key| not in |subkeys_to_keep|. | |
| 66 void PurgeOtherSubkeys(const std::string& key, | |
| 67 const std::set<std::string>& subkeys_to_keep); | |
| 68 | |
| 69 private: | |
| 70 // Points |path| at the cache directory for |key| and returns whether the | |
| 71 // directory exists. If |allow_create| is |true|, the directory is created if | |
| 72 // it did not exist yet. | |
| 73 bool VerifyKeyPath(const std::string& key, | |
| 74 bool allow_create, | |
| 75 base::FilePath* path); | |
| 76 | |
| 77 // Points |path| at the file in which data for (key, subkey) should be stored | |
| 78 // and returns whether the parent directory of this file exists. If | |
| 79 // |allow_create_key| is |true|, the directory is created if it did not exist | |
| 80 // yet. This method does not check whether the file at |path| exists or not. | |
| 81 bool VerifyKeyPathAndGetSubkeyPath(const std::string& key, | |
| 82 bool allow_create_key, | |
| 83 const std::string& subkey, | |
| 84 base::FilePath* subkey_path); | |
| 85 | |
| 86 base::FilePath cache_dir_; | |
| 87 | |
| 88 // Task runner that |this| runs on. | |
| 89 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(ResourceCache); | |
| 92 }; | |
| 93 | |
| 94 } // namespace policy | |
| 95 | |
| 96 #endif // CHROME_BROWSER_POLICY_CLOUD_RESOURCE_CACHE_H_ | |
| OLD | NEW |