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/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 const std::string& subkey, | 87 const std::string& subkey, |
88 std::string* data) { | 88 std::string* data) { |
89 DCHECK(CalledOnValidThread()); | 89 DCHECK(CalledOnValidThread()); |
90 base::FilePath subkey_path; | 90 base::FilePath subkey_path; |
91 // Only read from |subkey_path| if it is not a symlink. | 91 // Only read from |subkey_path| if it is not a symlink. |
92 if (!VerifyKeyPathAndGetSubkeyPath(key, false, subkey, &subkey_path) || | 92 if (!VerifyKeyPathAndGetSubkeyPath(key, false, subkey, &subkey_path) || |
93 file_util::IsLink(subkey_path)) { | 93 file_util::IsLink(subkey_path)) { |
94 return false; | 94 return false; |
95 } | 95 } |
96 data->clear(); | 96 data->clear(); |
97 return file_util::ReadFileToString(subkey_path, data); | 97 return base::ReadFileToString(subkey_path, data); |
98 } | 98 } |
99 | 99 |
100 void ResourceCache::LoadAllSubkeys( | 100 void ResourceCache::LoadAllSubkeys( |
101 const std::string& key, | 101 const std::string& key, |
102 std::map<std::string, std::string>* contents) { | 102 std::map<std::string, std::string>* contents) { |
103 DCHECK(CalledOnValidThread()); | 103 DCHECK(CalledOnValidThread()); |
104 contents->clear(); | 104 contents->clear(); |
105 base::FilePath key_path; | 105 base::FilePath key_path; |
106 if (!VerifyKeyPath(key, false, &key_path)) | 106 if (!VerifyKeyPath(key, false, &key_path)) |
107 return; | 107 return; |
108 | 108 |
109 base::FileEnumerator enumerator(key_path, false, base::FileEnumerator::FILES); | 109 base::FileEnumerator enumerator(key_path, false, base::FileEnumerator::FILES); |
110 for (base::FilePath path = enumerator.Next(); !path.empty(); | 110 for (base::FilePath path = enumerator.Next(); !path.empty(); |
111 path = enumerator.Next()) { | 111 path = enumerator.Next()) { |
112 const std::string encoded_subkey = path.BaseName().MaybeAsASCII(); | 112 const std::string encoded_subkey = path.BaseName().MaybeAsASCII(); |
113 std::string subkey; | 113 std::string subkey; |
114 std::string data; | 114 std::string data; |
115 // Only read from |subkey_path| if it is not a symlink and its name is | 115 // Only read from |subkey_path| if it is not a symlink and its name is |
116 // a base64-encoded string. | 116 // a base64-encoded string. |
117 if (!file_util::IsLink(path) && | 117 if (!file_util::IsLink(path) && |
118 Base64Decode(encoded_subkey, &subkey) && | 118 Base64Decode(encoded_subkey, &subkey) && |
119 file_util::ReadFileToString(path, &data)) { | 119 base::ReadFileToString(path, &data)) { |
120 (*contents)[subkey].swap(data); | 120 (*contents)[subkey].swap(data); |
121 } | 121 } |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 void ResourceCache::Delete(const std::string& key, const std::string& subkey) { | 125 void ResourceCache::Delete(const std::string& key, const std::string& subkey) { |
126 DCHECK(CalledOnValidThread()); | 126 DCHECK(CalledOnValidThread()); |
127 base::FilePath subkey_path; | 127 base::FilePath subkey_path; |
128 if (VerifyKeyPathAndGetSubkeyPath(key, false, subkey, &subkey_path)) | 128 if (VerifyKeyPathAndGetSubkeyPath(key, false, subkey, &subkey_path)) |
129 base::DeleteFile(subkey_path, false); | 129 base::DeleteFile(subkey_path, false); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 if (!VerifyKeyPath(key, allow_create_key, &key_path) || | 194 if (!VerifyKeyPath(key, allow_create_key, &key_path) || |
195 !Base64Encode(subkey, &encoded)) { | 195 !Base64Encode(subkey, &encoded)) { |
196 return false; | 196 return false; |
197 } | 197 } |
198 *path = key_path.AppendASCII(encoded); | 198 *path = key_path.AppendASCII(encoded); |
199 return true; | 199 return true; |
200 } | 200 } |
201 | 201 |
202 | 202 |
203 } // namespace policy | 203 } // namespace policy |
OLD | NEW |