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_manager_impl.h" | 5 #include "content/browser/download/download_manager_impl.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
18 #include "base/supports_user_data.h" | 18 #include "base/supports_user_data.h" |
19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
20 #include "base/sys_string_conversions.h" | 20 #include "base/sys_string_conversions.h" |
21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
22 #include "content/browser/download/byte_stream.h" | 22 #include "content/browser/download/byte_stream.h" |
23 #include "content/browser/download/download_create_info.h" | 23 #include "content/browser/download/download_create_info.h" |
24 #include "content/browser/download/download_file_factory.h" | 24 #include "content/browser/download/download_file_manager.h" |
25 #include "content/browser/download/download_item_impl.h" | 25 #include "content/browser/download/download_item_impl.h" |
26 #include "content/browser/download/download_stats.h" | 26 #include "content/browser/download/download_stats.h" |
27 #include "content/browser/renderer_host/render_view_host_impl.h" | 27 #include "content/browser/renderer_host/render_view_host_impl.h" |
28 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 28 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
29 #include "content/browser/web_contents/web_contents_impl.h" | 29 #include "content/browser/web_contents/web_contents_impl.h" |
30 #include "content/public/browser/browser_context.h" | 30 #include "content/public/browser/browser_context.h" |
31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
32 #include "content/public/browser/content_browser_client.h" | 32 #include "content/public/browser/content_browser_client.h" |
33 #include "content/public/browser/download_interrupt_reasons.h" | 33 #include "content/public/browser/download_interrupt_reasons.h" |
34 #include "content/public/browser/download_manager_delegate.h" | 34 #include "content/public/browser/download_manager_delegate.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 137 |
138 bool operator!=(const MapValueIteratorAdapter& that) const { | 138 bool operator!=(const MapValueIteratorAdapter& that) const { |
139 return iter_ != that.iter_; | 139 return iter_ != that.iter_; |
140 } | 140 } |
141 | 141 |
142 private: | 142 private: |
143 base::hash_map<int64, DownloadItem*>::const_iterator iter_; | 143 base::hash_map<int64, DownloadItem*>::const_iterator iter_; |
144 // Allow copy and assign. | 144 // Allow copy and assign. |
145 }; | 145 }; |
146 | 146 |
147 void EnsureNoPendingDownloadJobsOnFile(bool* result) { | 147 void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm, |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 148 bool* result) { |
149 *result = (content::DownloadFile::GetNumberOfDownloadFiles() == 0); | 149 if (dfm->NumberOfActiveDownloads()) |
| 150 *result = false; |
150 BrowserThread::PostTask( | 151 BrowserThread::PostTask( |
151 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 152 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
152 } | 153 } |
153 | 154 |
| 155 void EnsureNoPendingDownloadJobsOnIO(bool* result) { |
| 156 scoped_refptr<DownloadFileManager> download_file_manager = |
| 157 ResourceDispatcherHostImpl::Get()->download_file_manager(); |
| 158 BrowserThread::PostTask( |
| 159 BrowserThread::FILE, FROM_HERE, |
| 160 base::Bind(&EnsureNoPendingDownloadsOnFile, |
| 161 download_file_manager, result)); |
| 162 } |
| 163 |
154 class DownloadItemFactoryImpl : public content::DownloadItemFactory { | 164 class DownloadItemFactoryImpl : public content::DownloadItemFactory { |
155 public: | 165 public: |
156 DownloadItemFactoryImpl() {} | 166 DownloadItemFactoryImpl() {} |
157 virtual ~DownloadItemFactoryImpl() {} | 167 virtual ~DownloadItemFactoryImpl() {} |
158 | 168 |
159 virtual DownloadItemImpl* CreatePersistedItem( | 169 virtual DownloadItemImpl* CreatePersistedItem( |
160 DownloadItemImplDelegate* delegate, | 170 DownloadItemImplDelegate* delegate, |
161 content::DownloadId download_id, | 171 content::DownloadId download_id, |
162 const content::DownloadPersistentStoreInfo& info, | 172 const content::DownloadPersistentStoreInfo& info, |
163 const net::BoundNetLog& bound_net_log) OVERRIDE { | 173 const net::BoundNetLog& bound_net_log) OVERRIDE { |
(...skipping 22 matching lines...) Expand all Loading... |
186 }; | 196 }; |
187 | 197 |
188 } // namespace | 198 } // namespace |
189 | 199 |
190 namespace content { | 200 namespace content { |
191 | 201 |
192 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { | 202 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { |
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
194 bool result = true; | 204 bool result = true; |
195 BrowserThread::PostTask( | 205 BrowserThread::PostTask( |
196 BrowserThread::FILE, FROM_HERE, | 206 BrowserThread::IO, FROM_HERE, |
197 base::Bind(&EnsureNoPendingDownloadJobsOnFile, &result)); | 207 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
198 MessageLoop::current()->Run(); | 208 MessageLoop::current()->Run(); |
199 return result; | 209 return result; |
200 } | 210 } |
201 | 211 |
202 } // namespace content | 212 } // namespace content |
203 | 213 |
204 DownloadManagerImpl::DownloadManagerImpl( | 214 DownloadManagerImpl::DownloadManagerImpl( |
205 scoped_ptr<content::DownloadItemFactory> item_factory, | 215 DownloadFileManager* file_manager, |
| 216 scoped_ptr<content::DownloadItemFactory> factory, |
206 net::NetLog* net_log) | 217 net::NetLog* net_log) |
207 : item_factory_(item_factory.Pass()), | 218 : factory_(factory.Pass()), |
208 history_size_(0), | 219 history_size_(0), |
209 shutdown_needed_(false), | 220 shutdown_needed_(false), |
210 browser_context_(NULL), | 221 browser_context_(NULL), |
| 222 file_manager_(file_manager), |
211 delegate_(NULL), | 223 delegate_(NULL), |
212 net_log_(net_log) { | 224 net_log_(net_log) { |
213 if (!item_factory_.get()) | 225 DCHECK(file_manager); |
214 item_factory_.reset(new DownloadItemFactoryImpl()); | 226 if (!factory_.get()) |
215 file_factory_.reset(new content::DownloadFileFactory()); | 227 factory_.reset(new DownloadItemFactoryImpl()); |
216 } | 228 } |
217 | 229 |
218 DownloadManagerImpl::~DownloadManagerImpl() { | 230 DownloadManagerImpl::~DownloadManagerImpl() { |
219 DCHECK(!shutdown_needed_); | 231 DCHECK(!shutdown_needed_); |
220 } | 232 } |
221 | 233 |
222 DownloadId DownloadManagerImpl::GetNextId() { | 234 DownloadId DownloadManagerImpl::GetNextId() { |
223 DownloadId id; | 235 DownloadId id; |
224 if (delegate_) | 236 if (delegate_) |
225 id = delegate_->GetNextId(); | 237 id = delegate_->GetNextId(); |
226 if (!id.IsValid()) { | 238 if (!id.IsValid()) { |
227 static int next_id; | 239 static int next_id; |
228 id = DownloadId(browser_context_, ++next_id); | 240 id = DownloadId(browser_context_, ++next_id); |
229 } | 241 } |
230 | 242 |
231 return id; | 243 return id; |
232 } | 244 } |
233 | 245 |
234 void DownloadManagerImpl::DelegateStart(DownloadItemImpl* item) { | 246 DownloadFileManager* DownloadManagerImpl::GetDownloadFileManager() { |
235 content::DownloadTargetCallback callback = | 247 return file_manager_; |
236 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined, | |
237 this, item->GetId()); | |
238 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) { | |
239 FilePath target_path = item->GetForcedFilePath(); | |
240 // TODO(asanka): Determine a useful path if |target_path| is empty. | |
241 callback.Run(target_path, | |
242 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
243 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
244 target_path); | |
245 } | |
246 } | 248 } |
247 | 249 |
248 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { | 250 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { |
249 if (!delegate_) | 251 if (!delegate_) |
250 return true; | 252 return true; |
251 | 253 |
252 return delegate_->ShouldOpenDownload(item); | 254 return delegate_->ShouldOpenDownload(item); |
253 } | 255 } |
254 | 256 |
255 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { | 257 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { |
(...skipping 15 matching lines...) Expand all Loading... |
271 void DownloadManagerImpl::Shutdown() { | 273 void DownloadManagerImpl::Shutdown() { |
272 VLOG(20) << __FUNCTION__ << "()" | 274 VLOG(20) << __FUNCTION__ << "()" |
273 << " shutdown_needed_ = " << shutdown_needed_; | 275 << " shutdown_needed_ = " << shutdown_needed_; |
274 if (!shutdown_needed_) | 276 if (!shutdown_needed_) |
275 return; | 277 return; |
276 shutdown_needed_ = false; | 278 shutdown_needed_ = false; |
277 | 279 |
278 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); | 280 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
279 // TODO(benjhayden): Consider clearing observers_. | 281 // TODO(benjhayden): Consider clearing observers_. |
280 | 282 |
281 // The DownloadFiles will be canceled and deleted by their DownloadItems. | 283 DCHECK(file_manager_); |
| 284 BrowserThread::PostTask( |
| 285 BrowserThread::FILE, FROM_HERE, |
| 286 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, |
| 287 file_manager_, make_scoped_refptr(this))); |
282 | 288 |
283 AssertContainersConsistent(); | 289 AssertContainersConsistent(); |
284 | 290 |
285 // Go through all downloads in downloads_. Dangerous ones we need to | 291 // Go through all downloads in downloads_. Dangerous ones we need to |
286 // remove on disk, and in progress ones we need to cancel. | 292 // remove on disk, and in progress ones we need to cancel. |
287 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { | 293 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { |
288 DownloadItemImpl* download = it->second; | 294 DownloadItemImpl* download = it->second; |
289 | 295 |
290 // Save iterator from potential erases in this set done by called code. | 296 // Save iterator from potential erases in this set done by called code. |
291 // Iterators after an erasure point are still valid for lists and | 297 // Iterators after an erasure point are still valid for lists and |
(...skipping 22 matching lines...) Expand all Loading... |
314 // and all in progress downloads have been cancelled. We can now delete | 320 // and all in progress downloads have been cancelled. We can now delete |
315 // anything left. | 321 // anything left. |
316 | 322 |
317 active_downloads_.clear(); | 323 active_downloads_.clear(); |
318 STLDeleteValues(&downloads_); | 324 STLDeleteValues(&downloads_); |
319 downloads_.clear(); | 325 downloads_.clear(); |
320 | 326 |
321 // We'll have nothing more to report to the observers after this point. | 327 // We'll have nothing more to report to the observers after this point. |
322 observers_.Clear(); | 328 observers_.Clear(); |
323 | 329 |
| 330 file_manager_ = NULL; |
324 if (delegate_) | 331 if (delegate_) |
325 delegate_->Shutdown(); | 332 delegate_->Shutdown(); |
326 delegate_ = NULL; | 333 delegate_ = NULL; |
327 } | 334 } |
328 | 335 |
329 void DownloadManagerImpl::GetTemporaryDownloads( | 336 void DownloadManagerImpl::GetTemporaryDownloads( |
330 const FilePath& dir_path, DownloadVector* result) { | 337 const FilePath& dir_path, DownloadVector* result) { |
331 DCHECK(result); | 338 DCHECK(result); |
332 | 339 |
333 for (DownloadMap::iterator it = downloads_.begin(); | 340 for (DownloadMap::iterator it = downloads_.begin(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 384 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
378 DCHECK(browser_context); | 385 DCHECK(browser_context); |
379 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 386 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
380 shutdown_needed_ = true; | 387 shutdown_needed_ = true; |
381 | 388 |
382 browser_context_ = browser_context; | 389 browser_context_ = browser_context; |
383 | 390 |
384 return true; | 391 return true; |
385 } | 392 } |
386 | 393 |
| 394 // We have received a message from DownloadFileManager about a new download. |
387 content::DownloadId DownloadManagerImpl::StartDownload( | 395 content::DownloadId DownloadManagerImpl::StartDownload( |
388 scoped_ptr<DownloadCreateInfo> info, | 396 scoped_ptr<DownloadCreateInfo> info, |
389 scoped_ptr<content::ByteStreamReader> stream) { | 397 scoped_ptr<content::ByteStreamReader> stream) { |
390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
391 | 399 |
392 net::BoundNetLog bound_net_log = | 400 // |bound_net_log| will be used for logging both the download item's and |
393 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 401 // the download file's events. |
| 402 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get()); |
394 | 403 |
395 // We create the DownloadItem before the DownloadFile because the | 404 // If info->download_id was unknown on entry to this function, it was |
396 // DownloadItem already needs to handle a state in which there is | 405 // assigned in CreateDownloadItem. |
397 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads) | 406 DownloadId download_id = info->download_id; |
398 DownloadItemImpl* download = | |
399 CreateDownloadItem(info.get(), bound_net_log); | |
400 scoped_ptr<content::DownloadFile> download_file( | |
401 file_factory_->CreateFile( | |
402 info->save_info, info->url(), info->referrer_url, | |
403 info->received_bytes, GenerateFileHash(), | |
404 stream.Pass(), bound_net_log, | |
405 download->DestinationObserverAsWeakPtr())); | |
406 download->Start(download_file.Pass()); | |
407 | 407 |
408 return download->GetGlobalId(); | 408 DownloadFileManager::CreateDownloadFileCallback callback( |
| 409 base::Bind(&DownloadManagerImpl::OnDownloadFileCreated, |
| 410 this, download_id.local())); |
| 411 |
| 412 BrowserThread::PostTask( |
| 413 BrowserThread::FILE, FROM_HERE, |
| 414 base::Bind(&DownloadFileManager::CreateDownloadFile, |
| 415 file_manager_, base::Passed(info.Pass()), |
| 416 base::Passed(stream.Pass()), |
| 417 make_scoped_refptr(this), |
| 418 GenerateFileHash(), bound_net_log, |
| 419 callback)); |
| 420 |
| 421 return download_id; |
| 422 } |
| 423 |
| 424 void DownloadManagerImpl::OnDownloadFileCreated( |
| 425 int32 download_id, content::DownloadInterruptReason reason) { |
| 426 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 427 OnDownloadInterrupted(download_id, reason); |
| 428 // TODO(rdsmith): It makes no sense to continue along the |
| 429 // regular download path after we've gotten an error. But it's |
| 430 // the way the code has historically worked, and this allows us |
| 431 // to get the download persisted and observers of the download manager |
| 432 // notified, so tests work. When we execute all side effects of cancel |
| 433 // (including queue removal) immedately rather than waiting for |
| 434 // persistence we should replace this comment with a "return;". |
| 435 } |
| 436 |
| 437 DownloadMap::iterator download_iter = active_downloads_.find(download_id); |
| 438 if (download_iter == active_downloads_.end()) |
| 439 return; |
| 440 |
| 441 DownloadItemImpl* download = download_iter->second; |
| 442 content::DownloadTargetCallback callback = |
| 443 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined, |
| 444 this, download_id); |
| 445 if (!delegate_ || !delegate_->DetermineDownloadTarget(download, callback)) { |
| 446 FilePath target_path = download->GetForcedFilePath(); |
| 447 // TODO(asanka): Determine a useful path if |target_path| is empty. |
| 448 callback.Run(target_path, |
| 449 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 450 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 451 target_path); |
| 452 } |
409 } | 453 } |
410 | 454 |
411 void DownloadManagerImpl::OnDownloadTargetDetermined( | 455 void DownloadManagerImpl::OnDownloadTargetDetermined( |
412 int32 download_id, | 456 int32 download_id, |
413 const FilePath& target_path, | 457 const FilePath& target_path, |
414 DownloadItem::TargetDisposition disposition, | 458 DownloadItem::TargetDisposition disposition, |
415 content::DownloadDangerType danger_type, | 459 content::DownloadDangerType danger_type, |
416 const FilePath& intermediate_path) { | 460 const FilePath& intermediate_path) { |
417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 461 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
418 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | 462 DownloadMap::iterator download_iter = active_downloads_.find(download_id); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { | 506 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 507 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
464 if (ContainsKey(downloads_, download_id)) | 508 if (ContainsKey(downloads_, download_id)) |
465 downloads_[download_id]->OnDownloadedFileRemoved(); | 509 downloads_[download_id]->OnDownloadedFileRemoved(); |
466 } | 510 } |
467 | 511 |
468 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { | 512 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
469 return browser_context_; | 513 return browser_context_; |
470 } | 514 } |
471 | 515 |
472 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem( | 516 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( |
473 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) { | 517 DownloadCreateInfo* info) { |
474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 518 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
475 | 519 |
| 520 net::BoundNetLog bound_net_log = |
| 521 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
476 if (!info->download_id.IsValid()) | 522 if (!info->download_id.IsValid()) |
477 info->download_id = GetNextId(); | 523 info->download_id = GetNextId(); |
478 DownloadItemImpl* download = item_factory_->CreateActiveItem( | 524 DownloadItemImpl* download = factory_->CreateActiveItem( |
479 this, *info, | 525 this, *info, |
480 scoped_ptr<DownloadRequestHandleInterface>( | 526 scoped_ptr<DownloadRequestHandleInterface>( |
481 new DownloadRequestHandle(info->request_handle)).Pass(), | 527 new DownloadRequestHandle(info->request_handle)).Pass(), |
482 bound_net_log); | 528 bound_net_log); |
483 | 529 |
484 DCHECK(!ContainsKey(downloads_, download->GetId())); | 530 DCHECK(!ContainsKey(downloads_, download->GetId())); |
485 downloads_[download->GetId()] = download; | 531 downloads_[download->GetId()] = download; |
486 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 532 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
487 active_downloads_[download->GetId()] = download; | 533 active_downloads_[download->GetId()] = download; |
488 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 534 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
489 | 535 |
490 return download; | 536 return bound_net_log; |
491 } | 537 } |
492 | 538 |
493 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 539 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
494 const FilePath& main_file_path, | 540 const FilePath& main_file_path, |
495 const GURL& page_url, | 541 const GURL& page_url, |
496 const std::string& mime_type, | 542 const std::string& mime_type, |
497 DownloadItem::Observer* observer) { | 543 DownloadItem::Observer* observer) { |
498 net::BoundNetLog bound_net_log = | 544 net::BoundNetLog bound_net_log = |
499 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 545 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
500 DownloadItemImpl* download = item_factory_->CreateSavePageItem( | 546 DownloadItemImpl* download = factory_->CreateSavePageItem( |
501 this, | 547 this, |
502 main_file_path, | 548 main_file_path, |
503 page_url, | 549 page_url, |
504 GetNextId(), | 550 GetNextId(), |
505 mime_type, | 551 mime_type, |
506 bound_net_log); | 552 bound_net_log); |
507 | 553 |
508 download->AddObserver(observer); | 554 download->AddObserver(observer); |
509 | 555 |
510 DCHECK(!ContainsKey(downloads_, download->GetId())); | 556 DCHECK(!ContainsKey(downloads_, download->GetId())); |
511 downloads_[download->GetId()] = download; | 557 downloads_[download->GetId()] = download; |
512 DCHECK(!SavePageData::Get(download)); | 558 DCHECK(!SavePageData::Get(download)); |
513 new SavePageData(download); | 559 new SavePageData(download); |
514 DCHECK(SavePageData::Get(download)); | 560 DCHECK(SavePageData::Get(download)); |
515 | 561 |
516 // TODO(benjhayden): Fire OnDownloadCreated for SavePackage downloads when | 562 // TODO(benjhayden): Fire OnDownloadCreated for SavePackage downloads when |
517 // we're comfortable with the user interacting with them. | 563 // we're comfortable with the user interacting with them. |
518 // FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 564 // FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
519 | 565 |
520 // Will notify the observer in the callback. | 566 // Will notify the observer in the callback. |
521 if (delegate_) | 567 if (delegate_) |
522 delegate_->AddItemToPersistentStore(download); | 568 delegate_->AddItemToPersistentStore(download); |
523 | 569 |
524 return download; | 570 return download; |
525 } | 571 } |
526 | 572 |
| 573 void DownloadManagerImpl::UpdateDownload(int32 download_id, |
| 574 int64 bytes_so_far, |
| 575 int64 bytes_per_sec, |
| 576 const std::string& hash_state) { |
| 577 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 578 DownloadMap::iterator it = active_downloads_.find(download_id); |
| 579 if (it != active_downloads_.end()) { |
| 580 DownloadItemImpl* download = it->second; |
| 581 if (download->IsInProgress()) { |
| 582 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); |
| 583 if (delegate_) |
| 584 delegate_->UpdateItemInPersistentStore(download); |
| 585 } |
| 586 } |
| 587 } |
| 588 |
| 589 void DownloadManagerImpl::OnResponseCompleted(int32 download_id, |
| 590 int64 size, |
| 591 const std::string& hash) { |
| 592 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 593 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 594 << " size = " << size; |
| 595 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 596 |
| 597 // If it's not in active_downloads_, that means it was cancelled; just |
| 598 // ignore the notification. |
| 599 if (active_downloads_.count(download_id) == 0) |
| 600 return; |
| 601 |
| 602 DownloadItemImpl* download = active_downloads_[download_id]; |
| 603 download->OnAllDataSaved(size, hash); |
| 604 MaybeCompleteDownload(download); |
| 605 } |
| 606 |
527 void DownloadManagerImpl::AssertStateConsistent( | 607 void DownloadManagerImpl::AssertStateConsistent( |
528 DownloadItemImpl* download) const { | 608 DownloadItemImpl* download) const { |
529 CHECK(ContainsKey(downloads_, download->GetId())); | 609 CHECK(ContainsKey(downloads_, download->GetId())); |
530 | 610 |
531 int64 state = download->GetState(); | 611 int64 state = download->GetState(); |
532 base::debug::Alias(&state); | 612 base::debug::Alias(&state); |
533 if (ContainsKey(active_downloads_, download->GetId())) { | 613 if (ContainsKey(active_downloads_, download->GetId())) { |
534 if (download->IsPersisted()) | 614 if (download->IsPersisted()) |
535 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 615 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
536 if (DownloadItem::IN_PROGRESS != download->GetState()) | 616 if (DownloadItem::IN_PROGRESS != download->GetState()) |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
638 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { | 718 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
639 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 719 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
640 | 720 |
641 VLOG(20) << __FUNCTION__ << "()" | 721 VLOG(20) << __FUNCTION__ << "()" |
642 << " download = " << download->DebugString(true); | 722 << " download = " << download->DebugString(true); |
643 | 723 |
644 RemoveFromActiveList(download); | 724 RemoveFromActiveList(download); |
645 // This function is called from the DownloadItem, so DI state | 725 // This function is called from the DownloadItem, so DI state |
646 // should already have been updated. | 726 // should already have been updated. |
647 AssertStateConsistent(download); | 727 AssertStateConsistent(download); |
| 728 |
| 729 DCHECK(file_manager_); |
| 730 download->OffThreadCancel(); |
| 731 } |
| 732 |
| 733 void DownloadManagerImpl::OnDownloadInterrupted( |
| 734 int32 download_id, |
| 735 content::DownloadInterruptReason reason) { |
| 736 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 737 |
| 738 if (!ContainsKey(active_downloads_, download_id)) |
| 739 return; |
| 740 active_downloads_[download_id]->Interrupt(reason); |
648 } | 741 } |
649 | 742 |
650 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 743 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
651 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 744 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
652 DCHECK(download); | 745 DCHECK(download); |
653 | 746 |
654 // Clean up will happen when the history system create callback runs if we | 747 // Clean up will happen when the history system create callback runs if we |
655 // don't have a valid db_handle yet. | 748 // don't have a valid db_handle yet. |
656 if (download->IsPersisted()) { | 749 if (download->IsPersisted()) { |
657 active_downloads_.erase(download->GetId()); | 750 active_downloads_.erase(download->GetId()); |
658 if (delegate_) | 751 if (delegate_) |
659 delegate_->UpdateItemInPersistentStore(download); | 752 delegate_->UpdateItemInPersistentStore(download); |
660 } | 753 } |
661 } | 754 } |
662 | 755 |
663 bool DownloadManagerImpl::GenerateFileHash() { | 756 bool DownloadManagerImpl::GenerateFileHash() { |
664 return delegate_ && delegate_->GenerateFileHash(); | 757 return delegate_ && delegate_->GenerateFileHash(); |
665 } | 758 } |
666 | 759 |
667 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( | |
668 scoped_ptr<content::DownloadFileFactory> file_factory) { | |
669 file_factory_ = file_factory.Pass(); | |
670 } | |
671 | |
672 content::DownloadFileFactory* | |
673 DownloadManagerImpl::GetDownloadFileFactoryForTesting() { | |
674 return file_factory_.get(); | |
675 } | |
676 | |
677 int DownloadManagerImpl::RemoveDownloadItems( | 760 int DownloadManagerImpl::RemoveDownloadItems( |
678 const DownloadItemImplVector& pending_deletes) { | 761 const DownloadItemImplVector& pending_deletes) { |
679 if (pending_deletes.empty()) | 762 if (pending_deletes.empty()) |
680 return 0; | 763 return 0; |
681 | 764 |
682 // Delete from internal maps. | 765 // Delete from internal maps. |
683 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 766 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
684 it != pending_deletes.end(); | 767 it != pending_deletes.end(); |
685 ++it) { | 768 ++it) { |
686 DownloadItemImpl* download = *it; | 769 DownloadItemImpl* download = *it; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 857 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
775 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 858 void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
776 std::vector<DownloadPersistentStoreInfo>* entries) { | 859 std::vector<DownloadPersistentStoreInfo>* entries) { |
777 history_size_ = entries->size(); | 860 history_size_ = entries->size(); |
778 for (size_t i = 0; i < entries->size(); ++i) { | 861 for (size_t i = 0; i < entries->size(); ++i) { |
779 int64 db_handle = entries->at(i).db_handle; | 862 int64 db_handle = entries->at(i).db_handle; |
780 base::debug::Alias(&db_handle); | 863 base::debug::Alias(&db_handle); |
781 | 864 |
782 net::BoundNetLog bound_net_log = | 865 net::BoundNetLog bound_net_log = |
783 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 866 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
784 DownloadItemImpl* download = item_factory_->CreatePersistedItem( | 867 DownloadItemImpl* download = factory_->CreatePersistedItem( |
785 this, GetNextId(), entries->at(i), bound_net_log); | 868 this, GetNextId(), entries->at(i), bound_net_log); |
786 DCHECK(!ContainsKey(downloads_, download->GetId())); | 869 DCHECK(!ContainsKey(downloads_, download->GetId())); |
787 downloads_[download->GetId()] = download; | 870 downloads_[download->GetId()] = download; |
788 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 871 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
789 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 872 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
790 << " download = " << download->DebugString(true); | 873 << " download = " << download->DebugString(true); |
791 } | 874 } |
792 NotifyModelChanged(); | 875 NotifyModelChanged(); |
793 CheckForHistoryFilesRemoval(); | 876 CheckForHistoryFilesRemoval(); |
794 } | 877 } |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
989 ++num_unopened; | 1072 ++num_unopened; |
990 } | 1073 } |
991 download_stats::RecordOpensOutstanding(num_unopened); | 1074 download_stats::RecordOpensOutstanding(num_unopened); |
992 } | 1075 } |
993 | 1076 |
994 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | 1077 void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
995 DownloadItemImpl* download) { | 1078 DownloadItemImpl* download) { |
996 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1079 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
997 // download->GetFullPath() is only expected to be meaningful after this | 1080 // download->GetFullPath() is only expected to be meaningful after this |
998 // callback is received. Therefore we can now add the download to a persistent | 1081 // callback is received. Therefore we can now add the download to a persistent |
999 // store. If the rename failed, we processed an interrupt | 1082 // store. If the rename failed, we receive an OnDownloadInterrupted() call |
1000 // before we receive the DownloadRenamedToIntermediateName() call. | 1083 // before we receive the DownloadRenamedToIntermediateName() call. |
1001 if (delegate_) { | 1084 if (delegate_) { |
1002 delegate_->AddItemToPersistentStore(download); | 1085 delegate_->AddItemToPersistentStore(download); |
1003 } else { | 1086 } else { |
1004 OnItemAddedToPersistentStore(download->GetId(), | 1087 OnItemAddedToPersistentStore(download->GetId(), |
1005 DownloadItem::kUninitializedHandle); | 1088 DownloadItem::kUninitializedHandle); |
1006 } | 1089 } |
1007 } | 1090 } |
1008 | 1091 |
1009 void DownloadManagerImpl::DownloadRenamedToFinalName( | 1092 void DownloadManagerImpl::DownloadRenamedToFinalName( |
1010 DownloadItemImpl* download) { | 1093 DownloadItemImpl* download) { |
1011 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1094 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1012 // If the rename failed, we processed an interrupt before we get here. | 1095 // If the rename failed, we receive an OnDownloadInterrupted() call before we |
| 1096 // receive the DownloadRenamedToFinalName() call. |
1013 if (delegate_) { | 1097 if (delegate_) { |
1014 delegate_->UpdatePathForItemInPersistentStore( | 1098 delegate_->UpdatePathForItemInPersistentStore( |
1015 download, download->GetFullPath()); | 1099 download, download->GetFullPath()); |
1016 } | 1100 } |
1017 } | 1101 } |
OLD | NEW |