OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Each download is represented by a DownloadItem, and all DownloadItems | 5 // Each download is represented by a DownloadItem, and all DownloadItems |
6 // are owned by the DownloadManager which maintains a global list of all | 6 // are owned by the DownloadManager which maintains a global list of all |
7 // downloads. DownloadItems are created when a user initiates a download, | 7 // downloads. DownloadItems are created when a user initiates a download, |
8 // and exist for the duration of the browser life time. | 8 // and exist for the duration of the browser life time. |
9 // | 9 // |
10 // Download observers: | 10 // Download observers: |
11 // DownloadItem::Observer: | 11 // DownloadItem::Observer: |
12 // - allows observers to receive notifications about one download from start | 12 // - allows observers to receive notifications about one download from start |
13 // to completion | 13 // to completion |
14 // Use AddObserver() / RemoveObserver() on the appropriate download object to | 14 // Use AddObserver() / RemoveObserver() on the appropriate download object to |
15 // receive state updates. | 15 // receive state updates. |
16 | 16 |
17 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | 17 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ |
18 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | 18 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ |
19 #pragma once | 19 #pragma once |
20 | 20 |
21 #include <string> | 21 #include <string> |
22 | 22 |
23 #include "base/basictypes.h" | 23 #include "base/basictypes.h" |
24 #include "base/file_path.h" | 24 #include "base/file_path.h" |
25 #include "base/observer_list.h" | 25 #include "base/observer_list.h" |
26 #include "base/time.h" | 26 #include "base/time.h" |
27 #include "base/timer.h" | 27 #include "base/timer.h" |
28 #include "chrome/browser/download/download_process_handle.h" | 28 #include "chrome/browser/download/download_process_handle.h" |
29 #include "chrome/browser/download/download_state_info.h" | |
30 #include "chrome/browser/history/download_history_info.h" | |
29 #include "googleurl/src/gurl.h" | 31 #include "googleurl/src/gurl.h" |
30 | 32 |
33 struct DownloadCreateInfo; | |
31 class DownloadFileManager; | 34 class DownloadFileManager; |
32 class DownloadManager; | 35 class DownloadManager; |
33 struct DownloadCreateInfo; | |
34 | 36 |
35 // One DownloadItem per download. This is the model class that stores all the | 37 // One DownloadItem per download. This is the model class that stores all the |
36 // state for a download. Multiple views, such as a tab's download shelf and the | 38 // state for a download. Multiple views, such as a tab's download shelf and the |
37 // Destination tab's download view, may refer to a given DownloadItem. | 39 // Destination tab's download view, may refer to a given DownloadItem. |
38 // | 40 // |
39 // This is intended to be used only on the UI thread. | 41 // This is intended to be used only on the UI thread. |
40 class DownloadItem { | 42 class DownloadItem { |
41 public: | 43 public: |
42 enum DownloadState { | 44 enum DownloadState { |
43 // Download is actively progressing. | 45 // Download is actively progressing. |
44 IN_PROGRESS, | 46 IN_PROGRESS = 0, |
45 | 47 |
46 // Download is completely finished. | 48 // Download is completely finished. |
47 COMPLETE, | 49 COMPLETE, |
48 | 50 |
49 // Download has been cancelled. | 51 // Download has been cancelled. |
50 CANCELLED, | 52 CANCELLED, |
51 | 53 |
52 // This state indicates that the download item is about to be destroyed, | 54 // This state indicates that the download item is about to be destroyed, |
53 // and observers seeing this state should release all references. | 55 // and observers seeing this state should release all references. |
54 REMOVING, | 56 REMOVING, |
55 | 57 |
56 // This state indicates that the download has been interrupted. | 58 // This state indicates that the download has been interrupted. |
57 INTERRUPTED | 59 INTERRUPTED, |
60 | |
61 // Maximum value. | |
62 MAX_DOWNLOAD_STATE | |
58 }; | 63 }; |
59 | 64 |
60 enum SafetyState { | 65 enum SafetyState { |
61 SAFE = 0, | 66 SAFE = 0, |
62 DANGEROUS, | 67 DANGEROUS, |
63 DANGEROUS_BUT_VALIDATED // Dangerous but the user confirmed the download. | 68 DANGEROUS_BUT_VALIDATED // Dangerous but the user confirmed the download. |
64 }; | 69 }; |
65 | 70 |
66 // This enum is used by histograms. Do not change the ordering or remove | 71 // This enum is used by histograms. Do not change the ordering or remove |
67 // items. | 72 // items. |
(...skipping 26 matching lines...) Expand all Loading... | |
94 | 99 |
95 // Called when a downloaded file has been opened. | 100 // Called when a downloaded file has been opened. |
96 virtual void OnDownloadOpened(DownloadItem* download) = 0; | 101 virtual void OnDownloadOpened(DownloadItem* download) = 0; |
97 | 102 |
98 protected: | 103 protected: |
99 virtual ~Observer() {} | 104 virtual ~Observer() {} |
100 }; | 105 }; |
101 | 106 |
102 // Constructing from persistent store: | 107 // Constructing from persistent store: |
103 DownloadItem(DownloadManager* download_manager, | 108 DownloadItem(DownloadManager* download_manager, |
104 const DownloadCreateInfo& info); | 109 const DownloadHistoryInfo& info); |
105 | 110 |
106 // Constructing for a regular download: | 111 // Constructing for a regular download: |
107 DownloadItem(DownloadManager* download_manager, | 112 DownloadItem(DownloadManager* download_manager, |
108 const DownloadCreateInfo& info, | 113 const DownloadCreateInfo& info, |
109 bool is_otr); | 114 bool is_otr); |
110 | 115 |
111 // Constructing for the "Save Page As..." feature: | 116 // Constructing for the "Save Page As..." feature: |
112 DownloadItem(DownloadManager* download_manager, | 117 DownloadItem(DownloadManager* download_manager, |
113 const FilePath& path, | 118 const FilePath& path, |
114 const GURL& url, | 119 const GURL& url, |
115 bool is_otr); | 120 bool is_otr); |
116 | 121 |
117 ~DownloadItem(); | 122 ~DownloadItem(); |
118 | 123 |
119 void AddObserver(Observer* observer); | 124 void AddObserver(Observer* observer); |
120 void RemoveObserver(Observer* observer); | 125 void RemoveObserver(Observer* observer); |
121 | 126 |
122 // Notifies our observers periodically. | 127 // Notifies our observers periodically. |
123 void UpdateObservers(); | 128 void UpdateObservers(); |
124 | 129 |
125 // Whether it is OK to open this download. | 130 // Returns true if it is OK to open this download. |
126 bool CanOpenDownload(); | 131 bool CanOpenDownload(); |
127 | 132 |
128 // Tests if a file type should be opened automatically. | 133 // Tests if a file type should be opened automatically. |
129 bool ShouldOpenFileBasedOnExtension(); | 134 bool ShouldOpenFileBasedOnExtension(); |
130 | 135 |
131 // Registers this file extension for automatic opening upon download | 136 // Registers this file extension for automatic opening upon download |
132 // completion if 'open' is true, or prevents the extension from automatic | 137 // completion if 'open' is true, or prevents the extension from automatic |
133 // opening if 'open' is false. | 138 // opening if 'open' is false. |
134 void OpenFilesBasedOnExtension(bool open); | 139 void OpenFilesBasedOnExtension(bool open); |
135 | 140 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 // not estimate. | 189 // not estimate. |
185 bool TimeRemaining(base::TimeDelta* remaining) const; | 190 bool TimeRemaining(base::TimeDelta* remaining) const; |
186 | 191 |
187 // Simple speed estimate in bytes/s | 192 // Simple speed estimate in bytes/s |
188 int64 CurrentSpeed() const; | 193 int64 CurrentSpeed() const; |
189 | 194 |
190 // Rough percent complete, -1 means we don't know (since we didn't receive a | 195 // Rough percent complete, -1 means we don't know (since we didn't receive a |
191 // total size). | 196 // total size). |
192 int PercentComplete() const; | 197 int PercentComplete() const; |
193 | 198 |
194 // Whether or not this download has saved all of its data. | 199 // Called when the final path has been determined. |
200 void OnPathDetermined(const FilePath& path) { history_info_.path = path; } | |
201 | |
202 // Returns true if this download has saved all of its data. | |
195 bool all_data_saved() const { return all_data_saved_; } | 203 bool all_data_saved() const { return all_data_saved_; } |
196 | 204 |
197 // Update the fields that may have changed in DownloadCreateInfo as a | 205 // Update the fields that may have changed in DownloadStateInfo as a |
198 // result of analyzing the file and figuring out its type, location, etc. | 206 // result of analyzing the file and figuring out its type, location, etc. |
199 // May only be called once. | 207 // May only be called once. |
200 void SetFileCheckResults(const FilePath& path, | 208 void SetFileCheckResults(const DownloadStateInfo& state); |
201 bool is_dangerous_file, | 209 |
202 bool is_dangerous_url, | 210 // Updates the target file. |
203 int path_uniquifier, | 211 void UpdateTarget(); |
204 bool prompt, | |
205 bool is_extension_install, | |
206 const FilePath& original_name); | |
207 | 212 |
208 // Update the download's path, the actual file is renamed on the download | 213 // Update the download's path, the actual file is renamed on the download |
209 // thread. | 214 // thread. |
210 void Rename(const FilePath& full_path); | 215 void Rename(const FilePath& full_path); |
211 | 216 |
212 // Allow the user to temporarily pause a download or resume a paused download. | 217 // Allow the user to temporarily pause a download or resume a paused download. |
213 void TogglePause(); | 218 void TogglePause(); |
214 | 219 |
215 // Called when the download is ready to complete. | 220 // Called when the download is ready to complete. |
216 // This may perform final rename if necessary and will eventually call | 221 // This may perform final rename if necessary and will eventually call |
(...skipping 15 matching lines...) Expand all Loading... | |
232 // Returns true if the download has been cancelled or was interrupted. | 237 // Returns true if the download has been cancelled or was interrupted. |
233 bool IsCancelled() const; | 238 bool IsCancelled() const; |
234 | 239 |
235 // Returns true if the download was interrupted. | 240 // Returns true if the download was interrupted. |
236 bool IsInterrupted() const; | 241 bool IsInterrupted() const; |
237 | 242 |
238 // Returns true if we have all the data and know the final file name. | 243 // Returns true if we have all the data and know the final file name. |
239 bool IsComplete() const; | 244 bool IsComplete() const; |
240 | 245 |
241 // Accessors | 246 // Accessors |
242 DownloadState state() const { return state_; } | 247 DownloadState state() const; // TODO(ahendrickson) -- Rename to GetState(). |
Randy Smith (Not in Mondays)
2011/05/20 19:49:29
nit: Make the comment explain why. The goal is, i
ahendrickson
2011/05/20 22:12:45
Removed the need.
| |
243 FilePath full_path() const { return full_path_; } | 248 FilePath full_path() const { return history_info_.path; } |
244 void set_path_uniquifier(int uniquifier) { path_uniquifier_ = uniquifier; } | 249 void set_path_uniquifier(int uniquifier) { |
245 const GURL& url() const { return url_chain_.back(); } | 250 state_info_.path_uniquifier = uniquifier; |
246 const std::vector<GURL>& url_chain() const { return url_chain_; } | 251 } |
247 const GURL& original_url() const { return url_chain_.front(); } | 252 const GURL& GetURL() const; |
248 const GURL& referrer_url() const { return referrer_url_; } | 253 const std::vector<GURL>& url_chain() const { return history_info_.url_chain; } |
254 const GURL& original_url() const { return history_info_.url_chain.front(); } | |
255 const GURL& referrer_url() const { return history_info_.referrer_url; } | |
256 std::string content_disposition() const { return content_disposition_; } | |
249 std::string mime_type() const { return mime_type_; } | 257 std::string mime_type() const { return mime_type_; } |
250 std::string original_mime_type() const { return original_mime_type_; } | 258 std::string original_mime_type() const { return original_mime_type_; } |
251 int64 total_bytes() const { return total_bytes_; } | 259 std::string referrer_charset() const { return referrer_charset_; } |
Randy Smith (Not in Mondays)
2011/05/20 19:49:29
This looks like it was added by this CL, but doesn
ahendrickson
2011/05/20 22:12:45
It's part of the request information from Download
| |
252 void set_total_bytes(int64 total_bytes) { total_bytes_ = total_bytes; } | 260 int64 total_bytes() const { return history_info_.total_bytes; } |
253 int64 received_bytes() const { return received_bytes_; } | 261 void set_total_bytes(int64 total_bytes) { |
254 int32 id() const { return id_; } | 262 history_info_.total_bytes = total_bytes; |
255 base::Time start_time() const { return start_time_; } | 263 } |
256 void set_db_handle(int64 handle) { db_handle_ = handle; } | 264 int64 received_bytes() const { return history_info_.received_bytes; } |
257 int64 db_handle() const { return db_handle_; } | 265 int32 id() const { return history_info_.download_id; } |
266 base::Time start_time() const { return history_info_.start_time; } | |
267 void set_db_handle(int64 handle) { history_info_.db_handle = handle; } | |
268 int64 db_handle() const { return history_info_.db_handle; } | |
258 bool is_paused() const { return is_paused_; } | 269 bool is_paused() const { return is_paused_; } |
259 bool open_when_complete() const { return open_when_complete_; } | 270 bool open_when_complete() const { return open_when_complete_; } |
260 void set_open_when_complete(bool open) { open_when_complete_ = open; } | 271 void set_open_when_complete(bool open) { open_when_complete_ = open; } |
261 SafetyState safety_state() const { return safety_state_; } | 272 SafetyState safety_state() const { return safety_state_; } |
262 void set_safety_state(SafetyState safety_state) { | 273 void set_safety_state(SafetyState safety_state) { |
263 safety_state_ = safety_state; | 274 safety_state_ = safety_state; |
264 } | 275 } |
265 DangerType danger_type() { return danger_type_;} | 276 // Why |safety_state_| is not SAFE. |
277 DangerType GetDangerType() const; | |
278 bool IsDangerous() const; | |
279 void MarkUrlDangerous(); | |
280 | |
266 bool auto_opened() { return auto_opened_; } | 281 bool auto_opened() { return auto_opened_; } |
267 FilePath target_name() const { return target_name_; } | 282 FilePath target_name() const { return state_info_.target_name; } |
268 bool save_as() const { return save_as_; } | 283 bool save_as() const { return state_info_.prompt_user_for_save_location; } |
269 bool is_otr() const { return is_otr_; } | 284 bool is_otr() const { return is_otr_; } |
270 bool is_extension_install() const { return is_extension_install_; } | 285 bool is_extension_install() const { |
286 return state_info_.is_extension_install; | |
287 } | |
288 FilePath suggested_path() const { return state_info_.suggested_path; } | |
271 bool is_temporary() const { return is_temporary_; } | 289 bool is_temporary() const { return is_temporary_; } |
272 void set_opened(bool opened) { opened_ = opened; } | 290 void set_opened(bool opened) { opened_ = opened; } |
273 bool opened() const { return opened_; } | 291 bool opened() const { return opened_; } |
274 | 292 |
293 DownloadHistoryInfo history_info() const { return history_info_; } | |
294 DownloadStateInfo state_info() const { return state_info_; } | |
275 const DownloadProcessHandle& process_handle() const { | 295 const DownloadProcessHandle& process_handle() const { |
276 return process_handle_; | 296 return process_handle_; |
277 } | 297 } |
278 | 298 |
279 // Returns the final target file path for the download. | 299 // Returns the final target file path for the download. |
280 FilePath GetTargetFilePath() const; | 300 FilePath GetTargetFilePath() const; |
281 | 301 |
282 // Returns the file-name that should be reported to the user, which is | 302 // Returns the file-name that should be reported to the user, which is |
283 // target_name_ possibly with the uniquifier number. | 303 // target_name possibly with the uniquifier number. |
284 FilePath GetFileNameToReportUser() const; | 304 FilePath GetFileNameToReportUser() const; |
285 | 305 |
286 // Returns the user-verified target file path for the download. | 306 // Returns the user-verified target file path for the download. |
287 // This returns the same path as GetTargetFilePath() for safe downloads | 307 // This returns the same path as GetTargetFilePath() for safe downloads |
288 // but does not for dangerous downloads until the name is verified. | 308 // but does not for dangerous downloads until the name is verified. |
289 FilePath GetUserVerifiedFilePath() const; | 309 FilePath GetUserVerifiedFilePath() const; |
290 | 310 |
291 // Returns true if the current file name is not the final target name yet. | 311 // Returns true if the current file name is not the final target name yet. |
292 bool NeedsRename() const { | 312 bool NeedsRename() const { |
293 return target_name_ != full_path_.BaseName(); | 313 return state_info_.target_name != history_info_.path.BaseName(); |
294 } | 314 } |
295 | 315 |
296 std::string DebugString(bool verbose) const; | 316 std::string DebugString(bool verbose) const; |
297 | 317 |
298 private: | 318 private: |
299 void Init(bool start_timer); | 319 void Init(bool start_timer); |
300 | 320 |
301 // Internal helper for maintaining consistent received and total sizes. | 321 // Internal helper for maintaining consistent received and total sizes. |
302 void UpdateSize(int64 size); | 322 void UpdateSize(int64 size); |
303 | 323 |
304 // Called when the entire download operation (including renaming etc) | 324 // Called when the entire download operation (including renaming etc) |
305 // is completed. | 325 // is completed. |
306 void Completed(); | 326 void Completed(); |
307 | 327 |
308 // Start/stop sending periodic updates to our observers | 328 // Start/stop sending periodic updates to our observers |
309 void StartProgressTimer(); | 329 void StartProgressTimer(); |
310 void StopProgressTimer(); | 330 void StopProgressTimer(); |
311 | 331 |
312 // Request ID assigned by the ResourceDispatcherHost. | 332 // Information that is saved to the history DB. |
313 int32 id_; | 333 DownloadHistoryInfo history_info_; |
314 | 334 |
315 // Full path to the downloaded or downloading file. | 335 // State information used by the download manager. |
316 FilePath full_path_; | 336 DownloadStateInfo state_info_; |
317 | 337 |
318 // A number that should be appended to the path to make it unique, or 0 if the | 338 // The handle to the process information. Used for operations outside the |
319 // path should be used as is. | 339 // download system. |
320 int path_uniquifier_; | 340 DownloadProcessHandle process_handle_; |
321 | 341 |
322 // The chain of redirects that leading up to and including the final URL. | 342 // Information from the request. |
323 std::vector<GURL> url_chain_; | 343 // Content-disposition field from the header. |
344 std::string content_disposition_; | |
324 | 345 |
325 // The URL of the page that initiated the download. | 346 // Mime-type from the header. Subject to change. |
326 GURL referrer_url_; | |
327 | |
328 // The mimetype of the download | |
329 std::string mime_type_; | 347 std::string mime_type_; |
330 | 348 |
331 // The value of the content type header received when downloading | 349 // The value of the content type header sent with the downloaded item. It |
332 // this item. |mime_type_| may be different because of type sniffing. | 350 // may be different from |mime_type_|, which may be set based on heuristics |
351 // which may look at the file extension and first few bytes of the file. | |
333 std::string original_mime_type_; | 352 std::string original_mime_type_; |
334 | 353 |
335 // Total bytes expected | 354 // The charset of the referring page where the download request comes from. |
336 int64 total_bytes_; | 355 // It's used to construct a suggested filename. |
337 | 356 std::string referrer_charset_; |
338 // Current received bytes | |
339 int64 received_bytes_; | |
340 | 357 |
341 // Last error. | 358 // Last error. |
342 int last_os_error_; | 359 int last_os_error_; |
343 | 360 |
344 // Start time for calculating remaining time | 361 // Start time for calculating remaining time |
345 base::TimeTicks start_tick_; | 362 base::TimeTicks start_tick_; |
346 | 363 |
347 // The current state of this download | |
348 DownloadState state_; | |
349 | |
350 // The views of this item in the download shelf and download tab | 364 // The views of this item in the download shelf and download tab |
351 ObserverList<Observer> observers_; | 365 ObserverList<Observer> observers_; |
352 | 366 |
353 // Time the download was started | |
354 base::Time start_time_; | |
355 | |
356 // Our persistent store handle | |
357 int64 db_handle_; | |
358 | |
359 // Timer for regularly updating our observers | 367 // Timer for regularly updating our observers |
360 base::RepeatingTimer<DownloadItem> update_timer_; | 368 base::RepeatingTimer<DownloadItem> update_timer_; |
361 | 369 |
362 // Our owning object | 370 // Our owning object |
363 DownloadManager* download_manager_; | 371 DownloadManager* download_manager_; |
364 | 372 |
365 // In progress downloads may be paused by the user, we note it here | 373 // In progress downloads may be paused by the user, we note it here |
366 bool is_paused_; | 374 bool is_paused_; |
367 | 375 |
368 // A flag for indicating if the download should be opened at completion. | 376 // A flag for indicating if the download should be opened at completion. |
369 bool open_when_complete_; | 377 bool open_when_complete_; |
370 | 378 |
371 // Whether the download is considered potentially safe or dangerous | 379 // Indicates if the download is considered potentially safe or dangerous |
372 // (executable files are typically considered dangerous). | 380 // (executable files are typically considered dangerous). |
373 SafetyState safety_state_; | 381 SafetyState safety_state_; |
374 | 382 |
375 // Why |safety_state_| is not SAFE. | 383 // True if the download was auto-opened. We set this rather than using |
376 DangerType danger_type_; | |
377 | |
378 // Whether the download was auto-opened. We set this rather than using | |
379 // an observer as it's frequently possible for the download to be auto opened | 384 // an observer as it's frequently possible for the download to be auto opened |
380 // before the observer is added. | 385 // before the observer is added. |
381 bool auto_opened_; | 386 bool auto_opened_; |
382 | 387 |
383 // Dangerous downloads or ongoing downloads are given temporary names until | |
384 // the user approves them or the downloads finish. | |
385 // This stores their final target name. | |
386 FilePath target_name_; | |
387 | |
388 // The handle to the process information. Used for operations outside the | |
389 // download system. | |
390 DownloadProcessHandle process_handle_; | |
391 | |
392 // True if the item was downloaded as a result of 'save as...' | |
393 bool save_as_; | |
394 | |
395 // True if the download was initiated in an incognito window. | 388 // True if the download was initiated in an incognito window. |
396 bool is_otr_; | 389 bool is_otr_; |
397 | 390 |
398 // True if the item was downloaded for an extension installation. | |
399 bool is_extension_install_; | |
400 | |
401 // True if the item was downloaded temporarily. | 391 // True if the item was downloaded temporarily. |
402 bool is_temporary_; | 392 bool is_temporary_; |
403 | 393 |
404 // True if we've saved all the data for the download. | 394 // True if we've saved all the data for the download. |
405 bool all_data_saved_; | 395 bool all_data_saved_; |
406 | 396 |
407 // Did the user open the item either directly or indirectly (such as by | 397 // Did the user open the item either directly or indirectly (such as by |
408 // setting always open files of this type)? The shelf also sets this field | 398 // setting always open files of this type)? The shelf also sets this field |
409 // when the user closes the shelf before the item has been opened but should | 399 // when the user closes the shelf before the item has been opened but should |
410 // be treated as though the user opened it. | 400 // be treated as though the user opened it. |
411 bool opened_; | 401 bool opened_; |
412 | 402 |
413 DISALLOW_COPY_AND_ASSIGN(DownloadItem); | 403 DISALLOW_COPY_AND_ASSIGN(DownloadItem); |
414 }; | 404 }; |
415 | 405 |
416 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | 406 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ |
OLD | NEW |