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.
5 #ifndef COMPONENTS_POLICY_CORE_COMMON_CLOUD_RESOURCE_CACHE_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_CLOUD_RESOURCE_CACHE_H_
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"
19 class SequencedTaskRunner
;
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
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.
30 class POLICY_EXPORT ResourceCache
{
32 explicit ResourceCache(const base::FilePath
& cache_path
,
33 scoped_refptr
<base::SequencedTaskRunner
> task_runner
);
34 virtual ~ResourceCache();
36 // Stores |data| under (key, subkey). Returns true if the store suceeded, and
38 bool Store(const std::string
& key
,
39 const std::string
& subkey
,
40 const std::string
& data
);
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
45 bool Load(const std::string
& key
,
46 const std::string
& subkey
,
49 // Loads all the subkeys of |key| into |contents|.
50 void LoadAllSubkeys(const std::string
& key
,
51 std::map
<std::string
, std::string
>* contents
);
53 // Deletes (key, subkey).
54 void Delete(const std::string
& key
, const std::string
& subkey
);
56 // Deletes all the subkeys of |key|.
57 void Clear(const std::string
& key
);
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
);
63 // Deletes all keys not in |keys_to_keep|, along with their subkeys.
64 void PurgeOtherKeys(const std::set
<std::string
>& keys_to_keep
);
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
);
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
,
76 base::FilePath
* path
);
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
);
87 base::FilePath cache_dir_
;
89 // Task runner that |this| runs on.
90 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
92 DISALLOW_COPY_AND_ASSIGN(ResourceCache
);
97 #endif // COMPONENTS_POLICY_CORE_COMMON_CLOUD_RESOURCE_CACHE_H_