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" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/safe_numerics.h" | 12 #include "base/safe_numerics.h" |
13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 | 15 |
16 namespace policy { | 16 namespace policy { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Verifies that |value| is not empty and encodes it into base64url format, | 20 // Verifies that |value| is not empty and encodes it into base64url format, |
21 // which is safe to use as a file name on all platforms. | 21 // which is safe to use as a file name on all platforms. |
22 bool Base64Encode(const std::string& value, std::string* encoded) { | 22 bool Base64Encode(const std::string& value, std::string* encoded) { |
23 DCHECK(!value.empty()); | 23 DCHECK(!value.empty()); |
24 if (value.empty() || !base::Base64Encode(value, encoded)) | 24 if (value.empty() || !base::Base64Encode(value, encoded)) |
25 return false; | 25 return false; |
26 ReplaceChars(*encoded, "+", "-", encoded); | 26 base::ReplaceChars(*encoded, "+", "-", encoded); |
27 ReplaceChars(*encoded, "/", "_", encoded); | 27 base::ReplaceChars(*encoded, "/", "_", encoded); |
28 return true; | 28 return true; |
29 } | 29 } |
30 | 30 |
31 // Decodes all elements of |input| from base64url format and stores the decoded | 31 // Decodes all elements of |input| from base64url format and stores the decoded |
32 // elements in |output|. | 32 // elements in |output|. |
33 bool Base64Encode(const std::set<std::string>& input, | 33 bool Base64Encode(const std::set<std::string>& input, |
34 std::set<std::string>* output) { | 34 std::set<std::string>* output) { |
35 output->clear(); | 35 output->clear(); |
36 for (std::set<std::string>::const_iterator it = input.begin(); | 36 for (std::set<std::string>::const_iterator it = input.begin(); |
37 it != input.end(); ++it) { | 37 it != input.end(); ++it) { |
38 std::string encoded; | 38 std::string encoded; |
39 if (!Base64Encode(*it, &encoded)) { | 39 if (!Base64Encode(*it, &encoded)) { |
40 output->clear(); | 40 output->clear(); |
41 return false; | 41 return false; |
42 } | 42 } |
43 output->insert(encoded); | 43 output->insert(encoded); |
44 } | 44 } |
45 return true; | 45 return true; |
46 } | 46 } |
47 | 47 |
48 // Decodes |encoded| from base64url format and verifies that the result is not | 48 // Decodes |encoded| from base64url format and verifies that the result is not |
49 // emtpy. | 49 // emtpy. |
50 bool Base64Decode(const std::string& encoded, std::string* value) { | 50 bool Base64Decode(const std::string& encoded, std::string* value) { |
51 std::string buffer; | 51 std::string buffer; |
52 ReplaceChars(encoded, "-", "+", &buffer); | 52 base::ReplaceChars(encoded, "-", "+", &buffer); |
53 ReplaceChars(buffer, "_", "/", &buffer); | 53 base::ReplaceChars(buffer, "_", "/", &buffer); |
54 return base::Base64Decode(buffer, value) && !value->empty(); | 54 return base::Base64Decode(buffer, value) && !value->empty(); |
55 } | 55 } |
56 | 56 |
57 } // namespace | 57 } // namespace |
58 | 58 |
59 ResourceCache::ResourceCache( | 59 ResourceCache::ResourceCache( |
60 const base::FilePath& cache_dir, | 60 const base::FilePath& cache_dir, |
61 scoped_refptr<base::SequencedTaskRunner> task_runner) | 61 scoped_refptr<base::SequencedTaskRunner> task_runner) |
62 : cache_dir_(cache_dir), | 62 : cache_dir_(cache_dir), |
63 task_runner_(task_runner) { | 63 task_runner_(task_runner) { |
(...skipping 167 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 |