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