| 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 | 132 |
| 133 // static | 133 // static |
| 134 std::unique_ptr<ResourceHandler> DownloadResourceHandler::Create( | 134 std::unique_ptr<ResourceHandler> DownloadResourceHandler::Create( |
| 135 net::URLRequest* request) { | 135 net::URLRequest* request) { |
| 136 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 136 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 137 std::unique_ptr<ResourceHandler> handler( | 137 std::unique_ptr<ResourceHandler> handler( |
| 138 new DownloadResourceHandler(request)); | 138 new DownloadResourceHandler(request)); |
| 139 return handler; | 139 return handler; |
| 140 } | 140 } |
| 141 | 141 |
| 142 bool DownloadResourceHandler::OnRequestRedirected( | 142 void DownloadResourceHandler::OnRequestRedirected( |
| 143 const net::RedirectInfo& redirect_info, | 143 const net::RedirectInfo& redirect_info, |
| 144 ResourceResponse* response, | 144 ResourceResponse* response, |
| 145 bool* defer) { | 145 std::unique_ptr<ResourceController> controller) { |
| 146 return core_.OnRequestRedirected(); | 146 if (core_.OnRequestRedirected()) { |
| 147 controller->Resume(); |
| 148 } else { |
| 149 controller->Cancel(); |
| 150 } |
| 147 } | 151 } |
| 148 | 152 |
| 149 // Send the download creation information to the download thread. | 153 // Send the download creation information to the download thread. |
| 150 bool DownloadResourceHandler::OnResponseStarted( | 154 void DownloadResourceHandler::OnResponseStarted( |
| 151 ResourceResponse* response, | 155 ResourceResponse* response, |
| 152 bool* defer) { | 156 std::unique_ptr<ResourceController> controller) { |
| 153 // The MIME type in ResourceResponse is the product of | 157 // The MIME type in ResourceResponse is the product of |
| 154 // MimeTypeResourceHandler. | 158 // MimeTypeResourceHandler. |
| 155 return core_.OnResponseStarted(response->head.mime_type); | 159 if (core_.OnResponseStarted(response->head.mime_type)) { |
| 160 controller->Resume(); |
| 161 } else { |
| 162 controller->Cancel(); |
| 163 } |
| 156 } | 164 } |
| 157 | 165 |
| 158 bool DownloadResourceHandler::OnWillStart(const GURL& url, bool* defer) { | 166 void DownloadResourceHandler::OnWillStart( |
| 159 return true; | 167 const GURL& url, |
| 168 std::unique_ptr<ResourceController> controller) { |
| 169 controller->Resume(); |
| 160 } | 170 } |
| 161 | 171 |
| 162 // Create a new buffer, which will be handed to the download thread for file | 172 // Create a new buffer, which will be handed to the download thread for file |
| 163 // writing and deletion. | 173 // writing and deletion. |
| 164 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 174 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 165 int* buf_size, | 175 int* buf_size, |
| 166 int min_size) { | 176 int min_size) { |
| 167 return core_.OnWillRead(buf, buf_size, min_size); | 177 return core_.OnWillRead(buf, buf_size, min_size); |
| 168 } | 178 } |
| 169 | 179 |
| 170 // Pass the buffer to the download file writer. | 180 // Pass the buffer to the download file writer. |
| 171 bool DownloadResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 181 void DownloadResourceHandler::OnReadCompleted( |
| 172 return core_.OnReadCompleted(bytes_read, defer); | 182 int bytes_read, |
| 183 std::unique_ptr<ResourceController> controller) { |
| 184 DCHECK(!has_controller()); |
| 185 |
| 186 bool defer = false; |
| 187 if (!core_.OnReadCompleted(bytes_read, &defer)) { |
| 188 controller->Cancel(); |
| 189 return; |
| 190 } |
| 191 |
| 192 if (!defer) { |
| 193 controller->Resume(); |
| 194 } else { |
| 195 HoldController(std::move(controller)); |
| 196 } |
| 173 } | 197 } |
| 174 | 198 |
| 175 void DownloadResourceHandler::OnResponseCompleted( | 199 void DownloadResourceHandler::OnResponseCompleted( |
| 176 const net::URLRequestStatus& status, | 200 const net::URLRequestStatus& status, |
| 177 bool* defer) { | 201 std::unique_ptr<ResourceController> controller) { |
| 178 core_.OnResponseCompleted(status); | 202 core_.OnResponseCompleted(status); |
| 203 controller->Resume(); |
| 179 } | 204 } |
| 180 | 205 |
| 181 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 206 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
| 182 NOTREACHED(); | 207 NOTREACHED(); |
| 183 } | 208 } |
| 184 | 209 |
| 185 void DownloadResourceHandler::PauseRequest() { | 210 void DownloadResourceHandler::PauseRequest() { |
| 186 core_.PauseRequest(); | 211 core_.PauseRequest(); |
| 187 } | 212 } |
| 188 | 213 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 219 BrowserThread::PostTask( | 244 BrowserThread::PostTask( |
| 220 BrowserThread::UI, FROM_HERE, | 245 BrowserThread::UI, FROM_HERE, |
| 221 base::Bind(&StartOnUIThread, base::Passed(&create_info), | 246 base::Bind(&StartOnUIThread, base::Passed(&create_info), |
| 222 base::Passed(&tab_info_), base::Passed(&stream_reader), | 247 base::Passed(&tab_info_), base::Passed(&stream_reader), |
| 223 render_process_id, render_frame_id, | 248 render_process_id, render_frame_id, |
| 224 request_info->frame_tree_node_id(), callback)); | 249 request_info->frame_tree_node_id(), callback)); |
| 225 } | 250 } |
| 226 | 251 |
| 227 void DownloadResourceHandler::OnReadyToRead() { | 252 void DownloadResourceHandler::OnReadyToRead() { |
| 228 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 253 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 229 controller()->Resume(); | 254 Resume(); |
| 230 } | 255 } |
| 231 | 256 |
| 232 void DownloadResourceHandler::CancelRequest() { | 257 void DownloadResourceHandler::CancelRequest() { |
| 233 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 258 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 234 | 259 |
| 235 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 260 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 236 ResourceDispatcherHostImpl::Get()->CancelRequest( | 261 ResourceDispatcherHostImpl::Get()->CancelRequest( |
| 237 info->GetChildID(), | 262 info->GetChildID(), |
| 238 info->GetRequestID()); | 263 info->GetRequestID()); |
| 239 // This object has been deleted. | 264 // This object has been deleted. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 251 " }", | 276 " }", |
| 252 request() ? | 277 request() ? |
| 253 request()->url().spec().c_str() : | 278 request()->url().spec().c_str() : |
| 254 "<NULL request>", | 279 "<NULL request>", |
| 255 info->GetChildID(), | 280 info->GetChildID(), |
| 256 info->GetRequestID(), | 281 info->GetRequestID(), |
| 257 info->GetRouteID()); | 282 info->GetRouteID()); |
| 258 } | 283 } |
| 259 | 284 |
| 260 } // namespace content | 285 } // namespace content |
| OLD | NEW |