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