Chromium Code Reviews| 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 // NULL 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(NULL, 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(NULL, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); | |
|
asanka
2015/12/08 16:58:33
nit: nullptr for new code.
svaldez
2015/12/08 20:39:26
Done.
| |
| 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", | |
|
asanka
2015/12/08 16:58:32
Do we have lifetime guarantee that the DownloadReq
svaldez
2015/12/08 20:39:26
We don't have good lifetime guarantees about DRC's
asanka
2015/12/08 21:53:56
This is a guarantee currently afforded by Resource
svaldez
2015/12/09 17:20:41
Actually I ended up adding destruction of UrlDownl
| |
| 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() : 0; |
| 176 | |
| 177 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); | |
| 178 | 139 |
| 179 // Deleted in DownloadManager. | 140 // Deleted in DownloadManager. |
| 180 scoped_ptr<DownloadCreateInfo> info( | 141 scoped_ptr<DownloadCreateInfo> info( |
| 181 new DownloadCreateInfo(base::Time::Now(), | 142 new DownloadCreateInfo(base::Time::Now(), |
| 182 content_length, | 143 content_length, |
| 183 request()->net_log(), | 144 request()->net_log(), |
| 184 request_info->HasUserGesture(), | 145 false, |
| 185 request_info->GetPageTransition(), | 146 ui::PAGE_TRANSITION_LINK, |
| 186 save_info_.Pass())); | 147 save_info_.Pass())); |
| 187 | 148 |
| 188 // Create the ByteStream for sending data to the download sink. | 149 // Create the ByteStream for sending data to the download sink. |
| 189 scoped_ptr<ByteStreamReader> stream_reader; | 150 scoped_ptr<ByteStreamReader> stream_reader; |
| 190 CreateByteStream( | 151 CreateByteStream( |
| 191 base::ThreadTaskRunnerHandle::Get(), | 152 base::ThreadTaskRunnerHandle::Get(), |
| 192 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | 153 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), |
| 193 kDownloadByteStreamSize, &stream_writer_, &stream_reader); | 154 kDownloadByteStreamSize, &stream_writer_, &stream_reader); |
| 194 stream_writer_->RegisterCallback( | 155 stream_writer_->RegisterCallback( |
| 195 base::Bind(&DownloadResourceHandler::ResumeRequest, AsWeakPtr())); | 156 base::Bind(&DownloadRequestCore::ResumeRequest, AsWeakPtr())); |
| 196 | 157 |
| 197 info->download_id = download_id_; | 158 info->download_id = download_id_; |
| 198 info->url_chain = request()->url_chain(); | 159 info->url_chain = request()->url_chain(); |
| 199 info->referrer_url = GURL(request()->referrer()); | 160 info->referrer_url = GURL(request()->referrer()); |
| 200 info->mime_type = response->head.mime_type; | 161 string mime_type; |
| 162 request()->GetMimeType(&mime_type); | |
| 163 info->mime_type = mime_type; | |
| 201 info->remote_address = request()->GetSocketAddress().host(); | 164 info->remote_address = request()->GetSocketAddress().host(); |
| 202 if (request()->response_headers()) { | 165 if (request()->response_headers()) { |
| 203 // Grab the first content-disposition header. There may be more than one, | 166 // 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 | 167 // though as of this writing, the network stack ensures if there are, they |
| 205 // are all duplicates. | 168 // are all duplicates. |
| 206 request()->response_headers()->EnumerateHeader( | 169 request()->response_headers()->EnumerateHeader( |
| 207 nullptr, "content-disposition", &info->content_disposition); | 170 nullptr, "content-disposition", &info->content_disposition); |
| 208 } | 171 } |
| 209 RecordDownloadMimeType(info->mime_type); | 172 RecordDownloadMimeType(info->mime_type); |
| 210 RecordDownloadContentDisposition(info->content_disposition); | 173 RecordDownloadContentDisposition(info->content_disposition); |
| 211 | 174 |
| 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. | 175 // Get the last modified time and etag. |
| 217 const net::HttpResponseHeaders* headers = request()->response_headers(); | 176 const net::HttpResponseHeaders* headers = request()->response_headers(); |
| 218 if (headers) { | 177 if (headers) { |
| 219 if (headers->HasStrongValidators()) { | 178 if (headers->HasStrongValidators()) { |
| 220 // If we don't have strong validators as per RFC 2616 section 13.3.3, then | 179 // 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. | 180 // we neither store nor use them for range requests. |
| 222 if (!headers->EnumerateHeader(NULL, "Last-Modified", | 181 if (!headers->EnumerateHeader(NULL, "Last-Modified", |
| 223 &info->last_modified)) | 182 &info->last_modified)) |
| 224 info->last_modified.clear(); | 183 info->last_modified.clear(); |
| 225 if (!headers->EnumerateHeader(NULL, "ETag", &info->etag)) | 184 if (!headers->EnumerateHeader(NULL, "ETag", &info->etag)) |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 241 // Blink verifies that the requester of this download is allowed to set a | 200 // 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 | 201 // suggested name for the security origin of the downlaod URL. However, this |
| 243 // assumption doesn't hold if there were cross origin redirects. Therefore, | 202 // assumption doesn't hold if there were cross origin redirects. Therefore, |
| 244 // clear the suggested_name for such requests. | 203 // clear the suggested_name for such requests. |
| 245 if (info->url_chain.size() > 1 && | 204 if (info->url_chain.size() > 1 && |
| 246 info->url_chain.front().GetOrigin() != info->url_chain.back().GetOrigin()) | 205 info->url_chain.front().GetOrigin() != info->url_chain.back().GetOrigin()) |
| 247 info->save_info->suggested_name.clear(); | 206 info->save_info->suggested_name.clear(); |
| 248 | 207 |
| 249 BrowserThread::PostTask( | 208 BrowserThread::PostTask( |
| 250 BrowserThread::UI, FROM_HERE, | 209 BrowserThread::UI, FROM_HERE, |
| 251 base::Bind(&StartOnUIThread, | 210 base::Bind(&StartOnUIThread, base::Passed(&info), |
| 252 base::Passed(&info), | |
| 253 base::Passed(&tab_info_), | |
| 254 base::Passed(&stream_reader), | 211 base::Passed(&stream_reader), |
| 212 download_manager_, | |
| 255 // Pass to StartOnUIThread so that variable | 213 // Pass to StartOnUIThread so that variable |
| 256 // access is always on IO thread but function | 214 // access is always on IO thread but function |
| 257 // is called on UI thread. | 215 // is called on UI thread. |
| 258 started_cb_)); | 216 started_cb_)); |
| 259 // Guaranteed to be called in StartOnUIThread | 217 // Guaranteed to be called in StartOnUIThread |
| 260 started_cb_.Reset(); | 218 started_cb_.Reset(); |
| 261 | 219 |
| 262 return true; | 220 return true; |
| 263 } | 221 } |
| 264 | 222 |
| 265 void DownloadResourceHandler::CallStartedCB( | 223 void DownloadRequestCore::CallStartedCB( |
| 266 DownloadItem* item, | 224 DownloadItem* item, |
| 267 DownloadInterruptReason interrupt_reason) { | 225 DownloadInterruptReason interrupt_reason) { |
| 268 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 226 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 269 if (started_cb_.is_null()) | 227 if (started_cb_.is_null()) |
| 270 return; | 228 return; |
| 271 BrowserThread::PostTask( | 229 BrowserThread::PostTask( |
| 272 BrowserThread::UI, | 230 BrowserThread::UI, |
| 273 FROM_HERE, | 231 FROM_HERE, |
| 274 base::Bind( | 232 base::Bind( |
| 275 &CallStartedCBOnUIThread, started_cb_, item, interrupt_reason)); | 233 &CallStartedCBOnUIThread, started_cb_, item, interrupt_reason)); |
| 276 started_cb_.Reset(); | 234 started_cb_.Reset(); |
| 277 } | 235 } |
| 278 | 236 |
| 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 | 237 // Create a new buffer, which will be handed to the download thread for file |
| 289 // writing and deletion. | 238 // writing and deletion. |
| 290 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 239 bool DownloadRequestCore::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 291 int* buf_size, | 240 int* buf_size, |
| 292 int min_size) { | 241 int min_size) { |
| 293 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 242 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 294 DCHECK(buf && buf_size); | 243 DCHECK(buf && buf_size); |
| 295 DCHECK(!read_buffer_.get()); | 244 DCHECK(!read_buffer_.get()); |
| 296 | 245 |
| 297 *buf_size = min_size < 0 ? kReadBufSize : min_size; | 246 *buf_size = min_size < 0 ? kReadBufSize : min_size; |
| 298 last_buffer_size_ = *buf_size; | 247 last_buffer_size_ = *buf_size; |
| 299 read_buffer_ = new net::IOBuffer(*buf_size); | 248 read_buffer_ = new net::IOBuffer(*buf_size); |
| 300 *buf = read_buffer_.get(); | 249 *buf = read_buffer_.get(); |
| 301 return true; | 250 return true; |
| 302 } | 251 } |
| 303 | 252 |
| 304 // Pass the buffer to the download file writer. | 253 // Pass the buffer to the download file writer. |
| 305 bool DownloadResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 254 bool DownloadRequestCore::OnReadCompleted(int bytes_read, bool* defer) { |
| 306 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 255 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 307 DCHECK(read_buffer_.get()); | 256 DCHECK(read_buffer_.get()); |
| 308 | 257 |
| 309 base::TimeTicks now(base::TimeTicks::Now()); | 258 base::TimeTicks now(base::TimeTicks::Now()); |
| 310 if (!last_read_time_.is_null()) { | 259 if (!last_read_time_.is_null()) { |
| 311 double seconds_since_last_read = (now - last_read_time_).InSecondsF(); | 260 double seconds_since_last_read = (now - last_read_time_).InSecondsF(); |
| 312 if (now == last_read_time_) | 261 if (now == last_read_time_) |
| 313 // Use 1/10 ms as a "very small number" so that we avoid | 262 // 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. | 263 // divide-by-zero error and still record a very high potential bandwidth. |
| 315 seconds_since_last_read = 0.00001; | 264 seconds_since_last_read = 0.00001; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 334 } | 283 } |
| 335 | 284 |
| 336 read_buffer_ = NULL; // Drop our reference. | 285 read_buffer_ = NULL; // Drop our reference. |
| 337 | 286 |
| 338 if (pause_count_ > 0) | 287 if (pause_count_ > 0) |
| 339 *defer = was_deferred_ = true; | 288 *defer = was_deferred_ = true; |
| 340 | 289 |
| 341 return true; | 290 return true; |
| 342 } | 291 } |
| 343 | 292 |
| 344 void DownloadResourceHandler::OnResponseCompleted( | 293 void DownloadRequestCore::OnResponseCompleted( |
| 345 const net::URLRequestStatus& status, | 294 const net::URLRequestStatus& status) { |
| 346 const std::string& security_info, | |
| 347 bool* defer) { | |
| 348 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 295 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 349 int response_code = status.is_success() ? request()->GetResponseCode() : 0; | 296 int response_code = status.is_success() ? request()->GetResponseCode() : 0; |
| 350 DVLOG(20) << __FUNCTION__ << "()" << DebugString() | 297 DVLOG(20) << __FUNCTION__ << "()" << DebugString() |
| 351 << " status.status() = " << status.status() | 298 << " status.status() = " << status.status() |
| 352 << " status.error() = " << status.error() | 299 << " status.error() = " << status.error() |
| 353 << " response_code = " << response_code; | 300 << " response_code = " << response_code; |
| 354 | 301 |
| 355 net::Error error_code = net::OK; | 302 net::Error error_code = net::OK; |
| 356 if (status.status() == net::URLRequestStatus::FAILED || | 303 if (status.status() == net::URLRequestStatus::FAILED || |
| 357 // Note cancels as failures too. | 304 // Note cancels as failures too. |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 459 // we can drill down. | 406 // we can drill down. |
| 460 if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) { | 407 if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) { |
| 461 UMA_HISTOGRAM_SPARSE_SLOWLY("Download.MapErrorNetworkFailed", | 408 UMA_HISTOGRAM_SPARSE_SLOWLY("Download.MapErrorNetworkFailed", |
| 462 std::abs(status.error())); | 409 std::abs(status.error())); |
| 463 } | 410 } |
| 464 | 411 |
| 465 stream_writer_.reset(); // We no longer need the stream. | 412 stream_writer_.reset(); // We no longer need the stream. |
| 466 read_buffer_ = NULL; | 413 read_buffer_ = NULL; |
| 467 } | 414 } |
| 468 | 415 |
| 469 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 416 void DownloadRequestCore::PauseRequest() { |
| 470 NOTREACHED(); | |
| 471 } | |
| 472 | |
| 473 void DownloadResourceHandler::PauseRequest() { | |
| 474 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 417 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 475 | 418 |
| 476 ++pause_count_; | 419 ++pause_count_; |
| 477 } | 420 } |
| 478 | 421 |
| 479 void DownloadResourceHandler::ResumeRequest() { | 422 void DownloadRequestCore::ResumeRequest() { |
| 480 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 423 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 481 DCHECK_LT(0, pause_count_); | 424 DCHECK_LT(0, pause_count_); |
| 482 | 425 |
| 483 --pause_count_; | 426 --pause_count_; |
| 484 | 427 |
| 485 if (!was_deferred_) | 428 if (!was_deferred_) |
| 486 return; | 429 return; |
| 487 if (pause_count_ > 0) | 430 if (pause_count_ > 0) |
| 488 return; | 431 return; |
| 489 | 432 |
| 490 was_deferred_ = false; | 433 was_deferred_ = false; |
| 491 if (!last_stream_pause_time_.is_null()) { | 434 if (!last_stream_pause_time_.is_null()) { |
| 492 total_pause_time_ += (base::TimeTicks::Now() - last_stream_pause_time_); | 435 total_pause_time_ += (base::TimeTicks::Now() - last_stream_pause_time_); |
| 493 last_stream_pause_time_ = base::TimeTicks(); | 436 last_stream_pause_time_ = base::TimeTicks(); |
| 494 } | 437 } |
| 495 | |
| 496 controller()->Resume(); | |
| 497 } | 438 } |
| 498 | 439 |
| 499 void DownloadResourceHandler::CancelRequest() { | 440 std::string DownloadRequestCore::DebugString() const { |
| 500 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 501 | |
| 502 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
| 503 ResourceDispatcherHostImpl::Get()->CancelRequest( | |
| 504 info->GetChildID(), | |
| 505 info->GetRequestID()); | |
| 506 // This object has been deleted. | |
| 507 } | |
| 508 | |
| 509 std::string DownloadResourceHandler::DebugString() const { | |
| 510 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
| 511 return base::StringPrintf("{" | 441 return base::StringPrintf("{" |
| 512 " url_ = " "\"%s\"" | 442 " url_ = " "\"%s\"" |
| 513 " info = {" | |
| 514 " child_id = " "%d" | |
| 515 " request_id = " "%d" | |
| 516 " route_id = " "%d" | |
| 517 " }" | |
| 518 " }", | 443 " }", |
| 519 request() ? | 444 request() ? |
| 520 request()->url().spec().c_str() : | 445 request()->url().spec().c_str() : |
| 521 "<NULL request>", | 446 "<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 } | 447 } |
| 550 | 448 |
| 551 } // namespace content | 449 } // namespace content |
| OLD | NEW |