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.
5 #include "chrome/browser/chromeos/policy/cloud_external_data_store.h"
9 #include "base/logging.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/sha1.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "chrome/browser/policy/cloud/resource_cache.h"
19 // Encodes (policy, hash) into a single string.
20 std::string GetSubkey(const std::string& policy, const std::string& hash) {
21 DCHECK(!policy.empty());
22 DCHECK(!hash.empty());
23 return base::IntToString(policy.size()) + ":" +
24 base::IntToString(hash.size()) + ":" +
30 CloudExternalDataStore::CloudExternalDataStore(
31 const std::string& cache_key,
32 scoped_refptr<base::SequencedTaskRunner> task_runner,
34 : cache_key_(cache_key),
35 task_runner_(task_runner),
39 CloudExternalDataStore::~CloudExternalDataStore() {
40 DCHECK(task_runner_->RunsTasksOnCurrentThread());
43 void CloudExternalDataStore::Prune(
44 const CloudExternalDataManager::Metadata& metadata) {
45 DCHECK(task_runner_->RunsTasksOnCurrentThread());
46 std::set<std::string> subkeys_to_keep;
47 for (CloudExternalDataManager::Metadata::const_iterator it = metadata.begin();
48 it != metadata.end(); ++it) {
49 subkeys_to_keep.insert(GetSubkey(it->first, it->second.hash));
51 cache_->PurgeOtherSubkeys(cache_key_, subkeys_to_keep);
54 bool CloudExternalDataStore::Store(const std::string& policy,
55 const std::string& hash,
56 const std::string& data) {
57 DCHECK(task_runner_->RunsTasksOnCurrentThread());
58 return cache_->Store(cache_key_, GetSubkey(policy, hash), data);
61 bool CloudExternalDataStore::Load(const std::string& policy,
62 const std::string& hash,
65 DCHECK(task_runner_->RunsTasksOnCurrentThread());
66 const std::string subkey = GetSubkey(policy, hash);
67 if (cache_->Load(cache_key_, subkey, data)) {
68 if (data->size() <= max_size && base::SHA1HashString(*data) == hash)
70 // If the data is larger than allowed or does not match the expected hash,
72 cache_->Delete(cache_key_, subkey);