download_manager.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.h"
39#include "content/public/browser/download_id.h"
40#include "content/public/browser/download_interrupt_reasons.h"
41#include "content/public/browser/download_item.h"
42#include "net/base/net_errors.h"
43#include "net/base/net_log.h"
44
45class GURL;
46
47namespace content {
48
49class BrowserContext;
50class ByteStreamReader;
51class DownloadManagerDelegate;
52class DownloadQuery;
53class DownloadRequestHandle;
54class DownloadUrlParameters;
55struct DownloadCreateInfo;
56struct DownloadRetrieveInfo;
57
58// Browser's download manager: manages all downloads and destination view.
59class CONTENT_EXPORT DownloadManager
60    : public base::RefCountedThreadSafe<DownloadManager> {
61 public:
62  // Sets/Gets the delegate for this DownloadManager. The delegate has to live
63  // past its Shutdown method being called (by the DownloadManager).
64  virtual void SetDelegate(DownloadManagerDelegate* delegate) = 0;
65  virtual DownloadManagerDelegate* GetDelegate() const = 0;
66
67  // Shutdown the download manager. Content calls this when BrowserContext is
68  // being destructed. If the embedder needs this to be called earlier, it can
69  // call it. In that case, the delegate's Shutdown() method will only be called
70  // once.
71  virtual void Shutdown() = 0;
72
73  // Interface to implement for observers that wish to be informed of changes
74  // to the DownloadManager's collection of downloads.
75  class CONTENT_EXPORT Observer {
76   public:
77    // A DownloadItem was created. This item may be visible before the filename
78    // is determined; in this case the return value of GetTargetFileName() will
79    // be null.  This method may be called an arbitrary number of times, e.g.
80    // when loading history on startup.  As a result, consumers should avoid
81    // doing large amounts of work in OnDownloadCreated().  TODO(<whoever>):
82    // When we've fully specified the possible states of the DownloadItem in
83    // download_item.h, we should remove the caveat above.
84    virtual void OnDownloadCreated(
85        DownloadManager* manager, DownloadItem* item) {}
86
87    // A SavePackage has successfully finished.
88    virtual void OnSavePackageSuccessfullyFinished(
89        DownloadManager* manager, DownloadItem* item) {}
90
91    // Called when the DownloadManager is being destroyed to prevent Observers
92    // from calling back to a stale pointer.
93    virtual void ManagerGoingDown(DownloadManager* manager) {}
94
95   protected:
96    virtual ~Observer() {}
97  };
98
99  typedef std::vector<DownloadItem*> DownloadVector;
100
101  // Add all download items to |downloads|, no matter the type or state, without
102  // clearing |downloads| first.
103  virtual void GetAllDownloads(DownloadVector* downloads) = 0;
104
105  // Returns true if initialized properly.
106  virtual bool Init(BrowserContext* browser_context) = 0;
107
108  // Called by a download source (Currently DownloadResourceHandler)
109  // to initiate the non-source portions of a download.
110  // Returns the id assigned to the download.  If the DownloadCreateInfo
111  // specifies an id, that id will be used.
112  virtual DownloadItem* StartDownload(
113      scoped_ptr<DownloadCreateInfo> info,
114      scoped_ptr<ByteStreamReader> stream) = 0;
115
116  // Offthread target for cancelling a particular download.  Will be a no-op
117  // if the download has already been cancelled.
118  virtual void CancelDownload(int32 download_id) = 0;
119
120  // Remove downloads after remove_begin (inclusive) and before remove_end
121  // (exclusive). You may pass in null Time values to do an unbounded delete
122  // in either direction.
123  virtual int RemoveDownloadsBetween(base::Time remove_begin,
124                                     base::Time remove_end) = 0;
125
126  // Remove downloads will delete all downloads that have a timestamp that is
127  // the same or more recent than |remove_begin|. The number of downloads
128  // deleted is returned back to the caller.
129  virtual int RemoveDownloads(base::Time remove_begin) = 0;
130
131  // Remove all downloads will delete all downloads. The number of downloads
132  // deleted is returned back to the caller.
133  virtual int RemoveAllDownloads() = 0;
134
135  // See DownloadUrlParameters for details about controlling the download.
136  virtual void DownloadUrl(scoped_ptr<DownloadUrlParameters> parameters) = 0;
137
138  // Allow objects to observe the download creation process.
139  virtual void AddObserver(Observer* observer) = 0;
140
141  // Remove a download observer from ourself.
142  virtual void RemoveObserver(Observer* observer) = 0;
143
144  // Called by the embedder, after creating the download manager, to let it know
145  // about downloads from previous runs of the browser.
146  virtual DownloadItem* CreateDownloadItem(
147      const base::FilePath& current_path,
148      const base::FilePath& target_path,
149      const std::vector<GURL>& url_chain,
150      const GURL& referrer_url,
151      const base::Time& start_time,
152      const base::Time& end_time,
153      int64 received_bytes,
154      int64 total_bytes,
155      DownloadItem::DownloadState state,
156      DownloadDangerType danger_type,
157      DownloadInterruptReason interrupt_reason,
158      bool opened) = 0;
159
160  // The number of in progress (including paused) downloads.
161  // Performance note: this loops over all items. If profiling finds that this
162  // is too slow, use an AllDownloadItemNotifier to count in-progress items.
163  virtual int InProgressCount() const = 0;
164
165  virtual BrowserContext* GetBrowserContext() const = 0;
166
167  // Checks whether downloaded files still exist. Updates state of downloads
168  // that refer to removed files. The check runs in the background and may
169  // finish asynchronously after this method returns.
170  virtual void CheckForHistoryFilesRemoval() = 0;
171
172  // Get the download item for |id| if present, no matter what type of download
173  // it is or state it's in.
174  virtual DownloadItem* GetDownload(int id) = 0;
175
176 protected:
177  virtual ~DownloadManager() {}
178
179 private:
180  friend class base::RefCountedThreadSafe<DownloadManager>;
181};
182
183}  // namespace content
184
185#endif  // CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_
186