cloud_external_data_store.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1// Copyright 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_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_STORE_H_
6#define CHROME_BROWSER_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_STORE_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "components/policy/core/common/cloud/cloud_external_data_manager.h"
13
14namespace base {
15class SequencedTaskRunner;
16}
17
18namespace policy {
19
20class ResourceCache;
21
22// Stores external data referenced by policies. Data is keyed by (policy, hash),
23// the name of the policy referencing it and its SHA1 hash. Outdated entries are
24// removed by calling Prune() with the list of (policy, hash) entries that are
25// to be kept. Instances of this class may be created on any thread and may
26// share the same cache, however:
27// * After creation, the cache and all stores using it must always be accessed
28//   via the same |task_runner| only.
29// * Stores sharing a cache must use different cache_keys to avoid namespace
30//   overlaps.
31// * The cache must outlive all stores using it.
32class CloudExternalDataStore {
33 public:
34  CloudExternalDataStore(const std::string& cache_key,
35                         scoped_refptr<base::SequencedTaskRunner> task_runner,
36                         ResourceCache* cache);
37  ~CloudExternalDataStore();
38
39  // Removes all entries from the store whose (policy, hash) pair is not found
40  // in |metadata|.
41  void Prune(const CloudExternalDataManager::Metadata& metadata);
42
43  // Stores |data| under (policy, hash). Returns true if the store succeeded.
44  bool Store(const std::string& policy,
45             const std::string& hash,
46             const std::string& data);
47
48  // Loads the entry at (policy, hash) into |data|, verifies that it does not
49  // exceed |max_size| and matches the expected |hash|, then returns true.
50  // Returns false if no entry is found at (policy, hash), there is a problem
51  // during the load, the entry exceeds |max_size| or does not match |hash|.
52  bool Load(const std::string& policy,
53            const std::string& hash,
54            size_t max_size,
55            std::string* data);
56
57 private:
58  std::string cache_key_;
59
60  // Task runner that |this| runs on.
61  scoped_refptr<base::SequencedTaskRunner> task_runner_;
62
63  ResourceCache* cache_;  // Not owned.
64
65  DISALLOW_COPY_AND_ASSIGN(CloudExternalDataStore);
66};
67
68}  // namespace policy
69
70#endif  // CHROME_BROWSER_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_STORE_H_
71