Home | History | Annotate | Download | only in download
      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 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_
      6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 
     12 #include "base/containers/hash_tables.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "base/observer_list.h"
     17 #include "base/sequenced_task_runner_helpers.h"
     18 #include "base/synchronization/lock.h"
     19 #include "content/browser/download/download_item_impl_delegate.h"
     20 #include "content/common/content_export.h"
     21 #include "content/public/browser/download_manager.h"
     22 #include "content/public/browser/download_manager_delegate.h"
     23 #include "content/public/browser/download_url_parameters.h"
     24 
     25 namespace net {
     26 class BoundNetLog;
     27 }
     28 
     29 namespace content {
     30 class DownloadFileFactory;
     31 class DownloadItemFactory;
     32 class DownloadItemImpl;
     33 class DownloadRequestHandleInterface;
     34 
     35 class CONTENT_EXPORT DownloadManagerImpl : public DownloadManager,
     36                                            private DownloadItemImplDelegate {
     37  public:
     38   typedef base::Callback<void(DownloadItemImpl*)> DownloadItemImplCreated;
     39 
     40   // Caller guarantees that |net_log| will remain valid
     41   // for the lifetime of DownloadManagerImpl (until Shutdown() is called).
     42   DownloadManagerImpl(net::NetLog* net_log, BrowserContext* browser_context);
     43   virtual ~DownloadManagerImpl();
     44 
     45   // Implementation functions (not part of the DownloadManager interface).
     46 
     47   // Creates a download item for the SavePackage system.
     48   // Must be called on the UI thread.  Note that the DownloadManager
     49   // retains ownership.
     50   virtual void CreateSavePackageDownloadItem(
     51       const base::FilePath& main_file_path,
     52       const GURL& page_url,
     53       const std::string& mime_type,
     54       scoped_ptr<DownloadRequestHandleInterface> request_handle,
     55       const DownloadItemImplCreated& item_created);
     56 
     57   // Notifies DownloadManager about a successful completion of |download_item|.
     58   void OnSavePackageSuccessfullyFinished(DownloadItem* download_item);
     59 
     60   // DownloadManager functions.
     61   virtual void SetDelegate(DownloadManagerDelegate* delegate) OVERRIDE;
     62   virtual DownloadManagerDelegate* GetDelegate() const OVERRIDE;
     63   virtual void Shutdown() OVERRIDE;
     64   virtual void GetAllDownloads(DownloadVector* result) OVERRIDE;
     65   virtual void StartDownload(
     66       scoped_ptr<DownloadCreateInfo> info,
     67       scoped_ptr<ByteStreamReader> stream,
     68       const DownloadUrlParameters::OnStartedCallback& on_started) OVERRIDE;
     69   virtual int RemoveDownloadsBetween(base::Time remove_begin,
     70                                      base::Time remove_end) OVERRIDE;
     71   virtual int RemoveDownloads(base::Time remove_begin) OVERRIDE;
     72   virtual int RemoveAllDownloads() OVERRIDE;
     73   virtual void DownloadUrl(scoped_ptr<DownloadUrlParameters> params) OVERRIDE;
     74   virtual void AddObserver(Observer* observer) OVERRIDE;
     75   virtual void RemoveObserver(Observer* observer) OVERRIDE;
     76   virtual content::DownloadItem* CreateDownloadItem(
     77       uint32 id,
     78       const base::FilePath& current_path,
     79       const base::FilePath& target_path,
     80       const std::vector<GURL>& url_chain,
     81       const GURL& referrer_url,
     82       const std::string& mime_type,
     83       const std::string& original_mime_type,
     84       const base::Time& start_time,
     85       const base::Time& end_time,
     86       const std::string& etag,
     87       const std::string& last_modified,
     88       int64 received_bytes,
     89       int64 total_bytes,
     90       content::DownloadItem::DownloadState state,
     91       DownloadDangerType danger_type,
     92       DownloadInterruptReason interrupt_reason,
     93       bool opened) OVERRIDE;
     94   virtual int InProgressCount() const OVERRIDE;
     95   virtual int NonMaliciousInProgressCount() const OVERRIDE;
     96   virtual BrowserContext* GetBrowserContext() const OVERRIDE;
     97   virtual void CheckForHistoryFilesRemoval() OVERRIDE;
     98   virtual DownloadItem* GetDownload(uint32 id) OVERRIDE;
     99 
    100   // For testing; specifically, accessed from TestFileErrorInjector.
    101   void SetDownloadItemFactoryForTesting(
    102       scoped_ptr<DownloadItemFactory> item_factory);
    103   void SetDownloadFileFactoryForTesting(
    104       scoped_ptr<DownloadFileFactory> file_factory);
    105   virtual DownloadFileFactory* GetDownloadFileFactoryForTesting();
    106 
    107  private:
    108   typedef std::set<DownloadItem*> DownloadSet;
    109   typedef base::hash_map<uint32, DownloadItemImpl*> DownloadMap;
    110   typedef std::vector<DownloadItemImpl*> DownloadItemImplVector;
    111 
    112   // For testing.
    113   friend class DownloadManagerTest;
    114   friend class DownloadTest;
    115 
    116   void StartDownloadWithId(
    117       scoped_ptr<DownloadCreateInfo> info,
    118       scoped_ptr<ByteStreamReader> stream,
    119       const DownloadUrlParameters::OnStartedCallback& on_started,
    120       bool new_download,
    121       uint32 id);
    122 
    123   void CreateSavePackageDownloadItemWithId(
    124       const base::FilePath& main_file_path,
    125       const GURL& page_url,
    126       const std::string& mime_type,
    127       scoped_ptr<DownloadRequestHandleInterface> request_handle,
    128       const DownloadItemImplCreated& on_started,
    129       uint32 id);
    130 
    131   // Create a new active item based on the info.  Separate from
    132   // StartDownload() for testing.
    133   DownloadItemImpl* CreateActiveItem(uint32 id,
    134                                      const DownloadCreateInfo& info);
    135 
    136   // Get next download id. |callback| is called on the UI thread and may
    137   // be called synchronously.
    138   void GetNextId(const DownloadIdCallback& callback);
    139 
    140   // Called with the result of DownloadManagerDelegate::CheckForFileExistence.
    141   // Updates the state of the file and then notifies this update to the file's
    142   // observer.
    143   void OnFileExistenceChecked(uint32 download_id, bool result);
    144 
    145   // Overridden from DownloadItemImplDelegate
    146   // (Note that |GetBrowserContext| are present in both interfaces.)
    147   virtual void DetermineDownloadTarget(
    148       DownloadItemImpl* item, const DownloadTargetCallback& callback) OVERRIDE;
    149   virtual bool ShouldCompleteDownload(
    150       DownloadItemImpl* item, const base::Closure& complete_callback) OVERRIDE;
    151   virtual bool ShouldOpenFileBasedOnExtension(
    152       const base::FilePath& path) OVERRIDE;
    153   virtual bool ShouldOpenDownload(
    154       DownloadItemImpl* item,
    155       const ShouldOpenDownloadCallback& callback) OVERRIDE;
    156   virtual void CheckForFileRemoval(DownloadItemImpl* download_item) OVERRIDE;
    157   virtual void ResumeInterruptedDownload(
    158       scoped_ptr<content::DownloadUrlParameters> params,
    159       uint32 id) OVERRIDE;
    160   virtual void OpenDownload(DownloadItemImpl* download) OVERRIDE;
    161   virtual void ShowDownloadInShell(DownloadItemImpl* download) OVERRIDE;
    162   virtual void DownloadRemoved(DownloadItemImpl* download) OVERRIDE;
    163 
    164   // Factory for creation of downloads items.
    165   scoped_ptr<DownloadItemFactory> item_factory_;
    166 
    167   // Factory for the creation of download files.
    168   scoped_ptr<DownloadFileFactory> file_factory_;
    169 
    170   // |downloads_| is the owning set for all downloads known to the
    171   // DownloadManager.  This includes downloads started by the user in
    172   // this session, downloads initialized from the history system, and
    173   // "save page as" downloads.
    174   DownloadMap downloads_;
    175 
    176   int history_size_;
    177 
    178   // True if the download manager has been initialized and requires a shutdown.
    179   bool shutdown_needed_;
    180 
    181   // Observers that want to be notified of changes to the set of downloads.
    182   ObserverList<Observer> observers_;
    183 
    184   // The current active browser context.
    185   BrowserContext* browser_context_;
    186 
    187   // Allows an embedder to control behavior. Guaranteed to outlive this object.
    188   DownloadManagerDelegate* delegate_;
    189 
    190   net::NetLog* net_log_;
    191 
    192   base::WeakPtrFactory<DownloadManagerImpl> weak_factory_;
    193 
    194   DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl);
    195 };
    196 
    197 }  // namespace content
    198 
    199 #endif  // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_
    200