- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / download / download_manager_impl.cc
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.
4
5 #include "content/browser/download/download_manager_impl.h"
6
7 #include <iterator>
8
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/load_flags.h"
43 #include "net/base/request_priority.h"
44 #include "net/base/upload_bytes_element_reader.h"
45 #include "net/base/upload_data_stream.h"
46 #include "net/url_request/url_request_context.h"
47
48 namespace content {
49 namespace {
50
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));
60   request->set_load_flags(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(make_scoped_ptr(
67         net::UploadDataStream::CreateWithReader(reader.Pass(), 0)));
68   }
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::UploadDataStream(element_readers.Pass(), params->post_id())));
79   }
80
81   // If we're not at the beginning of the file, retrieve only the remaining
82   // portion.
83   bool has_last_modified = !params->last_modified().empty();
84   bool has_etag = !params->etag().empty();
85
86   // If we've asked for a range, we want to make sure that we only
87   // get that range if our current copy of the information is good.
88   // We shouldn't be asked to continue if we don't have a verifier.
89   DCHECK(params->offset() == 0 || has_etag || has_last_modified);
90
91   if (params->offset() > 0) {
92     request->SetExtraRequestHeaderByName(
93         "Range",
94         base::StringPrintf("bytes=%" PRId64 "-", params->offset()),
95         true);
96
97     if (has_last_modified) {
98       request->SetExtraRequestHeaderByName("If-Unmodified-Since",
99                                            params->last_modified(),
100                                            true);
101     }
102     if (has_etag) {
103       request->SetExtraRequestHeaderByName("If-Match", params->etag(), true);
104     }
105   }
106
107   for (DownloadUrlParameters::RequestHeadersType::const_iterator iter
108            = params->request_headers_begin();
109        iter != params->request_headers_end();
110        ++iter) {
111     request->SetExtraRequestHeaderByName(
112         iter->first, iter->second, false /*overwrite*/);
113   }
114
115   scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo());
116   save_info->file_path = params->file_path();
117   save_info->suggested_name = params->suggested_name();
118   save_info->offset = params->offset();
119   save_info->hash_state = params->hash_state();
120   save_info->prompt_for_save_location = params->prompt();
121   save_info->file_stream = params->GetFileStream();
122
123   ResourceDispatcherHost::Get()->BeginDownload(
124       request.Pass(),
125       params->referrer(),
126       params->content_initiated(),
127       params->resource_context(),
128       params->render_process_host_id(),
129       params->render_view_host_routing_id(),
130       params->prefer_cache(),
131       save_info.Pass(),
132       download_id,
133       params->callback());
134 }
135
136 class MapValueIteratorAdapter {
137  public:
138   explicit MapValueIteratorAdapter(
139       base::hash_map<int64, DownloadItem*>::const_iterator iter)
140     : iter_(iter) {
141   }
142   ~MapValueIteratorAdapter() {}
143
144   DownloadItem* operator*() { return iter_->second; }
145
146   MapValueIteratorAdapter& operator++() {
147     ++iter_;
148     return *this;
149   }
150
151   bool operator!=(const MapValueIteratorAdapter& that) const {
152     return iter_ != that.iter_;
153   }
154
155  private:
156   base::hash_map<int64, DownloadItem*>::const_iterator iter_;
157   // Allow copy and assign.
158 };
159
160 void EnsureNoPendingDownloadJobsOnFile(bool* result) {
161   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
162   *result = (DownloadFile::GetNumberOfDownloadFiles() == 0);
163   BrowserThread::PostTask(
164       BrowserThread::UI, FROM_HERE, base::MessageLoop::QuitClosure());
165 }
166
167 class DownloadItemFactoryImpl : public DownloadItemFactory {
168  public:
169   DownloadItemFactoryImpl() {}
170   virtual ~DownloadItemFactoryImpl() {}
171
172   virtual DownloadItemImpl* CreatePersistedItem(
173       DownloadItemImplDelegate* delegate,
174       uint32 download_id,
175       const base::FilePath& current_path,
176       const base::FilePath& target_path,
177       const std::vector<GURL>& url_chain,
178       const GURL& referrer_url,
179       const base::Time& start_time,
180       const base::Time& end_time,
181       const std::string& etag,
182       const std::string& last_modified,
183       int64 received_bytes,
184       int64 total_bytes,
185       DownloadItem::DownloadState state,
186       DownloadDangerType danger_type,
187       DownloadInterruptReason interrupt_reason,
188       bool opened,
189       const net::BoundNetLog& bound_net_log) OVERRIDE {
190     return new DownloadItemImpl(
191         delegate,
192         download_id,
193         current_path,
194         target_path,
195         url_chain,
196         referrer_url,
197         start_time,
198         end_time,
199         etag,
200         last_modified,
201         received_bytes,
202         total_bytes,
203         state,
204         danger_type,
205         interrupt_reason,
206         opened,
207         bound_net_log);
208   }
209
210   virtual DownloadItemImpl* CreateActiveItem(
211       DownloadItemImplDelegate* delegate,
212       uint32 download_id,
213       const DownloadCreateInfo& info,
214       const net::BoundNetLog& bound_net_log) OVERRIDE {
215     return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
216   }
217
218   virtual DownloadItemImpl* CreateSavePageItem(
219       DownloadItemImplDelegate* delegate,
220       uint32 download_id,
221       const base::FilePath& path,
222       const GURL& url,
223       const std::string& mime_type,
224       scoped_ptr<DownloadRequestHandleInterface> request_handle,
225       const net::BoundNetLog& bound_net_log) OVERRIDE {
226     return new DownloadItemImpl(delegate, download_id, path, url,
227                                 mime_type, request_handle.Pass(),
228                                 bound_net_log);
229   }
230 };
231
232 }  // namespace
233
234 DownloadManagerImpl::DownloadManagerImpl(
235     net::NetLog* net_log,
236     BrowserContext* browser_context)
237     : item_factory_(new DownloadItemFactoryImpl()),
238       file_factory_(new DownloadFileFactory()),
239       history_size_(0),
240       shutdown_needed_(true),
241       browser_context_(browser_context),
242       delegate_(NULL),
243       net_log_(net_log),
244       weak_factory_(this) {
245   DCHECK(browser_context);
246 }
247
248 DownloadManagerImpl::~DownloadManagerImpl() {
249   DCHECK(!shutdown_needed_);
250 }
251
252 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
253     uint32 id, const DownloadCreateInfo& info) {
254   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
255   DCHECK(!ContainsKey(downloads_, id));
256   net::BoundNetLog bound_net_log =
257       net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
258   DownloadItemImpl* download =
259       item_factory_->CreateActiveItem(this, id, info, bound_net_log);
260   downloads_[id] = download;
261   return download;
262 }
263
264 void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
265   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
266   if (delegate_) {
267     delegate_->GetNextId(callback);
268     return;
269   }
270   static uint32 next_id = content::DownloadItem::kInvalidId + 1;
271   callback.Run(next_id++);
272 }
273
274 void DownloadManagerImpl::DetermineDownloadTarget(
275     DownloadItemImpl* item, const DownloadTargetCallback& callback) {
276   // Note that this next call relies on
277   // DownloadItemImplDelegate::DownloadTargetCallback and
278   // DownloadManagerDelegate::DownloadTargetCallback having the same
279   // type.  If the types ever diverge, gasket code will need to
280   // be written here.
281   if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
282     base::FilePath target_path = item->GetForcedFilePath();
283     // TODO(asanka): Determine a useful path if |target_path| is empty.
284     callback.Run(target_path,
285                  DownloadItem::TARGET_DISPOSITION_OVERWRITE,
286                  DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
287                  target_path);
288   }
289 }
290
291 bool DownloadManagerImpl::ShouldCompleteDownload(
292     DownloadItemImpl* item, const base::Closure& complete_callback) {
293   if (!delegate_ ||
294       delegate_->ShouldCompleteDownload(item, complete_callback)) {
295     return true;
296   }
297   // Otherwise, the delegate has accepted responsibility to run the
298   // callback when the download is ready for completion.
299   return false;
300 }
301
302 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
303     const base::FilePath& path) {
304   if (!delegate_)
305     return false;
306
307   return delegate_->ShouldOpenFileBasedOnExtension(path);
308 }
309
310 bool DownloadManagerImpl::ShouldOpenDownload(
311     DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
312   if (!delegate_)
313     return true;
314
315   // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
316   // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
317   // to have the same type :-}.
318   return delegate_->ShouldOpenDownload(item, callback);
319 }
320
321 void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
322   delegate_ = delegate;
323 }
324
325 DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
326   return delegate_;
327 }
328
329 void DownloadManagerImpl::Shutdown() {
330   VLOG(20) << __FUNCTION__ << "()"
331            << " shutdown_needed_ = " << shutdown_needed_;
332   if (!shutdown_needed_)
333     return;
334   shutdown_needed_ = false;
335
336   FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
337   // TODO(benjhayden): Consider clearing observers_.
338
339   // If there are in-progress downloads, cancel them. This also goes for
340   // dangerous downloads which will remain in history if they aren't explicitly
341   // accepted or discarded. Canceling will remove the intermediate download
342   // file.
343   for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();
344        ++it) {
345     DownloadItemImpl* download = it->second;
346     if (download->GetState() == DownloadItem::IN_PROGRESS)
347       download->Cancel(false);
348   }
349   STLDeleteValues(&downloads_);
350   downloads_.clear();
351
352   // We'll have nothing more to report to the observers after this point.
353   observers_.Clear();
354
355   if (delegate_)
356     delegate_->Shutdown();
357   delegate_ = NULL;
358 }
359
360 void DownloadManagerImpl::StartDownload(
361     scoped_ptr<DownloadCreateInfo> info,
362     scoped_ptr<ByteStreamReader> stream,
363     const DownloadUrlParameters::OnStartedCallback& on_started) {
364   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
365   DCHECK(info);
366   uint32 download_id = info->download_id;
367   const bool new_download = (download_id == content::DownloadItem::kInvalidId);
368   base::Callback<void(uint32)> got_id(base::Bind(
369       &DownloadManagerImpl::StartDownloadWithId,
370       weak_factory_.GetWeakPtr(),
371       base::Passed(info.Pass()),
372       base::Passed(stream.Pass()),
373       on_started,
374       new_download));
375   if (new_download) {
376     GetNextId(got_id);
377   } else {
378     got_id.Run(download_id);
379   }
380 }
381
382 void DownloadManagerImpl::StartDownloadWithId(
383     scoped_ptr<DownloadCreateInfo> info,
384     scoped_ptr<ByteStreamReader> stream,
385     const DownloadUrlParameters::OnStartedCallback& on_started,
386     bool new_download,
387     uint32 id) {
388   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
389   DCHECK_NE(content::DownloadItem::kInvalidId, id);
390   DownloadItemImpl* download = NULL;
391   if (new_download) {
392     download = CreateActiveItem(id, *info);
393   } else {
394     DownloadMap::iterator item_iterator = downloads_.find(id);
395     // Trying to resume an interrupted download.
396     if (item_iterator == downloads_.end() ||
397         (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
398       // If the download is no longer known to the DownloadManager, then it was
399       // removed after it was resumed. Ignore. If the download is cancelled
400       // while resuming, then also ignore the request.
401       info->request_handle.CancelRequest();
402       if (!on_started.is_null())
403         on_started.Run(NULL, net::ERR_ABORTED);
404       return;
405     }
406     download = item_iterator->second;
407     DCHECK_EQ(DownloadItem::INTERRUPTED, download->GetState());
408   }
409
410   base::FilePath default_download_directory;
411   if (delegate_) {
412     base::FilePath website_save_directory;  // Unused
413     bool skip_dir_check = false;            // Unused
414     delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
415                           &default_download_directory, &skip_dir_check);
416   }
417
418   // Create the download file and start the download.
419   scoped_ptr<DownloadFile> download_file(
420       file_factory_->CreateFile(
421           info->save_info.Pass(), default_download_directory,
422           info->url(), info->referrer_url,
423           delegate_->GenerateFileHash(),
424           stream.Pass(), download->GetBoundNetLog(),
425           download->DestinationObserverAsWeakPtr()));
426
427   // Attach the client ID identifying the app to the AV system.
428   if (download_file.get() && delegate_) {
429     download_file->SetClientGuid(
430         delegate_->ApplicationClientIdForFileScanning());
431   }
432
433   scoped_ptr<DownloadRequestHandleInterface> req_handle(
434       new DownloadRequestHandle(info->request_handle));
435   download->Start(download_file.Pass(), req_handle.Pass());
436
437   // For interrupted downloads, Start() will transition the state to
438   // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
439   // For new downloads, we notify here, rather than earlier, so that
440   // the download_file is bound to download and all the usual
441   // setters (e.g. Cancel) work.
442   if (new_download)
443     FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
444
445   if (!on_started.is_null())
446     on_started.Run(download, net::OK);
447 }
448
449 void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
450   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
451   for (DownloadMap::iterator it = downloads_.begin();
452        it != downloads_.end(); ++it) {
453     DownloadItemImpl* item = it->second;
454     CheckForFileRemoval(item);
455   }
456 }
457
458 void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
459   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
460   if ((download_item->GetState() == DownloadItem::COMPLETE) &&
461       !download_item->GetFileExternallyRemoved() &&
462       delegate_) {
463     delegate_->CheckForFileExistence(
464         download_item,
465         base::Bind(&DownloadManagerImpl::OnFileExistenceChecked,
466                    weak_factory_.GetWeakPtr(), download_item->GetId()));
467   }
468 }
469
470 void DownloadManagerImpl::OnFileExistenceChecked(uint32 download_id,
471                                                  bool result) {
472   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
473   if (!result) {  // File does not exist.
474     if (ContainsKey(downloads_, download_id))
475       downloads_[download_id]->OnDownloadedFileRemoved();
476   }
477 }
478
479 BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
480   return browser_context_;
481 }
482
483 void DownloadManagerImpl::CreateSavePackageDownloadItem(
484     const base::FilePath& main_file_path,
485     const GURL& page_url,
486     const std::string& mime_type,
487     scoped_ptr<DownloadRequestHandleInterface> request_handle,
488     const DownloadItemImplCreated& item_created) {
489   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
490   GetNextId(base::Bind(
491       &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
492       weak_factory_.GetWeakPtr(),
493       main_file_path,
494       page_url,
495       mime_type,
496       base::Passed(request_handle.Pass()),
497       item_created));
498 }
499
500 void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
501     const base::FilePath& main_file_path,
502     const GURL& page_url,
503     const std::string& mime_type,
504     scoped_ptr<DownloadRequestHandleInterface> request_handle,
505     const DownloadItemImplCreated& item_created,
506     uint32 id) {
507   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
508   DCHECK_NE(content::DownloadItem::kInvalidId, id);
509   DCHECK(!ContainsKey(downloads_, id));
510   net::BoundNetLog bound_net_log =
511       net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
512   DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
513       this,
514       id,
515       main_file_path,
516       page_url,
517       mime_type,
518       request_handle.Pass(),
519       bound_net_log);
520   downloads_[download_item->GetId()] = download_item;
521   FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
522       this, download_item));
523   if (!item_created.is_null())
524     item_created.Run(download_item);
525 }
526
527 void DownloadManagerImpl::OnSavePackageSuccessfullyFinished(
528     DownloadItem* download_item) {
529   FOR_EACH_OBSERVER(Observer, observers_,
530                     OnSavePackageSuccessfullyFinished(this, download_item));
531 }
532
533 // Resume a download of a specific URL. We send the request to the
534 // ResourceDispatcherHost, and let it send us responses like a regular
535 // download.
536 void DownloadManagerImpl::ResumeInterruptedDownload(
537     scoped_ptr<content::DownloadUrlParameters> params,
538     uint32 id) {
539   RecordDownloadSource(INITIATED_BY_RESUMPTION);
540   BrowserThread::PostTask(
541       BrowserThread::IO,
542       FROM_HERE,
543       base::Bind(&BeginDownload, base::Passed(&params), id));
544 }
545
546 void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
547     scoped_ptr<DownloadItemFactory> item_factory) {
548   item_factory_ = item_factory.Pass();
549 }
550
551 void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
552     scoped_ptr<DownloadFileFactory> file_factory) {
553   file_factory_ = file_factory.Pass();
554 }
555
556 DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
557   return file_factory_.get();
558 }
559
560 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
561   if (!download)
562     return;
563
564   uint32 download_id = download->GetId();
565   if (downloads_.erase(download_id) == 0)
566     return;
567   delete download;
568 }
569
570 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
571                                                 base::Time remove_end) {
572   int count = 0;
573   DownloadMap::const_iterator it = downloads_.begin();
574   while (it != downloads_.end()) {
575     DownloadItemImpl* download = it->second;
576
577     // Increment done here to protect against invalidation below.
578     ++it;
579
580     if (download->GetStartTime() >= remove_begin &&
581         (remove_end.is_null() || download->GetStartTime() < remove_end) &&
582         (download->GetState() != DownloadItem::IN_PROGRESS)) {
583       // Erases the download from downloads_.
584       download->Remove();
585       count++;
586     }
587   }
588   return count;
589 }
590
591 int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
592   return RemoveDownloadsBetween(remove_begin, base::Time());
593 }
594
595 int DownloadManagerImpl::RemoveAllDownloads() {
596   // The null times make the date range unbounded.
597   int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time());
598   RecordClearAllSize(num_deleted);
599   return num_deleted;
600 }
601
602 void DownloadManagerImpl::DownloadUrl(
603     scoped_ptr<DownloadUrlParameters> params) {
604   if (params->post_id() >= 0) {
605     // Check this here so that the traceback is more useful.
606     DCHECK(params->prefer_cache());
607     DCHECK_EQ("POST", params->method());
608   }
609   BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
610       &BeginDownload, base::Passed(&params),
611       content::DownloadItem::kInvalidId));
612 }
613
614 void DownloadManagerImpl::AddObserver(Observer* observer) {
615   observers_.AddObserver(observer);
616 }
617
618 void DownloadManagerImpl::RemoveObserver(Observer* observer) {
619   observers_.RemoveObserver(observer);
620 }
621
622 DownloadItem* DownloadManagerImpl::CreateDownloadItem(
623     uint32 id,
624     const base::FilePath& current_path,
625     const base::FilePath& target_path,
626     const std::vector<GURL>& url_chain,
627     const GURL& referrer_url,
628     const base::Time& start_time,
629     const base::Time& end_time,
630     const std::string& etag,
631     const std::string& last_modified,
632     int64 received_bytes,
633     int64 total_bytes,
634     DownloadItem::DownloadState state,
635     DownloadDangerType danger_type,
636     DownloadInterruptReason interrupt_reason,
637     bool opened) {
638   if (ContainsKey(downloads_, id)) {
639     NOTREACHED();
640     return NULL;
641   }
642   DownloadItemImpl* item = item_factory_->CreatePersistedItem(
643       this,
644       id,
645       current_path,
646       target_path,
647       url_chain,
648       referrer_url,
649       start_time,
650       end_time,
651       etag,
652       last_modified,
653       received_bytes,
654       total_bytes,
655       state,
656       danger_type,
657       interrupt_reason,
658       opened,
659       net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
660   downloads_[id] = item;
661   FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
662   VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true);
663   return item;
664 }
665
666 int DownloadManagerImpl::InProgressCount() const {
667   int count = 0;
668   for (DownloadMap::const_iterator it = downloads_.begin();
669        it != downloads_.end(); ++it) {
670     if (it->second->GetState() == DownloadItem::IN_PROGRESS)
671       ++count;
672   }
673   return count;
674 }
675
676 int DownloadManagerImpl::NonMaliciousInProgressCount() const {
677   int count = 0;
678   for (DownloadMap::const_iterator it = downloads_.begin();
679        it != downloads_.end(); ++it) {
680     if (it->second->GetState() == DownloadItem::IN_PROGRESS &&
681         it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
682         it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
683         it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
684         it->second->GetDangerType() !=
685             DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
686       ++count;
687     }
688   }
689   return count;
690 }
691
692 DownloadItem* DownloadManagerImpl::GetDownload(uint32 download_id) {
693   return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL;
694 }
695
696 void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
697   for (DownloadMap::iterator it = downloads_.begin();
698        it != downloads_.end(); ++it) {
699     downloads->push_back(it->second);
700   }
701 }
702
703 void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
704   int num_unopened = 0;
705   for (DownloadMap::iterator it = downloads_.begin();
706        it != downloads_.end(); ++it) {
707     DownloadItemImpl* item = it->second;
708     if ((item->GetState() == DownloadItem::COMPLETE) &&
709         !item->GetOpened())
710       ++num_unopened;
711   }
712   RecordOpensOutstanding(num_unopened);
713
714   if (delegate_)
715     delegate_->OpenDownload(download);
716 }
717
718 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
719   if (delegate_)
720     delegate_->ShowDownloadInShell(download);
721 }
722
723 }  // namespace content