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/files/file_path.h" 14 #include "base/threading/non_thread_safe.h" 15 16 namespace policy { 17 18 // Manages storage of data at a given path. The data is keyed by a key and 19 // a subkey, and can be queried by (key, subkey) or (key) lookups. 20 // The contents of the cache have to be manually cleared using Delete() or 21 // PurgeOtherSubkeys(). 22 // Instances of this class can be created on any thread, but from then on must 23 // be always used from the same thread, and it must support file I/O. 24 class ResourceCache : public base::NonThreadSafe { 25 public: 26 explicit ResourceCache(const base::FilePath& cache_path); 27 virtual ~ResourceCache(); 28 29 // Stores |data| under (key, subkey). Returns true if the store suceeded, and 30 // false otherwise. 31 bool Store(const std::string& key, 32 const std::string& subkey, 33 const std::string& data); 34 35 // Loads the contents of (key, subkey) into |data| and returns true. Returns 36 // false if (key, subkey) isn't found or if there is a problem reading the 37 // data. 38 bool Load(const std::string& key, 39 const std::string& subkey, 40 std::string* data); 41 42 // Loads all the subkeys of |key| into |contents|. 43 void LoadAllSubkeys(const std::string& key, 44 std::map<std::string, std::string>* contents); 45 46 // Deletes (key, subkey). 47 void Delete(const std::string& key, const std::string& subkey); 48 49 // Deletes all keys not in |keys_to_keep|, along with their subkeys. 50 void PurgeOtherKeys(const std::set<std::string>& keys_to_keep); 51 52 // Deletes all the subkeys of |key| not in |subkeys_to_keep|. 53 void PurgeOtherSubkeys(const std::string& key, 54 const std::set<std::string>& subkeys_to_keep); 55 56 private: 57 // Points |path| at the cache directory for |key| and returns whether the 58 // directory exists. If |allow_create| is |true|, the directory is created if 59 // it did not exist yet. 60 bool VerifyKeyPath(const std::string& key, 61 bool allow_create, 62 base::FilePath* path); 63 64 // Points |path| at the file in which data for (key, subkey) should be stored 65 // and returns whether the parent directory of this file exists. If 66 // |allow_create_key| is |true|, the directory is created if it did not exist 67 // yet. This method does not check whether the file at |path| exists or not. 68 bool VerifyKeyPathAndGetSubkeyPath(const std::string& key, 69 bool allow_create_key, 70 const std::string& subkey, 71 base::FilePath* subkey_path); 72 73 base::FilePath cache_dir_; 74 75 DISALLOW_COPY_AND_ASSIGN(ResourceCache); 76 }; 77 78 } // namespace policy 79 80 #endif // CHROME_BROWSER_POLICY_CLOUD_RESOURCE_CACHE_H_ 81