| 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_request_core.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/metrics/sparse_histogram.h" | 13 #include "base/metrics/sparse_histogram.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "content/browser/byte_stream.h" | 17 #include "content/browser/byte_stream.h" |
| 18 #include "content/browser/download/download_create_info.h" | 18 #include "content/browser/download/download_create_info.h" |
| 19 #include "content/browser/download/download_interrupt_reasons_impl.h" | 19 #include "content/browser/download/download_interrupt_reasons_impl.h" |
| 20 #include "content/browser/download/download_manager_impl.h" | 20 #include "content/browser/download/download_manager_impl.h" |
| 21 #include "content/browser/download/download_request_handle.h" | |
| 22 #include "content/browser/download/download_stats.h" | 21 #include "content/browser/download/download_stats.h" |
| 23 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
| 24 #include "content/browser/loader/resource_request_info_impl.h" | |
| 25 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/download_interrupt_reasons.h" | 23 #include "content/public/browser/download_interrupt_reasons.h" |
| 27 #include "content/public/browser/download_item.h" | 24 #include "content/public/browser/download_item.h" |
| 28 #include "content/public/browser/download_manager_delegate.h" | 25 #include "content/public/browser/download_manager_delegate.h" |
| 29 #include "content/public/browser/navigation_entry.h" | 26 #include "content/public/browser/navigation_entry.h" |
| 30 #include "content/public/browser/power_save_blocker.h" | 27 #include "content/public/browser/power_save_blocker.h" |
| 31 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/common/resource_response.h" | |
| 33 #include "net/base/io_buffer.h" | 29 #include "net/base/io_buffer.h" |
| 34 #include "net/base/net_errors.h" | 30 #include "net/base/net_errors.h" |
| 35 #include "net/http/http_response_headers.h" | 31 #include "net/http/http_response_headers.h" |
| 36 #include "net/http/http_status_code.h" | 32 #include "net/http/http_status_code.h" |
| 37 #include "net/url_request/url_request_context.h" | 33 #include "net/url_request/url_request_context.h" |
| 38 | 34 |
| 39 namespace content { | 35 namespace content { |
| 40 | 36 |
| 41 struct DownloadResourceHandler::DownloadTabInfo { | |
| 42 GURL tab_url; | |
| 43 GURL tab_referrer_url; | |
| 44 }; | |
| 45 | |
| 46 namespace { | 37 namespace { |
| 47 | 38 |
| 48 void CallStartedCBOnUIThread( | 39 void CallStartedCBOnUIThread( |
| 49 const DownloadUrlParameters::OnStartedCallback& started_cb, | 40 const DownloadUrlParameters::OnStartedCallback& started_cb, |
| 50 DownloadItem* item, | 41 DownloadItem* item, |
| 51 DownloadInterruptReason interrupt_reason) { | 42 DownloadInterruptReason interrupt_reason) { |
| 52 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 43 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 53 | 44 |
| 54 if (started_cb.is_null()) | 45 if (started_cb.is_null()) |
| 55 return; | 46 return; |
| 56 started_cb.Run(item, interrupt_reason); | 47 started_cb.Run(item, interrupt_reason); |
| 57 } | 48 } |
| 58 | 49 |
| 59 // Static function in order to prevent any accidental accesses to | 50 // Static function in order to prevent any accidental accesses to |
| 60 // DownloadResourceHandler members from the UI thread. | 51 // DownloadRequestCore members from the UI thread. |
| 61 static void StartOnUIThread( | 52 static void StartOnUIThread( |
| 62 scoped_ptr<DownloadCreateInfo> info, | 53 scoped_ptr<DownloadCreateInfo> info, |
| 63 scoped_ptr<DownloadResourceHandler::DownloadTabInfo> tab_info, | |
| 64 scoped_ptr<ByteStreamReader> stream, | 54 scoped_ptr<ByteStreamReader> stream, |
| 55 base::WeakPtr<DownloadManager> download_manager, |
| 65 const DownloadUrlParameters::OnStartedCallback& started_cb) { | 56 const DownloadUrlParameters::OnStartedCallback& started_cb) { |
| 66 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 67 | 58 |
| 68 DownloadManager* download_manager = info->request_handle.GetDownloadManager(); | |
| 69 if (!download_manager) { | 59 if (!download_manager) { |
| 70 // NULL in unittests or if the page closed right after starting the | 60 // nullptr in unittests or if the page closed right after starting the |
| 71 // download. | 61 // download. |
| 72 if (!started_cb.is_null()) | 62 if (!started_cb.is_null()) |
| 73 started_cb.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); | 63 started_cb.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); |
| 74 | 64 |
| 75 // |stream| gets deleted on non-FILE thread, but it's ok since | 65 // |stream| gets deleted on non-FILE thread, but it's ok since |
| 76 // we're not using stream_writer_ yet. | 66 // we're not using stream_writer_ yet. |
| 77 | 67 |
| 78 return; | 68 return; |
| 79 } | 69 } |
| 80 | 70 |
| 81 info->tab_url = tab_info->tab_url; | |
| 82 info->tab_referrer_url = tab_info->tab_referrer_url; | |
| 83 | |
| 84 download_manager->StartDownload(info.Pass(), stream.Pass(), started_cb); | 71 download_manager->StartDownload(info.Pass(), stream.Pass(), started_cb); |
| 85 } | 72 } |
| 86 | 73 |
| 87 void InitializeDownloadTabInfoOnUIThread( | |
| 88 const DownloadRequestHandle& request_handle, | |
| 89 DownloadResourceHandler::DownloadTabInfo* tab_info) { | |
| 90 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 91 | |
| 92 WebContents* web_contents = request_handle.GetWebContents(); | |
| 93 if (web_contents) { | |
| 94 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); | |
| 95 if (entry) { | |
| 96 tab_info->tab_url = entry->GetURL(); | |
| 97 tab_info->tab_referrer_url = entry->GetReferrer().url; | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void DeleteOnUIThread( | |
| 103 scoped_ptr<DownloadResourceHandler::DownloadTabInfo> tab_info) {} | |
| 104 | |
| 105 } // namespace | 74 } // namespace |
| 106 | 75 |
| 107 const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024; | 76 const int DownloadRequestCore::kDownloadByteStreamSize = 100 * 1024; |
| 108 | 77 |
| 109 DownloadResourceHandler::DownloadResourceHandler( | 78 DownloadRequestCore::DownloadRequestCore( |
| 110 uint32 id, | 79 uint32 id, |
| 111 net::URLRequest* request, | 80 net::URLRequest* request, |
| 112 const DownloadUrlParameters::OnStartedCallback& started_cb, | 81 const DownloadUrlParameters::OnStartedCallback& started_cb, |
| 113 scoped_ptr<DownloadSaveInfo> save_info) | 82 scoped_ptr<DownloadSaveInfo> save_info, |
| 114 : ResourceHandler(request), | 83 base::WeakPtr<DownloadManagerImpl> download_manager) |
| 84 : request_(request), |
| 115 download_id_(id), | 85 download_id_(id), |
| 116 started_cb_(started_cb), | 86 started_cb_(started_cb), |
| 117 save_info_(save_info.Pass()), | 87 save_info_(save_info.Pass()), |
| 118 tab_info_(new DownloadTabInfo()), | |
| 119 last_buffer_size_(0), | 88 last_buffer_size_(0), |
| 120 bytes_read_(0), | 89 bytes_read_(0), |
| 121 pause_count_(0), | 90 pause_count_(0), |
| 122 was_deferred_(false), | 91 was_deferred_(false), |
| 123 on_response_started_called_(false) { | 92 on_response_started_called_(false), |
| 93 download_manager_(download_manager) { |
| 124 RecordDownloadCount(UNTHROTTLED_COUNT); | 94 RecordDownloadCount(UNTHROTTLED_COUNT); |
| 125 | 95 |
| 126 // Do UI thread initialization for tab_info_ asap after | |
| 127 // DownloadResourceHandler creation since the tab could be navigated | |
| 128 // before StartOnUIThread gets called. This is safe because deletion | |
| 129 // will occur via PostTask() as well, which will serialized behind this | |
| 130 // PostTask() | |
| 131 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); | |
| 132 BrowserThread::PostTask( | |
| 133 BrowserThread::UI, FROM_HERE, | |
| 134 base::Bind(&InitializeDownloadTabInfoOnUIThread, | |
| 135 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(), | |
| 136 request_info->GetRouteID(), | |
| 137 request_info->GetRequestID(), | |
| 138 request_info->frame_tree_node_id()), | |
| 139 tab_info_.get())); | |
| 140 power_save_blocker_ = PowerSaveBlocker::Create( | 96 power_save_blocker_ = PowerSaveBlocker::Create( |
| 141 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | 97 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| 142 PowerSaveBlocker::kReasonOther, "Download in progress"); | 98 PowerSaveBlocker::kReasonOther, "Download in progress"); |
| 143 } | 99 } |
| 144 | 100 |
| 145 bool DownloadResourceHandler::OnRequestRedirected( | 101 DownloadRequestCore::~DownloadRequestCore() { |
| 146 const net::RedirectInfo& redirect_info, | 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 147 ResourceResponse* response, | 103 |
| 148 bool* defer) { | 104 // This won't do anything if the callback was called before. |
| 149 return true; | 105 // If it goes through, it will likely be because OnWillStart() returned |
| 106 // false somewhere in the chain of resource handlers. |
| 107 CallStartedCB(nullptr, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); |
| 108 |
| 109 // Remove output stream callback if a stream exists. |
| 110 if (stream_writer_) |
| 111 stream_writer_->RegisterCallback(base::Closure()); |
| 112 |
| 113 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", |
| 114 base::TimeTicks::Now() - download_start_time_); |
| 150 } | 115 } |
| 151 | 116 |
| 152 // Send the download creation information to the download thread. | 117 // Send the download creation information to the download thread. |
| 153 bool DownloadResourceHandler::OnResponseStarted( | 118 bool DownloadRequestCore::OnResponseStarted() { |
| 154 ResourceResponse* response, | |
| 155 bool* defer) { | |
| 156 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 157 // There can be only one (call) | 120 // There can be only one (call) |
| 158 DCHECK(!on_response_started_called_); | 121 DCHECK(!on_response_started_called_); |
| 159 on_response_started_called_ = true; | 122 on_response_started_called_ = true; |
| 160 | 123 |
| 161 DVLOG(20) << __FUNCTION__ << "()" << DebugString(); | 124 DVLOG(20) << __FUNCTION__ << "()" << DebugString(); |
| 162 download_start_time_ = base::TimeTicks::Now(); | 125 download_start_time_ = base::TimeTicks::Now(); |
| 163 | 126 |
| 164 // If it's a download, we don't want to poison the cache with it. | 127 // If it's a download, we don't want to poison the cache with it. |
| 165 request()->StopCaching(); | 128 request()->StopCaching(); |
| 166 | 129 |
| 167 // Lower priority as well, so downloads don't contend for resources | 130 // Lower priority as well, so downloads don't contend for resources |
| 168 // with main frames. | 131 // with main frames. |
| 169 request()->SetPriority(net::IDLE); | 132 request()->SetPriority(net::IDLE); |
| 170 | 133 |
| 171 // If the content-length header is not present (or contains something other | 134 // If the content-length header is not present (or contains something other |
| 172 // than numbers), the incoming content_length is -1 (unknown size). | 135 // than numbers), the incoming content_length is -1 (unknown size). |
| 173 // Set the content length to 0 to indicate unknown size to DownloadManager. | 136 // Set the content length to 0 to indicate unknown size to DownloadManager. |
| 174 int64 content_length = | 137 int64 content_length = request()->GetExpectedContentSize() > 0 |
| 175 response->head.content_length > 0 ? response->head.content_length : 0; | 138 ? request()->GetExpectedContentSize() |
| 176 | 139 : 0; |
| 177 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); | |
| 178 | 140 |
| 179 // Deleted in DownloadManager. | 141 // Deleted in DownloadManager. |
| 180 scoped_ptr<DownloadCreateInfo> info( | 142 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo( |
| 181 new DownloadCreateInfo(base::Time::Now(), | 143 base::Time::Now(), content_length, request()->net_log(), false, |
| 182 content_length, | 144 ui::PAGE_TRANSITION_LINK, save_info_.Pass())); |
| 183 request()->net_log(), | |
| 184 request_info->HasUserGesture(), | |
| 185 request_info->GetPageTransition(), | |
| 186 save_info_.Pass())); | |
| 187 | 145 |
| 188 // Create the ByteStream for sending data to the download sink. | 146 // Create the ByteStream for sending data to the download sink. |
| 189 scoped_ptr<ByteStreamReader> stream_reader; | 147 scoped_ptr<ByteStreamReader> stream_reader; |
| 190 CreateByteStream( | 148 CreateByteStream( |
| 191 base::ThreadTaskRunnerHandle::Get(), | 149 base::ThreadTaskRunnerHandle::Get(), |
| 192 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | 150 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), |
| 193 kDownloadByteStreamSize, &stream_writer_, &stream_reader); | 151 kDownloadByteStreamSize, &stream_writer_, &stream_reader); |
| 194 stream_writer_->RegisterCallback( | 152 stream_writer_->RegisterCallback( |
| 195 base::Bind(&DownloadResourceHandler::ResumeRequest, AsWeakPtr())); | 153 base::Bind(&DownloadRequestCore::ResumeRequest, AsWeakPtr())); |
| 196 | 154 |
| 197 info->download_id = download_id_; | 155 info->download_id = download_id_; |
| 198 info->url_chain = request()->url_chain(); | 156 info->url_chain = request()->url_chain(); |
| 199 info->referrer_url = GURL(request()->referrer()); | 157 info->referrer_url = GURL(request()->referrer()); |
| 200 info->mime_type = response->head.mime_type; | 158 string mime_type; |
| 159 request()->GetMimeType(&mime_type); |
| 160 info->mime_type = mime_type; |
| 201 info->remote_address = request()->GetSocketAddress().host(); | 161 info->remote_address = request()->GetSocketAddress().host(); |
| 202 if (request()->response_headers()) { | 162 if (request()->response_headers()) { |
| 203 // Grab the first content-disposition header. There may be more than one, | 163 // Grab the first content-disposition header. There may be more than one, |
| 204 // though as of this writing, the network stack ensures if there are, they | 164 // though as of this writing, the network stack ensures if there are, they |
| 205 // are all duplicates. | 165 // are all duplicates. |
| 206 request()->response_headers()->EnumerateHeader( | 166 request()->response_headers()->EnumerateHeader( |
| 207 nullptr, "content-disposition", &info->content_disposition); | 167 nullptr, "content-disposition", &info->content_disposition); |
| 208 } | 168 } |
| 209 RecordDownloadMimeType(info->mime_type); | 169 RecordDownloadMimeType(info->mime_type); |
| 210 RecordDownloadContentDisposition(info->content_disposition); | 170 RecordDownloadContentDisposition(info->content_disposition); |
| 211 | 171 |
| 212 info->request_handle = DownloadRequestHandle( | |
| 213 AsWeakPtr(), request_info->GetChildID(), request_info->GetRouteID(), | |
| 214 request_info->GetRequestID(), request_info->frame_tree_node_id()); | |
| 215 | |
| 216 // Get the last modified time and etag. | 172 // Get the last modified time and etag. |
| 217 const net::HttpResponseHeaders* headers = request()->response_headers(); | 173 const net::HttpResponseHeaders* headers = request()->response_headers(); |
| 218 if (headers) { | 174 if (headers) { |
| 219 if (headers->HasStrongValidators()) { | 175 if (headers->HasStrongValidators()) { |
| 220 // If we don't have strong validators as per RFC 2616 section 13.3.3, then | 176 // If we don't have strong validators as per RFC 2616 section 13.3.3, then |
| 221 // we neither store nor use them for range requests. | 177 // we neither store nor use them for range requests. |
| 222 if (!headers->EnumerateHeader(NULL, "Last-Modified", | 178 if (!headers->EnumerateHeader(nullptr, "Last-Modified", |
| 223 &info->last_modified)) | 179 &info->last_modified)) |
| 224 info->last_modified.clear(); | 180 info->last_modified.clear(); |
| 225 if (!headers->EnumerateHeader(NULL, "ETag", &info->etag)) | 181 if (!headers->EnumerateHeader(nullptr, "ETag", &info->etag)) |
| 226 info->etag.clear(); | 182 info->etag.clear(); |
| 227 } | 183 } |
| 228 | 184 |
| 229 int status = headers->response_code(); | 185 int status = headers->response_code(); |
| 230 if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) { | 186 if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) { |
| 231 // Success & not range response; if we asked for a range, we didn't | 187 // Success & not range response; if we asked for a range, we didn't |
| 232 // get it--reset the file pointers to reflect that. | 188 // get it--reset the file pointers to reflect that. |
| 233 info->save_info->offset = 0; | 189 info->save_info->offset = 0; |
| 234 info->save_info->hash_state = ""; | 190 info->save_info->hash_state = ""; |
| 235 } | 191 } |
| 236 | 192 |
| 237 if (!headers->GetMimeType(&info->original_mime_type)) | 193 if (!headers->GetMimeType(&info->original_mime_type)) |
| 238 info->original_mime_type.clear(); | 194 info->original_mime_type.clear(); |
| 239 } | 195 } |
| 240 | 196 |
| 241 // Blink verifies that the requester of this download is allowed to set a | 197 // Blink verifies that the requester of this download is allowed to set a |
| 242 // suggested name for the security origin of the downlaod URL. However, this | 198 // suggested name for the security origin of the downlaod URL. However, this |
| 243 // assumption doesn't hold if there were cross origin redirects. Therefore, | 199 // assumption doesn't hold if there were cross origin redirects. Therefore, |
| 244 // clear the suggested_name for such requests. | 200 // clear the suggested_name for such requests. |
| 245 if (info->url_chain.size() > 1 && | 201 if (info->url_chain.size() > 1 && |
| 246 info->url_chain.front().GetOrigin() != info->url_chain.back().GetOrigin()) | 202 info->url_chain.front().GetOrigin() != info->url_chain.back().GetOrigin()) |
| 247 info->save_info->suggested_name.clear(); | 203 info->save_info->suggested_name.clear(); |
| 248 | 204 |
| 249 BrowserThread::PostTask( | 205 BrowserThread::PostTask( |
| 250 BrowserThread::UI, FROM_HERE, | 206 BrowserThread::UI, FROM_HERE, |
| 251 base::Bind(&StartOnUIThread, | 207 base::Bind(&StartOnUIThread, base::Passed(&info), |
| 252 base::Passed(&info), | 208 base::Passed(&stream_reader), download_manager_, |
| 253 base::Passed(&tab_info_), | |
| 254 base::Passed(&stream_reader), | |
| 255 // Pass to StartOnUIThread so that variable | 209 // Pass to StartOnUIThread so that variable |
| 256 // access is always on IO thread but function | 210 // access is always on IO thread but function |
| 257 // is called on UI thread. | 211 // is called on UI thread. |
| 258 started_cb_)); | 212 started_cb_)); |
| 259 // Guaranteed to be called in StartOnUIThread | 213 // Guaranteed to be called in StartOnUIThread |
| 260 started_cb_.Reset(); | 214 started_cb_.Reset(); |
| 261 | 215 |
| 262 return true; | 216 return true; |
| 263 } | 217 } |
| 264 | 218 |
| 265 void DownloadResourceHandler::CallStartedCB( | 219 void DownloadRequestCore::CallStartedCB( |
| 266 DownloadItem* item, | 220 DownloadItem* item, |
| 267 DownloadInterruptReason interrupt_reason) { | 221 DownloadInterruptReason interrupt_reason) { |
| 268 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 222 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 269 if (started_cb_.is_null()) | 223 if (started_cb_.is_null()) |
| 270 return; | 224 return; |
| 271 BrowserThread::PostTask( | 225 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 272 BrowserThread::UI, | 226 base::Bind(&CallStartedCBOnUIThread, started_cb_, |
| 273 FROM_HERE, | 227 item, interrupt_reason)); |
| 274 base::Bind( | |
| 275 &CallStartedCBOnUIThread, started_cb_, item, interrupt_reason)); | |
| 276 started_cb_.Reset(); | 228 started_cb_.Reset(); |
| 277 } | 229 } |
| 278 | 230 |
| 279 bool DownloadResourceHandler::OnWillStart(const GURL& url, bool* defer) { | |
| 280 return true; | |
| 281 } | |
| 282 | |
| 283 bool DownloadResourceHandler::OnBeforeNetworkStart(const GURL& url, | |
| 284 bool* defer) { | |
| 285 return true; | |
| 286 } | |
| 287 | |
| 288 // Create a new buffer, which will be handed to the download thread for file | 231 // Create a new buffer, which will be handed to the download thread for file |
| 289 // writing and deletion. | 232 // writing and deletion. |
| 290 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 233 bool DownloadRequestCore::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 291 int* buf_size, | 234 int* buf_size, |
| 292 int min_size) { | 235 int min_size) { |
| 293 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 236 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 294 DCHECK(buf && buf_size); | 237 DCHECK(buf && buf_size); |
| 295 DCHECK(!read_buffer_.get()); | 238 DCHECK(!read_buffer_.get()); |
| 296 | 239 |
| 297 *buf_size = min_size < 0 ? kReadBufSize : min_size; | 240 *buf_size = min_size < 0 ? kReadBufSize : min_size; |
| 298 last_buffer_size_ = *buf_size; | 241 last_buffer_size_ = *buf_size; |
| 299 read_buffer_ = new net::IOBuffer(*buf_size); | 242 read_buffer_ = new net::IOBuffer(*buf_size); |
| 300 *buf = read_buffer_.get(); | 243 *buf = read_buffer_.get(); |
| 301 return true; | 244 return true; |
| 302 } | 245 } |
| 303 | 246 |
| 304 // Pass the buffer to the download file writer. | 247 // Pass the buffer to the download file writer. |
| 305 bool DownloadResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 248 bool DownloadRequestCore::OnReadCompleted(int bytes_read, bool* defer) { |
| 306 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 249 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 307 DCHECK(read_buffer_.get()); | 250 DCHECK(read_buffer_.get()); |
| 308 | 251 |
| 309 base::TimeTicks now(base::TimeTicks::Now()); | 252 base::TimeTicks now(base::TimeTicks::Now()); |
| 310 if (!last_read_time_.is_null()) { | 253 if (!last_read_time_.is_null()) { |
| 311 double seconds_since_last_read = (now - last_read_time_).InSecondsF(); | 254 double seconds_since_last_read = (now - last_read_time_).InSecondsF(); |
| 312 if (now == last_read_time_) | 255 if (now == last_read_time_) |
| 313 // Use 1/10 ms as a "very small number" so that we avoid | 256 // Use 1/10 ms as a "very small number" so that we avoid |
| 314 // divide-by-zero error and still record a very high potential bandwidth. | 257 // divide-by-zero error and still record a very high potential bandwidth. |
| 315 seconds_since_last_read = 0.00001; | 258 seconds_since_last_read = 0.00001; |
| 316 | 259 |
| 317 double actual_bandwidth = (bytes_read)/seconds_since_last_read; | 260 double actual_bandwidth = (bytes_read) / seconds_since_last_read; |
| 318 double potential_bandwidth = last_buffer_size_/seconds_since_last_read; | 261 double potential_bandwidth = last_buffer_size_ / seconds_since_last_read; |
| 319 RecordBandwidth(actual_bandwidth, potential_bandwidth); | 262 RecordBandwidth(actual_bandwidth, potential_bandwidth); |
| 320 } | 263 } |
| 321 last_read_time_ = now; | 264 last_read_time_ = now; |
| 322 | 265 |
| 323 if (!bytes_read) | 266 if (!bytes_read) |
| 324 return true; | 267 return true; |
| 325 bytes_read_ += bytes_read; | 268 bytes_read_ += bytes_read; |
| 326 DCHECK(read_buffer_.get()); | 269 DCHECK(read_buffer_.get()); |
| 327 | 270 |
| 328 // Take the data ship it down the stream. If the stream is full, pause the | 271 // Take the data ship it down the stream. If the stream is full, pause the |
| 329 // request; the stream callback will resume it. | 272 // request; the stream callback will resume it. |
| 330 if (!stream_writer_->Write(read_buffer_, bytes_read)) { | 273 if (!stream_writer_->Write(read_buffer_, bytes_read)) { |
| 331 PauseRequest(); | 274 PauseRequest(); |
| 332 *defer = was_deferred_ = true; | 275 *defer = was_deferred_ = true; |
| 333 last_stream_pause_time_ = now; | 276 last_stream_pause_time_ = now; |
| 334 } | 277 } |
| 335 | 278 |
| 336 read_buffer_ = NULL; // Drop our reference. | 279 read_buffer_ = NULL; // Drop our reference. |
| 337 | 280 |
| 338 if (pause_count_ > 0) | 281 if (pause_count_ > 0) |
| 339 *defer = was_deferred_ = true; | 282 *defer = was_deferred_ = true; |
| 340 | 283 |
| 341 return true; | 284 return true; |
| 342 } | 285 } |
| 343 | 286 |
| 344 void DownloadResourceHandler::OnResponseCompleted( | 287 void DownloadRequestCore::OnResponseCompleted( |
| 345 const net::URLRequestStatus& status, | 288 const net::URLRequestStatus& status) { |
| 346 const std::string& security_info, | |
| 347 bool* defer) { | |
| 348 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 289 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 349 int response_code = status.is_success() ? request()->GetResponseCode() : 0; | 290 int response_code = status.is_success() ? request()->GetResponseCode() : 0; |
| 350 DVLOG(20) << __FUNCTION__ << "()" << DebugString() | 291 DVLOG(20) << __FUNCTION__ << "()" << DebugString() |
| 351 << " status.status() = " << status.status() | 292 << " status.status() = " << status.status() |
| 352 << " status.error() = " << status.error() | 293 << " status.error() = " << status.error() |
| 353 << " response_code = " << response_code; | 294 << " response_code = " << response_code; |
| 354 | 295 |
| 355 net::Error error_code = net::OK; | 296 net::Error error_code = net::OK; |
| 356 if (status.status() == net::URLRequestStatus::FAILED || | 297 if (status.status() == net::URLRequestStatus::FAILED || |
| 357 // Note cancels as failures too. | 298 // Note cancels as failures too. |
| 358 status.status() == net::URLRequestStatus::CANCELED) { | 299 status.status() == net::URLRequestStatus::CANCELED) { |
| 359 error_code = static_cast<net::Error>(status.error()); // Normal case. | 300 error_code = static_cast<net::Error>(status.error()); // Normal case. |
| 360 // Make sure that at least the fact of failure comes through. | 301 // Make sure that at least the fact of failure comes through. |
| 361 if (error_code == net::OK) | 302 if (error_code == net::OK) |
| 362 error_code = net::ERR_FAILED; | 303 error_code = net::ERR_FAILED; |
| 363 } | 304 } |
| 364 | 305 |
| 365 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are | 306 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are |
| 366 // allowed since a number of servers in the wild close the connection too | 307 // allowed since a number of servers in the wild close the connection too |
| 367 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 - | 308 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 - |
| 368 // treat downloads as complete in both cases, so we follow their lead. | 309 // treat downloads as complete in both cases, so we follow their lead. |
| 369 if (error_code == net::ERR_CONTENT_LENGTH_MISMATCH || | 310 if (error_code == net::ERR_CONTENT_LENGTH_MISMATCH || |
| 370 error_code == net::ERR_INCOMPLETE_CHUNKED_ENCODING) { | 311 error_code == net::ERR_INCOMPLETE_CHUNKED_ENCODING) { |
| 371 error_code = net::OK; | 312 error_code = net::OK; |
| 372 } | 313 } |
| 373 DownloadInterruptReason reason = | 314 DownloadInterruptReason reason = ConvertNetErrorToInterruptReason( |
| 374 ConvertNetErrorToInterruptReason( | 315 error_code, DOWNLOAD_INTERRUPT_FROM_NETWORK); |
| 375 error_code, DOWNLOAD_INTERRUPT_FROM_NETWORK); | |
| 376 | 316 |
| 377 if (status.status() == net::URLRequestStatus::CANCELED && | 317 if (status.status() == net::URLRequestStatus::CANCELED && |
| 378 status.error() == net::ERR_ABORTED) { | 318 status.error() == net::ERR_ABORTED) { |
| 379 // CANCELED + ERR_ABORTED == something outside of the network | 319 // CANCELED + ERR_ABORTED == something outside of the network |
| 380 // stack cancelled the request. There aren't that many things that | 320 // stack cancelled the request. There aren't that many things that |
| 381 // could do this to a download request (whose lifetime is separated from | 321 // could do this to a download request (whose lifetime is separated from |
| 382 // the tab from which it came). We map this to USER_CANCELLED as the | 322 // the tab from which it came). We map this to USER_CANCELLED as the |
| 383 // case we know about (system suspend because of laptop close) corresponds | 323 // case we know about (system suspend because of laptop close) corresponds |
| 384 // to a user action. | 324 // to a user action. |
| 385 // TODO(ahendrickson) -- Find a better set of codes to use here, as | 325 // TODO(ahendrickson) -- Find a better set of codes to use here, as |
| 386 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel. | 326 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel. |
| 387 if (net::IsCertStatusError(request()->ssl_info().cert_status)) | 327 if (net::IsCertStatusError(request()->ssl_info().cert_status)) |
| 388 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM; | 328 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM; |
| 389 else | 329 else |
| 390 reason = DOWNLOAD_INTERRUPT_REASON_USER_CANCELED; | 330 reason = DOWNLOAD_INTERRUPT_REASON_USER_CANCELED; |
| 391 } | 331 } |
| 392 | 332 |
| 393 if (status.is_success() && | 333 if (status.is_success() && reason == DOWNLOAD_INTERRUPT_REASON_NONE && |
| 394 reason == DOWNLOAD_INTERRUPT_REASON_NONE && | |
| 395 request()->response_headers()) { | 334 request()->response_headers()) { |
| 396 // Handle server's response codes. | 335 // Handle server's response codes. |
| 397 switch(response_code) { | 336 switch (response_code) { |
| 398 case -1: // Non-HTTP request. | 337 case -1: // Non-HTTP request. |
| 399 case net::HTTP_OK: | 338 case net::HTTP_OK: |
| 400 case net::HTTP_CREATED: | 339 case net::HTTP_CREATED: |
| 401 case net::HTTP_ACCEPTED: | 340 case net::HTTP_ACCEPTED: |
| 402 case net::HTTP_NON_AUTHORITATIVE_INFORMATION: | 341 case net::HTTP_NON_AUTHORITATIVE_INFORMATION: |
| 403 case net::HTTP_RESET_CONTENT: | 342 case net::HTTP_RESET_CONTENT: |
| 404 case net::HTTP_PARTIAL_CONTENT: | 343 case net::HTTP_PARTIAL_CONTENT: |
| 405 // Expected successful codes. | 344 // Expected successful codes. |
| 406 break; | 345 break; |
| 407 case net::HTTP_NO_CONTENT: | 346 case net::HTTP_NO_CONTENT: |
| 408 case net::HTTP_NOT_FOUND: | 347 case net::HTTP_NOT_FOUND: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 419 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE; | 358 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE; |
| 420 break; | 359 break; |
| 421 case net::HTTP_UNAUTHORIZED: | 360 case net::HTTP_UNAUTHORIZED: |
| 422 // Server didn't authorize this request. | 361 // Server didn't authorize this request. |
| 423 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED; | 362 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED; |
| 424 break; | 363 break; |
| 425 case net::HTTP_FORBIDDEN: | 364 case net::HTTP_FORBIDDEN: |
| 426 // Server forbids access to this resource. | 365 // Server forbids access to this resource. |
| 427 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN; | 366 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN; |
| 428 break; | 367 break; |
| 429 default: // All other errors. | 368 default: // All other errors. |
| 430 // Redirection and informational codes should have been handled earlier | 369 // Redirection and informational codes should have been handled earlier |
| 431 // in the stack. | 370 // in the stack. |
| 432 DCHECK_NE(3, response_code / 100); | 371 DCHECK_NE(3, response_code / 100); |
| 433 DCHECK_NE(1, response_code / 100); | 372 DCHECK_NE(1, response_code / 100); |
| 434 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; | 373 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; |
| 435 break; | 374 break; |
| 436 } | 375 } |
| 437 } | 376 } |
| 438 | 377 |
| 439 std::string accept_ranges; | 378 std::string accept_ranges; |
| 440 bool has_strong_validators = false; | 379 bool has_strong_validators = false; |
| 441 if (request()->response_headers()) { | 380 if (request()->response_headers()) { |
| 442 request()->response_headers()->EnumerateHeader( | 381 request()->response_headers()->EnumerateHeader(nullptr, "Accept-Ranges", |
| 443 NULL, "Accept-Ranges", &accept_ranges); | 382 &accept_ranges); |
| 444 has_strong_validators = | 383 has_strong_validators = |
| 445 request()->response_headers()->HasStrongValidators(); | 384 request()->response_headers()->HasStrongValidators(); |
| 446 } | 385 } |
| 447 RecordAcceptsRanges(accept_ranges, bytes_read_, has_strong_validators); | 386 RecordAcceptsRanges(accept_ranges, bytes_read_, has_strong_validators); |
| 448 RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_, | 387 RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_, |
| 449 total_pause_time_); | 388 total_pause_time_); |
| 450 | 389 |
| 451 CallStartedCB(NULL, reason); | 390 CallStartedCB(nullptr, reason); |
| 452 | 391 |
| 453 // Send the info down the stream. Conditional is in case we get | 392 // Send the info down the stream. Conditional is in case we get |
| 454 // OnResponseCompleted without OnResponseStarted. | 393 // OnResponseCompleted without OnResponseStarted. |
| 455 if (stream_writer_) | 394 if (stream_writer_) |
| 456 stream_writer_->Close(reason); | 395 stream_writer_->Close(reason); |
| 457 | 396 |
| 458 // If the error mapped to something unknown, record it so that | 397 // If the error mapped to something unknown, record it so that |
| 459 // we can drill down. | 398 // we can drill down. |
| 460 if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) { | 399 if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) { |
| 461 UMA_HISTOGRAM_SPARSE_SLOWLY("Download.MapErrorNetworkFailed", | 400 UMA_HISTOGRAM_SPARSE_SLOWLY("Download.MapErrorNetworkFailed", |
| 462 std::abs(status.error())); | 401 std::abs(status.error())); |
| 463 } | 402 } |
| 464 | 403 |
| 465 stream_writer_.reset(); // We no longer need the stream. | 404 stream_writer_.reset(); // We no longer need the stream. |
| 466 read_buffer_ = NULL; | 405 read_buffer_ = nullptr; |
| 467 } | 406 } |
| 468 | 407 |
| 469 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 408 void DownloadRequestCore::PauseRequest() { |
| 470 NOTREACHED(); | |
| 471 } | |
| 472 | |
| 473 void DownloadResourceHandler::PauseRequest() { | |
| 474 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 409 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 475 | 410 |
| 476 ++pause_count_; | 411 ++pause_count_; |
| 477 } | 412 } |
| 478 | 413 |
| 479 void DownloadResourceHandler::ResumeRequest() { | 414 void DownloadRequestCore::ResumeRequest() { |
| 480 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 415 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 481 DCHECK_LT(0, pause_count_); | 416 DCHECK_LT(0, pause_count_); |
| 482 | 417 |
| 483 --pause_count_; | 418 --pause_count_; |
| 484 | 419 |
| 485 if (!was_deferred_) | 420 if (!was_deferred_) |
| 486 return; | 421 return; |
| 487 if (pause_count_ > 0) | 422 if (pause_count_ > 0) |
| 488 return; | 423 return; |
| 489 | 424 |
| 490 was_deferred_ = false; | 425 was_deferred_ = false; |
| 491 if (!last_stream_pause_time_.is_null()) { | 426 if (!last_stream_pause_time_.is_null()) { |
| 492 total_pause_time_ += (base::TimeTicks::Now() - last_stream_pause_time_); | 427 total_pause_time_ += (base::TimeTicks::Now() - last_stream_pause_time_); |
| 493 last_stream_pause_time_ = base::TimeTicks(); | 428 last_stream_pause_time_ = base::TimeTicks(); |
| 494 } | 429 } |
| 495 | 430 |
| 496 controller()->Resume(); | 431 downloader_->Resume(); |
| 497 } | 432 } |
| 498 | 433 |
| 499 void DownloadResourceHandler::CancelRequest() { | 434 std::string DownloadRequestCore::DebugString() const { |
| 500 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 435 return base::StringPrintf( |
| 501 | 436 "{" |
| 502 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 437 " url_ = " |
| 503 ResourceDispatcherHostImpl::Get()->CancelRequest( | 438 "\"%s\"" |
| 504 info->GetChildID(), | 439 " }", |
| 505 info->GetRequestID()); | 440 request() ? request()->url().spec().c_str() : "<NULL request>"); |
| 506 // This object has been deleted. | |
| 507 } | |
| 508 | |
| 509 std::string DownloadResourceHandler::DebugString() const { | |
| 510 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
| 511 return base::StringPrintf("{" | |
| 512 " url_ = " "\"%s\"" | |
| 513 " info = {" | |
| 514 " child_id = " "%d" | |
| 515 " request_id = " "%d" | |
| 516 " route_id = " "%d" | |
| 517 " }" | |
| 518 " }", | |
| 519 request() ? | |
| 520 request()->url().spec().c_str() : | |
| 521 "<NULL request>", | |
| 522 info->GetChildID(), | |
| 523 info->GetRequestID(), | |
| 524 info->GetRouteID()); | |
| 525 } | |
| 526 | |
| 527 DownloadResourceHandler::~DownloadResourceHandler() { | |
| 528 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 529 | |
| 530 // This won't do anything if the callback was called before. | |
| 531 // If it goes through, it will likely be because OnWillStart() returned | |
| 532 // false somewhere in the chain of resource handlers. | |
| 533 CallStartedCB(NULL, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); | |
| 534 | |
| 535 // Remove output stream callback if a stream exists. | |
| 536 if (stream_writer_) | |
| 537 stream_writer_->RegisterCallback(base::Closure()); | |
| 538 | |
| 539 // tab_info_ must be destroyed on UI thread, since | |
| 540 // InitializeDownloadTabInfoOnUIThread might still be using it. | |
| 541 if (tab_info_.get()) { | |
| 542 BrowserThread::PostTask( | |
| 543 BrowserThread::UI, FROM_HERE, | |
| 544 base::Bind(&DeleteOnUIThread, base::Passed(&tab_info_))); | |
| 545 } | |
| 546 | |
| 547 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", | |
| 548 base::TimeTicks::Now() - download_start_time_); | |
| 549 } | 441 } |
| 550 | 442 |
| 551 } // namespace content | 443 } // namespace content |
| OLD | NEW |