download_item.h revision 513209b27ff55e2841eac0e4120199c23acce758
1// Copyright (c) 2010 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// Each download is represented by a DownloadItem, and all DownloadItems
6// are owned by the DownloadManager which maintains a global list of all
7// downloads. DownloadItems are created when a user initiates a download,
8// and exist for the duration of the browser life time.
9//
10// Download observers:
11//   DownloadItem::Observer:
12//     - allows observers to receive notifications about one download from start
13//       to completion
14// Use AddObserver() / RemoveObserver() on the appropriate download object to
15// receive state updates.
16
17#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_
18#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_
19#pragma once
20
21#include <string>
22
23#include "base/basictypes.h"
24#include "base/file_path.h"
25#include "base/observer_list.h"
26#include "base/time.h"
27#include "base/timer.h"
28#include "googleurl/src/gurl.h"
29
30class DownloadManager;
31struct DownloadCreateInfo;
32
33// One DownloadItem per download. This is the model class that stores all the
34// state for a download. Multiple views, such as a tab's download shelf and the
35// Destination tab's download view, may refer to a given DownloadItem.
36class DownloadItem {
37 public:
38  enum DownloadState {
39    IN_PROGRESS,
40    COMPLETE,
41    CANCELLED,
42    REMOVING
43  };
44
45  enum SafetyState {
46    SAFE = 0,
47    DANGEROUS,
48    DANGEROUS_BUT_VALIDATED  // Dangerous but the user confirmed the download.
49  };
50
51  // Interface that observers of a particular download must implement in order
52  // to receive updates to the download's status.
53  class Observer {
54   public:
55    virtual void OnDownloadUpdated(DownloadItem* download) = 0;
56
57    // Called when a downloaded file has been completed.
58    virtual void OnDownloadFileCompleted(DownloadItem* download) = 0;
59
60    // Called when a downloaded file has been opened.
61    virtual void OnDownloadOpened(DownloadItem* download) = 0;
62
63   protected:
64    virtual ~Observer() {}
65  };
66
67  // Constructing from persistent store:
68  DownloadItem(DownloadManager* download_manager,
69               const DownloadCreateInfo& info);
70
71  // Constructing for a regular download:
72  DownloadItem(DownloadManager* download_manager,
73               const DownloadCreateInfo& info,
74               bool is_otr);
75
76  // Constructing for the "Save Page As..." feature:
77  DownloadItem(DownloadManager* download_manager,
78               const FilePath& path,
79               const GURL& url,
80               bool is_otr);
81
82  ~DownloadItem();
83
84  void AddObserver(Observer* observer);
85  void RemoveObserver(Observer* observer);
86
87  // Notifies our observers periodically.
88  void UpdateObservers();
89
90  // Notifies our observers the downloaded file has been completed.
91  void NotifyObserversDownloadFileCompleted();
92
93  // Whether it is OK to open this download.
94  bool CanOpenDownload();
95
96  // Tests if a file type should be opened automatically.
97  bool ShouldOpenFileBasedOnExtension();
98
99  // Registers this file extension for automatic opening upon download
100  // completion if 'open' is true, or prevents the extension from automatic
101  // opening if 'open' is false.
102  void OpenFilesBasedOnExtension(bool open);
103
104  // Open the file associated with this download (wait for the download to
105  // complete if it is in progress).
106  void OpenDownload();
107
108  // Show the download via the OS shell.
109  void ShowDownloadInShell();
110
111  // Called when the user has validated the download of a dangerous file.
112  void DangerousDownloadValidated();
113
114  // Received a new chunk of data
115  void Update(int64 bytes_so_far);
116
117  // Cancel the download operation. We need to distinguish between cancels at
118  // exit (DownloadManager destructor) from user interface initiated cancels
119  // because at exit, the history system may not exist, and any updates to it
120  // require AddRef'ing the DownloadManager in the destructor which results in
121  // a DCHECK failure. Set 'update_history' to false when canceling from at
122  // exit to prevent this crash. This may result in a difference between the
123  // downloaded file's size on disk, and what the history system's last record
124  // of it is. At worst, we'll end up re-downloading a small portion of the file
125  // when resuming a download (assuming the server supports byte ranges).
126  void Cancel(bool update_history);
127
128  // Called when all data has been saved.
129  void OnAllDataSaved(int64 size);
130
131  // Called when the entire download operation (including renaming etc)
132  // is finished.
133  void Finished();
134
135  // The user wants to remove the download from the views and history. If
136  // |delete_file| is true, the file is deleted on the disk.
137  void Remove(bool delete_file);
138
139  // Simple calculation of the amount of time remaining to completion. Fills
140  // |*remaining| with the amount of time remaining if successful. Fails and
141  // returns false if we do not have the number of bytes or the speed so can
142  // not estimate.
143  bool TimeRemaining(base::TimeDelta* remaining) const;
144
145  // Simple speed estimate in bytes/s
146  int64 CurrentSpeed() const;
147
148  // Rough percent complete, -1 means we don't know (since we didn't receive a
149  // total size).
150  int PercentComplete() const;
151
152  // Update the download's path, the actual file is renamed on the download
153  // thread.
154  void Rename(const FilePath& full_path);
155
156  // Allow the user to temporarily pause a download or resume a paused download.
157  void TogglePause();
158
159  // Called when the name of the download is finalized.
160  void OnNameFinalized();
161
162  // Returns true if this item matches |query|. |query| must be lower-cased.
163  bool MatchesQuery(const string16& query) const;
164
165  // Accessors
166  DownloadState state() const { return state_; }
167  FilePath full_path() const { return full_path_; }
168  void set_path_uniquifier(int uniquifier) { path_uniquifier_ = uniquifier; }
169  GURL url() const { return url_; }
170  GURL referrer_url() const { return referrer_url_; }
171  std::string mime_type() const { return mime_type_; }
172  std::string original_mime_type() const { return original_mime_type_; }
173  int64 total_bytes() const { return total_bytes_; }
174  void set_total_bytes(int64 total_bytes) { total_bytes_ = total_bytes; }
175  int64 received_bytes() const { return received_bytes_; }
176  int32 id() const { return id_; }
177  base::Time start_time() const { return start_time_; }
178  void set_db_handle(int64 handle) { db_handle_ = handle; }
179  int64 db_handle() const { return db_handle_; }
180  bool is_paused() const { return is_paused_; }
181  bool open_when_complete() const { return open_when_complete_; }
182  void set_open_when_complete(bool open) { open_when_complete_ = open; }
183  int render_process_id() const { return render_process_id_; }
184  int request_id() const { return request_id_; }
185  SafetyState safety_state() const { return safety_state_; }
186  void set_safety_state(SafetyState safety_state) {
187    safety_state_ = safety_state;
188  }
189  bool auto_opened() { return auto_opened_; }
190  FilePath target_name() const { return target_name_; }
191  bool save_as() const { return save_as_; }
192  bool is_otr() const { return is_otr_; }
193  bool is_extension_install() const { return is_extension_install_; }
194  bool name_finalized() const { return name_finalized_; }
195  bool is_temporary() const { return is_temporary_; }
196  void set_opened(bool opened) { opened_ = opened; }
197  bool opened() const { return opened_; }
198
199  // Returns the final target file path for the download.
200  FilePath GetTargetFilePath() const;
201
202  // Returns the file-name that should be reported to the user, which is
203  // target_name_ possibly with the uniquifier number.
204  FilePath GetFileNameToReportUser() const;
205
206  // Returns the user-verified target name for the download.
207  // This returns the same path as target_name() for safe downloads
208  // but does not for dangerous downloads until the name is verified.
209  FilePath GetUserVerifiedFileName() const;
210
211  // Returns true if the current file name is not the final target name yet.
212  bool NeedsRename() const {
213    return target_name_ != full_path_.BaseName();
214  }
215
216 private:
217  void Init(bool start_timer);
218
219  // Internal helper for maintaining consistent received and total sizes.
220  void UpdateSize(int64 size);
221
222  // Start/stop sending periodic updates to our observers
223  void StartProgressTimer();
224  void StopProgressTimer();
225
226  // Request ID assigned by the ResourceDispatcherHost.
227  int32 id_;
228
229  // Full path to the downloaded or downloading file.
230  FilePath full_path_;
231
232  // A number that should be appended to the path to make it unique, or 0 if the
233  // path should be used as is.
234  int path_uniquifier_;
235
236  // The URL from whence we came.
237  GURL url_;
238
239  // The URL of the page that initiated the download.
240  GURL referrer_url_;
241
242  // The mimetype of the download
243  std::string mime_type_;
244
245  // The value of the content type header received when downloading
246  // this item.  |mime_type_| may be different because of type sniffing.
247  std::string original_mime_type_;
248
249  // Total bytes expected
250  int64 total_bytes_;
251
252  // Current received bytes
253  int64 received_bytes_;
254
255  // Start time for calculating remaining time
256  base::TimeTicks start_tick_;
257
258  // The current state of this download
259  DownloadState state_;
260
261  // The views of this item in the download shelf and download tab
262  ObserverList<Observer> observers_;
263
264  // Time the download was started
265  base::Time start_time_;
266
267  // Our persistent store handle
268  int64 db_handle_;
269
270  // Timer for regularly updating our observers
271  base::RepeatingTimer<DownloadItem> update_timer_;
272
273  // Our owning object
274  DownloadManager* download_manager_;
275
276  // In progress downloads may be paused by the user, we note it here
277  bool is_paused_;
278
279  // A flag for indicating if the download should be opened at completion.
280  bool open_when_complete_;
281
282  // Whether the download is considered potentially safe or dangerous
283  // (executable files are typically considered dangerous).
284  SafetyState safety_state_;
285
286  // Whether the download was auto-opened. We set this rather than using
287  // an observer as it's frequently possible for the download to be auto opened
288  // before the observer is added.
289  bool auto_opened_;
290
291  // Dangerous downloads or ongoing downloads are given temporary names until
292  // the user approves them or the downloads finish.
293  // This stores their final target name.
294  FilePath target_name_;
295
296  // For canceling or pausing requests.
297  int render_process_id_;
298  int request_id_;
299
300  // True if the item was downloaded as a result of 'save as...'
301  bool save_as_;
302
303  // True if the download was initiated in an incognito window.
304  bool is_otr_;
305
306  // True if the item was downloaded for an extension installation.
307  bool is_extension_install_;
308
309  // True if the filename is finalized.
310  bool name_finalized_;
311
312  // True if the item was downloaded temporarily.
313  bool is_temporary_;
314
315  // Did the user open the item either directly or indirectly (such as by
316  // setting always open files of this type)? The shelf also sets this field
317  // when the user closes the shelf before the item has been opened but should
318  // be treated as though the user opened it.
319  bool opened_;
320
321  DISALLOW_COPY_AND_ASSIGN(DownloadItem);
322};
323
324#endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_
325