cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / browser / download / download_manager_impl.cc
blob30296cb82e7bffbcf3716adf1e166e31be1069a9
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
3 // found in the LICENSE file.
5 #include "content/browser/download/download_manager_impl.h"
7 #include <iterator>
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/debug/alias.h"
12 #include "base/i18n/case_conversion.h"
13 #include "base/logging.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/stl_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/sys_string_conversions.h"
18 #include "base/supports_user_data.h"
19 #include "base/synchronization/lock.h"
20 #include "build/build_config.h"
21 #include "content/browser/byte_stream.h"
22 #include "content/browser/download/download_create_info.h"
23 #include "content/browser/download/download_file_factory.h"
24 #include "content/browser/download/download_item_factory.h"
25 #include "content/browser/download/download_item_impl.h"
26 #include "content/browser/download/download_stats.h"
27 #include "content/browser/loader/resource_dispatcher_host_impl.h"
28 #include "content/browser/renderer_host/render_view_host_impl.h"
29 #include "content/browser/web_contents/web_contents_impl.h"
30 #include "content/public/browser/browser_context.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/content_browser_client.h"
33 #include "content/public/browser/download_interrupt_reasons.h"
34 #include "content/public/browser/download_manager_delegate.h"
35 #include "content/public/browser/download_url_parameters.h"
36 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/notification_types.h"
38 #include "content/public/browser/render_process_host.h"
39 #include "content/public/browser/resource_context.h"
40 #include "content/public/browser/web_contents_delegate.h"
41 #include "content/public/common/referrer.h"
42 #include "net/base/elements_upload_data_stream.h"
43 #include "net/base/load_flags.h"
44 #include "net/base/request_priority.h"
45 #include "net/base/upload_bytes_element_reader.h"
46 #include "net/url_request/url_request_context.h"
48 namespace content {
49 namespace {
51 void BeginDownload(scoped_ptr<DownloadUrlParameters> params,
52 uint32 download_id) {
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
54 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
55 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
56 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
57 scoped_ptr<net::URLRequest> request(
58 params->resource_context()->GetRequestContext()->CreateRequest(
59 params->url(), net::DEFAULT_PRIORITY, NULL, NULL));
60 request->SetLoadFlags(request->load_flags() | params->load_flags());
61 request->set_method(params->method());
62 if (!params->post_body().empty()) {
63 const std::string& body = params->post_body();
64 scoped_ptr<net::UploadElementReader> reader(
65 net::UploadOwnedBytesElementReader::CreateWithString(body));
66 request->set_upload(
67 net::ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0));
69 if (params->post_id() >= 0) {
70 // The POST in this case does not have an actual body, and only works
71 // when retrieving data from cache. This is done because we don't want
72 // to do a re-POST without user consent, and currently don't have a good
73 // plan on how to display the UI for that.
74 DCHECK(params->prefer_cache());
75 DCHECK_EQ("POST", params->method());
76 ScopedVector<net::UploadElementReader> element_readers;
77 request->set_upload(make_scoped_ptr(
78 new net::ElementsUploadDataStream(element_readers.Pass(),
79 params->post_id())));
82 // If we're not at the beginning of the file, retrieve only the remaining
83 // portion.
84 bool has_last_modified = !params->last_modified().empty();
85 bool has_etag = !params->etag().empty();
87 // If we've asked for a range, we want to make sure that we only
88 // get that range if our current copy of the information is good.
89 // We shouldn't be asked to continue if we don't have a verifier.
90 DCHECK(params->offset() == 0 || has_etag || has_last_modified);
92 if (params->offset() > 0) {
93 request->SetExtraRequestHeaderByName(
94 "Range",
95 base::StringPrintf("bytes=%" PRId64 "-", params->offset()),
96 true);
98 if (has_last_modified) {
99 request->SetExtraRequestHeaderByName("If-Unmodified-Since",
100 params->last_modified(),
101 true);
103 if (has_etag) {
104 request->SetExtraRequestHeaderByName("If-Match", params->etag(), true);
108 for (DownloadUrlParameters::RequestHeadersType::const_iterator iter
109 = params->request_headers_begin();
110 iter != params->request_headers_end();
111 ++iter) {
112 request->SetExtraRequestHeaderByName(
113 iter->first, iter->second, false /*overwrite*/);
116 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo());
117 save_info->file_path = params->file_path();
118 save_info->suggested_name = params->suggested_name();
119 save_info->offset = params->offset();
120 save_info->hash_state = params->hash_state();
121 save_info->prompt_for_save_location = params->prompt();
122 save_info->file = params->GetFile();
124 ResourceDispatcherHost::Get()->BeginDownload(
125 request.Pass(),
126 params->referrer(),
127 params->content_initiated(),
128 params->resource_context(),
129 params->render_process_host_id(),
130 params->render_view_host_routing_id(),
131 params->prefer_cache(),
132 save_info.Pass(),
133 download_id,
134 params->callback());
137 class MapValueIteratorAdapter {
138 public:
139 explicit MapValueIteratorAdapter(
140 base::hash_map<int64, DownloadItem*>::const_iterator iter)
141 : iter_(iter) {
143 ~MapValueIteratorAdapter() {}
145 DownloadItem* operator*() { return iter_->second; }
147 MapValueIteratorAdapter& operator++() {
148 ++iter_;
149 return *this;
152 bool operator!=(const MapValueIteratorAdapter& that) const {
153 return iter_ != that.iter_;
156 private:
157 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
158 // Allow copy and assign.
161 class DownloadItemFactoryImpl : public DownloadItemFactory {
162 public:
163 DownloadItemFactoryImpl() {}
164 ~DownloadItemFactoryImpl() override {}
166 DownloadItemImpl* CreatePersistedItem(
167 DownloadItemImplDelegate* delegate,
168 uint32 download_id,
169 const base::FilePath& current_path,
170 const base::FilePath& target_path,
171 const std::vector<GURL>& url_chain,
172 const GURL& referrer_url,
173 const std::string& mime_type,
174 const std::string& original_mime_type,
175 const base::Time& start_time,
176 const base::Time& end_time,
177 const std::string& etag,
178 const std::string& last_modified,
179 int64 received_bytes,
180 int64 total_bytes,
181 DownloadItem::DownloadState state,
182 DownloadDangerType danger_type,
183 DownloadInterruptReason interrupt_reason,
184 bool opened,
185 const net::BoundNetLog& bound_net_log) override {
186 return new DownloadItemImpl(
187 delegate,
188 download_id,
189 current_path,
190 target_path,
191 url_chain,
192 referrer_url,
193 mime_type,
194 original_mime_type,
195 start_time,
196 end_time,
197 etag,
198 last_modified,
199 received_bytes,
200 total_bytes,
201 state,
202 danger_type,
203 interrupt_reason,
204 opened,
205 bound_net_log);
208 DownloadItemImpl* CreateActiveItem(
209 DownloadItemImplDelegate* delegate,
210 uint32 download_id,
211 const DownloadCreateInfo& info,
212 const net::BoundNetLog& bound_net_log) override {
213 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
216 DownloadItemImpl* CreateSavePageItem(
217 DownloadItemImplDelegate* delegate,
218 uint32 download_id,
219 const base::FilePath& path,
220 const GURL& url,
221 const std::string& mime_type,
222 scoped_ptr<DownloadRequestHandleInterface> request_handle,
223 const net::BoundNetLog& bound_net_log) override {
224 return new DownloadItemImpl(delegate, download_id, path, url,
225 mime_type, request_handle.Pass(),
226 bound_net_log);
230 } // namespace
232 DownloadManagerImpl::DownloadManagerImpl(
233 net::NetLog* net_log,
234 BrowserContext* browser_context)
235 : item_factory_(new DownloadItemFactoryImpl()),
236 file_factory_(new DownloadFileFactory()),
237 history_size_(0),
238 shutdown_needed_(true),
239 browser_context_(browser_context),
240 delegate_(NULL),
241 net_log_(net_log),
242 weak_factory_(this) {
243 DCHECK(browser_context);
246 DownloadManagerImpl::~DownloadManagerImpl() {
247 DCHECK(!shutdown_needed_);
250 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
251 uint32 id, const DownloadCreateInfo& info) {
252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
253 DCHECK(!ContainsKey(downloads_, id));
254 net::BoundNetLog bound_net_log =
255 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
256 DownloadItemImpl* download =
257 item_factory_->CreateActiveItem(this, id, info, bound_net_log);
258 downloads_[id] = download;
259 return download;
262 void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
264 if (delegate_) {
265 delegate_->GetNextId(callback);
266 return;
268 static uint32 next_id = content::DownloadItem::kInvalidId + 1;
269 callback.Run(next_id++);
272 void DownloadManagerImpl::DetermineDownloadTarget(
273 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
274 // Note that this next call relies on
275 // DownloadItemImplDelegate::DownloadTargetCallback and
276 // DownloadManagerDelegate::DownloadTargetCallback having the same
277 // type. If the types ever diverge, gasket code will need to
278 // be written here.
279 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
280 base::FilePath target_path = item->GetForcedFilePath();
281 // TODO(asanka): Determine a useful path if |target_path| is empty.
282 callback.Run(target_path,
283 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
284 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
285 target_path);
289 bool DownloadManagerImpl::ShouldCompleteDownload(
290 DownloadItemImpl* item, const base::Closure& complete_callback) {
291 if (!delegate_ ||
292 delegate_->ShouldCompleteDownload(item, complete_callback)) {
293 return true;
295 // Otherwise, the delegate has accepted responsibility to run the
296 // callback when the download is ready for completion.
297 return false;
300 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
301 const base::FilePath& path) {
302 if (!delegate_)
303 return false;
305 return delegate_->ShouldOpenFileBasedOnExtension(path);
308 bool DownloadManagerImpl::ShouldOpenDownload(
309 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
310 if (!delegate_)
311 return true;
313 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
314 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
315 // to have the same type :-}.
316 return delegate_->ShouldOpenDownload(item, callback);
319 void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
320 delegate_ = delegate;
323 DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
324 return delegate_;
327 void DownloadManagerImpl::Shutdown() {
328 VLOG(20) << __FUNCTION__ << "()"
329 << " shutdown_needed_ = " << shutdown_needed_;
330 if (!shutdown_needed_)
331 return;
332 shutdown_needed_ = false;
334 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
335 // TODO(benjhayden): Consider clearing observers_.
337 // If there are in-progress downloads, cancel them. This also goes for
338 // dangerous downloads which will remain in history if they aren't explicitly
339 // accepted or discarded. Canceling will remove the intermediate download
340 // file.
341 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();
342 ++it) {
343 DownloadItemImpl* download = it->second;
344 if (download->GetState() == DownloadItem::IN_PROGRESS)
345 download->Cancel(false);
347 STLDeleteValues(&downloads_);
348 downloads_.clear();
350 // We'll have nothing more to report to the observers after this point.
351 observers_.Clear();
353 if (delegate_)
354 delegate_->Shutdown();
355 delegate_ = NULL;
358 void DownloadManagerImpl::StartDownload(
359 scoped_ptr<DownloadCreateInfo> info,
360 scoped_ptr<ByteStreamReader> stream,
361 const DownloadUrlParameters::OnStartedCallback& on_started) {
362 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
363 DCHECK(info);
364 uint32 download_id = info->download_id;
365 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
366 base::Callback<void(uint32)> got_id(base::Bind(
367 &DownloadManagerImpl::StartDownloadWithId,
368 weak_factory_.GetWeakPtr(),
369 base::Passed(info.Pass()),
370 base::Passed(stream.Pass()),
371 on_started,
372 new_download));
373 if (new_download) {
374 GetNextId(got_id);
375 } else {
376 got_id.Run(download_id);
380 void DownloadManagerImpl::StartDownloadWithId(
381 scoped_ptr<DownloadCreateInfo> info,
382 scoped_ptr<ByteStreamReader> stream,
383 const DownloadUrlParameters::OnStartedCallback& on_started,
384 bool new_download,
385 uint32 id) {
386 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
387 DCHECK_NE(content::DownloadItem::kInvalidId, id);
388 DownloadItemImpl* download = NULL;
389 if (new_download) {
390 download = CreateActiveItem(id, *info);
391 } else {
392 DownloadMap::iterator item_iterator = downloads_.find(id);
393 // Trying to resume an interrupted download.
394 if (item_iterator == downloads_.end() ||
395 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
396 // If the download is no longer known to the DownloadManager, then it was
397 // removed after it was resumed. Ignore. If the download is cancelled
398 // while resuming, then also ignore the request.
399 info->request_handle.CancelRequest();
400 if (!on_started.is_null())
401 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
402 return;
404 download = item_iterator->second;
405 DCHECK_EQ(DownloadItem::INTERRUPTED, download->GetState());
406 download->MergeOriginInfoOnResume(*info);
409 base::FilePath default_download_directory;
410 if (delegate_) {
411 base::FilePath website_save_directory; // Unused
412 bool skip_dir_check = false; // Unused
413 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
414 &default_download_directory, &skip_dir_check);
417 // Create the download file and start the download.
418 scoped_ptr<DownloadFile> download_file(
419 file_factory_->CreateFile(
420 info->save_info.Pass(), default_download_directory,
421 info->url(), info->referrer_url,
422 delegate_ && delegate_->GenerateFileHash(),
423 stream.Pass(), download->GetBoundNetLog(),
424 download->DestinationObserverAsWeakPtr()));
426 // Attach the client ID identifying the app to the AV system.
427 if (download_file.get() && delegate_) {
428 download_file->SetClientGuid(
429 delegate_->ApplicationClientIdForFileScanning());
432 scoped_ptr<DownloadRequestHandleInterface> req_handle(
433 new DownloadRequestHandle(info->request_handle));
434 download->Start(download_file.Pass(), req_handle.Pass());
436 // For interrupted downloads, Start() will transition the state to
437 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
438 // For new downloads, we notify here, rather than earlier, so that
439 // the download_file is bound to download and all the usual
440 // setters (e.g. Cancel) work.
441 if (new_download)
442 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
444 if (!on_started.is_null())
445 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
448 void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
449 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
450 for (DownloadMap::iterator it = downloads_.begin();
451 it != downloads_.end(); ++it) {
452 DownloadItemImpl* item = it->second;
453 CheckForFileRemoval(item);
457 void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
458 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
459 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
460 !download_item->GetFileExternallyRemoved() &&
461 delegate_) {
462 delegate_->CheckForFileExistence(
463 download_item,
464 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked,
465 weak_factory_.GetWeakPtr(), download_item->GetId()));
469 void DownloadManagerImpl::OnFileExistenceChecked(uint32 download_id,
470 bool result) {
471 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
472 if (!result) { // File does not exist.
473 if (ContainsKey(downloads_, download_id))
474 downloads_[download_id]->OnDownloadedFileRemoved();
478 BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
479 return browser_context_;
482 void DownloadManagerImpl::CreateSavePackageDownloadItem(
483 const base::FilePath& main_file_path,
484 const GURL& page_url,
485 const std::string& mime_type,
486 scoped_ptr<DownloadRequestHandleInterface> request_handle,
487 const DownloadItemImplCreated& item_created) {
488 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
489 GetNextId(base::Bind(
490 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
491 weak_factory_.GetWeakPtr(),
492 main_file_path,
493 page_url,
494 mime_type,
495 base::Passed(request_handle.Pass()),
496 item_created));
499 void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
500 const base::FilePath& main_file_path,
501 const GURL& page_url,
502 const std::string& mime_type,
503 scoped_ptr<DownloadRequestHandleInterface> request_handle,
504 const DownloadItemImplCreated& item_created,
505 uint32 id) {
506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
507 DCHECK_NE(content::DownloadItem::kInvalidId, id);
508 DCHECK(!ContainsKey(downloads_, id));
509 net::BoundNetLog bound_net_log =
510 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
511 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
512 this,
514 main_file_path,
515 page_url,
516 mime_type,
517 request_handle.Pass(),
518 bound_net_log);
519 downloads_[download_item->GetId()] = download_item;
520 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
521 this, download_item));
522 if (!item_created.is_null())
523 item_created.Run(download_item);
526 void DownloadManagerImpl::OnSavePackageSuccessfullyFinished(
527 DownloadItem* download_item) {
528 FOR_EACH_OBSERVER(Observer, observers_,
529 OnSavePackageSuccessfullyFinished(this, download_item));
532 // Resume a download of a specific URL. We send the request to the
533 // ResourceDispatcherHost, and let it send us responses like a regular
534 // download.
535 void DownloadManagerImpl::ResumeInterruptedDownload(
536 scoped_ptr<content::DownloadUrlParameters> params,
537 uint32 id) {
538 RecordDownloadSource(INITIATED_BY_RESUMPTION);
539 BrowserThread::PostTask(
540 BrowserThread::IO,
541 FROM_HERE,
542 base::Bind(&BeginDownload, base::Passed(&params), id));
545 void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
546 scoped_ptr<DownloadItemFactory> item_factory) {
547 item_factory_ = item_factory.Pass();
550 void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
551 scoped_ptr<DownloadFileFactory> file_factory) {
552 file_factory_ = file_factory.Pass();
555 DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
556 return file_factory_.get();
559 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
560 if (!download)
561 return;
563 uint32 download_id = download->GetId();
564 if (downloads_.erase(download_id) == 0)
565 return;
566 delete download;
569 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
570 base::Time remove_end) {
571 int count = 0;
572 DownloadMap::const_iterator it = downloads_.begin();
573 while (it != downloads_.end()) {
574 DownloadItemImpl* download = it->second;
576 // Increment done here to protect against invalidation below.
577 ++it;
579 if (download->GetStartTime() >= remove_begin &&
580 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
581 (download->GetState() != DownloadItem::IN_PROGRESS)) {
582 // Erases the download from downloads_.
583 download->Remove();
584 count++;
587 return count;
590 int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
591 return RemoveDownloadsBetween(remove_begin, base::Time());
594 int DownloadManagerImpl::RemoveAllDownloads() {
595 // The null times make the date range unbounded.
596 int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time());
597 RecordClearAllSize(num_deleted);
598 return num_deleted;
601 void DownloadManagerImpl::DownloadUrl(
602 scoped_ptr<DownloadUrlParameters> params) {
603 if (params->post_id() >= 0) {
604 // Check this here so that the traceback is more useful.
605 DCHECK(params->prefer_cache());
606 DCHECK_EQ("POST", params->method());
608 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
609 &BeginDownload, base::Passed(&params),
610 content::DownloadItem::kInvalidId));
613 void DownloadManagerImpl::AddObserver(Observer* observer) {
614 observers_.AddObserver(observer);
617 void DownloadManagerImpl::RemoveObserver(Observer* observer) {
618 observers_.RemoveObserver(observer);
621 DownloadItem* DownloadManagerImpl::CreateDownloadItem(
622 uint32 id,
623 const base::FilePath& current_path,
624 const base::FilePath& target_path,
625 const std::vector<GURL>& url_chain,
626 const GURL& referrer_url,
627 const std::string& mime_type,
628 const std::string& original_mime_type,
629 const base::Time& start_time,
630 const base::Time& end_time,
631 const std::string& etag,
632 const std::string& last_modified,
633 int64 received_bytes,
634 int64 total_bytes,
635 DownloadItem::DownloadState state,
636 DownloadDangerType danger_type,
637 DownloadInterruptReason interrupt_reason,
638 bool opened) {
639 if (ContainsKey(downloads_, id)) {
640 NOTREACHED();
641 return NULL;
643 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
644 this,
646 current_path,
647 target_path,
648 url_chain,
649 referrer_url,
650 mime_type,
651 original_mime_type,
652 start_time,
653 end_time,
654 etag,
655 last_modified,
656 received_bytes,
657 total_bytes,
658 state,
659 danger_type,
660 interrupt_reason,
661 opened,
662 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
663 downloads_[id] = item;
664 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
665 VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true);
666 return item;
669 int DownloadManagerImpl::InProgressCount() const {
670 int count = 0;
671 for (DownloadMap::const_iterator it = downloads_.begin();
672 it != downloads_.end(); ++it) {
673 if (it->second->GetState() == DownloadItem::IN_PROGRESS)
674 ++count;
676 return count;
679 int DownloadManagerImpl::NonMaliciousInProgressCount() const {
680 int count = 0;
681 for (DownloadMap::const_iterator it = downloads_.begin();
682 it != downloads_.end(); ++it) {
683 if (it->second->GetState() == DownloadItem::IN_PROGRESS &&
684 it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
685 it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
686 it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
687 it->second->GetDangerType() !=
688 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
689 ++count;
692 return count;
695 DownloadItem* DownloadManagerImpl::GetDownload(uint32 download_id) {
696 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL;
699 void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
700 for (DownloadMap::iterator it = downloads_.begin();
701 it != downloads_.end(); ++it) {
702 downloads->push_back(it->second);
706 void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
707 int num_unopened = 0;
708 for (DownloadMap::iterator it = downloads_.begin();
709 it != downloads_.end(); ++it) {
710 DownloadItemImpl* item = it->second;
711 if ((item->GetState() == DownloadItem::COMPLETE) &&
712 !item->GetOpened())
713 ++num_unopened;
715 RecordOpensOutstanding(num_unopened);
717 if (delegate_)
718 delegate_->OpenDownload(download);
721 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
722 if (delegate_)
723 delegate_->ShowDownloadInShell(download);
726 } // namespace content