Commit a96b15e1 authored by Min Qin's avatar Min Qin Committed by Commit Bot

Revert "Reland "Move downloadResponseHandler to components/download""

This reverts commit c128a63a.

Reason for revert: <INSERT REASONING HERE>

Original change's description:
> Reland "Move downloadResponseHandler to components/download"
> 
> This is a reland of 824bbddf
> 
> Original change's description:
> > Move downloadResponseHandler to components/download
> > 
> > No major change in functionality, just moving the files
> > 
> > Bug: 803135
> > Change-Id: I93737ed4539c1b3ddc25212814ef9b9c903ac882
> > Reviewed-on: https://chromium-review.googlesource.com/944545
> > Reviewed-by: Helen Li <xunjieli@chromium.org>
> > Reviewed-by: David Trainor <dtrainor@chromium.org>
> > Reviewed-by: Bo <boliu@chromium.org>
> > Commit-Queue: Min Qin <qinmin@chromium.org>
> > Cr-Commit-Position: refs/heads/master@{#540736}
> 
> Bug: 803135
> Change-Id: I270c0f6edc212ffe1a2a9d06ccfbc95f1da8f3e2
> Reviewed-on: https://chromium-review.googlesource.com/949702
> Reviewed-by: Min Qin <qinmin@chromium.org>
> Reviewed-by: Helen Li <xunjieli@chromium.org>
> Reviewed-by: Bo <boliu@chromium.org>
> Commit-Queue: Min Qin <qinmin@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#540961}

TBR=boliu@chromium.org,dtrainor@chromium.org,qinmin@chromium.org,xunjieli@chromium.org

Change-Id: Iea107687b6008c6132ee6533fdff56e1d3db0265
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 803135
Reviewed-on: https://chromium-review.googlesource.com/950022Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Commit-Queue: Min Qin <qinmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#540965}
parent 50fa8329
...@@ -16,7 +16,6 @@ source_set("internal") { ...@@ -16,7 +16,6 @@ source_set("internal") {
"download_create_info.cc", "download_create_info.cc",
"download_interrupt_reasons_impl.cc", "download_interrupt_reasons_impl.cc",
"download_interrupt_reasons_utils.cc", "download_interrupt_reasons_utils.cc",
"download_response_handler.cc",
"download_stats.cc", "download_stats.cc",
"download_task_runner.cc", "download_task_runner.cc",
"download_ukm_helper.cc", "download_ukm_helper.cc",
......
...@@ -7,7 +7,6 @@ include_rules = [ ...@@ -7,7 +7,6 @@ include_rules = [
"+net/base/filename_util.h", "+net/base/filename_util.h",
"+net/http/http_content_disposition.h", "+net/http/http_content_disposition.h",
"+net/http/http_response_headers.h", "+net/http/http_response_headers.h",
"+net/http/http_status_code.h",
"+net/http/http_util.h", "+net/http/http_util.h",
"+services/metrics/public/cpp", "+services/metrics/public/cpp",
] ]
...@@ -2,210 +2,10 @@ ...@@ -2,210 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "components/download/public/common/download_utils.h"
#include "base/format_macros.h"
#include "base/strings/stringprintf.h"
#include "components/download/public/common/download_create_info.h"
#include "components/download/public/common/download_interrupt_reasons_utils.h"
#include "components/download/public/common/download_item.h" #include "components/download/public/common/download_item.h"
#include "components/download/public/common/download_save_info.h"
#include "components/download/public/common/download_stats.h"
#include "net/http/http_status_code.h"
namespace download { namespace download {
const uint32_t DownloadItem::kInvalidId = 0; const uint32_t DownloadItem::kInvalidId = 0;
DownloadInterruptReason HandleRequestCompletionStatus(
net::Error error_code,
bool has_strong_validators,
net::CertStatus cert_status,
DownloadInterruptReason abort_reason) {
// ERR_CONTENT_LENGTH_MISMATCH can be caused by 1 of the following reasons:
// 1. Server or proxy closes the connection too early.
// 2. The content-length header is wrong.
// If the download has strong validators, we can interrupt the download
// and let it resume automatically. Otherwise, resuming the download will
// cause it to restart and the download may never complete if the error was
// caused by reason 2. As a result, downloads without strong validators are
// treated as completed here.
// TODO(qinmin): check the metrics from downloads with strong validators,
// and decide whether we should interrupt downloads without strong validators
// rather than complete them.
if (error_code == net::ERR_CONTENT_LENGTH_MISMATCH &&
!has_strong_validators) {
error_code = net::OK;
RecordDownloadCount(COMPLETED_WITH_CONTENT_LENGTH_MISMATCH_COUNT);
}
if (error_code == net::ERR_ABORTED) {
// ERR_ABORTED == something outside of the network
// stack cancelled the request. There aren't that many things that
// could do this to a download request (whose lifetime is separated from
// the tab from which it came). We map this to USER_CANCELLED as the
// case we know about (system suspend because of laptop close) corresponds
// to a user action.
// TODO(asanka): A lid close or other power event should result in an
// interruption that doesn't discard the partial state, unlike
// USER_CANCELLED. (https://crbug.com/166179)
if (net::IsCertStatusError(cert_status))
return DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM;
else
return DOWNLOAD_INTERRUPT_REASON_USER_CANCELED;
} else if (abort_reason != DOWNLOAD_INTERRUPT_REASON_NONE) {
// If a more specific interrupt reason was specified before the request
// was explicitly cancelled, then use it.
return abort_reason;
}
return ConvertNetErrorToInterruptReason(error_code,
DOWNLOAD_INTERRUPT_FROM_NETWORK);
}
DownloadInterruptReason HandleSuccessfulServerResponse(
const net::HttpResponseHeaders& http_headers,
DownloadSaveInfo* save_info,
bool fetch_error_body) {
DownloadInterruptReason result = DOWNLOAD_INTERRUPT_REASON_NONE;
switch (http_headers.response_code()) {
case -1: // Non-HTTP request.
case net::HTTP_OK:
case net::HTTP_NON_AUTHORITATIVE_INFORMATION:
case net::HTTP_PARTIAL_CONTENT:
// Expected successful codes.
break;
case net::HTTP_CREATED:
case net::HTTP_ACCEPTED:
// Per RFC 7231 the entity being transferred is metadata about the
// resource at the target URL and not the resource at that URL (or the
// resource that would be at the URL once processing is completed in the
// case of HTTP_ACCEPTED). However, we currently don't have special
// handling for these response and they are downloaded the same as a
// regular response.
break;
case net::HTTP_NO_CONTENT:
case net::HTTP_RESET_CONTENT:
// These two status codes don't have an entity (or rather RFC 7231
// requires that there be no entity). They are treated the same as the
// resource not being found since there is no entity to download.
case net::HTTP_NOT_FOUND:
result = DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
break;
case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE:
// Retry by downloading from the start automatically:
// If we haven't received data when we get this error, we won't.
result = DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE;
break;
case net::HTTP_UNAUTHORIZED:
case net::HTTP_PROXY_AUTHENTICATION_REQUIRED:
// Server didn't authorize this request.
result = DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED;
break;
case net::HTTP_FORBIDDEN:
// Server forbids access to this resource.
result = DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN;
break;
default: // All other errors.
// Redirection and informational codes should have been handled earlier
// in the stack.
// TODO(xingliu): Handle HTTP_PRECONDITION_FAILED and resurrect
// DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION for range
// requests. This will change extensions::api::InterruptReason.
DCHECK_NE(3, http_headers.response_code() / 100);
DCHECK_NE(1, http_headers.response_code() / 100);
result = DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED;
}
if (result != DOWNLOAD_INTERRUPT_REASON_NONE && !fetch_error_body)
return result;
// The caller is expecting a partial response.
if (save_info && (save_info->offset > 0 || save_info->length > 0)) {
if (http_headers.response_code() != net::HTTP_PARTIAL_CONTENT) {
// Server should send partial content when "If-Match" or
// "If-Unmodified-Since" check passes, and the range request header has
// last byte position. e.g. "Range:bytes=50-99".
if (save_info->length != DownloadSaveInfo::kLengthFullContent &&
!fetch_error_body)
return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
// Requested a partial range, but received the entire response, when
// the range request header is "Range:bytes={offset}-".
// The response can be HTTP 200 or other error code when
// |fetch_error_body| is true.
save_info->offset = 0;
save_info->hash_of_partial_file.clear();
save_info->hash_state.reset();
return DOWNLOAD_INTERRUPT_REASON_NONE;
}
int64_t first_byte = -1;
int64_t last_byte = -1;
int64_t length = -1;
if (!http_headers.GetContentRangeFor206(&first_byte, &last_byte, &length))
return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
DCHECK_GE(first_byte, 0);
if (first_byte != save_info->offset ||
(save_info->length > 0 &&
last_byte != save_info->offset + save_info->length - 1)) {
// The server returned a different range than the one we requested. Assume
// the response is bad.
//
// In the future we should consider allowing offsets that are less than
// the offset we've requested, since in theory we can truncate the partial
// file at the offset and continue.
return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
}
return DOWNLOAD_INTERRUPT_REASON_NONE;
}
if (http_headers.response_code() == net::HTTP_PARTIAL_CONTENT)
return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
return DOWNLOAD_INTERRUPT_REASON_NONE;
}
void HandleResponseHeaders(const net::HttpResponseHeaders* headers,
DownloadCreateInfo* create_info) {
if (!headers)
return;
if (headers->HasStrongValidators()) {
// If we don't have strong validators as per RFC 7232 section 2, then
// we neither store nor use them for range requests.
if (!headers->EnumerateHeader(nullptr, "Last-Modified",
&create_info->last_modified))
create_info->last_modified.clear();
if (!headers->EnumerateHeader(nullptr, "ETag", &create_info->etag))
create_info->etag.clear();
}
// Grab the first content-disposition header. There may be more than one,
// though as of this writing, the network stack ensures if there are, they
// are all duplicates.
headers->EnumerateHeader(nullptr, "Content-Disposition",
&create_info->content_disposition);
// Parse the original mime type from the header, notice that actual mime type
// might be different due to mime type sniffing.
if (!headers->GetMimeType(&create_info->original_mime_type))
create_info->original_mime_type.clear();
// Content-Range is validated in HandleSuccessfulServerResponse.
// In RFC 7233, a single part 206 partial response must generate
// Content-Range. Accept-Range may be sent in 200 response to indicate the
// server can handle range request, but optional in 206 response.
create_info->accept_range =
headers->HasHeaderValue("Accept-Ranges", "bytes") ||
(headers->HasHeader("Content-Range") &&
headers->response_code() == net::HTTP_PARTIAL_CONTENT);
}
} // namespace download } // namespace download
...@@ -24,7 +24,6 @@ component("public") { ...@@ -24,7 +24,6 @@ component("public") {
"download_interrupt_reasons_utils.h", "download_interrupt_reasons_utils.h",
"download_item.h", "download_item.h",
"download_request_handle_interface.h", "download_request_handle_interface.h",
"download_response_handler.h",
"download_save_info.cc", "download_save_info.cc",
"download_save_info.h", "download_save_info.h",
"download_source.h", "download_source.h",
...@@ -33,7 +32,6 @@ component("public") { ...@@ -33,7 +32,6 @@ component("public") {
"download_ukm_helper.h", "download_ukm_helper.h",
"download_url_parameters.cc", "download_url_parameters.cc",
"download_url_parameters.h", "download_url_parameters.h",
"download_utils.h",
"rate_estimator.h", "rate_estimator.h",
"resume_mode.h", "resume_mode.h",
] ]
...@@ -42,7 +40,6 @@ component("public") { ...@@ -42,7 +40,6 @@ component("public") {
public_deps = [ public_deps = [
":interfaces", ":interfaces",
"//services/network/public/mojom",
] ]
deps = [ deps = [
......
include_rules = [ include_rules = [
"+crypto", "+crypto",
"+net/base/net_errors.h", "+net/base/net_errors.h",
"+net/cert/cert_status_flags.h",
"+net/http/http_response_headers.h",
"+net/http/http_response_info.h", "+net/http/http_response_info.h",
"+net/url_request/url_request.h", "+net/url_request/url_request.h",
# TODO(qinmin): remove this once network service is enabled. # TODO(qinmin): remove this once network service is enabled.
...@@ -10,7 +8,6 @@ include_rules = [ ...@@ -10,7 +8,6 @@ include_rules = [
"+net/traffic_annotation/network_traffic_annotation.h", "+net/traffic_annotation/network_traffic_annotation.h",
"+services/metrics/public/cpp", "+services/metrics/public/cpp",
"+services/network/public/cpp", "+services/network/public/cpp",
"+services/network/public/mojom",
"+storage/browser", "+storage/browser",
"+ui/base", "+ui/base",
] ]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_UTILS_H_
#define COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_UTILS_H_
#include "components/download/public/common/download_export.h"
#include "components/download/public/common/download_interrupt_reasons.h"
#include "net/base/net_errors.h"
#include "net/cert/cert_status_flags.h"
#include "net/http/http_response_headers.h"
namespace download {
struct DownloadCreateInfo;
struct DownloadSaveInfo;
// Handle the url request completion status and return the interrupt reasons.
// |cert_status| is ignored if error_code is not net::ERR_ABORTED.
COMPONENTS_DOWNLOAD_EXPORT DownloadInterruptReason
HandleRequestCompletionStatus(net::Error error_code,
bool has_strong_validators,
net::CertStatus cert_status,
DownloadInterruptReason abort_reason);
// Parse the HTTP server response code.
// If |fetch_error_body| is true, most of HTTP response codes will be accepted
// as successful response.
COMPONENTS_DOWNLOAD_EXPORT DownloadInterruptReason
HandleSuccessfulServerResponse(const net::HttpResponseHeaders& http_headers,
DownloadSaveInfo* save_info,
bool fetch_error_body);
// Parse response headers and update |create_info| accordingly.
COMPONENTS_DOWNLOAD_EXPORT void HandleResponseHeaders(
const net::HttpResponseHeaders* headers,
DownloadCreateInfo* create_info);
} // namespace download
#endif // COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_UTILS_H_
...@@ -715,6 +715,8 @@ jumbo_source_set("browser") { ...@@ -715,6 +715,8 @@ jumbo_source_set("browser") {
"download/download_request_utils.cc", "download/download_request_utils.cc",
"download/download_resource_handler.cc", "download/download_resource_handler.cc",
"download/download_resource_handler.h", "download/download_resource_handler.h",
"download/download_response_handler.cc",
"download/download_response_handler.h",
"download/download_utils.cc", "download/download_utils.cc",
"download/download_utils.h", "download/download_utils.h",
"download/download_worker.cc", "download/download_worker.cc",
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "components/download/public/common/download_item.h" #include "components/download/public/common/download_item.h"
#include "components/download/public/common/download_stats.h" #include "components/download/public/common/download_stats.h"
#include "components/download/public/common/download_task_runner.h" #include "components/download/public/common/download_task_runner.h"
#include "components/download/public/common/download_utils.h"
#include "content/browser/byte_stream.h" #include "content/browser/byte_stream.h"
#include "content/browser/download/download_manager_impl.h" #include "content/browser/download/download_manager_impl.h"
#include "content/browser/download/download_request_handle.h" #include "content/browser/download/download_request_handle.h"
...@@ -247,10 +246,10 @@ bool DownloadRequestCore::OnResponseStarted( ...@@ -247,10 +246,10 @@ bool DownloadRequestCore::OnResponseStarted(
download_start_time_ = base::TimeTicks::Now(); download_start_time_ = base::TimeTicks::Now();
download::DownloadInterruptReason result = download::DownloadInterruptReason result =
request()->response_headers() ? download::HandleSuccessfulServerResponse( request()->response_headers()
*request()->response_headers(), ? HandleSuccessfulServerResponse(*request()->response_headers(),
save_info_.get(), fetch_error_body_) save_info_.get(), fetch_error_body_)
: download::DOWNLOAD_INTERRUPT_REASON_NONE; : download::DOWNLOAD_INTERRUPT_REASON_NONE;
if (request()->response_headers()) { if (request()->response_headers()) {
download::RecordDownloadHttpResponseCode( download::RecordDownloadHttpResponseCode(
...@@ -288,7 +287,7 @@ bool DownloadRequestCore::OnResponseStarted( ...@@ -288,7 +287,7 @@ bool DownloadRequestCore::OnResponseStarted(
// Get the last modified time and etag. // Get the last modified time and etag.
const net::HttpResponseHeaders* headers = request()->response_headers(); const net::HttpResponseHeaders* headers = request()->response_headers();
download::HandleResponseHeaders(headers, create_info.get()); HandleResponseHeaders(headers, create_info.get());
// If the content-length header is not present (or contains something other // If the content-length header is not present (or contains something other
// than numbers), the incoming content_length is -1 (unknown size). // than numbers), the incoming content_length is -1 (unknown size).
...@@ -400,10 +399,9 @@ void DownloadRequestCore::OnResponseCompleted( ...@@ -400,10 +399,9 @@ void DownloadRequestCore::OnResponseCompleted(
if (error_code == net::OK) if (error_code == net::OK)
error_code = net::ERR_FAILED; error_code = net::ERR_FAILED;
} }
download::DownloadInterruptReason reason = download::DownloadInterruptReason reason = HandleRequestCompletionStatus(
download::HandleRequestCompletionStatus(error_code, has_strong_validators, error_code, has_strong_validators, request()->ssl_info().cert_status,
request()->ssl_info().cert_status, abort_reason_);
abort_reason_);
std::string accept_ranges; std::string accept_ranges;
if (request()->response_headers()) { if (request()->response_headers()) {
......
...@@ -2,45 +2,48 @@ ...@@ -2,45 +2,48 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "components/download/public/common/download_response_handler.h" #include "content/browser/download/download_response_handler.h"
#include <memory> #include <memory>
#include "components/download/public/common/download_stats.h" #include "components/download/public/common/download_stats.h"
#include "components/download/public/common/download_url_parameters.h" #include "components/download/public/common/download_url_parameters.h"
#include "components/download/public/common/download_utils.h" #include "content/browser/download/download_utils.h"
#include "net/http/http_status_code.h" #include "net/http/http_status_code.h"
#include "net/log/net_log_with_source.h"
namespace download { namespace content {
namespace { namespace {
mojom::NetworkRequestStatus ConvertInterruptReasonToMojoNetworkRequestStatus( download::mojom::NetworkRequestStatus
DownloadInterruptReason reason) { ConvertInterruptReasonToMojoNetworkRequestStatus(
download::DownloadInterruptReason reason) {
switch (reason) { switch (reason) {
case DOWNLOAD_INTERRUPT_REASON_NONE: case download::DOWNLOAD_INTERRUPT_REASON_NONE:
return mojom::NetworkRequestStatus::OK; return download::mojom::NetworkRequestStatus::OK;
case DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT: case download::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT:
return mojom::NetworkRequestStatus::NETWORK_TIMEOUT; return download::mojom::NetworkRequestStatus::NETWORK_TIMEOUT;
case DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED: case download::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED:
return mojom::NetworkRequestStatus::NETWORK_DISCONNECTED; return download::mojom::NetworkRequestStatus::NETWORK_DISCONNECTED;
case DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN: case download::DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN:
return mojom::NetworkRequestStatus::NETWORK_SERVER_DOWN; return download::mojom::NetworkRequestStatus::NETWORK_SERVER_DOWN;
case DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE: case download::DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE:
return mojom::NetworkRequestStatus::SERVER_NO_RANGE; return download::mojom::NetworkRequestStatus::SERVER_NO_RANGE;
case DOWNLOAD_INTERRUPT_REASON_SERVER_CONTENT_LENGTH_MISMATCH: case download::DOWNLOAD_INTERRUPT_REASON_SERVER_CONTENT_LENGTH_MISMATCH:
return mojom::NetworkRequestStatus::SERVER_CONTENT_LENGTH_MISMATCH; return download::mojom::NetworkRequestStatus::
case DOWNLOAD_INTERRUPT_REASON_SERVER_UNREACHABLE: SERVER_CONTENT_LENGTH_MISMATCH;
return mojom::NetworkRequestStatus::SERVER_UNREACHABLE; case download::DOWNLOAD_INTERRUPT_REASON_SERVER_UNREACHABLE:
case DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM: return download::mojom::NetworkRequestStatus::SERVER_UNREACHABLE;
return mojom::NetworkRequestStatus::SERVER_CERT_PROBLEM; case download::DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM:
case DOWNLOAD_INTERRUPT_REASON_USER_CANCELED: return download::mojom::NetworkRequestStatus::SERVER_CERT_PROBLEM;
return mojom::NetworkRequestStatus::USER_CANCELED; case download::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED:
case DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED: return download::mojom::NetworkRequestStatus::USER_CANCELED;
return mojom::NetworkRequestStatus::NETWORK_FAILED; case download::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED:
return download::mojom::NetworkRequestStatus::NETWORK_FAILED;
default: default:
NOTREACHED(); NOTREACHED();
return mojom::NetworkRequestStatus::NETWORK_FAILED; return download::mojom::NetworkRequestStatus::NETWORK_FAILED;
} }
} }
...@@ -49,12 +52,12 @@ mojom::NetworkRequestStatus ConvertInterruptReasonToMojoNetworkRequestStatus( ...@@ -49,12 +52,12 @@ mojom::NetworkRequestStatus ConvertInterruptReasonToMojoNetworkRequestStatus(
DownloadResponseHandler::DownloadResponseHandler( DownloadResponseHandler::DownloadResponseHandler(
network::ResourceRequest* resource_request, network::ResourceRequest* resource_request,
Delegate* delegate, Delegate* delegate,
std::unique_ptr<DownloadSaveInfo> save_info, std::unique_ptr<download::DownloadSaveInfo> save_info,
bool is_parallel_request, bool is_parallel_request,
bool is_transient, bool is_transient,
bool fetch_error_body, bool fetch_error_body,
const std::string& request_origin, const std::string& request_origin,
DownloadSource download_source, download::DownloadSource download_source,
std::vector<GURL> url_chain) std::vector<GURL> url_chain)
: delegate_(delegate), : delegate_(delegate),
started_(false), started_(false),
...@@ -68,9 +71,10 @@ DownloadResponseHandler::DownloadResponseHandler( ...@@ -68,9 +71,10 @@ DownloadResponseHandler::DownloadResponseHandler(
download_source_(download_source), download_source_(download_source),
has_strong_validators_(false), has_strong_validators_(false),
is_partial_request_(save_info_->offset > 0), is_partial_request_(save_info_->offset > 0),
abort_reason_(DOWNLOAD_INTERRUPT_REASON_NONE) { abort_reason_(download::DOWNLOAD_INTERRUPT_REASON_NONE) {
if (!is_parallel_request) { if (!is_parallel_request) {
RecordDownloadCountWithSource(UNTHROTTLED_COUNT, download_source); download::RecordDownloadCountWithSource(download::UNTHROTTLED_COUNT,
download_source);
} }
if (resource_request->request_initiator.has_value()) if (resource_request->request_initiator.has_value())
origin_ = resource_request->request_initiator.value().GetURL(); origin_ = resource_request->request_initiator.value().GetURL();
...@@ -92,8 +96,9 @@ void DownloadResponseHandler::OnReceiveResponse( ...@@ -92,8 +96,9 @@ void DownloadResponseHandler::OnReceiveResponse(
// |RecordDownloadSourcePageTransitionType| here. // |RecordDownloadSourcePageTransitionType| here.
if (head.headers) { if (head.headers) {
has_strong_validators_ = head.headers->HasStrongValidators(); has_strong_validators_ = head.headers->HasStrongValidators();
RecordDownloadHttpResponseCode(head.headers->response_code()); download::RecordDownloadHttpResponseCode(head.headers->response_code());
RecordDownloadContentDisposition(create_info_->content_disposition); download::RecordDownloadContentDisposition(
create_info_->content_disposition);
} }
// Blink verifies that the requester of this download is allowed to set a // Blink verifies that the requester of this download is allowed to set a
...@@ -107,25 +112,27 @@ void DownloadResponseHandler::OnReceiveResponse( ...@@ -107,25 +112,27 @@ void DownloadResponseHandler::OnReceiveResponse(
create_info_->save_info->suggested_name.clear(); create_info_->save_info->suggested_name.clear();
} }
if (create_info_->result != DOWNLOAD_INTERRUPT_REASON_NONE) if (create_info_->result != download::DOWNLOAD_INTERRUPT_REASON_NONE)
OnResponseStarted(mojom::DownloadStreamHandlePtr()); OnResponseStarted(download::mojom::DownloadStreamHandlePtr());
} }
std::unique_ptr<DownloadCreateInfo> std::unique_ptr<download::DownloadCreateInfo>
DownloadResponseHandler::CreateDownloadCreateInfo( DownloadResponseHandler::CreateDownloadCreateInfo(
const network::ResourceResponseHead& head) { const network::ResourceResponseHead& head) {
auto create_info = std::make_unique<DownloadCreateInfo>( // TODO(qinmin): instead of using NetLogWithSource, introduce new logging
// class for download.
auto create_info = std::make_unique<download::DownloadCreateInfo>(
base::Time::Now(), std::move(save_info_)); base::Time::Now(), std::move(save_info_));
DownloadInterruptReason result = download::DownloadInterruptReason result =
head.headers head.headers
? HandleSuccessfulServerResponse( ? HandleSuccessfulServerResponse(
*head.headers, create_info->save_info.get(), fetch_error_body_) *head.headers, create_info->save_info.get(), fetch_error_body_)
: DOWNLOAD_INTERRUPT_REASON_NONE; : download::DOWNLOAD_INTERRUPT_REASON_NONE;
create_info->total_bytes = head.content_length > 0 ? head.content_length : 0; create_info->total_bytes = head.content_length > 0 ? head.content_length : 0;
create_info->result = result; create_info->result = result;
if (result == DOWNLOAD_INTERRUPT_REASON_NONE) if (result == download::DOWNLOAD_INTERRUPT_REASON_NONE)
create_info->remote_address = head.socket_address.host(); create_info->remote_address = head.socket_address.host();
create_info->method = method_; create_info->method = method_;
create_info->connection_info = head.connection_info; create_info->connection_info = head.connection_info;
...@@ -149,8 +156,8 @@ void DownloadResponseHandler::OnReceiveRedirect( ...@@ -149,8 +156,8 @@ void DownloadResponseHandler::OnReceiveRedirect(
if (is_partial_request_) { if (is_partial_request_) {
// A redirect while attempting a partial resumption indicates a potential // A redirect while attempting a partial resumption indicates a potential
// middle box. Trigger another interruption so that the // middle box. Trigger another interruption so that the
// DownloadItem can retry. // download::DownloadItem can retry.
abort_reason_ = DOWNLOAD_INTERRUPT_REASON_SERVER_UNREACHABLE; abort_reason_ = download::DOWNLOAD_INTERRUPT_REASON_SERVER_UNREACHABLE;
OnComplete(network::URLLoaderCompletionStatus(net::OK)); OnComplete(network::URLLoaderCompletionStatus(net::OK));
return; return;
} }
...@@ -179,8 +186,8 @@ void DownloadResponseHandler::OnStartLoadingResponseBody( ...@@ -179,8 +186,8 @@ void DownloadResponseHandler::OnStartLoadingResponseBody(
if (started_) if (started_)
return; return;
mojom::DownloadStreamHandlePtr stream_handle = download::mojom::DownloadStreamHandlePtr stream_handle =
mojom::DownloadStreamHandle::New(); download::mojom::DownloadStreamHandle::New();
stream_handle->stream = std::move(body); stream_handle->stream = std::move(body);
stream_handle->client_request = mojo::MakeRequest(&client_ptr_); stream_handle->client_request = mojo::MakeRequest(&client_ptr_);
OnResponseStarted(std::move(stream_handle)); OnResponseStarted(std::move(stream_handle));
...@@ -188,7 +195,7 @@ void DownloadResponseHandler::OnStartLoadingResponseBody( ...@@ -188,7 +195,7 @@ void DownloadResponseHandler::OnStartLoadingResponseBody(
void DownloadResponseHandler::OnComplete( void DownloadResponseHandler::OnComplete(
const network::URLLoaderCompletionStatus& status) { const network::URLLoaderCompletionStatus& status) {
DownloadInterruptReason reason = HandleRequestCompletionStatus( download::DownloadInterruptReason reason = HandleRequestCompletionStatus(
static_cast<net::Error>(status.error_code), has_strong_validators_, static_cast<net::Error>(status.error_code), has_strong_validators_,
cert_status_, abort_reason_); cert_status_, abort_reason_);
...@@ -205,14 +212,14 @@ void DownloadResponseHandler::OnComplete( ...@@ -205,14 +212,14 @@ void DownloadResponseHandler::OnComplete(
create_info_ = CreateDownloadCreateInfo(network::ResourceResponseHead()); create_info_ = CreateDownloadCreateInfo(network::ResourceResponseHead());
create_info_->result = reason; create_info_->result = reason;
OnResponseStarted(mojom::DownloadStreamHandlePtr()); OnResponseStarted(download::mojom::DownloadStreamHandlePtr());
} }
void DownloadResponseHandler::OnResponseStarted( void DownloadResponseHandler::OnResponseStarted(
mojom::DownloadStreamHandlePtr stream_handle) { download::mojom::DownloadStreamHandlePtr stream_handle) {
started_ = true; started_ = true;
delegate_->OnResponseStarted(std::move(create_info_), delegate_->OnResponseStarted(std::move(create_info_),
std::move(stream_handle)); std::move(stream_handle));
} }
} // namespace download } // namespace content
...@@ -2,45 +2,48 @@ ...@@ -2,45 +2,48 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_RESPONSE_HANDLER_H_ #ifndef CONTENT_BROWSER_DOWNLOAD_RESPONSE_HANDLER_
#define COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_RESPONSE_HANDLER_H_ #define CONTENT_BROWSER_DOWNLOAD_RESPONSE_HANDLER_
#include <vector> #include <vector>
#include "components/download/public/common/download_create_info.h" #include "components/download/public/common/download_create_info.h"
#include "components/download/public/common/download_export.h"
#include "components/download/public/common/download_source.h" #include "components/download/public/common/download_source.h"
#include "components/download/public/common/download_stream.mojom.h" #include "components/download/public/common/download_stream.mojom.h"
#include "content/public/common/referrer.h"
#include "net/cert/cert_status_flags.h" #include "net/cert/cert_status_flags.h"
#include "services/network/public/cpp/resource_response.h" #include "services/network/public/cpp/resource_response.h"
#include "services/network/public/mojom/url_loader.mojom.h" #include "services/network/public/mojom/url_loader.mojom.h"
namespace download { namespace download {
struct DownloadCreateInfo;
} // namespace download
namespace content {
// This class is responsible for handling the server response for a download. // This class is responsible for handling the server response for a download.
// It passes the DataPipeConsumerHandle and completion status to the download // It passes the DataPipeConsumerHandle and completion status to the download
// sink. The class is common to both navigation triggered downloads and // sink. The class is common to both navigation triggered downloads and
// context menu downloads // context menu downloads
class COMPONENTS_DOWNLOAD_EXPORT DownloadResponseHandler class DownloadResponseHandler : public network::mojom::URLLoaderClient {
: public network::mojom::URLLoaderClient {
public: public:
// Class for handling the stream once response starts. // Class for handling the stream once response starts.
class Delegate { class Delegate {
public: public:
virtual void OnResponseStarted( virtual void OnResponseStarted(
std::unique_ptr<DownloadCreateInfo> download_create_info, std::unique_ptr<download::DownloadCreateInfo> download_create_info,
mojom::DownloadStreamHandlePtr stream_handle) = 0; download::mojom::DownloadStreamHandlePtr stream_handle) = 0;
virtual void OnReceiveRedirect() = 0; virtual void OnReceiveRedirect() = 0;
}; };
DownloadResponseHandler(network::ResourceRequest* resource_request, DownloadResponseHandler(network::ResourceRequest* resource_request,
Delegate* delegate, Delegate* delegate,
std::unique_ptr<DownloadSaveInfo> save_info, std::unique_ptr<download::DownloadSaveInfo> save_info,
bool is_parallel_request, bool is_parallel_request,
bool is_transient, bool is_transient,
bool fetch_error_body, bool fetch_error_body,
const std::string& request_origin, const std::string& request_origin,
DownloadSource download_source, download::DownloadSource download_source,
std::vector<GURL> url_chain); std::vector<GURL> url_chain);
~DownloadResponseHandler() override; ~DownloadResponseHandler() override;
...@@ -62,41 +65,42 @@ class COMPONENTS_DOWNLOAD_EXPORT DownloadResponseHandler ...@@ -62,41 +65,42 @@ class COMPONENTS_DOWNLOAD_EXPORT DownloadResponseHandler
void OnComplete(const network::URLLoaderCompletionStatus& status) override; void OnComplete(const network::URLLoaderCompletionStatus& status) override;
private: private:
std::unique_ptr<DownloadCreateInfo> CreateDownloadCreateInfo( std::unique_ptr<download::DownloadCreateInfo> CreateDownloadCreateInfo(
const network::ResourceResponseHead& head); const network::ResourceResponseHead& head);
// Helper method that is called when response is received. // Helper method that is called when response is received.
void OnResponseStarted(mojom::DownloadStreamHandlePtr stream_handle); void OnResponseStarted(
download::mojom::DownloadStreamHandlePtr stream_handle);
Delegate* const delegate_; Delegate* const delegate_;
std::unique_ptr<DownloadCreateInfo> create_info_; std::unique_ptr<download::DownloadCreateInfo> create_info_;
bool started_; bool started_;
// Information needed to create DownloadCreateInfo when the time comes. // Information needed to create DownloadCreateInfo when the time comes.
std::unique_ptr<DownloadSaveInfo> save_info_; std::unique_ptr<download::DownloadSaveInfo> save_info_;
std::vector<GURL> url_chain_; std::vector<GURL> url_chain_;
std::string method_; std::string method_;
GURL referrer_; GURL referrer_;
bool is_transient_; bool is_transient_;
bool fetch_error_body_; bool fetch_error_body_;
std::string request_origin_; std::string request_origin_;
DownloadSource download_source_; download::DownloadSource download_source_;
net::CertStatus cert_status_; net::CertStatus cert_status_;
bool has_strong_validators_; bool has_strong_validators_;
GURL origin_; GURL origin_;
bool is_partial_request_; bool is_partial_request_;
// The abort reason if this class decides to block the download. // The abort reason if this class decides to block the download.
DownloadInterruptReason abort_reason_; download::DownloadInterruptReason abort_reason_;
// Mojo interface ptr to send the completion status to the download sink. // Mojo interface ptr to send the completion status to the download sink.
mojom::DownloadStreamClientPtr client_ptr_; download::mojom::DownloadStreamClientPtr client_ptr_;
DISALLOW_COPY_AND_ASSIGN(DownloadResponseHandler); DISALLOW_COPY_AND_ASSIGN(DownloadResponseHandler);
}; };
} // namespace download } // namespace content
#endif // COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_RESPONSE_HANDLER_H_ #endif // CONTENT_BROWSER_DOWNLOAD_RESPONSE_HANDLER
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "components/download/public/common/download_create_info.h" #include "components/download/public/common/download_create_info.h"
#include "components/download/public/common/download_interrupt_reasons_utils.h" #include "components/download/public/common/download_interrupt_reasons_utils.h"
#include "components/download/public/common/download_save_info.h" #include "components/download/public/common/download_save_info.h"
#include "components/download/public/common/download_stats.h"
#include "components/download/public/common/download_url_parameters.h" #include "components/download/public/common/download_url_parameters.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h" #include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/loader/upload_data_stream_builder.h" #include "content/browser/loader/upload_data_stream_builder.h"
...@@ -27,6 +28,7 @@ ...@@ -27,6 +28,7 @@
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/base/upload_bytes_element_reader.h" #include "net/base/upload_bytes_element_reader.h"
#include "net/http/http_request_headers.h" #include "net/http/http_request_headers.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
#include "services/network/public/cpp/resource_request.h" #include "services/network/public/cpp/resource_request.h"
...@@ -129,6 +131,53 @@ storage::BlobStorageContext* BlobStorageContextGetter( ...@@ -129,6 +131,53 @@ storage::BlobStorageContext* BlobStorageContextGetter(
return blob_context->context(); return blob_context->context();
} }
download::DownloadInterruptReason HandleRequestCompletionStatus(
net::Error error_code,
bool has_strong_validators,
net::CertStatus cert_status,
download::DownloadInterruptReason abort_reason) {
// ERR_CONTENT_LENGTH_MISMATCH can be caused by 1 of the following reasons:
// 1. Server or proxy closes the connection too early.
// 2. The content-length header is wrong.
// If the download has strong validators, we can interrupt the download
// and let it resume automatically. Otherwise, resuming the download will
// cause it to restart and the download may never complete if the error was
// caused by reason 2. As a result, downloads without strong validators are
// treated as completed here.
// TODO(qinmin): check the metrics from downloads with strong validators,
// and decide whether we should interrupt downloads without strong validators
// rather than complete them.
if (error_code == net::ERR_CONTENT_LENGTH_MISMATCH &&
!has_strong_validators) {
error_code = net::OK;
download::RecordDownloadCount(
download::COMPLETED_WITH_CONTENT_LENGTH_MISMATCH_COUNT);
}
if (error_code == net::ERR_ABORTED) {
// ERR_ABORTED == something outside of the network
// stack cancelled the request. There aren't that many things that
// could do this to a download request (whose lifetime is separated from
// the tab from which it came). We map this to USER_CANCELLED as the
// case we know about (system suspend because of laptop close) corresponds
// to a user action.
// TODO(asanka): A lid close or other power event should result in an
// interruption that doesn't discard the partial state, unlike
// USER_CANCELLED. (https://crbug.com/166179)
if (net::IsCertStatusError(cert_status))
return download::DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM;
else
return download::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED;
} else if (abort_reason != download::DOWNLOAD_INTERRUPT_REASON_NONE) {
// If a more specific interrupt reason was specified before the request
// was explicitly cancelled, then use it.
return abort_reason;
}
return download::ConvertNetErrorToInterruptReason(
error_code, download::DOWNLOAD_INTERRUPT_FROM_NETWORK);
}
std::unique_ptr<network::ResourceRequest> CreateResourceRequest( std::unique_ptr<network::ResourceRequest> CreateResourceRequest(
download::DownloadUrlParameters* params) { download::DownloadUrlParameters* params) {
DCHECK(params->offset() >= 0); DCHECK(params->offset() >= 0);
...@@ -237,6 +286,152 @@ std::unique_ptr<net::URLRequest> CreateURLRequestOnIOThread( ...@@ -237,6 +286,152 @@ std::unique_ptr<net::URLRequest> CreateURLRequestOnIOThread(
return request; return request;
} }
download::DownloadInterruptReason HandleSuccessfulServerResponse(
const net::HttpResponseHeaders& http_headers,
download::DownloadSaveInfo* save_info,
bool fetch_error_body) {
download::DownloadInterruptReason result =
download::DOWNLOAD_INTERRUPT_REASON_NONE;
switch (http_headers.response_code()) {
case -1: // Non-HTTP request.
case net::HTTP_OK:
case net::HTTP_NON_AUTHORITATIVE_INFORMATION:
case net::HTTP_PARTIAL_CONTENT:
// Expected successful codes.
break;
case net::HTTP_CREATED:
case net::HTTP_ACCEPTED:
// Per RFC 7231 the entity being transferred is metadata about the
// resource at the target URL and not the resource at that URL (or the
// resource that would be at the URL once processing is completed in the
// case of HTTP_ACCEPTED). However, we currently don't have special
// handling for these response and they are downloaded the same as a
// regular response.
break;
case net::HTTP_NO_CONTENT:
case net::HTTP_RESET_CONTENT:
// These two status codes don't have an entity (or rather RFC 7231
// requires that there be no entity). They are treated the same as the
// resource not being found since there is no entity to download.
case net::HTTP_NOT_FOUND:
result = download::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
break;
case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE:
// Retry by downloading from the start automatically:
// If we haven't received data when we get this error, we won't.
result = download::DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE;
break;
case net::HTTP_UNAUTHORIZED:
case net::HTTP_PROXY_AUTHENTICATION_REQUIRED:
// Server didn't authorize this request.
result = download::DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED;
break;
case net::HTTP_FORBIDDEN:
// Server forbids access to this resource.
result = download::DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN;
break;
default: // All other errors.
// Redirection and informational codes should have been handled earlier
// in the stack.
// TODO(xingliu): Handle HTTP_PRECONDITION_FAILED and resurrect
// download::DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION for range
// requests. This will change extensions::api::download::InterruptReason.
DCHECK_NE(3, http_headers.response_code() / 100);
DCHECK_NE(1, http_headers.response_code() / 100);
result = download::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED;
}
if (result != download::DOWNLOAD_INTERRUPT_REASON_NONE && !fetch_error_body)
return result;
// The caller is expecting a partial response.
if (save_info && (save_info->offset > 0 || save_info->length > 0)) {
if (http_headers.response_code() != net::HTTP_PARTIAL_CONTENT) {
// Server should send partial content when "If-Match" or
// "If-Unmodified-Since" check passes, and the range request header has
// last byte position. e.g. "Range:bytes=50-99".
if (save_info->length != download::DownloadSaveInfo::kLengthFullContent &&
!fetch_error_body)
return download::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
// Requested a partial range, but received the entire response, when
// the range request header is "Range:bytes={offset}-".
// The response can be HTTP 200 or other error code when
// |fetch_error_body| is true.
save_info->offset = 0;
save_info->hash_of_partial_file.clear();
save_info->hash_state.reset();
return download::DOWNLOAD_INTERRUPT_REASON_NONE;
}
int64_t first_byte = -1;
int64_t last_byte = -1;
int64_t length = -1;
if (!http_headers.GetContentRangeFor206(&first_byte, &last_byte, &length))
return download::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
DCHECK_GE(first_byte, 0);
if (first_byte != save_info->offset ||
(save_info->length > 0 &&
last_byte != save_info->offset + save_info->length - 1)) {
// The server returned a different range than the one we requested. Assume
// the response is bad.
//
// In the future we should consider allowing offsets that are less than
// the offset we've requested, since in theory we can truncate the partial
// file at the offset and continue.
return download::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
}
return download::DOWNLOAD_INTERRUPT_REASON_NONE;
}
if (http_headers.response_code() == net::HTTP_PARTIAL_CONTENT)
return download::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
return download::DOWNLOAD_INTERRUPT_REASON_NONE;
}
void HandleResponseHeaders(const net::HttpResponseHeaders* headers,
download::DownloadCreateInfo* create_info) {
if (!headers)
return;
if (headers->HasStrongValidators()) {
// If we don't have strong validators as per RFC 7232 section 2, then
// we neither store nor use them for range requests.
if (!headers->EnumerateHeader(nullptr, "Last-Modified",
&create_info->last_modified))
create_info->last_modified.clear();
if (!headers->EnumerateHeader(nullptr, "ETag", &create_info->etag))
create_info->etag.clear();
}
// Grab the first content-disposition header. There may be more than one,
// though as of this writing, the network stack ensures if there are, they
// are all duplicates.
headers->EnumerateHeader(nullptr, "Content-Disposition",
&create_info->content_disposition);
// Parse the original mime type from the header, notice that actual mime type
// might be different due to mime type sniffing.
if (!headers->GetMimeType(&create_info->original_mime_type))
create_info->original_mime_type.clear();
// Content-Range is validated in HandleSuccessfulServerResponse.
// In RFC 7233, a single part 206 partial response must generate
// Content-Range. Accept-Range may be sent in 200 response to indicate the
// server can handle range request, but optional in 206 response.
create_info->accept_range =
headers->HasHeaderValue("Accept-Ranges", "bytes") ||
(headers->HasHeader("Content-Range") &&
headers->response_code() == net::HTTP_PARTIAL_CONTENT);
}
base::Optional<download::DownloadEntry> GetInProgressEntry( base::Optional<download::DownloadEntry> GetInProgressEntry(
const std::string& guid, const std::string& guid,
BrowserContext* browser_context) { BrowserContext* browser_context) {
......
...@@ -5,13 +5,17 @@ ...@@ -5,13 +5,17 @@
#ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_UTILS_H_ #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_UTILS_H_
#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_UTILS_H_ #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_UTILS_H_
#include <memory>
#include "base/optional.h" #include "base/optional.h"
#include "components/download/public/common/download_interrupt_reasons.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "net/base/net_errors.h"
#include "net/cert/cert_status_flags.h"
#include "net/http/http_response_headers.h"
namespace download { namespace download {
struct DownloadCreateInfo;
struct DownloadEntry; struct DownloadEntry;
struct DownloadSaveInfo;
class DownloadUrlParameters; class DownloadUrlParameters;
} // namespace download } // namespace download
...@@ -32,6 +36,14 @@ namespace content { ...@@ -32,6 +36,14 @@ namespace content {
class BrowserContext; class BrowserContext;
class ResourceContext; class ResourceContext;
// Handle the url request completion status and return the interrupt reasons.
// |cert_status| is ignored if error_code is not net::ERR_ABORTED.
download::DownloadInterruptReason CONTENT_EXPORT
HandleRequestCompletionStatus(net::Error error_code,
bool has_strong_validators,
net::CertStatus cert_status,
download::DownloadInterruptReason abort_reason);
// Create a ResourceRequest from |params|. // Create a ResourceRequest from |params|.
std::unique_ptr<network::ResourceRequest> CONTENT_EXPORT std::unique_ptr<network::ResourceRequest> CONTENT_EXPORT
CreateResourceRequest(download::DownloadUrlParameters* params); CreateResourceRequest(download::DownloadUrlParameters* params);
...@@ -40,6 +52,19 @@ CreateResourceRequest(download::DownloadUrlParameters* params); ...@@ -40,6 +52,19 @@ CreateResourceRequest(download::DownloadUrlParameters* params);
std::unique_ptr<net::URLRequest> CONTENT_EXPORT std::unique_ptr<net::URLRequest> CONTENT_EXPORT
CreateURLRequestOnIOThread(download::DownloadUrlParameters* params); CreateURLRequestOnIOThread(download::DownloadUrlParameters* params);
// Parse the HTTP server response code.
// If |fetch_error_body| is true, most of HTTP response codes will be accepted
// as successful response.
download::DownloadInterruptReason CONTENT_EXPORT
HandleSuccessfulServerResponse(const net::HttpResponseHeaders& http_headers,
download::DownloadSaveInfo* save_info,
bool fetch_error_body);
// Parse response headers and update |create_info| accordingly.
CONTENT_EXPORT void HandleResponseHeaders(
const net::HttpResponseHeaders* headers,
download::DownloadCreateInfo* create_info);
// Get the entry based on |guid| from in progress cache. // Get the entry based on |guid| from in progress cache.
CONTENT_EXPORT base::Optional<download::DownloadEntry> GetInProgressEntry( CONTENT_EXPORT base::Optional<download::DownloadEntry> GetInProgressEntry(
const std::string& guid, const std::string& guid,
......
...@@ -130,7 +130,7 @@ void ResourceDownloader::Start( ...@@ -130,7 +130,7 @@ void ResourceDownloader::Start(
guid_ = download_url_parameters->guid(); guid_ = download_url_parameters->guid();
// Set up the URLLoaderClient. // Set up the URLLoaderClient.
url_loader_client_ = std::make_unique<download::DownloadResponseHandler>( url_loader_client_ = std::make_unique<DownloadResponseHandler>(
resource_request_.get(), this, resource_request_.get(), this,
std::make_unique<download::DownloadSaveInfo>( std::make_unique<download::DownloadSaveInfo>(
download_url_parameters->GetSaveInfo()), download_url_parameters->GetSaveInfo()),
...@@ -172,7 +172,7 @@ void ResourceDownloader::InterceptResponse( ...@@ -172,7 +172,7 @@ void ResourceDownloader::InterceptResponse(
auto save_info = std::make_unique<download::DownloadSaveInfo>(); auto save_info = std::make_unique<download::DownloadSaveInfo>();
if (suggested_filename.has_value()) if (suggested_filename.has_value())
save_info->suggested_name = base::UTF8ToUTF16(suggested_filename.value()); save_info->suggested_name = base::UTF8ToUTF16(suggested_filename.value());
url_loader_client_ = std::make_unique<download::DownloadResponseHandler>( url_loader_client_ = std::make_unique<DownloadResponseHandler>(
resource_request_.get(), this, std::move(save_info), false, false, false, resource_request_.get(), this, std::move(save_info), false, false, false,
std::string(), download::DownloadSource::NAVIGATION, std::string(), download::DownloadSource::NAVIGATION,
std::move(url_chain)); std::move(url_chain));
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#ifndef CONTENT_BROWSER_DOWNLOAD_RESOURCE_DOWNLOADER_ #ifndef CONTENT_BROWSER_DOWNLOAD_RESOURCE_DOWNLOADER_
#define CONTENT_BROWSER_DOWNLOAD_RESOURCE_DOWNLOADER_ #define CONTENT_BROWSER_DOWNLOAD_RESOURCE_DOWNLOADER_
#include "components/download/public/common/download_response_handler.h" #include "content/browser/download/download_response_handler.h"
#include "content/browser/download/url_download_handler.h" #include "content/browser/download/url_download_handler.h"
#include "content/public/browser/ssl_status.h" #include "content/public/browser/ssl_status.h"
#include "mojo/public/cpp/bindings/binding.h" #include "mojo/public/cpp/bindings/binding.h"
...@@ -18,7 +18,7 @@ class SharedURLLoaderFactory; ...@@ -18,7 +18,7 @@ class SharedURLLoaderFactory;
// Class for handing the download of a url. // Class for handing the download of a url.
class ResourceDownloader : public UrlDownloadHandler, class ResourceDownloader : public UrlDownloadHandler,
public download::DownloadResponseHandler::Delegate { public DownloadResponseHandler::Delegate {
public: public:
// Called to start a download, must be called on IO thread. // Called to start a download, must be called on IO thread.
static std::unique_ptr<ResourceDownloader> BeginDownload( static std::unique_ptr<ResourceDownloader> BeginDownload(
...@@ -56,7 +56,7 @@ class ResourceDownloader : public UrlDownloadHandler, ...@@ -56,7 +56,7 @@ class ResourceDownloader : public UrlDownloadHandler,
uint32_t download_id); uint32_t download_id);
~ResourceDownloader() override; ~ResourceDownloader() override;
// download::DownloadResponseHandler::Delegate // DownloadResponseHandler::Delegate
void OnResponseStarted( void OnResponseStarted(
std::unique_ptr<download::DownloadCreateInfo> download_create_info, std::unique_ptr<download::DownloadCreateInfo> download_create_info,
download::mojom::DownloadStreamHandlePtr stream_handle) override; download::mojom::DownloadStreamHandlePtr stream_handle) override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment