| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "services/url_response_disk_cache/url_response_disk_cache_impl.h" | 5 #include "services/url_response_disk_cache/url_response_disk_cache_impl.h" |
| 6 | 6 |
| 7 #include <type_traits> | 7 #include <type_traits> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 61 |
| 62 Array<uint8_t> PathToArray(const base::FilePath& path) { | 62 Array<uint8_t> PathToArray(const base::FilePath& path) { |
| 63 if (path.empty()) | 63 if (path.empty()) |
| 64 return Array<uint8_t>(); | 64 return Array<uint8_t>(); |
| 65 const std::string& string = path.value(); | 65 const std::string& string = path.value(); |
| 66 Array<uint8_t> result(string.size()); | 66 Array<uint8_t> result(string.size()); |
| 67 memcpy(&result.front(), string.data(), string.size()); | 67 memcpy(&result.front(), string.data(), string.size()); |
| 68 return result.Pass(); | 68 return result.Pass(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 // This method remove the query string of an url if one is present. It does |
| 72 // match the behavior of the application manager, which connects to the same app |
| 73 // if requested twice with different query parameters. |
| 74 std::string CanonicalizeURL(const std::string& url) { |
| 75 GURL gurl(url); |
| 76 |
| 77 if (!gurl.has_query()) { |
| 78 return gurl.spec(); |
| 79 } |
| 80 |
| 81 GURL::Replacements repl; |
| 82 repl.SetQueryStr(""); |
| 83 std::string result = gurl.ReplaceComponents(repl).spec(); |
| 84 // Remove the dangling '?' because it's ugly. |
| 85 base::ReplaceChars(result, "?", "", &result); |
| 86 return result; |
| 87 } |
| 88 |
| 71 // Encode a string in ascii. This uses _ as an escape character. It also escapes | 89 // Encode a string in ascii. This uses _ as an escape character. It also escapes |
| 72 // ':' because it is an usual path separator, and '#' because dart refuses it in | 90 // ':' because it is an usual path separator, and '#' because dart refuses it in |
| 73 // URLs. | 91 // URLs. |
| 74 std::string EncodeString(const std::string& string) { | 92 std::string EncodeString(const std::string& string) { |
| 75 std::string result = ""; | 93 std::string result = ""; |
| 76 for (size_t i = 0; i < string.size(); ++i) { | 94 for (size_t i = 0; i < string.size(); ++i) { |
| 77 unsigned char c = string[i]; | 95 unsigned char c = string[i]; |
| 78 if (c >= 32 && c < 128 && c != '_' && c != ':' && c != '#') { | 96 if (c >= 32 && c < 128 && c != '_' && c != ':' && c != '#') { |
| 79 result += c; | 97 result += c; |
| 80 } else { | 98 } else { |
| 81 result += base::StringPrintf("_%02x", c); | 99 result += base::StringPrintf("_%02x", c); |
| 82 } | 100 } |
| 83 } | 101 } |
| 84 return result; | 102 return result; |
| 85 } | 103 } |
| 86 | 104 |
| 87 // This service use a directory under HOME to store all of its data, | 105 // This service use a directory under HOME to store all of its data, |
| 88 base::FilePath GetBaseDirectory() { | 106 base::FilePath GetBaseDirectory() { |
| 89 return base::FilePath(getenv("HOME")).Append(".mojo_url_response_disk_cache"); | 107 return base::FilePath(getenv("HOME")).Append(".mojo_url_response_disk_cache"); |
| 90 } | 108 } |
| 91 | 109 |
| 92 // Returns the directory used store cached data for the given |url|, under | 110 // Returns the directory used store cached data for the given |url|, under |
| 93 // |base_directory|. | 111 // |base_directory|. |
| 94 base::FilePath GetDirName(base::FilePath base_directory, | 112 base::FilePath GetDirName(base::FilePath base_directory, |
| 95 const std::string& url) { | 113 const std::string& url) { |
| 96 // TODO(qsr): If the speed of directory traversal is problematic, this might | 114 // TODO(qsr): If the speed of directory traversal is problematic, this might |
| 97 // need to change to use less directories. | 115 // need to change to use less directories. |
| 98 return base_directory.Append(EncodeString(url)); | 116 return base_directory.Append(EncodeString(CanonicalizeURL(url))); |
| 99 } | 117 } |
| 100 | 118 |
| 101 // Returns the directory that the consumer can use to cache its own data. | 119 // Returns the directory that the consumer can use to cache its own data. |
| 102 base::FilePath GetConsumerCacheDirectory(const base::FilePath& main_cache) { | 120 base::FilePath GetConsumerCacheDirectory(const base::FilePath& main_cache) { |
| 103 return main_cache.Append("consumer_cache"); | 121 return main_cache.Append("consumer_cache"); |
| 104 } | 122 } |
| 105 | 123 |
| 106 // Returns the path of the sentinel used to keep track of a zipped response has | 124 // Returns the path of the sentinel used to keep track of a zipped response has |
| 107 // already been extracted. | 125 // already been extracted. |
| 108 base::FilePath GetExtractedSentinel(const base::FilePath& main_cache) { | 126 base::FilePath GetExtractedSentinel(const base::FilePath& main_cache) { |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 return; | 348 return; |
| 331 } | 349 } |
| 332 } | 350 } |
| 333 // We can ignore write error, as it will just force to clear the cache on the | 351 // We can ignore write error, as it will just force to clear the cache on the |
| 334 // next request. | 352 // next request. |
| 335 WriteFile(GetExtractedSentinel(base_dir), nullptr, 0); | 353 WriteFile(GetExtractedSentinel(base_dir), nullptr, 0); |
| 336 callback.Run(extracted_dir, cache_dir); | 354 callback.Run(extracted_dir, cache_dir); |
| 337 } | 355 } |
| 338 | 356 |
| 339 } // namespace mojo | 357 } // namespace mojo |
| OLD | NEW |