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

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

Issue 10823406: Remove DownloadFileManager in favor of direct ownership of DownloadFiles. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added weasel words to justify current notification source. 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/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_manager.h" 24 #include "content/browser/download/download_file_factory.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
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 EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm, 147 void EnsureNoPendingDownloadJobsOnFile(bool* result) {
148 bool* result) { 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
149 if (dfm->NumberOfActiveDownloads()) 149 *result = (content::DownloadFile::GetNumberOfDownloadFiles() == 0);
150 *result = false;
151 BrowserThread::PostTask( 150 BrowserThread::PostTask(
152 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); 151 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
153 } 152 }
154 153
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
164 class DownloadItemFactoryImpl : public content::DownloadItemFactory { 154 class DownloadItemFactoryImpl : public content::DownloadItemFactory {
165 public: 155 public:
166 DownloadItemFactoryImpl() {} 156 DownloadItemFactoryImpl() {}
167 virtual ~DownloadItemFactoryImpl() {} 157 virtual ~DownloadItemFactoryImpl() {}
168 158
169 virtual DownloadItemImpl* CreatePersistedItem( 159 virtual DownloadItemImpl* CreatePersistedItem(
170 DownloadItemImplDelegate* delegate, 160 DownloadItemImplDelegate* delegate,
171 content::DownloadId download_id, 161 content::DownloadId download_id,
172 const content::DownloadPersistentStoreInfo& info, 162 const content::DownloadPersistentStoreInfo& info,
173 const net::BoundNetLog& bound_net_log) OVERRIDE { 163 const net::BoundNetLog& bound_net_log) OVERRIDE {
(...skipping 22 matching lines...) Expand all
196 }; 186 };
197 187
198 } // namespace 188 } // namespace
199 189
200 namespace content { 190 namespace content {
201 191
202 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { 192 bool DownloadManager::EnsureNoPendingDownloadsForTesting() {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
204 bool result = true; 194 bool result = true;
205 BrowserThread::PostTask( 195 BrowserThread::PostTask(
206 BrowserThread::IO, FROM_HERE, 196 BrowserThread::FILE, FROM_HERE,
207 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); 197 base::Bind(&EnsureNoPendingDownloadJobsOnFile, &result));
208 MessageLoop::current()->Run(); 198 MessageLoop::current()->Run();
209 return result; 199 return result;
210 } 200 }
211 201
212 } // namespace content 202 } // namespace content
213 203
214 DownloadManagerImpl::DownloadManagerImpl( 204 DownloadManagerImpl::DownloadManagerImpl(
215 DownloadFileManager* file_manager, 205 scoped_ptr<content::DownloadItemFactory> item_factory,
216 scoped_ptr<content::DownloadItemFactory> factory,
217 net::NetLog* net_log) 206 net::NetLog* net_log)
218 : factory_(factory.Pass()), 207 : item_factory_(item_factory.Pass()),
219 history_size_(0), 208 history_size_(0),
220 shutdown_needed_(false), 209 shutdown_needed_(false),
221 browser_context_(NULL), 210 browser_context_(NULL),
222 file_manager_(file_manager),
223 delegate_(NULL), 211 delegate_(NULL),
224 net_log_(net_log) { 212 net_log_(net_log) {
225 DCHECK(file_manager); 213 if (!item_factory_.get())
226 if (!factory_.get()) 214 item_factory_.reset(new DownloadItemFactoryImpl());
227 factory_.reset(new DownloadItemFactoryImpl()); 215 file_factory_.reset(new content::DownloadFileFactory());
benjhayden 2012/08/22 19:07:31 Why isn't this in the init list?
Randy Smith (Not in Mondays) 2012/08/23 16:55:58 My initial response was going to be "because no on
228 } 216 }
229 217
230 DownloadManagerImpl::~DownloadManagerImpl() { 218 DownloadManagerImpl::~DownloadManagerImpl() {
231 DCHECK(!shutdown_needed_); 219 DCHECK(!shutdown_needed_);
232 } 220 }
233 221
234 DownloadId DownloadManagerImpl::GetNextId() { 222 DownloadId DownloadManagerImpl::GetNextId() {
235 DownloadId id; 223 DownloadId id;
236 if (delegate_) 224 if (delegate_)
237 id = delegate_->GetNextId(); 225 id = delegate_->GetNextId();
238 if (!id.IsValid()) { 226 if (!id.IsValid()) {
239 static int next_id; 227 static int next_id;
240 id = DownloadId(browser_context_, ++next_id); 228 id = DownloadId(browser_context_, ++next_id);
241 } 229 }
242 230
243 return id; 231 return id;
244 } 232 }
245 233
246 DownloadFileManager* DownloadManagerImpl::GetDownloadFileManager() { 234 void DownloadManagerImpl::DelegateStart(DownloadItemImpl* item) {
247 return file_manager_; 235 content::DownloadTargetCallback callback =
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 }
248 } 246 }
249 247
250 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { 248 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) {
251 if (!delegate_) 249 if (!delegate_)
252 return true; 250 return true;
253 251
254 return delegate_->ShouldOpenDownload(item); 252 return delegate_->ShouldOpenDownload(item);
255 } 253 }
256 254
257 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { 255 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
(...skipping 15 matching lines...) Expand all
273 void DownloadManagerImpl::Shutdown() { 271 void DownloadManagerImpl::Shutdown() {
274 VLOG(20) << __FUNCTION__ << "()" 272 VLOG(20) << __FUNCTION__ << "()"
275 << " shutdown_needed_ = " << shutdown_needed_; 273 << " shutdown_needed_ = " << shutdown_needed_;
276 if (!shutdown_needed_) 274 if (!shutdown_needed_)
277 return; 275 return;
278 shutdown_needed_ = false; 276 shutdown_needed_ = false;
279 277
280 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); 278 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
281 // TODO(benjhayden): Consider clearing observers_. 279 // TODO(benjhayden): Consider clearing observers_.
282 280
283 DCHECK(file_manager_); 281 // The DownloadFiles will be canceled and deleted by their DownloadItems.
284 BrowserThread::PostTask(
285 BrowserThread::FILE, FROM_HERE,
286 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
287 file_manager_, make_scoped_refptr(this)));
288 282
289 AssertContainersConsistent(); 283 AssertContainersConsistent();
290 284
291 // Go through all downloads in downloads_. Dangerous ones we need to 285 // Go through all downloads in downloads_. Dangerous ones we need to
292 // remove on disk, and in progress ones we need to cancel. 286 // remove on disk, and in progress ones we need to cancel.
293 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { 287 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) {
294 DownloadItemImpl* download = it->second; 288 DownloadItemImpl* download = it->second;
295 289
296 // Save iterator from potential erases in this set done by called code. 290 // Save iterator from potential erases in this set done by called code.
297 // Iterators after an erasure point are still valid for lists and 291 // Iterators after an erasure point are still valid for lists and
(...skipping 22 matching lines...) Expand all
320 // and all in progress downloads have been cancelled. We can now delete 314 // and all in progress downloads have been cancelled. We can now delete
321 // anything left. 315 // anything left.
322 316
323 active_downloads_.clear(); 317 active_downloads_.clear();
324 STLDeleteValues(&downloads_); 318 STLDeleteValues(&downloads_);
325 downloads_.clear(); 319 downloads_.clear();
326 320
327 // We'll have nothing more to report to the observers after this point. 321 // We'll have nothing more to report to the observers after this point.
328 observers_.Clear(); 322 observers_.Clear();
329 323
330 file_manager_ = NULL;
331 if (delegate_) 324 if (delegate_)
332 delegate_->Shutdown(); 325 delegate_->Shutdown();
333 delegate_ = NULL; 326 delegate_ = NULL;
334 } 327 }
335 328
336 void DownloadManagerImpl::GetTemporaryDownloads( 329 void DownloadManagerImpl::GetTemporaryDownloads(
337 const FilePath& dir_path, DownloadVector* result) { 330 const FilePath& dir_path, DownloadVector* result) {
338 DCHECK(result); 331 DCHECK(result);
339 332
340 for (DownloadMap::iterator it = downloads_.begin(); 333 for (DownloadMap::iterator it = downloads_.begin();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { 377 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
385 DCHECK(browser_context); 378 DCHECK(browser_context);
386 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; 379 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
387 shutdown_needed_ = true; 380 shutdown_needed_ = true;
388 381
389 browser_context_ = browser_context; 382 browser_context_ = browser_context;
390 383
391 return true; 384 return true;
392 } 385 }
393 386
394 // We have received a message from DownloadFileManager about a new download.
395 content::DownloadId DownloadManagerImpl::StartDownload( 387 content::DownloadId DownloadManagerImpl::StartDownload(
396 scoped_ptr<DownloadCreateInfo> info, 388 scoped_ptr<DownloadCreateInfo> info,
397 scoped_ptr<content::ByteStreamReader> stream) { 389 scoped_ptr<content::ByteStreamReader> stream) {
398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
399 391
400 // |bound_net_log| will be used for logging both the download item's and 392 net::BoundNetLog bound_net_log =
401 // the download file's events. 393 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
402 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get());
403 394
404 // If info->download_id was unknown on entry to this function, it was 395 // We create the DownloadItem before the DownloadFile because the
405 // assigned in CreateDownloadItem. 396 // DownloadItem already needs to handle a state in which there is
406 DownloadId download_id = info->download_id; 397 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads)
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 DownloadFileManager::CreateDownloadFileCallback callback( 408 return download->GetGlobalId();
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 }
453 } 409 }
454 410
455 void DownloadManagerImpl::OnDownloadTargetDetermined( 411 void DownloadManagerImpl::OnDownloadTargetDetermined(
456 int32 download_id, 412 int32 download_id,
457 const FilePath& target_path, 413 const FilePath& target_path,
458 DownloadItem::TargetDisposition disposition, 414 DownloadItem::TargetDisposition disposition,
459 content::DownloadDangerType danger_type, 415 content::DownloadDangerType danger_type,
460 const FilePath& intermediate_path) { 416 const FilePath& intermediate_path) {
461 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
462 DownloadMap::iterator download_iter = active_downloads_.find(download_id); 418 DownloadMap::iterator download_iter = active_downloads_.find(download_id);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { 462 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) {
507 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
508 if (ContainsKey(downloads_, download_id)) 464 if (ContainsKey(downloads_, download_id))
509 downloads_[download_id]->OnDownloadedFileRemoved(); 465 downloads_[download_id]->OnDownloadedFileRemoved();
510 } 466 }
511 467
512 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { 468 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
513 return browser_context_; 469 return browser_context_;
514 } 470 }
515 471
516 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( 472 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem(
517 DownloadCreateInfo* info) { 473 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) {
518 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
519 475
520 net::BoundNetLog bound_net_log =
521 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
522 if (!info->download_id.IsValid()) 476 if (!info->download_id.IsValid())
523 info->download_id = GetNextId(); 477 info->download_id = GetNextId();
524 DownloadItemImpl* download = factory_->CreateActiveItem( 478 DownloadItemImpl* download = item_factory_->CreateActiveItem(
525 this, *info, 479 this, *info,
526 scoped_ptr<DownloadRequestHandleInterface>( 480 scoped_ptr<DownloadRequestHandleInterface>(
527 new DownloadRequestHandle(info->request_handle)).Pass(), 481 new DownloadRequestHandle(info->request_handle)).Pass(),
528 bound_net_log); 482 bound_net_log);
529 483
530 DCHECK(!ContainsKey(downloads_, download->GetId())); 484 DCHECK(!ContainsKey(downloads_, download->GetId()));
531 downloads_[download->GetId()] = download; 485 downloads_[download->GetId()] = download;
532 DCHECK(!ContainsKey(active_downloads_, download->GetId())); 486 DCHECK(!ContainsKey(active_downloads_, download->GetId()));
533 active_downloads_[download->GetId()] = download; 487 active_downloads_[download->GetId()] = download;
534 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); 488 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
535 489
536 return bound_net_log; 490 return download;
537 } 491 }
538 492
539 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( 493 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem(
540 const FilePath& main_file_path, 494 const FilePath& main_file_path,
541 const GURL& page_url, 495 const GURL& page_url,
542 const std::string& mime_type, 496 const std::string& mime_type,
543 DownloadItem::Observer* observer) { 497 DownloadItem::Observer* observer) {
544 net::BoundNetLog bound_net_log = 498 net::BoundNetLog bound_net_log =
545 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); 499 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
546 DownloadItemImpl* download = factory_->CreateSavePageItem( 500 DownloadItemImpl* download = item_factory_->CreateSavePageItem(
547 this, 501 this,
548 main_file_path, 502 main_file_path,
549 page_url, 503 page_url,
550 GetNextId(), 504 GetNextId(),
551 mime_type, 505 mime_type,
552 bound_net_log); 506 bound_net_log);
553 507
554 download->AddObserver(observer); 508 download->AddObserver(observer);
555 509
556 DCHECK(!ContainsKey(downloads_, download->GetId())); 510 DCHECK(!ContainsKey(downloads_, download->GetId()));
557 downloads_[download->GetId()] = download; 511 downloads_[download->GetId()] = download;
558 DCHECK(!SavePageData::Get(download)); 512 DCHECK(!SavePageData::Get(download));
559 new SavePageData(download); 513 new SavePageData(download);
560 DCHECK(SavePageData::Get(download)); 514 DCHECK(SavePageData::Get(download));
561 515
562 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); 516 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
563 517
564 // Will notify the observer in the callback. 518 // Will notify the observer in the callback.
565 if (delegate_) 519 if (delegate_)
566 delegate_->AddItemToPersistentStore(download); 520 delegate_->AddItemToPersistentStore(download);
567 521
568 return download; 522 return download;
569 } 523 }
570 524
571 void DownloadManagerImpl::UpdateDownload(int32 download_id,
572 int64 bytes_so_far,
573 int64 bytes_per_sec,
574 const std::string& hash_state) {
575 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
576 DownloadMap::iterator it = active_downloads_.find(download_id);
577 if (it != active_downloads_.end()) {
578 DownloadItemImpl* download = it->second;
579 if (download->IsInProgress()) {
580 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state);
581 if (delegate_)
582 delegate_->UpdateItemInPersistentStore(download);
583 }
584 }
585 }
586
587 void DownloadManagerImpl::OnResponseCompleted(int32 download_id,
588 int64 size,
589 const std::string& hash) {
590 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
591 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
592 << " size = " << size;
593 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
594
595 // If it's not in active_downloads_, that means it was cancelled; just
596 // ignore the notification.
597 if (active_downloads_.count(download_id) == 0)
598 return;
599
600 DownloadItemImpl* download = active_downloads_[download_id];
601 download->OnAllDataSaved(size, hash);
602 MaybeCompleteDownload(download);
603 }
604
605 void DownloadManagerImpl::AssertStateConsistent( 525 void DownloadManagerImpl::AssertStateConsistent(
606 DownloadItemImpl* download) const { 526 DownloadItemImpl* download) const {
607 CHECK(ContainsKey(downloads_, download->GetId())); 527 CHECK(ContainsKey(downloads_, download->GetId()));
608 528
609 int64 state = download->GetState(); 529 int64 state = download->GetState();
610 base::debug::Alias(&state); 530 base::debug::Alias(&state);
611 if (ContainsKey(active_downloads_, download->GetId())) { 531 if (ContainsKey(active_downloads_, download->GetId())) {
612 if (download->IsPersisted()) 532 if (download->IsPersisted())
613 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 533 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
614 if (DownloadItem::IN_PROGRESS != download->GetState()) 534 if (DownloadItem::IN_PROGRESS != download->GetState())
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
716 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { 636 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) {
717 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 637 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
718 638
719 VLOG(20) << __FUNCTION__ << "()" 639 VLOG(20) << __FUNCTION__ << "()"
720 << " download = " << download->DebugString(true); 640 << " download = " << download->DebugString(true);
721 641
722 RemoveFromActiveList(download); 642 RemoveFromActiveList(download);
723 // This function is called from the DownloadItem, so DI state 643 // This function is called from the DownloadItem, so DI state
724 // should already have been updated. 644 // should already have been updated.
725 AssertStateConsistent(download); 645 AssertStateConsistent(download);
726
727 DCHECK(file_manager_);
728 download->OffThreadCancel();
729 }
730
731 void DownloadManagerImpl::OnDownloadInterrupted(
732 int32 download_id,
733 content::DownloadInterruptReason reason) {
734 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
735
736 if (!ContainsKey(active_downloads_, download_id))
737 return;
738 active_downloads_[download_id]->Interrupt(reason);
739 } 646 }
740 647
741 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { 648 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) {
742 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 649 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
743 DCHECK(download); 650 DCHECK(download);
744 651
745 // Clean up will happen when the history system create callback runs if we 652 // Clean up will happen when the history system create callback runs if we
746 // don't have a valid db_handle yet. 653 // don't have a valid db_handle yet.
747 if (download->IsPersisted()) { 654 if (download->IsPersisted()) {
748 active_downloads_.erase(download->GetId()); 655 active_downloads_.erase(download->GetId());
749 if (delegate_) 656 if (delegate_)
750 delegate_->UpdateItemInPersistentStore(download); 657 delegate_->UpdateItemInPersistentStore(download);
751 } 658 }
752 } 659 }
753 660
754 bool DownloadManagerImpl::GenerateFileHash() { 661 bool DownloadManagerImpl::GenerateFileHash() {
755 return delegate_ && delegate_->GenerateFileHash(); 662 return delegate_ && delegate_->GenerateFileHash();
756 } 663 }
757 664
665 void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
666 scoped_ptr<content::DownloadFileFactory> file_factory) {
667 file_factory_ = file_factory.Pass();
668 }
669
670 content::DownloadFileFactory*
671 DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
672 return file_factory_.get();
673 }
674
758 int DownloadManagerImpl::RemoveDownloadItems( 675 int DownloadManagerImpl::RemoveDownloadItems(
759 const DownloadItemImplVector& pending_deletes) { 676 const DownloadItemImplVector& pending_deletes) {
760 if (pending_deletes.empty()) 677 if (pending_deletes.empty())
761 return 0; 678 return 0;
762 679
763 // Delete from internal maps. 680 // Delete from internal maps.
764 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); 681 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin();
765 it != pending_deletes.end(); 682 it != pending_deletes.end();
766 ++it) { 683 ++it) {
767 DownloadItemImpl* download = *it; 684 DownloadItemImpl* download = *it;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
855 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). 772 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
856 void DownloadManagerImpl::OnPersistentStoreQueryComplete( 773 void DownloadManagerImpl::OnPersistentStoreQueryComplete(
857 std::vector<DownloadPersistentStoreInfo>* entries) { 774 std::vector<DownloadPersistentStoreInfo>* entries) {
858 history_size_ = entries->size(); 775 history_size_ = entries->size();
859 for (size_t i = 0; i < entries->size(); ++i) { 776 for (size_t i = 0; i < entries->size(); ++i) {
860 int64 db_handle = entries->at(i).db_handle; 777 int64 db_handle = entries->at(i).db_handle;
861 base::debug::Alias(&db_handle); 778 base::debug::Alias(&db_handle);
862 779
863 net::BoundNetLog bound_net_log = 780 net::BoundNetLog bound_net_log =
864 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); 781 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
865 DownloadItemImpl* download = factory_->CreatePersistedItem( 782 DownloadItemImpl* download = item_factory_->CreatePersistedItem(
866 this, GetNextId(), entries->at(i), bound_net_log); 783 this, GetNextId(), entries->at(i), bound_net_log);
867 DCHECK(!ContainsKey(downloads_, download->GetId())); 784 DCHECK(!ContainsKey(downloads_, download->GetId()));
868 downloads_[download->GetId()] = download; 785 downloads_[download->GetId()] = download;
869 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); 786 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
870 VLOG(20) << __FUNCTION__ << "()" << i << ">" 787 VLOG(20) << __FUNCTION__ << "()" << i << ">"
871 << " download = " << download->DebugString(true); 788 << " download = " << download->DebugString(true);
872 } 789 }
873 NotifyModelChanged(); 790 NotifyModelChanged();
874 CheckForHistoryFilesRemoval(); 791 CheckForHistoryFilesRemoval();
875 } 792 }
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 // Finalize this download if it finished before the history callback. 960 // Finalize this download if it finished before the history callback.
1044 if (!item->IsInProgress()) 961 if (!item->IsInProgress())
1045 SavePageDownloadFinished(item); 962 SavePageDownloadFinished(item);
1046 } 963 }
1047 964
1048 void DownloadManagerImpl::SavePageDownloadFinished( 965 void DownloadManagerImpl::SavePageDownloadFinished(
1049 content::DownloadItem* download) { 966 content::DownloadItem* download) {
1050 if (download->IsPersisted()) { 967 if (download->IsPersisted()) {
1051 if (delegate_) 968 if (delegate_)
1052 delegate_->UpdateItemInPersistentStore(download); 969 delegate_->UpdateItemInPersistentStore(download);
1053 if (download->IsComplete())
1054 content::NotificationService::current()->Notify(
1055 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
1056 content::Source<DownloadManager>(this),
1057 content::Details<DownloadItem>(download));
1058 } 970 }
1059 } 971 }
1060 972
1061 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { 973 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) {
1062 if (delegate_) 974 if (delegate_)
1063 delegate_->UpdateItemInPersistentStore(download); 975 delegate_->UpdateItemInPersistentStore(download);
1064 int num_unopened = 0; 976 int num_unopened = 0;
1065 for (DownloadMap::iterator it = downloads_.begin(); 977 for (DownloadMap::iterator it = downloads_.begin();
1066 it != downloads_.end(); ++it) { 978 it != downloads_.end(); ++it) {
1067 DownloadItemImpl* item = it->second; 979 DownloadItemImpl* item = it->second;
1068 if (item->IsComplete() && 980 if (item->IsComplete() &&
1069 !item->GetOpened()) 981 !item->GetOpened())
1070 ++num_unopened; 982 ++num_unopened;
1071 } 983 }
1072 download_stats::RecordOpensOutstanding(num_unopened); 984 download_stats::RecordOpensOutstanding(num_unopened);
1073 } 985 }
1074 986
1075 void DownloadManagerImpl::DownloadRenamedToIntermediateName( 987 void DownloadManagerImpl::DownloadRenamedToIntermediateName(
1076 DownloadItemImpl* download) { 988 DownloadItemImpl* download) {
1077 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 989 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1078 // download->GetFullPath() is only expected to be meaningful after this 990 // download->GetFullPath() is only expected to be meaningful after this
1079 // callback is received. Therefore we can now add the download to a persistent 991 // callback is received. Therefore we can now add the download to a persistent
1080 // store. If the rename failed, we receive an OnDownloadInterrupted() call 992 // store. If the rename failed, we processed an interrupt
1081 // before we receive the DownloadRenamedToIntermediateName() call. 993 // before we receive the DownloadRenamedToIntermediateName() call.
1082 if (delegate_) { 994 if (delegate_) {
1083 delegate_->AddItemToPersistentStore(download); 995 delegate_->AddItemToPersistentStore(download);
1084 } else { 996 } else {
1085 OnItemAddedToPersistentStore(download->GetId(), 997 OnItemAddedToPersistentStore(download->GetId(),
1086 DownloadItem::kUninitializedHandle); 998 DownloadItem::kUninitializedHandle);
1087 } 999 }
1088 } 1000 }
1089 1001
1090 void DownloadManagerImpl::DownloadRenamedToFinalName( 1002 void DownloadManagerImpl::DownloadRenamedToFinalName(
1091 DownloadItemImpl* download) { 1003 DownloadItemImpl* download) {
1092 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1004 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1093 // If the rename failed, we receive an OnDownloadInterrupted() call before we 1005 // If the rename failed, we processed an interrupt before we get here.
1094 // receive the DownloadRenamedToFinalName() call.
1095 if (delegate_) { 1006 if (delegate_) {
1096 delegate_->UpdatePathForItemInPersistentStore( 1007 delegate_->UpdatePathForItemInPersistentStore(
1097 download, download->GetFullPath()); 1008 download, download->GetFullPath());
1098 } 1009 }
1099 } 1010 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698