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