| 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 <dirent.h> | 7 #include <dirent.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 | 10 |
| 11 #include <type_traits> | 11 #include <type_traits> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback_helpers.h" |
| 14 #include "base/files/file.h" | 15 #include "base/files/file.h" |
| 15 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 16 #include "base/location.h" | 17 #include "base/location.h" |
| 17 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 20 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
| 23 #include "crypto/random.h" |
| 21 #include "mojo/data_pipe_utils/data_pipe_utils.h" | 24 #include "mojo/data_pipe_utils/data_pipe_utils.h" |
| 22 #include "mojo/public/cpp/application/application_connection.h" | 25 #include "mojo/public/cpp/application/application_connection.h" |
| 23 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 26 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
| 27 #include "mojo/public/interfaces/network/http_header.mojom.h" |
| 24 #include "services/url_response_disk_cache/url_response_disk_cache_entry.mojom.h
" | 28 #include "services/url_response_disk_cache/url_response_disk_cache_entry.mojom.h
" |
| 25 #include "third_party/zlib/google/zip_reader.h" | 29 #include "third_party/zlib/google/zip_reader.h" |
| 26 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 27 | 31 |
| 28 namespace mojo { | 32 namespace mojo { |
| 29 | 33 |
| 30 namespace { | 34 namespace { |
| 31 | 35 |
| 32 // The current version of the cache. This should only be incremented. When this | 36 // The current version of the cache. This should only be incremented. When this |
| 33 // is incremented, all current cache entries will be invalidated. | 37 // is incremented, all current cache entries will be invalidated. |
| 34 const uint32_t kCurrentVersion = 2; | 38 const uint32_t kCurrentVersion = 0; |
| 39 |
| 40 // The delay to wait before starting deleting data. This is delayed to not |
| 41 // interfere with the shell startup. |
| 42 const uint32_t kTrashDelayInSeconds = 60; |
| 35 | 43 |
| 36 const char kEtagHeader[] = "etag"; | 44 const char kEtagHeader[] = "etag"; |
| 37 | 45 |
| 38 const char kEntryName[] = "entry"; | 46 // Create a new identifier for a cache entry. This will be used as an unique |
| 39 const char kEntryTmpName[] = "entry.tmp"; | 47 // directory name. |
| 40 | 48 std::string GetNewIdentifier() { |
| 41 // TODO(darin): These Serialize / Deserialize methods should not live here. | 49 char bytes[32]; |
| 42 // They use private details of the bindings system. Instead, we should provide | 50 crypto::RandBytes(bytes, arraysize(bytes)); |
| 43 // these as helper functions under mojo/public/cpp/bindings/. | 51 return base::HexEncode(bytes, arraysize(bytes)); |
| 44 | |
| 45 template <typename T> | |
| 46 void Serialize(T input, std::string* output) { | |
| 47 typedef typename mojo::internal::WrapperTraits<T>::DataType DataType; | |
| 48 size_t size = GetSerializedSize_(input); | |
| 49 | |
| 50 output->clear(); | |
| 51 output->resize(size); | |
| 52 | |
| 53 mojo::internal::FixedBuffer buf; | |
| 54 buf.Initialize(&output->at(0), size); | |
| 55 | |
| 56 DataType data_type; | |
| 57 Serialize_(input.Pass(), &buf, &data_type); | |
| 58 std::vector<Handle> handles; | |
| 59 data_type->EncodePointersAndHandles(&handles); | |
| 60 } | 52 } |
| 61 | 53 |
| 62 template <typename T> | 54 void DoNothing(const base::FilePath& fp1, const base::FilePath& fp2) {} |
| 63 bool Deserialize(std::string input, T* output) { | 55 |
| 64 typedef typename mojo::internal::WrapperTraits<T>::DataType DataType; | 56 void MovePathIntoDir(const base::FilePath& source, |
| 65 mojo::internal::BoundsChecker bounds_checker(&input[0], input.size(), 0); | 57 const base::FilePath& destination) { |
| 66 if (!std::remove_pointer<DataType>::type::Validate(&input[0], | 58 if (!PathExists(source)) |
| 67 &bounds_checker)) { | 59 return; |
| 68 return false; | 60 |
| 61 base::FilePath tmp_dir; |
| 62 base::CreateTemporaryDirInDir(destination, "", &tmp_dir); |
| 63 base::File::Error error; |
| 64 if (!base::ReplaceFile(source, tmp_dir, &error)) { |
| 65 LOG(ERROR) << "Failed to clear dir content: " << error; |
| 69 } | 66 } |
| 70 DataType data_type = reinterpret_cast<DataType>(&input[0]); | |
| 71 std::vector<Handle> handles; | |
| 72 data_type->DecodePointersAndHandles(&handles); | |
| 73 Deserialize_(data_type, output); | |
| 74 return true; | |
| 75 } | 67 } |
| 76 | 68 |
| 77 void SaveEntry(CacheEntryPtr entry, base::ScopedFD dir) { | 69 void ClearTrashDir(scoped_refptr<base::TaskRunner> task_runner, |
| 78 TRACE_EVENT0("url_response_disk_cache", "SaveEntry"); | 70 const base::FilePath& trash_dir) { |
| 79 | 71 // Delete the trash directory. |
| 80 std::string serialized_entry; | 72 task_runner->PostDelayedTask( |
| 81 Serialize(entry.Pass(), &serialized_entry); | 73 FROM_HERE, |
| 82 DCHECK_LT(serialized_entry.size(), | 74 base::Bind(base::IgnoreResult(&base::DeleteFile), trash_dir, true), |
| 83 static_cast<size_t>(std::numeric_limits<int>::max())); | 75 base::TimeDelta::FromSeconds(kTrashDelayInSeconds)); |
| 84 | |
| 85 int file_fd = | |
| 86 HANDLE_EINTR(openat(dir.get(), kEntryTmpName, O_WRONLY | O_CREAT, 0600)); | |
| 87 if (file_fd < 0) | |
| 88 return; | |
| 89 base::File file(file_fd); | |
| 90 if (file.WriteAtCurrentPos(serialized_entry.data(), | |
| 91 serialized_entry.size()) != | |
| 92 static_cast<int>(serialized_entry.size())) | |
| 93 return; | |
| 94 // The file must be closed before the file is moved. | |
| 95 file.Close(); | |
| 96 renameat(dir.get(), kEntryTmpName, dir.get(), kEntryName); | |
| 97 } | 76 } |
| 98 | 77 |
| 99 Array<uint8_t> PathToArray(const base::FilePath& path) { | 78 Array<uint8_t> PathToArray(const base::FilePath& path) { |
| 100 if (path.empty()) | 79 if (path.empty()) |
| 101 return Array<uint8_t>(); | 80 return Array<uint8_t>(); |
| 102 const std::string& string = path.value(); | 81 const std::string& string = path.value(); |
| 103 Array<uint8_t> result(string.size()); | 82 Array<uint8_t> result(string.size()); |
| 104 memcpy(&result.front(), string.data(), string.size()); | 83 memcpy(&result.front(), string.data(), string.size()); |
| 105 return result.Pass(); | 84 return result.Pass(); |
| 106 } | 85 } |
| 107 | 86 |
| 108 // This method remove the query string of an url if one is present. It does | 87 // This method remove the query string of an url if one is present. It does |
| 109 // match the behavior of the application manager, which connects to the same app | 88 // match the behavior of the application manager, which connects to the same app |
| 110 // if requested twice with different query parameters. | 89 // if requested twice with different query parameters. |
| 111 std::string CanonicalizeURL(const std::string& url) { | 90 std::string CanonicalizeURL(const std::string& url) { |
| 112 GURL gurl(url); | 91 GURL gurl(url); |
| 113 | 92 |
| 114 if (!gurl.has_query()) { | 93 if (!gurl.has_query()) { |
| 115 return gurl.spec(); | 94 return gurl.spec(); |
| 116 } | 95 } |
| 117 | 96 |
| 118 GURL::Replacements repl; | 97 GURL::Replacements repl; |
| 119 repl.SetQueryStr(""); | 98 repl.SetQueryStr(""); |
| 120 std::string result = gurl.ReplaceComponents(repl).spec(); | 99 std::string result = gurl.ReplaceComponents(repl).spec(); |
| 121 // Remove the dangling '?' because it's ugly. | 100 // Remove the dangling '?' because it's ugly. |
| 122 base::ReplaceChars(result, "?", "", &result); | 101 base::ReplaceChars(result, "?", "", &result); |
| 123 return result; | 102 return result; |
| 124 } | 103 } |
| 125 | 104 |
| 126 // Encode a string in ascii. This uses _ as an escape character. It also escapes | |
| 127 // ':' because it is an usual path separator, and '#' because dart refuses it in | |
| 128 // URLs. | |
| 129 std::string EncodeString(const std::string& string) { | |
| 130 std::string result = ""; | |
| 131 for (size_t i = 0; i < string.size(); ++i) { | |
| 132 unsigned char c = string[i]; | |
| 133 if (c >= 32 && c < 128 && c != '_' && c != ':' && c != '#') { | |
| 134 result += c; | |
| 135 } else { | |
| 136 result += base::StringPrintf("_%02x", c); | |
| 137 } | |
| 138 } | |
| 139 return result; | |
| 140 } | |
| 141 | |
| 142 // 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, |
| 143 base::FilePath GetBaseDirectory() { | 106 base::FilePath GetBaseDirectory() { |
| 144 return base::FilePath(getenv("HOME")).Append(".mojo_url_response_disk_cache"); | 107 return base::FilePath(getenv("HOME")).Append(".mojo_url_response_disk_cache"); |
| 145 } | 108 } |
| 146 | 109 |
| 110 // Returns the directory containing live data for the cache. |
| 147 base::FilePath GetCacheDirectory() { | 111 base::FilePath GetCacheDirectory() { |
| 148 return GetBaseDirectory().Append("cache"); | 112 return GetBaseDirectory().Append("cache"); |
| 149 } | 113 } |
| 150 | 114 |
| 115 // Returns a temporary that will be deleted after startup. This is used to have |
| 116 // a consistent directory for outdated files in case the trash process doesn't |
| 117 // finish. |
| 151 base::FilePath GetTrashDirectory() { | 118 base::FilePath GetTrashDirectory() { |
| 152 return GetBaseDirectory().Append("trash"); | 119 return GetBaseDirectory().Append("trash"); |
| 153 } | 120 } |
| 154 | 121 |
| 155 // Returns the directory used store cached data for the given |url|, under | 122 // Returns a staging directory to save file before an entry can be saved in the |
| 156 // |base_directory|. | 123 // database. This directory is deleted when the service is started. This is used |
| 157 base::FilePath GetDirName(base::FilePath base_directory, | 124 // to prevent leaking files if there is an interruption while downloading a |
| 158 const std::string& url) { | 125 // response. |
| 159 // TODO(qsr): If the speed of directory traversal is problematic, this might | 126 base::FilePath GetStagingDirectory() { |
| 160 // need to change to use less directories. | 127 return GetBaseDirectory().Append("staging"); |
| 161 return base_directory.Append(EncodeString(CanonicalizeURL(url))); | |
| 162 } | 128 } |
| 163 | 129 |
| 164 // Returns the directory that the consumer can use to cache its own data. | 130 // Returns path of the directory that the consumer can use to cache its own |
| 165 base::FilePath GetConsumerCacheDirectory(const base::FilePath& main_cache) { | 131 // data. |
| 166 return main_cache.Append("consumer_cache"); | 132 base::FilePath GetConsumerCacheDirectory( |
| 133 const base::FilePath& entry_directory) { |
| 134 return entry_directory.Append("consumer_cache"); |
| 167 } | 135 } |
| 168 | 136 |
| 169 // Returns the path of the sentinel used to keep track of a zipped response has | 137 // Returns the path of the sentinel used to mark that a zipped response has |
| 170 // already been extracted. | 138 // already been extracted. |
| 171 base::FilePath GetExtractedSentinel(const base::FilePath& main_cache) { | 139 base::FilePath GetExtractionSentinel(const base::FilePath& entry_directory) { |
| 172 return main_cache.Append("extracted_sentinel"); | 140 return entry_directory.Append("extraction_sentinel"); |
| 141 } |
| 142 |
| 143 // Returns the path of the directory where a zipped content is extracted. |
| 144 base::FilePath GetExtractionDirectory(const base::FilePath& entry_directory) { |
| 145 return entry_directory.Append("extracted"); |
| 173 } | 146 } |
| 174 | 147 |
| 175 // Runs the given callback. If |success| is false, call back with an error. | 148 // Runs the given callback. If |success| is false, call back with an error. |
| 176 // Otherwise, store |entry| in |entry_path|, then call back with the given | 149 // Otherwise, store a new entry in the databse, then call back with the content |
| 177 // paths. | 150 // path and the consumer cache path. |
| 178 void RunCallbackWithSuccess( | 151 void RunCallbackWithSuccess( |
| 179 const URLResponseDiskCacheImpl::FilePathPairCallback& callback, | 152 const URLResponseDiskCacheImpl::ResponseFileAndCacheDirCallback& callback, |
| 180 const base::FilePath& content_path, | 153 const std::string& identifier, |
| 181 const base::FilePath& cache_dir, | 154 const std::string& request_origin, |
| 182 const base::FilePath& entry_path, | 155 const std::string& url, |
| 183 CacheEntryPtr entry, | 156 URLResponsePtr response, |
| 184 base::TaskRunner* task_runner, | 157 scoped_refptr<URLResponseDiskCacheDB> db, |
| 158 scoped_refptr<base::TaskRunner> task_runner, |
| 185 bool success) { | 159 bool success) { |
| 186 TRACE_EVENT1("url_response_disk_cache", "RunCallbackWithSuccess", | 160 TRACE_EVENT2("url_response_disk_cache", "RunCallbackWithSuccess", "url", url, |
| 187 "content_path", content_path.value()); | 161 "identifier", identifier); |
| 188 if (!success) { | 162 if (!success) { |
| 189 callback.Run(base::FilePath(), base::FilePath()); | 163 callback.Run(base::FilePath(), base::FilePath()); |
| 190 return; | 164 return; |
| 191 } | 165 } |
| 192 | 166 |
| 193 // Save the entry in a background thread. The entry directory is opened and | 167 base::FilePath staged_response_body_path = |
| 194 // passed because if this service wants to replace the content later on, it | 168 GetStagingDirectory().Append(identifier); |
| 195 // will start by moving the current directory. | 169 base::FilePath entry_directory = GetCacheDirectory().Append(identifier); |
| 196 base::ScopedFD dir(HANDLE_EINTR( | 170 base::FilePath response_body_path = entry_directory.Append(identifier); |
| 197 open(entry_path.DirName().value().c_str(), O_RDONLY | O_DIRECTORY))); | 171 base::FilePath consumer_cache_directory = |
| 198 task_runner->PostTask(FROM_HERE, | 172 GetConsumerCacheDirectory(entry_directory); |
| 199 base::Bind(&SaveEntry, base::Passed(entry.Pass()), | |
| 200 base::Passed(dir.Pass()))); | |
| 201 | 173 |
| 202 callback.Run(content_path, cache_dir); | 174 CacheEntryPtr entry = CacheEntry::New(); |
| 175 entry->response = response.Pass(); |
| 176 entry->entry_directory = entry_directory.value(); |
| 177 entry->response_body_path = response_body_path.value(); |
| 178 |
| 179 db->PutNew(request_origin, url, entry.Pass()); |
| 180 |
| 181 if (!base::CreateDirectoryAndGetError(entry_directory, nullptr) || |
| 182 !base::CreateDirectoryAndGetError(consumer_cache_directory, nullptr) || |
| 183 !base::ReplaceFile(staged_response_body_path, response_body_path, |
| 184 nullptr)) { |
| 185 MovePathIntoDir(entry_directory, GetStagingDirectory()); |
| 186 callback.Run(base::FilePath(), base::FilePath()); |
| 187 return; |
| 188 } |
| 189 |
| 190 callback.Run(response_body_path, consumer_cache_directory); |
| 203 } | 191 } |
| 204 | 192 |
| 205 // Run the given mojo callback with the given paths. | 193 // Run the given mojo callback with the given paths. |
| 206 void RunMojoCallback( | 194 void RunMojoCallback( |
| 207 const Callback<void(Array<uint8_t>, Array<uint8_t>)>& callback, | 195 const Callback<void(Array<uint8_t>, Array<uint8_t>)>& callback, |
| 208 const base::FilePath& path1, | 196 const base::FilePath& path1, |
| 209 const base::FilePath& path2) { | 197 const base::FilePath& path2) { |
| 210 callback.Run(PathToArray(path1), PathToArray(path2)); | 198 callback.Run(PathToArray(path1), PathToArray(path2)); |
| 211 } | 199 } |
| 212 | 200 |
| 213 // Returns the list of values for the given |header_name| in the given list of | 201 // Returns the list of values for the given |header_name| in the given list of |
| 214 // headers. | 202 // headers. |
| 215 template <typename HeaderType> | |
| 216 std::vector<std::string> GetHeaderValues(const std::string& header_name, | 203 std::vector<std::string> GetHeaderValues(const std::string& header_name, |
| 217 const Array<HeaderType>& headers) { | 204 const Array<HttpHeaderPtr>& headers) { |
| 218 std::vector<std::string> result; | 205 std::vector<std::string> result; |
| 219 for (size_t i = 0u; i < headers.size(); ++i) { | 206 for (size_t i = 0u; i < headers.size(); ++i) { |
| 220 std::string name = headers[i]->name; | 207 const std::string& name = headers[i]->name.get(); |
| 221 if (base::LowerCaseEqualsASCII(name, header_name.c_str())) | 208 if (base::LowerCaseEqualsASCII(name, header_name.c_str())) |
| 222 result.push_back(headers[i]->value); | 209 result.push_back(headers[i]->value); |
| 223 } | 210 } |
| 224 return result; | 211 return result; |
| 225 } | 212 } |
| 226 | 213 |
| 227 // Returns whether the given directory |dir| constains a valid entry file for | 214 // Returns whether the given |entry| is valid. |
| 228 // the given |response|. If this is the case and |output| is not |nullptr|, then | 215 bool IsCacheEntryValid(const CacheEntryPtr& entry) { |
| 229 // the deserialized entry is returned in |*output|. | 216 return entry && PathExists(base::FilePath(entry->response_body_path)); |
| 230 bool IsCacheEntryValid(const base::FilePath& dir, | 217 } |
| 231 URLResponse* response, | |
| 232 CacheEntryPtr* output) { | |
| 233 // Find the entry file, and deserialize it. | |
| 234 base::FilePath entry_path = dir.Append(kEntryName); | |
| 235 if (!base::PathExists(entry_path)) | |
| 236 return false; | |
| 237 std::string serialized_entry; | |
| 238 if (!ReadFileToString(entry_path, &serialized_entry)) | |
| 239 return false; | |
| 240 CacheEntryPtr entry; | |
| 241 if (!Deserialize(serialized_entry, &entry)) | |
| 242 return false; | |
| 243 | 218 |
| 244 // Obsolete entries are invalidated. | 219 // Returns whether the given directory |entry| is valid and its content can be |
| 245 if (entry->version != kCurrentVersion) | 220 // used for the given |response|. |
| 221 bool IsCacheEntryFresh(const URLResponsePtr& response, |
| 222 const CacheEntryPtr& entry) { |
| 223 DCHECK(response); |
| 224 if (!IsCacheEntryValid(entry)) |
| 246 return false; | 225 return false; |
| 247 | 226 |
| 248 // If |entry| or |response| has not headers, it is not possible to check if | 227 // If |entry| or |response| has not headers, it is not possible to check if |
| 249 // the entry is valid, so returns |false|. | 228 // the entry is valid, so returns |false|. |
| 250 if (entry->headers.is_null() || response->headers.is_null()) | 229 if (entry->response->headers.is_null() || response->headers.is_null()) |
| 251 return false; | 230 return false; |
| 252 | 231 |
| 253 // Only handle etag for the moment. | 232 // Only handle etag for the moment. |
| 254 std::string etag_header_name = kEtagHeader; | 233 std::string etag_header_name = kEtagHeader; |
| 255 std::vector<std::string> entry_etags = | 234 std::vector<std::string> entry_etags = |
| 256 GetHeaderValues(etag_header_name, entry->headers); | 235 GetHeaderValues(etag_header_name, entry->response->headers); |
| 257 if (entry_etags.size() == 0) | 236 if (entry_etags.size() == 0) |
| 258 return false; | 237 return false; |
| 259 std::vector<std::string> response_etags = | 238 std::vector<std::string> response_etags = |
| 260 GetHeaderValues(etag_header_name, response->headers); | 239 GetHeaderValues(etag_header_name, response->headers); |
| 261 if (response_etags.size() == 0) | 240 if (response_etags.size() == 0) |
| 262 return false; | 241 return false; |
| 263 | 242 |
| 264 // Looking for the first etag header. | 243 // Looking for the first etag header. |
| 265 bool result = entry_etags[0] == response_etags[0]; | 244 return entry_etags[0] == response_etags[0]; |
| 266 | 245 } |
| 267 // Returns |entry| if requested. | 246 |
| 268 if (output) | 247 void PruneCache(scoped_refptr<URLResponseDiskCacheDB> db, |
| 269 *output = entry.Pass(); | 248 const scoped_ptr<URLResponseDiskCacheDB::Iterator>& iterator) { |
| 270 | 249 CacheKeyPtr last_key; |
| 271 return result; | 250 CacheKeyPtr key; |
| 251 CacheEntryPtr entry; |
| 252 while (iterator->HasNext()) { |
| 253 iterator->GetNext(&key, &entry); |
| 254 if (last_key && last_key->request_origin == key->request_origin && |
| 255 last_key->url == key->url) { |
| 256 base::FilePath entry_directory = base::FilePath(entry->entry_directory); |
| 257 if (base::DeleteFile(entry_directory, true)) |
| 258 db->Delete(key.Clone()); |
| 259 } |
| 260 last_key = key.Pass(); |
| 261 } |
| 272 } | 262 } |
| 273 | 263 |
| 274 } // namespace | 264 } // namespace |
| 275 | 265 |
| 276 // static | 266 // static |
| 277 void URLResponseDiskCacheImpl::ClearCache(base::TaskRunner* task_runner) { | 267 scoped_refptr<URLResponseDiskCacheDB> URLResponseDiskCacheImpl::CreateDB( |
| 278 // Create a unique subdirectory in trash. | 268 scoped_refptr<base::TaskRunner> task_runner, |
| 269 bool force_clean) { |
| 270 // Create the trash directory if needed. |
| 279 base::FilePath trash_dir = GetTrashDirectory(); | 271 base::FilePath trash_dir = GetTrashDirectory(); |
| 280 base::CreateDirectory(trash_dir); | 272 base::CreateDirectory(trash_dir); |
| 281 base::FilePath dest_dir; | 273 |
| 282 base::CreateTemporaryDirInDir(trash_dir, "", &dest_dir); | 274 // Clean the trash directory when exiting this method. |
| 283 | 275 base::ScopedClosureRunner trash_cleanup( |
| 284 // Move the current cache directory, if present, into trash. | 276 base::Bind(&ClearTrashDir, task_runner, trash_dir)); |
| 285 base::FilePath cache_dir = GetCacheDirectory(); | 277 |
| 286 if (PathExists(cache_dir)) { | 278 // Move the staging directory to trash. |
| 287 base::File::Error error; | 279 MovePathIntoDir(GetStagingDirectory(), trash_dir); |
| 288 if (!base::ReplaceFile(cache_dir, dest_dir, &error)) { | 280 // And recreate it. |
| 289 LOG(ERROR) << "Failed to clear cache content: " << error; | 281 base::CreateDirectory(GetStagingDirectory()); |
| 282 |
| 283 base::FilePath db_path = GetBaseDirectory().Append("db"); |
| 284 |
| 285 if (!force_clean && PathExists(db_path)) { |
| 286 scoped_refptr<URLResponseDiskCacheDB> db = |
| 287 new URLResponseDiskCacheDB(db_path); |
| 288 if (db->GetVersion() == kCurrentVersion) { |
| 289 task_runner->PostDelayedTask( |
| 290 FROM_HERE, |
| 291 base::Bind(&PruneCache, db, base::Passed(db->GetIterator())), |
| 292 base::TimeDelta::FromSeconds(kTrashDelayInSeconds)); |
| 293 return db; |
| 290 } | 294 } |
| 291 } | 295 } |
| 292 | 296 |
| 293 // Delete the trash directory. | 297 // Move the database to trash. |
| 294 task_runner->PostTask( | 298 MovePathIntoDir(db_path, trash_dir); |
| 295 FROM_HERE, | 299 // Move the current cache content to trash. |
| 296 base::Bind(base::IgnoreResult(&base::DeleteFile), trash_dir, true)); | 300 MovePathIntoDir(GetCacheDirectory(), trash_dir); |
| 301 |
| 302 scoped_refptr<URLResponseDiskCacheDB> result = |
| 303 new URLResponseDiskCacheDB(db_path); |
| 304 result->SetVersion(kCurrentVersion); |
| 305 return result; |
| 297 } | 306 } |
| 298 | 307 |
| 299 URLResponseDiskCacheImpl::URLResponseDiskCacheImpl( | 308 URLResponseDiskCacheImpl::URLResponseDiskCacheImpl( |
| 300 base::TaskRunner* task_runner, | 309 scoped_refptr<base::TaskRunner> task_runner, |
| 310 scoped_refptr<URLResponseDiskCacheDB> db, |
| 301 const std::string& remote_application_url, | 311 const std::string& remote_application_url, |
| 302 InterfaceRequest<URLResponseDiskCache> request) | 312 InterfaceRequest<URLResponseDiskCache> request) |
| 303 : task_runner_(task_runner), binding_(this, request.Pass()) { | 313 : task_runner_(task_runner), db_(db), binding_(this, request.Pass()) { |
| 304 base_directory_ = GetCacheDirectory(); | 314 request_origin_ = GURL(remote_application_url).GetOrigin().spec(); |
| 305 // The cached files are shared only for application of the same origin. | |
| 306 if (remote_application_url != "") { | |
| 307 base_directory_ = base_directory_.Append( | |
| 308 EncodeString(GURL(remote_application_url).GetOrigin().spec())); | |
| 309 } | |
| 310 } | 315 } |
| 311 | 316 |
| 312 URLResponseDiskCacheImpl::~URLResponseDiskCacheImpl() { | 317 URLResponseDiskCacheImpl::~URLResponseDiskCacheImpl() { |
| 313 } | 318 } |
| 314 | 319 |
| 315 void URLResponseDiskCacheImpl::GetFile(URLResponsePtr response, | 320 void URLResponseDiskCacheImpl::Get(const String& url, |
| 316 const GetFileCallback& callback) { | 321 const GetCallback& callback) { |
| 317 return GetFileInternal(response.Pass(), | 322 CacheEntryPtr entry = db_->GetNewest(request_origin_, CanonicalizeURL(url)); |
| 318 base::Bind(&RunMojoCallback, callback)); | 323 if (!IsCacheEntryValid(entry)) { |
| 319 } | 324 callback.Run(URLResponsePtr(), Array<uint8_t>(), Array<uint8_t>()); |
| 320 | 325 return; |
| 321 void URLResponseDiskCacheImpl::GetExtractedContent( | 326 } |
| 327 callback.Run(entry->response.Pass(), |
| 328 PathToArray(base::FilePath(entry->response_body_path)), |
| 329 PathToArray(GetConsumerCacheDirectory( |
| 330 base::FilePath(entry->entry_directory)))); |
| 331 } |
| 332 |
| 333 void URLResponseDiskCacheImpl::Update(URLResponsePtr response) { |
| 334 UpdateAndGetInternal(response.Pass(), base::Bind(&DoNothing)); |
| 335 } |
| 336 |
| 337 void URLResponseDiskCacheImpl::UpdateAndGet( |
| 322 URLResponsePtr response, | 338 URLResponsePtr response, |
| 323 const GetExtractedContentCallback& callback) { | 339 const UpdateAndGetCallback& callback) { |
| 324 base::FilePath dir = GetDirName(base_directory_, response->url); | 340 UpdateAndGetInternal(response.Pass(), base::Bind(&RunMojoCallback, callback)); |
| 325 base::FilePath extracted_dir = dir.Append("extracted"); | 341 } |
| 326 if (IsCacheEntryValid(dir, response.get(), nullptr) && | 342 |
| 327 PathExists(GetExtractedSentinel(dir))) { | 343 void URLResponseDiskCacheImpl::UpdateAndGetExtracted( |
| 328 callback.Run(PathToArray(extracted_dir), | |
| 329 PathToArray(GetConsumerCacheDirectory(dir))); | |
| 330 return; | |
| 331 } | |
| 332 | |
| 333 GetFileInternal( | |
| 334 response.Pass(), | |
| 335 base::Bind(&URLResponseDiskCacheImpl::GetExtractedContentInternal, | |
| 336 base::Unretained(this), base::Bind(&RunMojoCallback, callback), | |
| 337 dir, extracted_dir)); | |
| 338 } | |
| 339 | |
| 340 void URLResponseDiskCacheImpl::GetFileInternal( | |
| 341 URLResponsePtr response, | 344 URLResponsePtr response, |
| 342 const FilePathPairCallback& callback) { | 345 const UpdateAndGetExtractedCallback& callback) { |
| 343 base::FilePath dir = GetDirName(base_directory_, response->url); | 346 if (response->error || |
| 347 (response->status_code >= 400 && response->status_code < 600)) { |
| 348 callback.Run(Array<uint8_t>(), Array<uint8_t>()); |
| 349 return; |
| 350 } |
| 351 |
| 352 std::string url = CanonicalizeURL(response->url); |
| 344 | 353 |
| 345 // Check if the response is cached and valid. If that's the case, returns the | 354 // Check if the response is cached and valid. If that's the case, returns the |
| 346 // cached value. | 355 // cached value. |
| 347 CacheEntryPtr entry; | 356 CacheEntryPtr entry = db_->GetNewest(request_origin_, url); |
| 348 if (IsCacheEntryValid(dir, response.get(), &entry)) { | 357 |
| 349 callback.Run(base::FilePath(entry->content_path), | 358 if (!IsCacheEntryFresh(response, entry)) { |
| 350 GetConsumerCacheDirectory(dir)); | 359 UpdateAndGetInternal( |
| 351 return; | 360 response.Pass(), |
| 352 } | 361 base::Bind(&URLResponseDiskCacheImpl::UpdateAndGetExtractedInternal, |
| 353 | 362 base::Unretained(this), |
| 354 // As the response was either not cached or the cached value is not valid, if | 363 base::Bind(&RunMojoCallback, callback))); |
| 355 // the cache directory for the response exists, it needs to be cleaned. | 364 return; |
| 356 if (base::PathExists(dir)) { | 365 } |
| 357 base::FilePath to_delete; | 366 |
| 358 CHECK(CreateTemporaryDirInDir(base_directory_, "to_delete", &to_delete)); | 367 base::FilePath entry_directory = base::FilePath(entry->entry_directory); |
| 359 CHECK(Move(dir, to_delete)); | 368 base::FilePath extraction_directory = GetExtractionDirectory(entry_directory); |
| 360 task_runner_->PostTask( | 369 if (!PathExists(GetExtractionSentinel(entry_directory))) { |
| 361 FROM_HERE, | 370 UpdateAndGetExtractedInternal(base::Bind(&RunMojoCallback, callback), |
| 362 base::Bind(base::IgnoreResult(&base::DeleteFile), to_delete, true)); | 371 base::FilePath(entry->response_body_path), |
| 363 } | 372 GetConsumerCacheDirectory(entry_directory)); |
| 364 | 373 return; |
| 365 // If the response has not a valid body, and it is not possible to create | 374 } |
| 366 // either the cache directory or the consumer cache directory, returns an | 375 |
| 367 // error. | 376 callback.Run(PathToArray(extraction_directory), |
| 368 if (!response->body.is_valid() || | 377 PathToArray(GetConsumerCacheDirectory(entry_directory))); |
| 369 !base::CreateDirectoryAndGetError(dir, nullptr) || | 378 } |
| 370 !base::CreateDirectoryAndGetError(GetConsumerCacheDirectory(dir), | 379 |
| 371 nullptr)) { | 380 void URLResponseDiskCacheImpl::UpdateAndGetInternal( |
| 372 callback.Run(base::FilePath(), base::FilePath()); | 381 URLResponsePtr response, |
| 373 return; | 382 const ResponseFileAndCacheDirCallback& callback) { |
| 374 } | 383 if (response->error || |
| 375 | 384 (response->status_code >= 400 && response->status_code < 600)) { |
| 376 // Fill the entry values for the request. | 385 callback.Run(base::FilePath(), base::FilePath()); |
| 377 base::FilePath entry_path = dir.Append(kEntryName); | 386 return; |
| 378 base::FilePath content; | 387 } |
| 379 CHECK(CreateTemporaryFileInDir(dir, &content)); | 388 |
| 380 entry = CacheEntry::New(); | 389 std::string url = CanonicalizeURL(response->url); |
| 381 entry->version = kCurrentVersion; | 390 |
| 382 entry->url = response->url; | 391 // Check if the response is cached and valid. If that's the case, returns |
| 383 entry->content_path = content.value(); | 392 // the cached value. |
| 384 for (size_t i = 0u; i < response->headers.size(); ++i) { | 393 CacheEntryPtr entry = db_->GetNewest(request_origin_, url); |
| 385 auto cache_header = CacheHeaders::New(); | 394 if (IsCacheEntryFresh(response, entry)) { |
| 386 cache_header->name = response->headers[i]->name; | 395 callback.Run( |
| 387 cache_header->value = response->headers[i]->value; | 396 base::FilePath(entry->response_body_path), |
| 388 entry->headers.push_back(cache_header.Pass()); | 397 GetConsumerCacheDirectory(base::FilePath(entry->entry_directory))); |
| 389 } | 398 return; |
| 390 // Asynchronously copy the response body to the cached file. The entry is send | 399 } |
| 391 // to the callback so that it is saved on disk only if the copy of the body | 400 |
| 392 // succeded. | 401 if (!response->body.is_valid()) { |
| 402 callback.Run(base::FilePath(), base::FilePath()); |
| 403 return; |
| 404 } |
| 405 |
| 406 std::string identifier = GetNewIdentifier(); |
| 407 // The content is copied to the staging directory so that files are not leaked |
| 408 // if the shell terminates before an entry is saved to the database. |
| 409 base::FilePath staged_response_body_path = |
| 410 GetStagingDirectory().Append(identifier); |
| 411 |
| 412 ScopedDataPipeConsumerHandle body = response->body.Pass(); |
| 413 |
| 414 // Asynchronously copy the response body to the staging directory. The |
| 415 // callback will move it to the cache directory and save an entry in the |
| 416 // database only if the copy of the body succeded. |
| 393 common::CopyToFile( | 417 common::CopyToFile( |
| 394 response->body.Pass(), content, task_runner_, | 418 body.Pass(), staged_response_body_path, task_runner_.get(), |
| 395 base::Bind(&RunCallbackWithSuccess, callback, content, | 419 base::Bind(&RunCallbackWithSuccess, callback, identifier, request_origin_, |
| 396 GetConsumerCacheDirectory(dir), entry_path, | 420 url, base::Passed(response.Pass()), db_, task_runner_)); |
| 397 base::Passed(entry.Pass()), base::Unretained(task_runner_))); | 421 } |
| 398 } | 422 |
| 399 | 423 void URLResponseDiskCacheImpl::UpdateAndGetExtractedInternal( |
| 400 void URLResponseDiskCacheImpl::GetExtractedContentInternal( | 424 const ResponseFileAndCacheDirCallback& callback, |
| 401 const FilePathPairCallback& callback, | 425 const base::FilePath& response_body_path, |
| 402 const base::FilePath& base_dir, | 426 const base::FilePath& consumer_cache_directory) { |
| 403 const base::FilePath& extracted_dir, | 427 TRACE_EVENT1("url_response_disk_cache", "UpdateAndGetExtractedInternal", |
| 404 const base::FilePath& content, | 428 "response_body_path", response_body_path.value()); |
| 405 const base::FilePath& cache_dir) { | |
| 406 TRACE_EVENT1("url_response_disk_cache", "GetExtractedContentInternal", | |
| 407 "extracted_dir", extracted_dir.value()); | |
| 408 // If it is not possible to get the cached file, returns an error. | 429 // If it is not possible to get the cached file, returns an error. |
| 409 if (content.empty()) { | 430 if (response_body_path.empty()) { |
| 410 callback.Run(base::FilePath(), base::FilePath()); | 431 callback.Run(base::FilePath(), base::FilePath()); |
| 411 return; | 432 return; |
| 433 } |
| 434 |
| 435 base::FilePath entry_directory = consumer_cache_directory.DirName(); |
| 436 base::FilePath extraction_directory = GetExtractionDirectory(entry_directory); |
| 437 base::FilePath extraction_sentinel = GetExtractionSentinel(entry_directory); |
| 438 |
| 439 if (PathExists(extraction_sentinel)) { |
| 440 callback.Run(extraction_directory, consumer_cache_directory); |
| 441 return; |
| 442 } |
| 443 |
| 444 if (PathExists(extraction_directory)) { |
| 445 if (!base::DeleteFile(extraction_directory, true)) { |
| 446 callback.Run(base::FilePath(), base::FilePath()); |
| 447 return; |
| 448 } |
| 412 } | 449 } |
| 413 | 450 |
| 414 // Unzip the content to the extracted directory. In case of any error, returns | 451 // Unzip the content to the extracted directory. In case of any error, returns |
| 415 // an error. | 452 // an error. |
| 416 zip::ZipReader reader; | 453 zip::ZipReader reader; |
| 417 if (!reader.Open(content)) { | 454 if (!reader.Open(response_body_path)) { |
| 418 callback.Run(base::FilePath(), base::FilePath()); | 455 callback.Run(base::FilePath(), base::FilePath()); |
| 419 return; | 456 return; |
| 420 } | 457 } |
| 421 while (reader.HasMore()) { | 458 while (reader.HasMore()) { |
| 422 bool success = reader.OpenCurrentEntryInZip(); | 459 bool success = reader.OpenCurrentEntryInZip(); |
| 423 success = success && reader.ExtractCurrentEntryIntoDirectory(extracted_dir); | 460 success = success && |
| 461 reader.ExtractCurrentEntryIntoDirectory(extraction_directory); |
| 424 success = success && reader.AdvanceToNextEntry(); | 462 success = success && reader.AdvanceToNextEntry(); |
| 425 if (!success) { | 463 if (!success) { |
| 426 callback.Run(base::FilePath(), base::FilePath()); | 464 callback.Run(base::FilePath(), base::FilePath()); |
| 427 return; | 465 return; |
| 428 } | 466 } |
| 429 } | 467 } |
| 430 // We can ignore write error, as it will just force to clear the cache on the | 468 // We can ignore write error, as it will just force to clear the cache on the |
| 431 // next request. | 469 // next request. |
| 432 WriteFile(GetExtractedSentinel(base_dir), nullptr, 0); | 470 WriteFile(GetExtractionSentinel(entry_directory), nullptr, 0); |
| 433 callback.Run(extracted_dir, cache_dir); | 471 callback.Run(extraction_directory, consumer_cache_directory); |
| 434 } | 472 } |
| 435 | 473 |
| 436 } // namespace mojo | 474 } // namespace mojo |
| OLD | NEW |