This source file includes following definitions.
- CallStartedCBOnUIThread
- StartOnUIThread
- InitializeDownloadTabInfoOnUIThread
- on_response_started_called_
- OnUploadProgress
- OnRequestRedirected
- OnResponseStarted
- CallStartedCB
- OnWillStart
- OnBeforeNetworkStart
- OnWillRead
- OnReadCompleted
- OnResponseCompleted
- OnDataDownloaded
- PauseRequest
- ResumeRequest
- CancelRequest
- DebugString
#include "content/browser/download/download_resource_handler.h"
#include <string>
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/metrics/histogram.h"
#include "base/metrics/stats_counters.h"
#include "base/strings/stringprintf.h"
#include "content/browser/byte_stream.h"
#include "content/browser/download/download_create_info.h"
#include "content/browser/download/download_interrupt_reasons_impl.h"
#include "content/browser/download/download_manager_impl.h"
#include "content/browser/download/download_request_handle.h"
#include "content/browser/download/download_stats.h"
#include "content/browser/loader/resource_dispatcher_host_impl.h"
#include "content/browser/loader/resource_request_info_impl.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_interrupt_reasons.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/download_manager_delegate.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/resource_response.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_request_context.h"
namespace content {
struct DownloadResourceHandler::DownloadTabInfo {
GURL tab_url;
GURL tab_referrer_url;
};
namespace {
void CallStartedCBOnUIThread(
const DownloadUrlParameters::OnStartedCallback& started_cb,
DownloadItem* item,
DownloadInterruptReason interrupt_reason) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (started_cb.is_null())
return;
started_cb.Run(item, interrupt_reason);
}
static void StartOnUIThread(
scoped_ptr<DownloadCreateInfo> info,
DownloadResourceHandler::DownloadTabInfo* tab_info,
scoped_ptr<ByteStreamReader> stream,
const DownloadUrlParameters::OnStartedCallback& started_cb) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DownloadManager* download_manager = info->request_handle.GetDownloadManager();
if (!download_manager) {
if (!started_cb.is_null())
started_cb.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
return;
}
info->tab_url = tab_info->tab_url;
info->tab_referrer_url = tab_info->tab_referrer_url;
download_manager->StartDownload(info.Pass(), stream.Pass(), started_cb);
}
void InitializeDownloadTabInfoOnUIThread(
const DownloadRequestHandle& request_handle,
DownloadResourceHandler::DownloadTabInfo* tab_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
WebContents* web_contents = request_handle.GetWebContents();
if (web_contents) {
NavigationEntry* entry = web_contents->GetController().GetVisibleEntry();
if (entry) {
tab_info->tab_url = entry->GetURL();
tab_info->tab_referrer_url = entry->GetReferrer().url;
}
}
}
}
const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024;
DownloadResourceHandler::DownloadResourceHandler(
uint32 id,
net::URLRequest* request,
const DownloadUrlParameters::OnStartedCallback& started_cb,
scoped_ptr<DownloadSaveInfo> save_info)
: ResourceHandler(request),
download_id_(id),
started_cb_(started_cb),
save_info_(save_info.Pass()),
last_buffer_size_(0),
bytes_read_(0),
pause_count_(0),
was_deferred_(false),
on_response_started_called_(false) {
RecordDownloadCount(UNTHROTTLED_COUNT);
const ResourceRequestInfoImpl* request_info = GetRequestInfo();
tab_info_ = new DownloadTabInfo();
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&InitializeDownloadTabInfoOnUIThread,
DownloadRequestHandle(AsWeakPtr(),
request_info->GetChildID(),
request_info->GetRouteID(),
request_info->GetRequestID()),
tab_info_));
}
bool DownloadResourceHandler::OnUploadProgress(int request_id,
uint64 position,
uint64 size) {
return true;
}
bool DownloadResourceHandler::OnRequestRedirected(
int request_id,
const GURL& url,
ResourceResponse* response,
bool* defer) {
request()->set_first_party_for_cookies(url);
return true;
}
bool DownloadResourceHandler::OnResponseStarted(
int request_id,
ResourceResponse* response,
bool* defer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(!on_response_started_called_);
on_response_started_called_ = true;
VLOG(20) << __FUNCTION__ << "()" << DebugString()
<< " request_id = " << request_id;
download_start_time_ = base::TimeTicks::Now();
request()->StopCaching();
request()->SetPriority(net::IDLE);
int64 content_length =
response->head.content_length > 0 ? response->head.content_length : 0;
const ResourceRequestInfoImpl* request_info = GetRequestInfo();
scoped_ptr<DownloadCreateInfo> info(
new DownloadCreateInfo(base::Time::Now(),
content_length,
request()->net_log(),
request_info->HasUserGesture(),
request_info->GetPageTransition(),
save_info_.Pass()));
scoped_ptr<ByteStreamReader> stream_reader;
CreateByteStream(
base::MessageLoopProxy::current(),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
kDownloadByteStreamSize, &stream_writer_, &stream_reader);
stream_writer_->RegisterCallback(
base::Bind(&DownloadResourceHandler::ResumeRequest, AsWeakPtr()));
info->download_id = download_id_;
info->url_chain = request()->url_chain();
info->referrer_url = GURL(request()->referrer());
info->mime_type = response->head.mime_type;
info->remote_address = request()->GetSocketAddress().host();
request()->GetResponseHeaderByName("content-disposition",
&info->content_disposition);
RecordDownloadMimeType(info->mime_type);
RecordDownloadContentDisposition(info->content_disposition);
info->request_handle =
DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(),
request_info->GetRouteID(),
request_info->GetRequestID());
const net::HttpResponseHeaders* headers = request()->response_headers();
if (headers) {
if (headers->HasStrongValidators()) {
if (!headers->EnumerateHeader(NULL, "Last-Modified",
&info->last_modified))
info->last_modified.clear();
if (!headers->EnumerateHeader(NULL, "ETag", &info->etag))
info->etag.clear();
}
int status = headers->response_code();
if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) {
info->save_info->offset = 0;
info->save_info->hash_state = "";
}
if (!headers->GetMimeType(&info->original_mime_type))
info->original_mime_type.clear();
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&StartOnUIThread,
base::Passed(&info),
base::Owned(tab_info_),
base::Passed(&stream_reader),
started_cb_));
tab_info_ = NULL;
started_cb_.Reset();
return true;
}
void DownloadResourceHandler::CallStartedCB(
DownloadItem* item,
DownloadInterruptReason interrupt_reason) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (started_cb_.is_null())
return;
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(
&CallStartedCBOnUIThread, started_cb_, item, interrupt_reason));
started_cb_.Reset();
}
bool DownloadResourceHandler::OnWillStart(int request_id,
const GURL& url,
bool* defer) {
return true;
}
bool DownloadResourceHandler::OnBeforeNetworkStart(int request_id,
const GURL& url,
bool* defer) {
return true;
}
bool DownloadResourceHandler::OnWillRead(int request_id,
scoped_refptr<net::IOBuffer>* buf,
int* buf_size,
int min_size) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(buf && buf_size);
DCHECK(!read_buffer_.get());
*buf_size = min_size < 0 ? kReadBufSize : min_size;
last_buffer_size_ = *buf_size;
read_buffer_ = new net::IOBuffer(*buf_size);
*buf = read_buffer_.get();
return true;
}
bool DownloadResourceHandler::OnReadCompleted(int request_id, int bytes_read,
bool* defer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(read_buffer_.get());
base::TimeTicks now(base::TimeTicks::Now());
if (!last_read_time_.is_null()) {
double seconds_since_last_read = (now - last_read_time_).InSecondsF();
if (now == last_read_time_)
seconds_since_last_read = 0.00001;
double actual_bandwidth = (bytes_read)/seconds_since_last_read;
double potential_bandwidth = last_buffer_size_/seconds_since_last_read;
RecordBandwidth(actual_bandwidth, potential_bandwidth);
}
last_read_time_ = now;
if (!bytes_read)
return true;
bytes_read_ += bytes_read;
DCHECK(read_buffer_.get());
if (!stream_writer_->Write(read_buffer_, bytes_read)) {
PauseRequest();
*defer = was_deferred_ = true;
last_stream_pause_time_ = now;
}
read_buffer_ = NULL;
if (pause_count_ > 0)
*defer = was_deferred_ = true;
return true;
}
void DownloadResourceHandler::OnResponseCompleted(
int request_id,
const net::URLRequestStatus& status,
const std::string& security_info,
bool* defer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
int response_code = status.is_success() ? request()->GetResponseCode() : 0;
VLOG(20) << __FUNCTION__ << "()" << DebugString()
<< " request_id = " << request_id
<< " status.status() = " << status.status()
<< " status.error() = " << status.error()
<< " response_code = " << response_code;
net::Error error_code = net::OK;
if (status.status() == net::URLRequestStatus::FAILED ||
status.status() == net::URLRequestStatus::CANCELED) {
error_code = static_cast<net::Error>(status.error());
if (error_code == net::OK)
error_code = net::ERR_FAILED;
}
if (error_code == net::ERR_CONTENT_LENGTH_MISMATCH ||
error_code == net::ERR_INCOMPLETE_CHUNKED_ENCODING) {
error_code = net::OK;
}
DownloadInterruptReason reason =
ConvertNetErrorToInterruptReason(
error_code, DOWNLOAD_INTERRUPT_FROM_NETWORK);
if (status.status() == net::URLRequestStatus::CANCELED &&
status.error() == net::ERR_ABORTED) {
reason = DOWNLOAD_INTERRUPT_REASON_USER_CANCELED;
}
if (status.is_success() &&
reason == DOWNLOAD_INTERRUPT_REASON_NONE &&
request()->response_headers()) {
switch(response_code) {
case -1:
case net::HTTP_OK:
case net::HTTP_CREATED:
case net::HTTP_ACCEPTED:
case net::HTTP_NON_AUTHORITATIVE_INFORMATION:
case net::HTTP_RESET_CONTENT:
case net::HTTP_PARTIAL_CONTENT:
break;
case net::HTTP_NO_CONTENT:
case net::HTTP_NOT_FOUND:
reason = DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
break;
case net::HTTP_PRECONDITION_FAILED:
reason = DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION;
break;
case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE:
reason = DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE;
break;
default:
DCHECK_NE(3, response_code / 100);
DCHECK_NE(1, response_code / 100);
reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED;
break;
}
}
std::string accept_ranges;
bool has_strong_validators = false;
if (request()->response_headers()) {
request()->response_headers()->EnumerateHeader(
NULL, "Accept-Ranges", &accept_ranges);
has_strong_validators =
request()->response_headers()->HasStrongValidators();
}
RecordAcceptsRanges(accept_ranges, bytes_read_, has_strong_validators);
RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_,
total_pause_time_);
CallStartedCB(NULL, reason);
if (stream_writer_)
stream_writer_->Close(reason);
if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) {
UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed",
std::abs(status.error()),
net::GetAllErrorCodesForUma());
}
stream_writer_.reset();
read_buffer_ = NULL;
}
void DownloadResourceHandler::OnDataDownloaded(
int request_id,
int bytes_downloaded) {
NOTREACHED();
}
void DownloadResourceHandler::PauseRequest() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
++pause_count_;
}
void DownloadResourceHandler::ResumeRequest() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK_LT(0, pause_count_);
--pause_count_;
if (!was_deferred_)
return;
if (pause_count_ > 0)
return;
was_deferred_ = false;
if (!last_stream_pause_time_.is_null()) {
total_pause_time_ += (base::TimeTicks::Now() - last_stream_pause_time_);
last_stream_pause_time_ = base::TimeTicks();
}
controller()->Resume();
}
void DownloadResourceHandler::CancelRequest() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
const ResourceRequestInfo* info = GetRequestInfo();
ResourceDispatcherHostImpl::Get()->CancelRequest(
info->GetChildID(),
info->GetRequestID());
}
std::string DownloadResourceHandler::DebugString() const {
const ResourceRequestInfo* info = GetRequestInfo();
return base::StringPrintf("{"
" url_ = " "\"%s\""
" info = {"
" child_id = " "%d"
" request_id = " "%d"
" route_id = " "%d"
" }"
" }",
request() ?
request()->url().spec().c_str() :
"<NULL request>",
info->GetChildID(),
info->GetRequestID(),
info->GetRouteID());
}
DownloadResourceHandler::~DownloadResourceHandler() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
CallStartedCB(NULL, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED);
if (stream_writer_)
stream_writer_->RegisterCallback(base::Closure());
if (tab_info_)
BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, tab_info_);
UMA_HISTOGRAM_TIMES("SB2.DownloadDuration",
base::TimeTicks::Now() - download_start_time_);
}
}