Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: content/browser/download/download_manager_impl.cc

Issue 10799005: Replace the DownloadFileManager with direct ownership (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed Windows compile error. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
19 #include "base/sys_string_conversions.h" 19 #include "base/sys_string_conversions.h"
20 #include "build/build_config.h" 20 #include "build/build_config.h"
21 #include "content/browser/download/byte_stream.h" 21 #include "content/browser/download/byte_stream.h"
22 #include "content/browser/download/download_create_info.h" 22 #include "content/browser/download/download_create_info.h"
23 #include "content/browser/download/download_file_manager.h" 23 #include "content/browser/download/download_file_factory.h"
24 #include "content/browser/download/download_item_impl.h" 24 #include "content/browser/download/download_item_impl.h"
25 #include "content/browser/download/download_stats.h" 25 #include "content/browser/download/download_stats.h"
26 #include "content/browser/renderer_host/render_view_host_impl.h" 26 #include "content/browser/renderer_host/render_view_host_impl.h"
27 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" 27 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
28 #include "content/browser/web_contents/web_contents_impl.h" 28 #include "content/browser/web_contents/web_contents_impl.h"
29 #include "content/public/browser/browser_context.h" 29 #include "content/public/browser/browser_context.h"
30 #include "content/public/browser/browser_thread.h" 30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/content_browser_client.h" 31 #include "content/public/browser/content_browser_client.h"
32 #include "content/public/browser/download_interrupt_reasons.h" 32 #include "content/public/browser/download_interrupt_reasons.h"
33 #include "content/public/browser/download_manager_delegate.h" 33 #include "content/public/browser/download_manager_delegate.h"
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 136
137 bool operator!=(const MapValueIteratorAdapter& that) const { 137 bool operator!=(const MapValueIteratorAdapter& that) const {
138 return iter_ != that.iter_; 138 return iter_ != that.iter_;
139 } 139 }
140 140
141 private: 141 private:
142 base::hash_map<int64, DownloadItem*>::const_iterator iter_; 142 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
143 // Allow copy and assign. 143 // Allow copy and assign.
144 }; 144 };
145 145
146 void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm, 146 void EnsureNoPendingDownloadJobsOnFile(bool* result) {
147 bool* result) { 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
148 if (dfm->NumberOfActiveDownloads()) 148 *result = (content::DownloadFile::GetNumberOfDownloadFiles() == 0);
149 *result = false;
150 BrowserThread::PostTask( 149 BrowserThread::PostTask(
151 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); 150 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
152 } 151 }
153 152
154 void EnsureNoPendingDownloadJobsOnIO(bool* result) {
155 scoped_refptr<DownloadFileManager> download_file_manager =
156 ResourceDispatcherHostImpl::Get()->download_file_manager();
157 BrowserThread::PostTask(
158 BrowserThread::FILE, FROM_HERE,
159 base::Bind(&EnsureNoPendingDownloadsOnFile,
160 download_file_manager, result));
161 }
162
163 class DownloadItemFactoryImpl : public content::DownloadItemFactory { 153 class DownloadItemFactoryImpl : public content::DownloadItemFactory {
164 public: 154 public:
165 DownloadItemFactoryImpl() {} 155 DownloadItemFactoryImpl() {}
166 virtual ~DownloadItemFactoryImpl() {} 156 virtual ~DownloadItemFactoryImpl() {}
167 157
168 virtual DownloadItemImpl* CreatePersistedItem( 158 virtual DownloadItemImpl* CreatePersistedItem(
169 DownloadItemImplDelegate* delegate, 159 DownloadItemImplDelegate* delegate,
170 content::DownloadId download_id, 160 content::DownloadId download_id,
171 const content::DownloadPersistentStoreInfo& info, 161 const content::DownloadPersistentStoreInfo& info,
172 const net::BoundNetLog& bound_net_log) OVERRIDE { 162 const net::BoundNetLog& bound_net_log) OVERRIDE {
(...skipping 24 matching lines...) Expand all
197 }; 187 };
198 188
199 } // namespace 189 } // namespace
200 190
201 namespace content { 191 namespace content {
202 192
203 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { 193 bool DownloadManager::EnsureNoPendingDownloadsForTesting() {
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
205 bool result = true; 195 bool result = true;
206 BrowserThread::PostTask( 196 BrowserThread::PostTask(
207 BrowserThread::IO, FROM_HERE, 197 BrowserThread::FILE, FROM_HERE,
208 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); 198 base::Bind(&EnsureNoPendingDownloadJobsOnFile, &result));
209 MessageLoop::current()->Run(); 199 MessageLoop::current()->Run();
210 return result; 200 return result;
211 } 201 }
212 202
213 } // namespace content 203 } // namespace content
214 204
215 DownloadManagerImpl::DownloadManagerImpl( 205 DownloadManagerImpl::DownloadManagerImpl(
216 DownloadFileManager* file_manager,
217 scoped_ptr<content::DownloadItemFactory> factory, 206 scoped_ptr<content::DownloadItemFactory> factory,
218 net::NetLog* net_log) 207 net::NetLog* net_log)
219 : factory_(factory.Pass()), 208 : factory_(factory.Pass()),
220 history_size_(0), 209 history_size_(0),
221 shutdown_needed_(false), 210 shutdown_needed_(false),
222 browser_context_(NULL), 211 browser_context_(NULL),
223 file_manager_(file_manager),
224 delegate_(NULL), 212 delegate_(NULL),
225 net_log_(net_log) { 213 net_log_(net_log) {
226 DCHECK(file_manager);
227 if (!factory_.get()) 214 if (!factory_.get())
228 factory_.reset(new DownloadItemFactoryImpl()); 215 factory_.reset(new DownloadItemFactoryImpl());
benjhayden 2012/08/10 15:56:41 rename to item_factory_?
Randy Smith (Not in Mondays) 2012/08/10 20:09:00 Done.
216 file_factory_.reset(new content::DownloadFileFactory());
229 } 217 }
230 218
231 DownloadManagerImpl::~DownloadManagerImpl() { 219 DownloadManagerImpl::~DownloadManagerImpl() {
232 DCHECK(!shutdown_needed_); 220 DCHECK(!shutdown_needed_);
233 } 221 }
234 222
235 DownloadId DownloadManagerImpl::GetNextId() { 223 DownloadId DownloadManagerImpl::GetNextId() {
236 DownloadId id; 224 DownloadId id;
237 if (delegate_) 225 if (delegate_)
238 id = delegate_->GetNextId(); 226 id = delegate_->GetNextId();
239 if (!id.IsValid()) { 227 if (!id.IsValid()) {
240 static int next_id; 228 static int next_id;
241 id = DownloadId(browser_context_, ++next_id); 229 id = DownloadId(browser_context_, ++next_id);
242 } 230 }
243 231
244 return id; 232 return id;
245 } 233 }
246 234
247 DownloadFileManager* DownloadManagerImpl::GetDownloadFileManager() { 235 content::DownloadFileFactory*
248 return file_manager_; 236 DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
237 return file_factory_.get();
238 }
239
240 void DownloadManagerImpl::DelegateStart(DownloadItemImpl* item) {
241 content::DownloadTargetCallback callback =
242 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined,
243 this, item->GetId());
244 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
245 FilePath target_path = item->GetForcedFilePath();
246 // TODO(asanka): Determine a useful path if |target_path| is empty.
247 callback.Run(target_path,
248 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
249 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
250 target_path);
251 }
249 } 252 }
250 253
251 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { 254 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) {
252 if (!delegate_) 255 if (!delegate_)
253 return true; 256 return true;
254 257
255 return delegate_->ShouldOpenDownload(item); 258 return delegate_->ShouldOpenDownload(item);
256 } 259 }
257 260
258 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { 261 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
(...skipping 15 matching lines...) Expand all
274 void DownloadManagerImpl::Shutdown() { 277 void DownloadManagerImpl::Shutdown() {
275 VLOG(20) << __FUNCTION__ << "()" 278 VLOG(20) << __FUNCTION__ << "()"
276 << " shutdown_needed_ = " << shutdown_needed_; 279 << " shutdown_needed_ = " << shutdown_needed_;
277 if (!shutdown_needed_) 280 if (!shutdown_needed_)
278 return; 281 return;
279 shutdown_needed_ = false; 282 shutdown_needed_ = false;
280 283
281 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); 284 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
282 // TODO(benjhayden): Consider clearing observers_. 285 // TODO(benjhayden): Consider clearing observers_.
283 286
284 DCHECK(file_manager_); 287 // The DownloadFiles will be canceled and deleted by their DownloadItems.
285 BrowserThread::PostTask(
286 BrowserThread::FILE, FROM_HERE,
287 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
288 file_manager_, make_scoped_refptr(this)));
289 288
290 AssertContainersConsistent(); 289 AssertContainersConsistent();
291 290
292 // Go through all downloads in downloads_. Dangerous ones we need to 291 // Go through all downloads in downloads_. Dangerous ones we need to
293 // remove on disk, and in progress ones we need to cancel. 292 // remove on disk, and in progress ones we need to cancel.
294 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { 293 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) {
295 DownloadItemImpl* download = it->second; 294 DownloadItemImpl* download = it->second;
296 295
297 // 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.
298 // 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
321 // 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
322 // anything left. 321 // anything left.
323 322
324 active_downloads_.clear(); 323 active_downloads_.clear();
325 STLDeleteValues(&downloads_); 324 STLDeleteValues(&downloads_);
326 downloads_.clear(); 325 downloads_.clear();
327 326
328 // 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.
329 observers_.Clear(); 328 observers_.Clear();
330 329
331 file_manager_ = NULL;
332 if (delegate_) 330 if (delegate_)
333 delegate_->Shutdown(); 331 delegate_->Shutdown();
334 delegate_ = NULL; 332 delegate_ = NULL;
335 } 333 }
336 334
337 void DownloadManagerImpl::GetTemporaryDownloads( 335 void DownloadManagerImpl::GetTemporaryDownloads(
338 const FilePath& dir_path, DownloadVector* result) { 336 const FilePath& dir_path, DownloadVector* result) {
339 DCHECK(result); 337 DCHECK(result);
340 338
341 for (DownloadMap::iterator it = downloads_.begin(); 339 for (DownloadMap::iterator it = downloads_.begin();
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { 387 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
390 DCHECK(browser_context); 388 DCHECK(browser_context);
391 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; 389 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
392 shutdown_needed_ = true; 390 shutdown_needed_ = true;
393 391
394 browser_context_ = browser_context; 392 browser_context_ = browser_context;
395 393
396 return true; 394 return true;
397 } 395 }
398 396
399 // We have received a message from DownloadFileManager about a new download.
400 content::DownloadId DownloadManagerImpl::StartDownload( 397 content::DownloadId DownloadManagerImpl::StartDownload(
401 scoped_ptr<DownloadCreateInfo> info, 398 scoped_ptr<DownloadCreateInfo> info,
402 scoped_ptr<content::ByteStreamReader> stream) { 399 scoped_ptr<content::ByteStreamReader> stream) {
403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 400 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
404 401
405 // |bound_net_log| will be used for logging both the download item's and 402 net::BoundNetLog bound_net_log =
406 // the download file's events. 403 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
407 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get());
408 404
409 // If info->download_id was unknown on entry to this function, it was 405 // We create the DownloadItem before the DownloadFile because the
410 // assigned in CreateDownloadItem. 406 // DownloadItem already needs to handle a state in which there is
411 DownloadId download_id = info->download_id; 407 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads)
408 DownloadItemImpl* download =
409 CreateDownloadItem(info.get(), bound_net_log);
410 scoped_ptr<content::DownloadFile> download_file(
411 file_factory_->CreateFile(
412 info->save_info, info->url(), info->referrer_url,
413 info->received_bytes, GenerateFileHash(),
414 stream.Pass(), bound_net_log,
415 download->DestinationObserverAsWeakPtr()));
416 download->Start(download_file.Pass());
412 417
413 DownloadFileManager::CreateDownloadFileCallback callback( 418 return download->GetGlobalId();
benjhayden 2012/08/10 15:56:41 could StartDownload() return the local id now that
Randy Smith (Not in Mondays) 2012/08/10 20:09:00 As discussed offline, punting for another CL. Thi
414 base::Bind(&DownloadManagerImpl::OnDownloadFileCreated,
415 this, download_id.local()));
416
417 BrowserThread::PostTask(
418 BrowserThread::FILE, FROM_HERE,
419 base::Bind(&DownloadFileManager::CreateDownloadFile,
420 file_manager_, base::Passed(info.Pass()),
421 base::Passed(stream.Pass()),
422 make_scoped_refptr(this),
423 GenerateFileHash(), bound_net_log,
424 callback));
425
426 return download_id;
427 }
428
429 void DownloadManagerImpl::OnDownloadFileCreated(
430 int32 download_id, content::DownloadInterruptReason reason) {
431 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) {
432 OnDownloadInterrupted(download_id, reason);
433 // TODO(rdsmith): It makes no sense to continue along the
434 // regular download path after we've gotten an error. But it's
435 // the way the code has historically worked, and this allows us
436 // to get the download persisted and observers of the download manager
437 // notified, so tests work. When we execute all side effects of cancel
438 // (including queue removal) immedately rather than waiting for
439 // persistence we should replace this comment with a "return;".
440 }
441
442 DownloadMap::iterator download_iter = active_downloads_.find(download_id);
443 if (download_iter == active_downloads_.end())
444 return;
445
446 DownloadItemImpl* download = download_iter->second;
447 content::DownloadTargetCallback callback =
448 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined,
449 this, download_id);
450 if (!delegate_ || !delegate_->DetermineDownloadTarget(download, callback)) {
451 FilePath target_path = download->GetForcedFilePath();
452 // TODO(asanka): Determine a useful path if |target_path| is empty.
453 callback.Run(target_path,
454 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
455 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
456 target_path);
457 }
458 } 419 }
459 420
460 void DownloadManagerImpl::OnDownloadTargetDetermined( 421 void DownloadManagerImpl::OnDownloadTargetDetermined(
461 int32 download_id, 422 int32 download_id,
462 const FilePath& target_path, 423 const FilePath& target_path,
463 DownloadItem::TargetDisposition disposition, 424 DownloadItem::TargetDisposition disposition,
464 content::DownloadDangerType danger_type, 425 content::DownloadDangerType danger_type,
465 const FilePath& intermediate_path) { 426 const FilePath& intermediate_path) {
466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
467 DownloadMap::iterator download_iter = active_downloads_.find(download_id); 428 DownloadMap::iterator download_iter = active_downloads_.find(download_id);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { 472 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) {
512 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 473 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
513 if (ContainsKey(downloads_, download_id)) 474 if (ContainsKey(downloads_, download_id))
514 downloads_[download_id]->OnDownloadedFileRemoved(); 475 downloads_[download_id]->OnDownloadedFileRemoved();
515 } 476 }
516 477
517 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { 478 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
518 return browser_context_; 479 return browser_context_;
519 } 480 }
520 481
521 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( 482 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem(
522 DownloadCreateInfo* info) { 483 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) {
523 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
524 485
525 net::BoundNetLog bound_net_log =
526 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
527 if (!info->download_id.IsValid()) 486 if (!info->download_id.IsValid())
528 info->download_id = GetNextId(); 487 info->download_id = GetNextId();
529 DownloadItemImpl* download = factory_->CreateActiveItem( 488 DownloadItemImpl* download = factory_->CreateActiveItem(
530 this, *info, 489 this, *info,
531 scoped_ptr<DownloadRequestHandleInterface>( 490 scoped_ptr<DownloadRequestHandleInterface>(
532 new DownloadRequestHandle(info->request_handle)).Pass(), 491 new DownloadRequestHandle(info->request_handle)).Pass(),
533 browser_context_->IsOffTheRecord(), bound_net_log); 492 browser_context_->IsOffTheRecord(), bound_net_log);
534 493
535 DCHECK(!ContainsKey(downloads_, download->GetId())); 494 DCHECK(!ContainsKey(downloads_, download->GetId()));
536 downloads_[download->GetId()] = download; 495 downloads_[download->GetId()] = download;
537 DCHECK(!ContainsKey(active_downloads_, download->GetId())); 496 DCHECK(!ContainsKey(active_downloads_, download->GetId()));
538 active_downloads_[download->GetId()] = download; 497 active_downloads_[download->GetId()] = download;
539 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); 498 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
540 499
541 return bound_net_log; 500 return download;
542 } 501 }
543 502
544 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( 503 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem(
545 const FilePath& main_file_path, 504 const FilePath& main_file_path,
546 const GURL& page_url, 505 const GURL& page_url,
547 bool is_otr, 506 bool is_otr,
548 const std::string& mime_type, 507 const std::string& mime_type,
549 DownloadItem::Observer* observer) { 508 DownloadItem::Observer* observer) {
550 net::BoundNetLog bound_net_log = 509 net::BoundNetLog bound_net_log =
551 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); 510 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
(...skipping 18 matching lines...) Expand all
570 // we're comfortable with the user interacting with them. 529 // we're comfortable with the user interacting with them.
571 // FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); 530 // FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
572 531
573 // Will notify the observer in the callback. 532 // Will notify the observer in the callback.
574 if (delegate_) 533 if (delegate_)
575 delegate_->AddItemToPersistentStore(download); 534 delegate_->AddItemToPersistentStore(download);
576 535
577 return download; 536 return download;
578 } 537 }
579 538
580 void DownloadManagerImpl::UpdateDownload(int32 download_id,
581 int64 bytes_so_far,
582 int64 bytes_per_sec,
583 const std::string& hash_state) {
584 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
585 DownloadMap::iterator it = active_downloads_.find(download_id);
586 if (it != active_downloads_.end()) {
587 DownloadItemImpl* download = it->second;
588 if (download->IsInProgress()) {
589 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state);
590 if (delegate_)
591 delegate_->UpdateItemInPersistentStore(download);
592 }
593 }
594 }
595
596 void DownloadManagerImpl::OnResponseCompleted(int32 download_id,
597 int64 size,
598 const std::string& hash) {
599 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
600 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
601 << " size = " << size;
602 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
603
604 // If it's not in active_downloads_, that means it was cancelled; just
605 // ignore the notification.
606 if (active_downloads_.count(download_id) == 0)
607 return;
608
609 DownloadItemImpl* download = active_downloads_[download_id];
610 download->OnAllDataSaved(size, hash);
611 MaybeCompleteDownload(download);
612 }
613
614 void DownloadManagerImpl::AssertStateConsistent( 539 void DownloadManagerImpl::AssertStateConsistent(
615 DownloadItemImpl* download) const { 540 DownloadItemImpl* download) const {
616 CHECK(ContainsKey(downloads_, download->GetId())); 541 CHECK(ContainsKey(downloads_, download->GetId()));
617 542
618 int64 state = download->GetState(); 543 int64 state = download->GetState();
619 base::debug::Alias(&state); 544 base::debug::Alias(&state);
620 if (ContainsKey(active_downloads_, download->GetId())) { 545 if (ContainsKey(active_downloads_, download->GetId())) {
621 if (download->IsPersisted()) 546 if (download->IsPersisted())
622 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 547 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
623 if (DownloadItem::IN_PROGRESS != download->GetState()) 548 if (DownloadItem::IN_PROGRESS != download->GetState())
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { 650 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) {
726 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 651 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
727 652
728 VLOG(20) << __FUNCTION__ << "()" 653 VLOG(20) << __FUNCTION__ << "()"
729 << " download = " << download->DebugString(true); 654 << " download = " << download->DebugString(true);
730 655
731 RemoveFromActiveList(download); 656 RemoveFromActiveList(download);
732 // This function is called from the DownloadItem, so DI state 657 // This function is called from the DownloadItem, so DI state
733 // should already have been updated. 658 // should already have been updated.
734 AssertStateConsistent(download); 659 AssertStateConsistent(download);
735
736 DCHECK(file_manager_);
737 download->OffThreadCancel();
738 }
739
740 void DownloadManagerImpl::OnDownloadInterrupted(
741 int32 download_id,
742 content::DownloadInterruptReason reason) {
743 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
744
745 if (!ContainsKey(active_downloads_, download_id))
746 return;
747 active_downloads_[download_id]->Interrupt(reason);
748 } 660 }
749 661
750 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { 662 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) {
751 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 663 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
752 DCHECK(download); 664 DCHECK(download);
753 665
754 // Clean up will happen when the history system create callback runs if we 666 // Clean up will happen when the history system create callback runs if we
755 // don't have a valid db_handle yet. 667 // don't have a valid db_handle yet.
756 if (download->IsPersisted()) { 668 if (download->IsPersisted()) {
757 active_downloads_.erase(download->GetId()); 669 active_downloads_.erase(download->GetId());
758 if (delegate_) 670 if (delegate_)
759 delegate_->UpdateItemInPersistentStore(download); 671 delegate_->UpdateItemInPersistentStore(download);
760 } 672 }
761 } 673 }
762 674
763 bool DownloadManagerImpl::GenerateFileHash() { 675 bool DownloadManagerImpl::GenerateFileHash() {
764 return delegate_ && delegate_->GenerateFileHash(); 676 return delegate_ && delegate_->GenerateFileHash();
765 } 677 }
766 678
679 void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
benjhayden 2012/08/10 15:56:41 This should be up by GetDownloadFileFactoryForTest
Randy Smith (Not in Mondays) 2012/08/10 20:09:00 Done.
680 scoped_ptr<content::DownloadFileFactory> file_factory) {
681 file_factory_ = file_factory.Pass();
682 }
683
767 int DownloadManagerImpl::RemoveDownloadItems( 684 int DownloadManagerImpl::RemoveDownloadItems(
768 const DownloadItemImplVector& pending_deletes) { 685 const DownloadItemImplVector& pending_deletes) {
769 if (pending_deletes.empty()) 686 if (pending_deletes.empty())
770 return 0; 687 return 0;
771 688
772 // Delete from internal maps. 689 // Delete from internal maps.
773 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); 690 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin();
774 it != pending_deletes.end(); 691 it != pending_deletes.end();
775 ++it) { 692 ++it) {
776 DownloadItemImpl* download = *it; 693 DownloadItemImpl* download = *it;
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
1079 ++num_unopened; 996 ++num_unopened;
1080 } 997 }
1081 download_stats::RecordOpensOutstanding(num_unopened); 998 download_stats::RecordOpensOutstanding(num_unopened);
1082 } 999 }
1083 1000
1084 void DownloadManagerImpl::DownloadRenamedToIntermediateName( 1001 void DownloadManagerImpl::DownloadRenamedToIntermediateName(
1085 DownloadItemImpl* download) { 1002 DownloadItemImpl* download) {
1086 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1003 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1087 // download->GetFullPath() is only expected to be meaningful after this 1004 // download->GetFullPath() is only expected to be meaningful after this
1088 // callback is received. Therefore we can now add the download to a persistent 1005 // callback is received. Therefore we can now add the download to a persistent
1089 // store. If the rename failed, we receive an OnDownloadInterrupted() call 1006 // store. If the rename failed, we processed an interrupt
1090 // before we receive the DownloadRenamedToIntermediateName() call. 1007 // before we receive the DownloadRenamedToIntermediateName() call.
1091 if (delegate_) { 1008 if (delegate_) {
1092 delegate_->AddItemToPersistentStore(download); 1009 delegate_->AddItemToPersistentStore(download);
1093 } else { 1010 } else {
1094 OnItemAddedToPersistentStore(download->GetId(), 1011 OnItemAddedToPersistentStore(download->GetId(),
1095 DownloadItem::kUninitializedHandle); 1012 DownloadItem::kUninitializedHandle);
1096 } 1013 }
1097 } 1014 }
1098 1015
1099 void DownloadManagerImpl::DownloadRenamedToFinalName( 1016 void DownloadManagerImpl::DownloadRenamedToFinalName(
1100 DownloadItemImpl* download) { 1017 DownloadItemImpl* download) {
1101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1018 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1102 // If the rename failed, we receive an OnDownloadInterrupted() call before we 1019 // If the rename failed, we processed an interrupt before we get here.
1103 // receive the DownloadRenamedToFinalName() call.
1104 if (delegate_) { 1020 if (delegate_) {
1105 delegate_->UpdatePathForItemInPersistentStore( 1021 delegate_->UpdatePathForItemInPersistentStore(
1106 download, download->GetFullPath()); 1022 download, download->GetFullPath());
1107 } 1023 }
1108 } 1024 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698