| 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/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 tab_info->tab_referrer_url = entry->GetReferrer().url; | 76 tab_info->tab_referrer_url = entry->GetReferrer().url; |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 void DeleteOnUIThread( | 81 void DeleteOnUIThread( |
| 82 scoped_ptr<DownloadResourceHandler::DownloadTabInfo> tab_info) {} | 82 scoped_ptr<DownloadResourceHandler::DownloadTabInfo> tab_info) {} |
| 83 | 83 |
| 84 } // namespace | 84 } // namespace |
| 85 | 85 |
| 86 DownloadResourceHandler::DownloadResourceHandler( | 86 DownloadResourceHandler::DownloadResourceHandler(net::URLRequest* request) |
| 87 uint32_t id, | |
| 88 net::URLRequest* request, | |
| 89 const DownloadUrlParameters::OnStartedCallback& started_cb, | |
| 90 scoped_ptr<DownloadSaveInfo> save_info) | |
| 91 : ResourceHandler(request), | 87 : ResourceHandler(request), |
| 92 download_id_(id), | |
| 93 started_cb_(started_cb), | |
| 94 tab_info_(new DownloadTabInfo()), | 88 tab_info_(new DownloadTabInfo()), |
| 95 core_(request, | 89 core_(request, this) { |
| 96 std::move(save_info), | |
| 97 base::Bind(&DownloadResourceHandler::OnCoreReadyToRead, | |
| 98 base::Unretained(this))) { | |
| 99 // Do UI thread initialization for tab_info_ asap after | 90 // Do UI thread initialization for tab_info_ asap after |
| 100 // DownloadResourceHandler creation since the tab could be navigated | 91 // DownloadResourceHandler creation since the tab could be navigated |
| 101 // before StartOnUIThread gets called. This is safe because deletion | 92 // before StartOnUIThread gets called. This is safe because deletion |
| 102 // will occur via PostTask() as well, which will serialized behind this | 93 // will occur via PostTask() as well, which will serialized behind this |
| 103 // PostTask() | 94 // PostTask() |
| 104 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); | 95 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); |
| 105 BrowserThread::PostTask( | 96 BrowserThread::PostTask( |
| 106 BrowserThread::UI, FROM_HERE, | 97 BrowserThread::UI, FROM_HERE, |
| 107 base::Bind(&InitializeDownloadTabInfoOnUIThread, | 98 base::Bind(&InitializeDownloadTabInfoOnUIThread, |
| 108 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(), | 99 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(), |
| 109 request_info->GetRouteID(), | 100 request_info->GetRouteID(), |
| 110 request_info->GetRequestID(), | 101 request_info->GetRequestID(), |
| 111 request_info->frame_tree_node_id()), | 102 request_info->frame_tree_node_id()), |
| 112 tab_info_.get())); | 103 tab_info_.get())); |
| 113 } | 104 } |
| 114 | 105 |
| 115 DownloadResourceHandler::~DownloadResourceHandler() { | 106 DownloadResourceHandler::~DownloadResourceHandler() { |
| 116 // This won't do anything if the callback was called before. | |
| 117 // If it goes through, it will likely be because OnWillStart() returned | |
| 118 // false somewhere in the chain of resource handlers. | |
| 119 CallStartedCB(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); | |
| 120 | |
| 121 if (tab_info_) { | 107 if (tab_info_) { |
| 122 BrowserThread::PostTask( | 108 BrowserThread::PostTask( |
| 123 BrowserThread::UI, FROM_HERE, | 109 BrowserThread::UI, FROM_HERE, |
| 124 base::Bind(&DeleteOnUIThread, base::Passed(&tab_info_))); | 110 base::Bind(&DeleteOnUIThread, base::Passed(&tab_info_))); |
| 125 } | 111 } |
| 126 } | 112 } |
| 127 | 113 |
| 128 bool DownloadResourceHandler::OnRequestRedirected( | 114 bool DownloadResourceHandler::OnRequestRedirected( |
| 129 const net::RedirectInfo& redirect_info, | 115 const net::RedirectInfo& redirect_info, |
| 130 ResourceResponse* response, | 116 ResourceResponse* response, |
| 131 bool* defer) { | 117 bool* defer) { |
| 132 return true; | 118 return true; |
| 133 } | 119 } |
| 134 | 120 |
| 135 // Send the download creation information to the download thread. | 121 // Send the download creation information to the download thread. |
| 136 bool DownloadResourceHandler::OnResponseStarted( | 122 bool DownloadResourceHandler::OnResponseStarted( |
| 137 ResourceResponse* response, | 123 ResourceResponse* response, |
| 138 bool* defer) { | 124 bool* defer) { |
| 139 scoped_ptr<DownloadCreateInfo> create_info; | |
| 140 scoped_ptr<ByteStreamReader> stream_reader; | |
| 141 | |
| 142 core_.OnResponseStarted(&create_info, &stream_reader); | |
| 143 | |
| 144 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); | |
| 145 create_info->download_id = download_id_; | |
| 146 create_info->has_user_gesture = request_info->HasUserGesture(); | |
| 147 create_info->transition_type = request_info->GetPageTransition(); | |
| 148 create_info->request_handle.reset(new DownloadRequestHandle( | |
| 149 AsWeakPtr(), request_info->GetChildID(), request_info->GetRouteID(), | |
| 150 request_info->GetRequestID(), request_info->frame_tree_node_id())); | |
| 151 | |
| 152 // The MIME type in ResourceResponse is the product of | 125 // The MIME type in ResourceResponse is the product of |
| 153 // MimeTypeResourceHandler. | 126 // MimeTypeResourceHandler. |
| 154 create_info->mime_type = response->head.mime_type; | 127 original_mime_type_ = response->head.mime_type; |
| 155 | 128 return core_.OnResponseStarted(); |
| 156 BrowserThread::PostTask( | |
| 157 BrowserThread::UI, FROM_HERE, | |
| 158 base::Bind(&StartOnUIThread, base::Passed(&create_info), | |
| 159 base::Passed(&tab_info_), base::Passed(&stream_reader), | |
| 160 base::ResetAndReturn(&started_cb_))); | |
| 161 return true; | |
| 162 } | |
| 163 | |
| 164 void DownloadResourceHandler::CallStartedCB( | |
| 165 DownloadInterruptReason interrupt_reason) { | |
| 166 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 167 if (started_cb_.is_null()) | |
| 168 return; | |
| 169 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 170 base::Bind(base::ResetAndReturn(&started_cb_), | |
| 171 nullptr, interrupt_reason)); | |
| 172 } | 129 } |
| 173 | 130 |
| 174 bool DownloadResourceHandler::OnWillStart(const GURL& url, bool* defer) { | 131 bool DownloadResourceHandler::OnWillStart(const GURL& url, bool* defer) { |
| 175 return true; | 132 return true; |
| 176 } | 133 } |
| 177 | 134 |
| 178 bool DownloadResourceHandler::OnBeforeNetworkStart(const GURL& url, | 135 bool DownloadResourceHandler::OnBeforeNetworkStart(const GURL& url, |
| 179 bool* defer) { | 136 bool* defer) { |
| 180 return true; | 137 return true; |
| 181 } | 138 } |
| 182 | 139 |
| 183 // Create a new buffer, which will be handed to the download thread for file | 140 // Create a new buffer, which will be handed to the download thread for file |
| 184 // writing and deletion. | 141 // writing and deletion. |
| 185 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 142 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 186 int* buf_size, | 143 int* buf_size, |
| 187 int min_size) { | 144 int min_size) { |
| 188 return core_.OnWillRead(buf, buf_size, min_size); | 145 return core_.OnWillRead(buf, buf_size, min_size); |
| 189 } | 146 } |
| 190 | 147 |
| 191 // Pass the buffer to the download file writer. | 148 // Pass the buffer to the download file writer. |
| 192 bool DownloadResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 149 bool DownloadResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
| 193 return core_.OnReadCompleted(bytes_read, defer); | 150 return core_.OnReadCompleted(bytes_read, defer); |
| 194 } | 151 } |
| 195 | 152 |
| 196 void DownloadResourceHandler::OnResponseCompleted( | 153 void DownloadResourceHandler::OnResponseCompleted( |
| 197 const net::URLRequestStatus& status, | 154 const net::URLRequestStatus& status, |
| 198 const std::string& security_info, | 155 const std::string& security_info, |
| 199 bool* defer) { | 156 bool* defer) { |
| 200 DownloadInterruptReason result = core_.OnResponseCompleted(status); | 157 core_.OnResponseCompleted(status); |
| 201 CallStartedCB(result); | |
| 202 } | 158 } |
| 203 | 159 |
| 204 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 160 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
| 205 NOTREACHED(); | 161 NOTREACHED(); |
| 206 } | 162 } |
| 207 | 163 |
| 208 void DownloadResourceHandler::PauseRequest() { | 164 void DownloadResourceHandler::PauseRequest() { |
| 209 core_.PauseRequest(); | 165 core_.PauseRequest(); |
| 210 } | 166 } |
| 211 | 167 |
| 212 void DownloadResourceHandler::ResumeRequest() { | 168 void DownloadResourceHandler::ResumeRequest() { |
| 213 core_.ResumeRequest(); | 169 core_.ResumeRequest(); |
| 214 } | 170 } |
| 215 | 171 |
| 216 void DownloadResourceHandler::OnCoreReadyToRead() { | 172 void DownloadResourceHandler::OnStart( |
| 173 scoped_ptr<DownloadCreateInfo> create_info, |
| 174 scoped_ptr<ByteStreamReader> stream_reader, |
| 175 const DownloadUrlParameters::OnStartedCallback& callback) { |
| 176 // If the user cancels the download, then don't call start. Instead ignore the |
| 177 // download entirely. |
| 178 if (create_info->result == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED && |
| 179 create_info->download_id == DownloadItem::kInvalidId) { |
| 180 if (!callback.is_null()) |
| 181 BrowserThread::PostTask( |
| 182 BrowserThread::UI, FROM_HERE, |
| 183 base::Bind(callback, nullptr, create_info->result)); |
| 184 return; |
| 185 } |
| 186 |
| 187 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); |
| 188 create_info->has_user_gesture = request_info->HasUserGesture(); |
| 189 create_info->transition_type = request_info->GetPageTransition(); |
| 190 |
| 191 create_info->request_handle.reset(new DownloadRequestHandle( |
| 192 AsWeakPtr(), request_info->GetChildID(), request_info->GetRouteID(), |
| 193 request_info->GetRequestID(), request_info->frame_tree_node_id())); |
| 194 |
| 195 if (create_info->result == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 196 create_info->mime_type = original_mime_type_; |
| 197 |
| 198 BrowserThread::PostTask( |
| 199 BrowserThread::UI, FROM_HERE, |
| 200 base::Bind(&StartOnUIThread, base::Passed(&create_info), |
| 201 base::Passed(&tab_info_), base::Passed(&stream_reader), |
| 202 callback)); |
| 203 } |
| 204 |
| 205 void DownloadResourceHandler::OnReadyToRead() { |
| 217 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 206 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 218 controller()->Resume(); | 207 controller()->Resume(); |
| 219 } | 208 } |
| 220 | 209 |
| 221 void DownloadResourceHandler::CancelRequest() { | 210 void DownloadResourceHandler::CancelRequest() { |
| 222 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 211 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 223 | 212 |
| 224 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 213 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 225 ResourceDispatcherHostImpl::Get()->CancelRequest( | 214 ResourceDispatcherHostImpl::Get()->CancelRequest( |
| 226 info->GetChildID(), | 215 info->GetChildID(), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 240 " }", | 229 " }", |
| 241 request() ? | 230 request() ? |
| 242 request()->url().spec().c_str() : | 231 request()->url().spec().c_str() : |
| 243 "<NULL request>", | 232 "<NULL request>", |
| 244 info->GetChildID(), | 233 info->GetChildID(), |
| 245 info->GetRequestID(), | 234 info->GetRequestID(), |
| 246 info->GetRouteID()); | 235 info->GetRouteID()); |
| 247 } | 236 } |
| 248 | 237 |
| 249 } // namespace content | 238 } // namespace content |
| OLD | NEW |