OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_file_manager.h" | 5 #include "content/browser/download/download_file_manager.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/task.h" | 10 #include "base/task.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
13 #include "content/browser/download/download_buffer.h" | 13 #include "content/browser/download/download_buffer.h" |
14 #include "content/browser/download/download_file.h" | 14 #include "content/browser/download/download_file.h" |
15 #include "content/browser/download/download_create_info.h" | 15 #include "content/browser/download/download_create_info.h" |
16 #include "content/browser/download/download_manager.h" | 16 #include "content/browser/download/download_manager.h" |
17 #include "content/browser/download/interrupt_reasons.h" | |
18 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 17 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
19 #include "content/browser/tab_contents/tab_contents.h" | 18 #include "content/browser/tab_contents/tab_contents.h" |
20 #include "content/public/browser/download_manager_delegate.h" | 19 #include "content/public/browser/download_manager_delegate.h" |
21 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
22 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
23 | 22 |
24 namespace { | 23 namespace { |
25 | 24 |
26 // Throttle updates to the UI thread so that a fast moving download doesn't | 25 // Throttle updates to the UI thread so that a fast moving download doesn't |
27 // cause it to become unresponsive (in milliseconds). | 26 // cause it to become unresponsive (in milliseconds). |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 DOWNLOAD_INTERRUPT_FROM_DISK))); | 181 DOWNLOAD_INTERRUPT_FROM_DISK))); |
183 } | 182 } |
184 } | 183 } |
185 } | 184 } |
186 data->Release(); | 185 data->Release(); |
187 } | 186 } |
188 } | 187 } |
189 | 188 |
190 void DownloadFileManager::OnResponseCompleted( | 189 void DownloadFileManager::OnResponseCompleted( |
191 DownloadId global_id, | 190 DownloadId global_id, |
192 net::Error net_error, | 191 InterruptReason reason, |
193 const std::string& security_info) { | 192 const std::string& security_info) { |
194 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id | 193 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id |
195 << " net_error = " << net_error | 194 << " reason = " << InterruptReasonDebugString(reason) |
196 << " security_info = \"" << security_info << "\""; | 195 << " security_info = \"" << security_info << "\""; |
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
198 DownloadFile* download_file = GetDownloadFile(global_id); | 197 DownloadFile* download_file = GetDownloadFile(global_id); |
199 if (!download_file) | 198 if (!download_file) |
200 return; | 199 return; |
201 | 200 |
202 download_file->Finish(); | 201 download_file->Finish(); |
203 | 202 |
204 DownloadManager* download_manager = download_file->GetDownloadManager(); | 203 DownloadManager* download_manager = download_file->GetDownloadManager(); |
205 if (!download_manager) { | 204 if (!download_manager) { |
206 CancelDownload(global_id); | 205 CancelDownload(global_id); |
207 return; | 206 return; |
208 } | 207 } |
209 | 208 |
210 std::string hash; | 209 std::string hash; |
211 if (!download_file->GetSha256Hash(&hash)) | 210 if (!download_file->GetSha256Hash(&hash)) |
212 hash.clear(); | 211 hash.clear(); |
213 | 212 |
214 // ERR_CONNECTION_CLOSED is allowed since a number of servers in the wild | 213 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) { |
215 // advertise a larger Content-Length than the amount of bytes in the message | |
216 // body, and then close the connection. Other browsers - IE8, Firefox 4.0.1, | |
217 // and Safari 5.0.4 - treat the download as complete in this case, so we | |
218 // follow their lead. | |
219 if (net_error == net::OK || net_error == net::ERR_CONNECTION_CLOSED) { | |
220 BrowserThread::PostTask( | 214 BrowserThread::PostTask( |
221 BrowserThread::UI, | 215 BrowserThread::UI, |
222 FROM_HERE, | 216 FROM_HERE, |
223 NewRunnableMethod( | 217 NewRunnableMethod( |
224 download_manager, | 218 download_manager, |
225 &DownloadManager::OnResponseCompleted, | 219 &DownloadManager::OnResponseCompleted, |
226 global_id.local(), | 220 global_id.local(), |
227 download_file->bytes_so_far(), | 221 download_file->bytes_so_far(), |
228 hash)); | 222 hash)); |
229 } else { | 223 } else { |
230 BrowserThread::PostTask( | 224 BrowserThread::PostTask( |
231 BrowserThread::UI, | 225 BrowserThread::UI, |
232 FROM_HERE, | 226 FROM_HERE, |
233 NewRunnableMethod( | 227 NewRunnableMethod( |
234 download_manager, | 228 download_manager, |
235 &DownloadManager::OnDownloadInterrupted, | 229 &DownloadManager::OnDownloadInterrupted, |
236 global_id.local(), | 230 global_id.local(), |
237 download_file->bytes_so_far(), | 231 download_file->bytes_so_far(), |
238 ConvertNetErrorToInterruptReason( | 232 reason)); |
239 net_error, | |
240 DOWNLOAD_INTERRUPT_FROM_NETWORK))); | |
241 } | 233 } |
242 // We need to keep the download around until the UI thread has finalized | 234 // We need to keep the download around until the UI thread has finalized |
243 // the name. | 235 // the name. |
244 } | 236 } |
245 | 237 |
246 // This method will be sent via a user action, or shutdown on the UI thread, and | 238 // This method will be sent via a user action, or shutdown on the UI thread, and |
247 // run on the download thread. Since this message has been sent from the UI | 239 // run on the download thread. Since this message has been sent from the UI |
248 // thread, the download may have already completed and won't exist in our map. | 240 // thread, the download may have already completed and won't exist in our map. |
249 void DownloadFileManager::CancelDownload(DownloadId global_id) { | 241 void DownloadFileManager::CancelDownload(DownloadId global_id) { |
250 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id; | 242 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id; |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 << " id = " << global_id | 427 << " id = " << global_id |
436 << " download_file = " << download_file->DebugString(); | 428 << " download_file = " << download_file->DebugString(); |
437 | 429 |
438 downloads_.erase(global_id); | 430 downloads_.erase(global_id); |
439 | 431 |
440 delete download_file; | 432 delete download_file; |
441 | 433 |
442 if (downloads_.empty()) | 434 if (downloads_.empty()) |
443 StopUpdateTimer(); | 435 StopUpdateTimer(); |
444 } | 436 } |
OLD | NEW |