| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/appcache/appcache_url_request_job.h" | 5 #include "content/browser/appcache/appcache_url_request_job.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "content/browser/appcache/appcache.h" | 18 #include "content/browser/appcache/appcache.h" |
| 19 #include "content/browser/appcache/appcache_group.h" | 19 #include "content/browser/appcache/appcache_group.h" |
| 20 #include "content/browser/appcache/appcache_histograms.h" | 20 #include "content/browser/appcache/appcache_histograms.h" |
| 21 #include "content/browser/appcache/appcache_host.h" | 21 #include "content/browser/appcache/appcache_host.h" |
| 22 #include "content/browser/appcache/appcache_service_impl.h" | 22 #include "content/browser/appcache/appcache_service_impl.h" |
| 23 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
| 24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 25 #include "net/http/http_request_headers.h" | 25 #include "net/http/http_request_headers.h" |
| 26 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
| 27 #include "net/http/http_util.h" | |
| 28 #include "net/log/net_log_event_type.h" | 27 #include "net/log/net_log_event_type.h" |
| 29 #include "net/log/net_log_with_source.h" | 28 #include "net/log/net_log_with_source.h" |
| 30 #include "net/url_request/url_request.h" | 29 #include "net/url_request/url_request.h" |
| 31 #include "net/url_request/url_request_status.h" | 30 #include "net/url_request/url_request_status.h" |
| 32 | 31 |
| 33 namespace content { | 32 namespace content { |
| 34 | 33 |
| 35 AppCacheURLRequestJob::~AppCacheURLRequestJob() { | 34 AppCacheURLRequestJob::~AppCacheURLRequestJob() { |
| 36 if (storage_) | 35 if (storage_) |
| 37 storage_->CancelDelegateCallbacks(this); | 36 storage_->CancelDelegateCallbacks(this); |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 void AppCacheURLRequestJob::OnResponseInfoLoaded( | 303 void AppCacheURLRequestJob::OnResponseInfoLoaded( |
| 305 AppCacheResponseInfo* response_info, | 304 AppCacheResponseInfo* response_info, |
| 306 int64_t response_id) { | 305 int64_t response_id) { |
| 307 DCHECK(IsDeliveringAppCacheResponse()); | 306 DCHECK(IsDeliveringAppCacheResponse()); |
| 308 if (response_info) { | 307 if (response_info) { |
| 309 info_ = response_info; | 308 info_ = response_info; |
| 310 reader_.reset( | 309 reader_.reset( |
| 311 storage_->CreateResponseReader(manifest_url_, entry_.response_id())); | 310 storage_->CreateResponseReader(manifest_url_, entry_.response_id())); |
| 312 | 311 |
| 313 if (is_range_request()) | 312 if (is_range_request()) |
| 314 SetupRangeResponse(); | 313 SetupRangeResponse(reader_.get()); |
| 315 | 314 |
| 316 NotifyHeadersComplete(); | 315 NotifyHeadersComplete(); |
| 317 } else { | 316 } else { |
| 318 if (storage_->service()->storage() == storage_) { | 317 if (storage_->service()->storage() == storage_) { |
| 319 // A resource that is expected to be in the appcache is missing. | 318 // A resource that is expected to be in the appcache is missing. |
| 320 // See http://code.google.com/p/chromium/issues/detail?id=50657 | 319 // See http://code.google.com/p/chromium/issues/detail?id=50657 |
| 321 // Instead of failing the request, we restart the request. The retry | 320 // Instead of failing the request, we restart the request. The retry |
| 322 // attempt will fallthru to the network instead of trying to load | 321 // attempt will fallthru to the network instead of trying to load |
| 323 // from the appcache. | 322 // from the appcache. |
| 324 storage_->service()->CheckAppCacheResponse(manifest_url_, cache_id_, | 323 storage_->service()->CheckAppCacheResponse(manifest_url_, cache_id_, |
| 325 entry_.response_id()); | 324 entry_.response_id()); |
| 326 AppCacheHistograms::CountResponseRetrieval( | 325 AppCacheHistograms::CountResponseRetrieval( |
| 327 false, is_main_resource_, manifest_url_.GetOrigin()); | 326 false, is_main_resource_, manifest_url_.GetOrigin()); |
| 328 } | 327 } |
| 329 cache_entry_not_found_ = true; | 328 cache_entry_not_found_ = true; |
| 330 NotifyRestartRequired(); | 329 NotifyRestartRequired(); |
| 331 } | 330 } |
| 332 } | 331 } |
| 333 | 332 |
| 334 const net::HttpResponseInfo* AppCacheURLRequestJob::http_info() const { | 333 const net::HttpResponseInfo* AppCacheURLRequestJob::http_info() const { |
| 335 if (!info_.get()) | 334 if (!info_.get()) |
| 336 return NULL; | 335 return NULL; |
| 337 if (range_response_info_) | 336 if (range_response_info_) |
| 338 return range_response_info_.get(); | 337 return range_response_info_.get(); |
| 339 return info_->http_response_info(); | 338 return info_->http_response_info(); |
| 340 } | 339 } |
| 341 | 340 |
| 342 void AppCacheURLRequestJob::SetupRangeResponse() { | |
| 343 DCHECK(is_range_request() && info_.get() && reader_.get() && | |
| 344 IsDeliveringAppCacheResponse()); | |
| 345 int resource_size = static_cast<int>(info_->response_data_size()); | |
| 346 if (resource_size < 0 || !range_requested_.ComputeBounds(resource_size)) { | |
| 347 range_requested_ = net::HttpByteRange(); | |
| 348 return; | |
| 349 } | |
| 350 | |
| 351 DCHECK(range_requested_.IsValid()); | |
| 352 int offset = static_cast<int>(range_requested_.first_byte_position()); | |
| 353 int length = static_cast<int>(range_requested_.last_byte_position() - | |
| 354 range_requested_.first_byte_position() + 1); | |
| 355 | |
| 356 // Tell the reader about the range to read. | |
| 357 reader_->SetReadRange(offset, length); | |
| 358 | |
| 359 // Make a copy of the full response headers and fix them up | |
| 360 // for the range we'll be returning. | |
| 361 range_response_info_.reset( | |
| 362 new net::HttpResponseInfo(*info_->http_response_info())); | |
| 363 net::HttpResponseHeaders* headers = range_response_info_->headers.get(); | |
| 364 headers->UpdateWithNewRange( | |
| 365 range_requested_, resource_size, true /* replace status line */); | |
| 366 } | |
| 367 | |
| 368 void AppCacheURLRequestJob::OnReadComplete(int result) { | 341 void AppCacheURLRequestJob::OnReadComplete(int result) { |
| 369 DCHECK(IsDeliveringAppCacheResponse()); | 342 DCHECK(IsDeliveringAppCacheResponse()); |
| 370 if (result == 0) { | 343 if (result == 0) { |
| 371 AppCacheHistograms::CountResponseRetrieval( | 344 AppCacheHistograms::CountResponseRetrieval( |
| 372 true, is_main_resource_, manifest_url_.GetOrigin()); | 345 true, is_main_resource_, manifest_url_.GetOrigin()); |
| 373 } else if (result < 0) { | 346 } else if (result < 0) { |
| 374 if (storage_->service()->storage() == storage_) { | 347 if (storage_->service()->storage() == storage_) { |
| 375 storage_->service()->CheckAppCacheResponse(manifest_url_, cache_id_, | 348 storage_->service()->CheckAppCacheResponse(manifest_url_, cache_id_, |
| 376 entry_.response_id()); | 349 entry_.response_id()); |
| 377 } | 350 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 } | 406 } |
| 434 | 407 |
| 435 net::HostPortPair AppCacheURLRequestJob::GetSocketAddress() const { | 408 net::HostPortPair AppCacheURLRequestJob::GetSocketAddress() const { |
| 436 if (!http_info()) | 409 if (!http_info()) |
| 437 return net::HostPortPair(); | 410 return net::HostPortPair(); |
| 438 return http_info()->socket_address; | 411 return http_info()->socket_address; |
| 439 } | 412 } |
| 440 | 413 |
| 441 void AppCacheURLRequestJob::SetExtraRequestHeaders( | 414 void AppCacheURLRequestJob::SetExtraRequestHeaders( |
| 442 const net::HttpRequestHeaders& headers) { | 415 const net::HttpRequestHeaders& headers) { |
| 443 std::string value; | 416 InitializeRangeRequestInfo(headers); |
| 444 std::vector<net::HttpByteRange> ranges; | |
| 445 if (!headers.GetHeader(net::HttpRequestHeaders::kRange, &value) || | |
| 446 !net::HttpUtil::ParseRangeHeader(value, &ranges)) { | |
| 447 return; | |
| 448 } | |
| 449 | |
| 450 // If multiple ranges are requested, we play dumb and | |
| 451 // return the entire response with 200 OK. | |
| 452 if (ranges.size() == 1U) | |
| 453 range_requested_ = ranges[0]; | |
| 454 } | 417 } |
| 455 | 418 |
| 456 void AppCacheURLRequestJob::NotifyRestartRequired() { | 419 void AppCacheURLRequestJob::NotifyRestartRequired() { |
| 457 on_prepare_to_restart_callback_.Run(); | 420 on_prepare_to_restart_callback_.Run(); |
| 458 URLRequestJob::NotifyRestartRequired(); | 421 URLRequestJob::NotifyRestartRequired(); |
| 459 } | 422 } |
| 460 | 423 |
| 461 } // namespace content | 424 } // namespace content |
| OLD | NEW |