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 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 } | 196 } |
197 | 197 |
198 // Run the given mojo callback with the given paths. | 198 // Run the given mojo callback with the given paths. |
199 void RunMojoCallback( | 199 void RunMojoCallback( |
200 const Callback<void(Array<uint8_t>, Array<uint8_t>)>& callback, | 200 const Callback<void(Array<uint8_t>, Array<uint8_t>)>& callback, |
201 const base::FilePath& path1, | 201 const base::FilePath& path1, |
202 const base::FilePath& path2) { | 202 const base::FilePath& path2) { |
203 callback.Run(PathToArray(path1), PathToArray(path2)); | 203 callback.Run(PathToArray(path1), PathToArray(path2)); |
204 } | 204 } |
205 | 205 |
| 206 mojo::URLResponsePtr GetMinimalResponse(const std::string& url) { |
| 207 mojo::URLResponsePtr response = mojo::URLResponse::New(); |
| 208 response->url = url; |
| 209 response->status_code = 200; |
| 210 return response.Pass(); |
| 211 } |
| 212 |
| 213 void RunMojoCallbackWithResponse( |
| 214 const Callback<void(mojo::URLResponsePtr, Array<uint8_t>, Array<uint8_t>)>& |
| 215 callback, |
| 216 const std::string& url, |
| 217 const base::FilePath& path1, |
| 218 const base::FilePath& path2) { |
| 219 mojo::URLResponsePtr response; |
| 220 if (!path1.empty()) |
| 221 response = GetMinimalResponse(url); |
| 222 callback.Run(response.Pass(), PathToArray(path1), PathToArray(path2)); |
| 223 } |
| 224 |
206 // Returns the list of values for the given |header_name| in the given list of | 225 // Returns the list of values for the given |header_name| in the given list of |
207 // headers. | 226 // headers. |
208 std::vector<std::string> GetHeaderValues(const std::string& header_name, | 227 std::vector<std::string> GetHeaderValues(const std::string& header_name, |
209 const Array<HttpHeaderPtr>& headers) { | 228 const Array<HttpHeaderPtr>& headers) { |
210 std::vector<std::string> result; | 229 std::vector<std::string> result; |
211 for (size_t i = 0u; i < headers.size(); ++i) { | 230 for (size_t i = 0u; i < headers.size(); ++i) { |
212 const std::string& name = headers[i]->name.get(); | 231 const std::string& name = headers[i]->name.get(); |
213 if (base::LowerCaseEqualsASCII(name, header_name.c_str())) | 232 if (base::LowerCaseEqualsASCII(name, header_name.c_str())) |
214 result.push_back(headers[i]->value); | 233 result.push_back(headers[i]->value); |
215 } | 234 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 MovePathIntoDir(GetCacheDirectory(), trash_dir); | 332 MovePathIntoDir(GetCacheDirectory(), trash_dir); |
314 | 333 |
315 scoped_refptr<URLResponseDiskCacheDB> result = | 334 scoped_refptr<URLResponseDiskCacheDB> result = |
316 new URLResponseDiskCacheDB(db_path); | 335 new URLResponseDiskCacheDB(db_path); |
317 result->SetVersion(kCurrentVersion); | 336 result->SetVersion(kCurrentVersion); |
318 return result; | 337 return result; |
319 } | 338 } |
320 | 339 |
321 URLResponseDiskCacheImpl::URLResponseDiskCacheImpl( | 340 URLResponseDiskCacheImpl::URLResponseDiskCacheImpl( |
322 scoped_refptr<base::TaskRunner> task_runner, | 341 scoped_refptr<base::TaskRunner> task_runner, |
| 342 URLResponseDiskCacheDelegate* delegate, |
323 scoped_refptr<URLResponseDiskCacheDB> db, | 343 scoped_refptr<URLResponseDiskCacheDB> db, |
324 const std::string& remote_application_url, | 344 const std::string& remote_application_url, |
325 InterfaceRequest<URLResponseDiskCache> request) | 345 InterfaceRequest<URLResponseDiskCache> request) |
326 : task_runner_(task_runner), db_(db), binding_(this, request.Pass()) { | 346 : task_runner_(task_runner), |
| 347 delegate_(delegate), |
| 348 db_(db), |
| 349 binding_(this, request.Pass()) { |
327 request_origin_ = GURL(remote_application_url).GetOrigin().spec(); | 350 request_origin_ = GURL(remote_application_url).GetOrigin().spec(); |
328 } | 351 } |
329 | 352 |
330 URLResponseDiskCacheImpl::~URLResponseDiskCacheImpl() { | 353 URLResponseDiskCacheImpl::~URLResponseDiskCacheImpl() { |
331 } | 354 } |
332 | 355 |
333 bool IsInvalidated(const CacheEntryPtr& entry) { | 356 bool IsInvalidated(const CacheEntryPtr& entry) { |
334 if (!entry) | 357 if (!entry) |
335 return true; | 358 return true; |
336 return base::Time::Now() - | 359 return base::Time::Now() - |
337 base::Time::FromInternalValue(entry->last_invalidation) > | 360 base::Time::FromInternalValue(entry->last_invalidation) > |
338 base::TimeDelta::FromSeconds(kTimeUntilInvalidationInSeconds); | 361 base::TimeDelta::FromSeconds(kTimeUntilInvalidationInSeconds); |
339 } | 362 } |
340 | 363 |
341 void URLResponseDiskCacheImpl::Get(const String& url, | 364 void URLResponseDiskCacheImpl::Get(const String& url, |
342 const GetCallback& callback) { | 365 const GetCallback& callback) { |
| 366 std::string canonilized_url = CanonicalizeURL(url); |
343 CacheKeyPtr key; | 367 CacheKeyPtr key; |
344 CacheEntryPtr entry = | 368 CacheEntryPtr entry = db_->GetNewest(request_origin_, canonilized_url, &key); |
345 db_->GetNewest(request_origin_, CanonicalizeURL(url), &key); | 369 if (!entry && delegate_) { |
| 370 // This is the first request for the given |url|. Ask |delegate| for initial |
| 371 // content. |
| 372 |
| 373 std::string identifier = GetNewIdentifier(); |
| 374 // The content is copied to the staging directory so that files are not |
| 375 // leaked if the shell terminates before an entry is saved to the database. |
| 376 base::FilePath staged_response_body_path = |
| 377 GetStagingDirectory().Append(identifier); |
| 378 |
| 379 delegate_->GetInitialPath( |
| 380 task_runner_, canonilized_url, staged_response_body_path, |
| 381 base::Bind( |
| 382 RunCallbackWithSuccess, |
| 383 base::Bind(&RunMojoCallbackWithResponse, callback, canonilized_url), |
| 384 identifier, request_origin_, canonilized_url, |
| 385 base::Passed(GetMinimalResponse(canonilized_url)), db_, |
| 386 task_runner_)); |
| 387 return; |
| 388 } |
346 if (IsInvalidated(entry) || !IsCacheEntryValid(entry)) { | 389 if (IsInvalidated(entry) || !IsCacheEntryValid(entry)) { |
347 callback.Run(URLResponsePtr(), Array<uint8_t>(), Array<uint8_t>()); | 390 callback.Run(URLResponsePtr(), Array<uint8_t>(), Array<uint8_t>()); |
348 return; | 391 return; |
349 } | 392 } |
350 callback.Run(entry->response.Pass(), | 393 callback.Run(entry->response.Pass(), |
351 PathToArray(base::FilePath(entry->response_body_path)), | 394 PathToArray(base::FilePath(entry->response_body_path)), |
352 PathToArray(GetConsumerCacheDirectory( | 395 PathToArray(GetConsumerCacheDirectory( |
353 base::FilePath(entry->entry_directory)))); | 396 base::FilePath(entry->entry_directory)))); |
354 UpdateLastInvalidation(db_, key.Pass(), base::Time::Now()); | 397 UpdateLastInvalidation(db_, key.Pass(), base::Time::Now()); |
355 } | 398 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 return; | 515 return; |
473 } | 516 } |
474 } | 517 } |
475 // We can ignore write error, as it will just force to clear the cache on the | 518 // We can ignore write error, as it will just force to clear the cache on the |
476 // next request. | 519 // next request. |
477 WriteFile(GetExtractionSentinel(entry_directory), nullptr, 0); | 520 WriteFile(GetExtractionSentinel(entry_directory), nullptr, 0); |
478 callback.Run(extraction_directory, consumer_cache_directory); | 521 callback.Run(extraction_directory, consumer_cache_directory); |
479 } | 522 } |
480 | 523 |
481 } // namespace mojo | 524 } // namespace mojo |
OLD | NEW |