| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/policy/cloud/resource_cache.h" | 5 #include "chrome/browser/policy/cloud/resource_cache.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 (file_util::WriteFile(subkey_path, data.data(), size) == size); | 87 (file_util::WriteFile(subkey_path, data.data(), size) == size); |
| 88 } | 88 } |
| 89 | 89 |
| 90 bool ResourceCache::Load(const std::string& key, | 90 bool ResourceCache::Load(const std::string& key, |
| 91 const std::string& subkey, | 91 const std::string& subkey, |
| 92 std::string* data) { | 92 std::string* data) { |
| 93 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 93 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 94 base::FilePath subkey_path; | 94 base::FilePath subkey_path; |
| 95 // Only read from |subkey_path| if it is not a symlink. | 95 // Only read from |subkey_path| if it is not a symlink. |
| 96 if (!VerifyKeyPathAndGetSubkeyPath(key, false, subkey, &subkey_path) || | 96 if (!VerifyKeyPathAndGetSubkeyPath(key, false, subkey, &subkey_path) || |
| 97 file_util::IsLink(subkey_path)) { | 97 base::IsLink(subkey_path)) { |
| 98 return false; | 98 return false; |
| 99 } | 99 } |
| 100 data->clear(); | 100 data->clear(); |
| 101 return base::ReadFileToString(subkey_path, data); | 101 return base::ReadFileToString(subkey_path, data); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void ResourceCache::LoadAllSubkeys( | 104 void ResourceCache::LoadAllSubkeys( |
| 105 const std::string& key, | 105 const std::string& key, |
| 106 std::map<std::string, std::string>* contents) { | 106 std::map<std::string, std::string>* contents) { |
| 107 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 107 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 108 contents->clear(); | 108 contents->clear(); |
| 109 base::FilePath key_path; | 109 base::FilePath key_path; |
| 110 if (!VerifyKeyPath(key, false, &key_path)) | 110 if (!VerifyKeyPath(key, false, &key_path)) |
| 111 return; | 111 return; |
| 112 | 112 |
| 113 base::FileEnumerator enumerator(key_path, false, base::FileEnumerator::FILES); | 113 base::FileEnumerator enumerator(key_path, false, base::FileEnumerator::FILES); |
| 114 for (base::FilePath path = enumerator.Next(); !path.empty(); | 114 for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 115 path = enumerator.Next()) { | 115 path = enumerator.Next()) { |
| 116 const std::string encoded_subkey = path.BaseName().MaybeAsASCII(); | 116 const std::string encoded_subkey = path.BaseName().MaybeAsASCII(); |
| 117 std::string subkey; | 117 std::string subkey; |
| 118 std::string data; | 118 std::string data; |
| 119 // Only read from |subkey_path| if it is not a symlink and its name is | 119 // Only read from |subkey_path| if it is not a symlink and its name is |
| 120 // a base64-encoded string. | 120 // a base64-encoded string. |
| 121 if (!file_util::IsLink(path) && | 121 if (!base::IsLink(path) && |
| 122 Base64Decode(encoded_subkey, &subkey) && | 122 Base64Decode(encoded_subkey, &subkey) && |
| 123 base::ReadFileToString(path, &data)) { | 123 base::ReadFileToString(path, &data)) { |
| 124 (*contents)[subkey].swap(data); | 124 (*contents)[subkey].swap(data); |
| 125 } | 125 } |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 | 128 |
| 129 void ResourceCache::Delete(const std::string& key, const std::string& subkey) { | 129 void ResourceCache::Delete(const std::string& key, const std::string& subkey) { |
| 130 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 130 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 131 base::FilePath subkey_path; | 131 base::FilePath subkey_path; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 if (!VerifyKeyPath(key, allow_create_key, &key_path) || | 231 if (!VerifyKeyPath(key, allow_create_key, &key_path) || |
| 232 !Base64Encode(subkey, &encoded)) { | 232 !Base64Encode(subkey, &encoded)) { |
| 233 return false; | 233 return false; |
| 234 } | 234 } |
| 235 *path = key_path.AppendASCII(encoded); | 235 *path = key_path.AppendASCII(encoded); |
| 236 return true; | 236 return true; |
| 237 } | 237 } |
| 238 | 238 |
| 239 | 239 |
| 240 } // namespace policy | 240 } // namespace policy |
| OLD | NEW |