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/download/download_resource_handler.h" | 5 #include "content/browser/download/download_resource_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 } // namespace | 68 } // namespace |
69 | 69 |
70 const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024; | 70 const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024; |
71 | 71 |
72 DownloadResourceHandler::DownloadResourceHandler( | 72 DownloadResourceHandler::DownloadResourceHandler( |
73 uint32 id, | 73 uint32 id, |
74 net::URLRequest* request, | 74 net::URLRequest* request, |
75 const DownloadUrlParameters::OnStartedCallback& started_cb, | 75 const DownloadUrlParameters::OnStartedCallback& started_cb, |
76 scoped_ptr<DownloadSaveInfo> save_info) | 76 scoped_ptr<DownloadSaveInfo> save_info) |
77 : download_id_(id), | 77 : ResourceHandler(request), |
| 78 download_id_(id), |
78 content_length_(0), | 79 content_length_(0), |
79 request_(request), | |
80 started_cb_(started_cb), | 80 started_cb_(started_cb), |
81 save_info_(save_info.Pass()), | 81 save_info_(save_info.Pass()), |
82 last_buffer_size_(0), | 82 last_buffer_size_(0), |
83 bytes_read_(0), | 83 bytes_read_(0), |
84 pause_count_(0), | 84 pause_count_(0), |
85 was_deferred_(false), | 85 was_deferred_(false), |
86 on_response_started_called_(false) { | 86 on_response_started_called_(false) { |
87 RecordDownloadCount(UNTHROTTLED_COUNT); | 87 RecordDownloadCount(UNTHROTTLED_COUNT); |
88 } | 88 } |
89 | 89 |
90 bool DownloadResourceHandler::OnUploadProgress(int request_id, | 90 bool DownloadResourceHandler::OnUploadProgress(int request_id, |
91 uint64 position, | 91 uint64 position, |
92 uint64 size) { | 92 uint64 size) { |
93 return true; | 93 return true; |
94 } | 94 } |
95 | 95 |
96 bool DownloadResourceHandler::OnRequestRedirected( | 96 bool DownloadResourceHandler::OnRequestRedirected( |
97 int request_id, | 97 int request_id, |
98 const GURL& url, | 98 const GURL& url, |
99 ResourceResponse* response, | 99 ResourceResponse* response, |
100 bool* defer) { | 100 bool* defer) { |
101 // We treat a download as a main frame load, and thus update the policy URL | 101 // We treat a download as a main frame load, and thus update the policy URL |
102 // on redirects. | 102 // on redirects. |
103 request_->set_first_party_for_cookies(url); | 103 request()->set_first_party_for_cookies(url); |
104 return true; | 104 return true; |
105 } | 105 } |
106 | 106 |
107 // Send the download creation information to the download thread. | 107 // Send the download creation information to the download thread. |
108 bool DownloadResourceHandler::OnResponseStarted( | 108 bool DownloadResourceHandler::OnResponseStarted( |
109 int request_id, | 109 int request_id, |
110 ResourceResponse* response, | 110 ResourceResponse* response, |
111 bool* defer) { | 111 bool* defer) { |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
113 // There can be only one (call) | 113 // There can be only one (call) |
114 DCHECK(!on_response_started_called_); | 114 DCHECK(!on_response_started_called_); |
115 on_response_started_called_ = true; | 115 on_response_started_called_ = true; |
116 | 116 |
117 VLOG(20) << __FUNCTION__ << "()" << DebugString() | 117 VLOG(20) << __FUNCTION__ << "()" << DebugString() |
118 << " request_id = " << request_id; | 118 << " request_id = " << request_id; |
119 download_start_time_ = base::TimeTicks::Now(); | 119 download_start_time_ = base::TimeTicks::Now(); |
120 | 120 |
121 // If it's a download, we don't want to poison the cache with it. | 121 // If it's a download, we don't want to poison the cache with it. |
122 request_->StopCaching(); | 122 request()->StopCaching(); |
123 | 123 |
124 // Lower priority as well, so downloads don't contend for resources | 124 // Lower priority as well, so downloads don't contend for resources |
125 // with main frames. | 125 // with main frames. |
126 request_->SetPriority(net::IDLE); | 126 request()->SetPriority(net::IDLE); |
127 | 127 |
128 std::string content_disposition; | 128 std::string content_disposition; |
129 request_->GetResponseHeaderByName("content-disposition", | 129 request()->GetResponseHeaderByName("content-disposition", |
130 &content_disposition); | 130 &content_disposition); |
131 SetContentDisposition(content_disposition); | 131 SetContentDisposition(content_disposition); |
132 SetContentLength(response->head.content_length); | 132 SetContentLength(response->head.content_length); |
133 | 133 |
134 const ResourceRequestInfoImpl* request_info = | 134 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); |
135 ResourceRequestInfoImpl::ForRequest(request_); | |
136 | 135 |
137 // Deleted in DownloadManager. | 136 // Deleted in DownloadManager. |
138 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo( | 137 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo( |
139 base::Time::Now(), content_length_, | 138 base::Time::Now(), content_length_, |
140 request_->net_log(), request_info->HasUserGesture(), | 139 request()->net_log(), request_info->HasUserGesture(), |
141 request_info->GetPageTransition())); | 140 request_info->GetPageTransition())); |
142 | 141 |
143 // Create the ByteStream for sending data to the download sink. | 142 // Create the ByteStream for sending data to the download sink. |
144 scoped_ptr<ByteStreamReader> stream_reader; | 143 scoped_ptr<ByteStreamReader> stream_reader; |
145 CreateByteStream( | 144 CreateByteStream( |
146 base::MessageLoopProxy::current(), | 145 base::MessageLoopProxy::current(), |
147 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | 146 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), |
148 kDownloadByteStreamSize, &stream_writer_, &stream_reader); | 147 kDownloadByteStreamSize, &stream_writer_, &stream_reader); |
149 stream_writer_->RegisterCallback( | 148 stream_writer_->RegisterCallback( |
150 base::Bind(&DownloadResourceHandler::ResumeRequest, AsWeakPtr())); | 149 base::Bind(&DownloadResourceHandler::ResumeRequest, AsWeakPtr())); |
151 | 150 |
152 info->download_id = download_id_; | 151 info->download_id = download_id_; |
153 info->url_chain = request_->url_chain(); | 152 info->url_chain = request()->url_chain(); |
154 info->referrer_url = GURL(request_->referrer()); | 153 info->referrer_url = GURL(request()->referrer()); |
155 info->start_time = base::Time::Now(); | 154 info->start_time = base::Time::Now(); |
156 info->total_bytes = content_length_; | 155 info->total_bytes = content_length_; |
157 info->has_user_gesture = request_info->HasUserGesture(); | 156 info->has_user_gesture = request_info->HasUserGesture(); |
158 info->content_disposition = content_disposition_; | 157 info->content_disposition = content_disposition_; |
159 info->mime_type = response->head.mime_type; | 158 info->mime_type = response->head.mime_type; |
160 info->remote_address = request_->GetSocketAddress().host(); | 159 info->remote_address = request()->GetSocketAddress().host(); |
161 RecordDownloadMimeType(info->mime_type); | 160 RecordDownloadMimeType(info->mime_type); |
162 RecordDownloadContentDisposition(info->content_disposition); | 161 RecordDownloadContentDisposition(info->content_disposition); |
163 | 162 |
164 info->request_handle = | 163 info->request_handle = |
165 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(), | 164 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(), |
166 request_info->GetRouteID(), | 165 request_info->GetRouteID(), |
167 request_info->GetRequestID()); | 166 request_info->GetRequestID()); |
168 | 167 |
169 // Get the last modified time and etag. | 168 // Get the last modified time and etag. |
170 const net::HttpResponseHeaders* headers = request_->response_headers(); | 169 const net::HttpResponseHeaders* headers = request()->response_headers(); |
171 if (headers) { | 170 if (headers) { |
172 std::string last_modified_hdr; | 171 std::string last_modified_hdr; |
173 if (headers->EnumerateHeader(NULL, "Last-Modified", &last_modified_hdr)) | 172 if (headers->EnumerateHeader(NULL, "Last-Modified", &last_modified_hdr)) |
174 info->last_modified = last_modified_hdr; | 173 info->last_modified = last_modified_hdr; |
175 if (headers->EnumerateHeader(NULL, "ETag", &etag_)) | 174 if (headers->EnumerateHeader(NULL, "ETag", &etag_)) |
176 info->etag = etag_; | 175 info->etag = etag_; |
177 | 176 |
178 int status = headers->response_code(); | 177 int status = headers->response_code(); |
179 if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) { | 178 if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) { |
180 // Success & not range response; if we asked for a range, we didn't | 179 // Success & not range response; if we asked for a range, we didn't |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 } | 224 } |
226 | 225 |
227 bool DownloadResourceHandler::OnWillStart(int request_id, | 226 bool DownloadResourceHandler::OnWillStart(int request_id, |
228 const GURL& url, | 227 const GURL& url, |
229 bool* defer) { | 228 bool* defer) { |
230 return true; | 229 return true; |
231 } | 230 } |
232 | 231 |
233 // Create a new buffer, which will be handed to the download thread for file | 232 // Create a new buffer, which will be handed to the download thread for file |
234 // writing and deletion. | 233 // writing and deletion. |
235 bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | 234 bool DownloadResourceHandler::OnWillRead(int request_id, |
236 int* buf_size, int min_size) { | 235 scoped_refptr<net::IOBuffer>* buf, |
| 236 int* buf_size, |
| 237 int min_size) { |
237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
238 DCHECK(buf && buf_size); | 239 DCHECK(buf && buf_size); |
239 DCHECK(!read_buffer_.get()); | 240 DCHECK(!read_buffer_.get()); |
240 | 241 |
241 *buf_size = min_size < 0 ? kReadBufSize : min_size; | 242 *buf_size = min_size < 0 ? kReadBufSize : min_size; |
242 last_buffer_size_ = *buf_size; | 243 last_buffer_size_ = *buf_size; |
243 read_buffer_ = new net::IOBuffer(*buf_size); | 244 read_buffer_ = new net::IOBuffer(*buf_size); |
244 *buf = read_buffer_.get(); | 245 *buf = read_buffer_.get(); |
245 return true; | 246 return true; |
246 } | 247 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 *defer = was_deferred_ = true; | 285 *defer = was_deferred_ = true; |
285 | 286 |
286 return true; | 287 return true; |
287 } | 288 } |
288 | 289 |
289 bool DownloadResourceHandler::OnResponseCompleted( | 290 bool DownloadResourceHandler::OnResponseCompleted( |
290 int request_id, | 291 int request_id, |
291 const net::URLRequestStatus& status, | 292 const net::URLRequestStatus& status, |
292 const std::string& security_info) { | 293 const std::string& security_info) { |
293 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
294 int response_code = status.is_success() ? request_->GetResponseCode() : 0; | 295 int response_code = status.is_success() ? request()->GetResponseCode() : 0; |
295 VLOG(20) << __FUNCTION__ << "()" << DebugString() | 296 VLOG(20) << __FUNCTION__ << "()" << DebugString() |
296 << " request_id = " << request_id | 297 << " request_id = " << request_id |
297 << " status.status() = " << status.status() | 298 << " status.status() = " << status.status() |
298 << " status.error() = " << status.error() | 299 << " status.error() = " << status.error() |
299 << " response_code = " << response_code; | 300 << " response_code = " << response_code; |
300 | 301 |
301 net::Error error_code = net::OK; | 302 net::Error error_code = net::OK; |
302 if (status.status() == net::URLRequestStatus::FAILED || | 303 if (status.status() == net::URLRequestStatus::FAILED || |
303 // Note cancels as failures too. | 304 // Note cancels as failures too. |
304 status.status() == net::URLRequestStatus::CANCELED) { | 305 status.status() == net::URLRequestStatus::CANCELED) { |
(...skipping 23 matching lines...) Expand all Loading... |
328 // the tab from which it came). We map this to USER_CANCELLED as the | 329 // the tab from which it came). We map this to USER_CANCELLED as the |
329 // case we know about (system suspend because of laptop close) corresponds | 330 // case we know about (system suspend because of laptop close) corresponds |
330 // to a user action. | 331 // to a user action. |
331 // TODO(ahendrickson) -- Find a better set of codes to use here, as | 332 // TODO(ahendrickson) -- Find a better set of codes to use here, as |
332 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel. | 333 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel. |
333 reason = DOWNLOAD_INTERRUPT_REASON_USER_CANCELED; | 334 reason = DOWNLOAD_INTERRUPT_REASON_USER_CANCELED; |
334 } | 335 } |
335 | 336 |
336 if (status.is_success() && | 337 if (status.is_success() && |
337 reason == DOWNLOAD_INTERRUPT_REASON_NONE && | 338 reason == DOWNLOAD_INTERRUPT_REASON_NONE && |
338 request_->response_headers()) { | 339 request()->response_headers()) { |
339 // Handle server's response codes. | 340 // Handle server's response codes. |
340 switch(response_code) { | 341 switch(response_code) { |
341 case -1: // Non-HTTP request. | 342 case -1: // Non-HTTP request. |
342 case net::HTTP_OK: | 343 case net::HTTP_OK: |
343 case net::HTTP_CREATED: | 344 case net::HTTP_CREATED: |
344 case net::HTTP_ACCEPTED: | 345 case net::HTTP_ACCEPTED: |
345 case net::HTTP_NON_AUTHORITATIVE_INFORMATION: | 346 case net::HTTP_NON_AUTHORITATIVE_INFORMATION: |
346 case net::HTTP_RESET_CONTENT: | 347 case net::HTTP_RESET_CONTENT: |
347 case net::HTTP_PARTIAL_CONTENT: | 348 case net::HTTP_PARTIAL_CONTENT: |
348 // Expected successful codes. | 349 // Expected successful codes. |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 total_pause_time_ += (base::TimeTicks::Now() - last_stream_pause_time_); | 441 total_pause_time_ += (base::TimeTicks::Now() - last_stream_pause_time_); |
441 last_stream_pause_time_ = base::TimeTicks(); | 442 last_stream_pause_time_ = base::TimeTicks(); |
442 } | 443 } |
443 | 444 |
444 controller()->Resume(); | 445 controller()->Resume(); |
445 } | 446 } |
446 | 447 |
447 void DownloadResourceHandler::CancelRequest() { | 448 void DownloadResourceHandler::CancelRequest() { |
448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 449 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
449 | 450 |
450 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); | 451 const ResourceRequestInfo* info = GetRequestInfo(); |
451 ResourceDispatcherHostImpl::Get()->CancelRequest( | 452 ResourceDispatcherHostImpl::Get()->CancelRequest( |
452 info->GetChildID(), | 453 info->GetChildID(), |
453 info->GetRequestID(), | 454 info->GetRequestID(), |
454 false); | 455 false); |
455 } | 456 } |
456 | 457 |
457 std::string DownloadResourceHandler::DebugString() const { | 458 std::string DownloadResourceHandler::DebugString() const { |
458 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); | 459 const ResourceRequestInfo* info = GetRequestInfo(); |
459 return base::StringPrintf("{" | 460 return base::StringPrintf("{" |
460 " url_ = " "\"%s\"" | 461 " url_ = " "\"%s\"" |
461 " info = {" | 462 " info = {" |
462 " child_id = " "%d" | 463 " child_id = " "%d" |
463 " request_id = " "%d" | 464 " request_id = " "%d" |
464 " route_id = " "%d" | 465 " route_id = " "%d" |
465 " }" | 466 " }" |
466 " }", | 467 " }", |
467 request_ ? | 468 request() ? |
468 request_->url().spec().c_str() : | 469 request()->url().spec().c_str() : |
469 "<NULL request>", | 470 "<NULL request>", |
470 info->GetChildID(), | 471 info->GetChildID(), |
471 info->GetRequestID(), | 472 info->GetRequestID(), |
472 info->GetRouteID()); | 473 info->GetRouteID()); |
473 } | 474 } |
474 | 475 |
475 DownloadResourceHandler::~DownloadResourceHandler() { | 476 DownloadResourceHandler::~DownloadResourceHandler() { |
476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
477 | 478 |
478 // This won't do anything if the callback was called before. | 479 // This won't do anything if the callback was called before. |
479 // If it goes through, it will likely be because OnWillStart() returned | 480 // If it goes through, it will likely be because OnWillStart() returned |
480 // false somewhere in the chain of resource handlers. | 481 // false somewhere in the chain of resource handlers. |
481 CallStartedCB(NULL, net::ERR_ACCESS_DENIED); | 482 CallStartedCB(NULL, net::ERR_ACCESS_DENIED); |
482 | 483 |
483 // Remove output stream callback if a stream exists. | 484 // Remove output stream callback if a stream exists. |
484 if (stream_writer_) | 485 if (stream_writer_) |
485 stream_writer_->RegisterCallback(base::Closure()); | 486 stream_writer_->RegisterCallback(base::Closure()); |
486 | 487 |
487 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", | 488 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", |
488 base::TimeTicks::Now() - download_start_time_); | 489 base::TimeTicks::Now() - download_start_time_); |
489 } | 490 } |
490 | 491 |
491 } // namespace content | 492 } // namespace content |
OLD | NEW |