Home | History | Annotate | Download | only in browser
      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 // The DownloadManager object manages the process of downloading, including
      6 // updates to the history system and providing the information for displaying
      7 // the downloads view in the Destinations tab. There is one DownloadManager per
      8 // active browser context in Chrome.
      9 //
     10 // Download observers:
     11 // Objects that are interested in notifications about new downloads, or progress
     12 // updates for a given download must implement one of the download observer
     13 // interfaces:
     14 //   DownloadManager::Observer:
     15 //     - allows observers, primarily views, to be notified when changes to the
     16 //       set of all downloads (such as new downloads, or deletes) occur
     17 // Use AddObserver() / RemoveObserver() on the appropriate download object to
     18 // receive state updates.
     19 //
     20 // Download state persistence:
     21 // The DownloadManager uses the history service for storing persistent
     22 // information about the state of all downloads. The history system maintains a
     23 // separate table for this called 'downloads'. At the point that the
     24 // DownloadManager is constructed, we query the history service for the state of
     25 // all persisted downloads.
     26 
     27 #ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_
     28 #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_
     29 
     30 #include <string>
     31 #include <vector>
     32 
     33 #include "base/basictypes.h"
     34 #include "base/callback.h"
     35 #include "base/files/file_path.h"
     36 #include "base/gtest_prod_util.h"
     37 #include "base/sequenced_task_runner_helpers.h"
     38 #include "base/time/time.h"
     39 #include "content/public/browser/download_interrupt_reasons.h"
     40 #include "content/public/browser/download_item.h"
     41 #include "content/public/browser/download_url_parameters.h"
     42 #include "net/base/net_errors.h"
     43 #include "net/base/net_log.h"
     44 
     45 class GURL;
     46 
     47 namespace content {
     48 
     49 class BrowserContext;
     50 class ByteStreamReader;
     51 class DownloadManagerDelegate;
     52 class DownloadQuery;
     53 class DownloadRequestHandle;
     54 struct DownloadCreateInfo;
     55 
     56 // Browser's download manager: manages all downloads and destination view.
     57 class CONTENT_EXPORT DownloadManager : public base::SupportsUserData::Data {
     58  public:
     59   virtual ~DownloadManager() {}
     60 
     61   // Sets/Gets the delegate for this DownloadManager. The delegate has to live
     62   // past its Shutdown method being called (by the DownloadManager).
     63   virtual void SetDelegate(DownloadManagerDelegate* delegate) = 0;
     64   virtual DownloadManagerDelegate* GetDelegate() const = 0;
     65 
     66   // Shutdown the download manager. Content calls this when BrowserContext is
     67   // being destructed. If the embedder needs this to be called earlier, it can
     68   // call it. In that case, the delegate's Shutdown() method will only be called
     69   // once.
     70   virtual void Shutdown() = 0;
     71 
     72   // Interface to implement for observers that wish to be informed of changes
     73   // to the DownloadManager's collection of downloads.
     74   class CONTENT_EXPORT Observer {
     75    public:
     76     // A DownloadItem was created. This item may be visible before the filename
     77     // is determined; in this case the return value of GetTargetFileName() will
     78     // be null.  This method may be called an arbitrary number of times, e.g.
     79     // when loading history on startup.  As a result, consumers should avoid
     80     // doing large amounts of work in OnDownloadCreated().  TODO(<whoever>):
     81     // When we've fully specified the possible states of the DownloadItem in
     82     // download_item.h, we should remove the caveat above.
     83     virtual void OnDownloadCreated(
     84         DownloadManager* manager, DownloadItem* item) {}
     85 
     86     // A SavePackage has successfully finished.
     87     virtual void OnSavePackageSuccessfullyFinished(
     88         DownloadManager* manager, DownloadItem* item) {}
     89 
     90     // Called when the DownloadManager is being destroyed to prevent Observers
     91     // from calling back to a stale pointer.
     92     virtual void ManagerGoingDown(DownloadManager* manager) {}
     93 
     94    protected:
     95     virtual ~Observer() {}
     96   };
     97 
     98   typedef std::vector<DownloadItem*> DownloadVector;
     99 
    100   // Add all download items to |downloads|, no matter the type or state, without
    101   // clearing |downloads| first.
    102   virtual void GetAllDownloads(DownloadVector* downloads) = 0;
    103 
    104   // Called by a download source (Currently DownloadResourceHandler)
    105   // to initiate the non-source portions of a download.
    106   // Returns the id assigned to the download.  If the DownloadCreateInfo
    107   // specifies an id, that id will be used.
    108   virtual void StartDownload(
    109       scoped_ptr<DownloadCreateInfo> info,
    110       scoped_ptr<ByteStreamReader> stream,
    111       const DownloadUrlParameters::OnStartedCallback& on_started) = 0;
    112 
    113   // Remove downloads after remove_begin (inclusive) and before remove_end
    114   // (exclusive). You may pass in null Time values to do an unbounded delete
    115   // in either direction.
    116   virtual int RemoveDownloadsBetween(base::Time remove_begin,
    117                                      base::Time remove_end) = 0;
    118 
    119   // Remove downloads will delete all downloads that have a timestamp that is
    120   // the same or more recent than |remove_begin|. The number of downloads
    121   // deleted is returned back to the caller.
    122   virtual int RemoveDownloads(base::Time remove_begin) = 0;
    123 
    124   // Remove all downloads will delete all downloads. The number of downloads
    125   // deleted is returned back to the caller.
    126   virtual int RemoveAllDownloads() = 0;
    127 
    128   // See DownloadUrlParameters for details about controlling the download.
    129   virtual void DownloadUrl(scoped_ptr<DownloadUrlParameters> parameters) = 0;
    130 
    131   // Allow objects to observe the download creation process.
    132   virtual void AddObserver(Observer* observer) = 0;
    133 
    134   // Remove a download observer from ourself.
    135   virtual void RemoveObserver(Observer* observer) = 0;
    136 
    137   // Called by the embedder, after creating the download manager, to let it know
    138   // about downloads from previous runs of the browser.
    139   virtual DownloadItem* CreateDownloadItem(
    140       uint32 id,
    141       const base::FilePath& current_path,
    142       const base::FilePath& target_path,
    143       const std::vector<GURL>& url_chain,
    144       const GURL& referrer_url,
    145       const std::string& mime_type,
    146       const std::string& original_mime_type,
    147       const base::Time& start_time,
    148       const base::Time& end_time,
    149       const std::string& etag,
    150       const std::string& last_modified,
    151       int64 received_bytes,
    152       int64 total_bytes,
    153       DownloadItem::DownloadState state,
    154       DownloadDangerType danger_type,
    155       DownloadInterruptReason interrupt_reason,
    156       bool opened) = 0;
    157 
    158   // The number of in progress (including paused) downloads.
    159   // Performance note: this loops over all items. If profiling finds that this
    160   // is too slow, use an AllDownloadItemNotifier to count in-progress items.
    161   virtual int InProgressCount() const = 0;
    162 
    163   // The number of in progress (including paused) downloads.
    164   // Performance note: this loops over all items. If profiling finds that this
    165   // is too slow, use an AllDownloadItemNotifier to count in-progress items.
    166   // This excludes downloads that are marked as malicious.
    167   virtual int NonMaliciousInProgressCount() const = 0;
    168 
    169   virtual BrowserContext* GetBrowserContext() const = 0;
    170 
    171   // Checks whether downloaded files still exist. Updates state of downloads
    172   // that refer to removed files. The check runs in the background and may
    173   // finish asynchronously after this method returns.
    174   virtual void CheckForHistoryFilesRemoval() = 0;
    175 
    176   // Get the download item for |id| if present, no matter what type of download
    177   // it is or state it's in.
    178   virtual DownloadItem* GetDownload(uint32 id) = 0;
    179 };
    180 
    181 }  // namespace content
    182 
    183 #endif  // CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_
    184