Chromium Code Reviews| 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 CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | 17 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ |
| 18 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | 18 #define CONTENT_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 "content/browser/download/download_item_interface.h" | |
| 28 #include "content/browser/download/download_request_handle.h" | 29 #include "content/browser/download/download_request_handle.h" |
| 29 #include "content/browser/download/download_state_info.h" | 30 #include "content/browser/download/download_state_info.h" |
| 30 #include "content/browser/download/interrupt_reasons.h" | 31 #include "content/browser/download/interrupt_reasons.h" |
| 31 #include "content/common/content_export.h" | 32 #include "content/common/content_export.h" |
| 32 #include "googleurl/src/gurl.h" | 33 #include "googleurl/src/gurl.h" |
| 33 #include "net/base/net_errors.h" | 34 #include "net/base/net_errors.h" |
| 34 | 35 |
| 35 class DownloadFileManager; | 36 class DownloadFileManager; |
| 36 class DownloadId; | 37 class DownloadId; |
| 37 class DownloadManager; | 38 class DownloadManager; |
| 38 struct DownloadCreateInfo; | 39 struct DownloadCreateInfo; |
| 39 struct DownloadPersistentStoreInfo; | 40 struct DownloadPersistentStoreInfo; |
| 40 | 41 |
| 41 // One DownloadItem per download. This is the model class that stores all the | 42 // One DownloadItem per download. This is the model class that stores all the |
| 42 // state for a download. Multiple views, such as a tab's download shelf and the | 43 // state for a download. Multiple views, such as a tab's download shelf and the |
| 43 // Destination tab's download view, may refer to a given DownloadItem. | 44 // Destination tab's download view, may refer to a given DownloadItem. |
| 44 // | 45 // |
| 45 // This is intended to be used only on the UI thread. | 46 // This is intended to be used only on the UI thread. |
| 46 class CONTENT_EXPORT DownloadItem { | 47 class CONTENT_EXPORT DownloadItem : public DownloadItemInterface { |
|
cbentzel
2011/10/20 18:18:53
Naming issue: we usually don't have Interface appe
| |
| 47 public: | 48 public: |
| 48 enum DownloadState { | |
| 49 // Download is actively progressing. | |
| 50 IN_PROGRESS = 0, | |
| 51 | |
| 52 // Download is completely finished. | |
| 53 COMPLETE, | |
| 54 | |
| 55 // Download has been cancelled. | |
| 56 CANCELLED, | |
| 57 | |
| 58 // This state indicates that the download item is about to be destroyed, | |
| 59 // and observers seeing this state should release all references. | |
| 60 REMOVING, | |
| 61 | |
| 62 // This state indicates that the download has been interrupted. | |
| 63 INTERRUPTED, | |
| 64 | |
| 65 // Maximum value. | |
| 66 MAX_DOWNLOAD_STATE | |
| 67 }; | |
| 68 | |
| 69 enum SafetyState { | |
| 70 SAFE = 0, | |
| 71 DANGEROUS, | |
| 72 DANGEROUS_BUT_VALIDATED // Dangerous but the user confirmed the download. | |
| 73 }; | |
| 74 | |
| 75 // This enum is used by histograms. Do not change the ordering or remove | |
| 76 // items. | |
| 77 enum DangerType { | |
| 78 NOT_DANGEROUS = 0, | |
| 79 | |
| 80 // A dangerous file to the system (e.g.: an executable or extension from | |
| 81 // places other than gallery). | |
| 82 DANGEROUS_FILE, | |
| 83 | |
| 84 // Safebrowsing service shows this URL leads to malicious file download. | |
| 85 DANGEROUS_URL, | |
| 86 | |
| 87 // Memory space for histograms is determined by the max. | |
| 88 // ALWAYS ADD NEW VALUES BEFORE THIS ONE. | |
| 89 DANGEROUS_TYPE_MAX | |
| 90 }; | |
| 91 | |
| 92 // Reason for deleting the download. Passed to Delete(). | 49 // Reason for deleting the download. Passed to Delete(). |
| 93 enum DeleteReason { | 50 enum DeleteReason { |
| 94 DELETE_DUE_TO_BROWSER_SHUTDOWN = 0, | 51 DELETE_DUE_TO_BROWSER_SHUTDOWN = 0, |
| 95 DELETE_DUE_TO_USER_DISCARD | 52 DELETE_DUE_TO_USER_DISCARD |
| 96 }; | 53 }; |
| 97 | 54 |
| 98 // A fake download table ID which represents a download that has started, | 55 // A fake download table ID which represents a download that has started, |
| 99 // but is not yet in the table. | 56 // but is not yet in the table. |
| 100 static const int kUninitializedHandle; | 57 static const int kUninitializedHandle; |
| 101 | 58 |
| 102 // Interface that observers of a particular download must implement in order | 59 // Interface that observers of a particular download must implement in order |
| 103 // to receive updates to the download's status. | 60 // to receive updates to the download's status. |
| 104 class CONTENT_EXPORT Observer { | 61 class CONTENT_EXPORT Observer { |
| 105 public: | 62 public: |
| 106 virtual void OnDownloadUpdated(DownloadItem* download) = 0; | 63 virtual void OnDownloadUpdated(DownloadItem* download) = 0; |
| 107 | 64 |
| 108 // Called when a downloaded file has been opened. | 65 // Called when a downloaded file has been opened. |
| 109 virtual void OnDownloadOpened(DownloadItem* download) = 0; | 66 virtual void OnDownloadOpened(DownloadItem* download) = 0; |
| 110 | 67 |
| 111 protected: | 68 protected: |
| 112 virtual ~Observer() {} | 69 virtual ~Observer() {} |
| 113 }; | 70 }; |
| 114 | 71 |
| 115 // Constructing from persistent store: | 72 // Constructing from persistent store: |
| 116 DownloadItem(DownloadManager* download_manager, | 73 DownloadItem(DownloadManager* download_manager, |
| 117 const DownloadPersistentStoreInfo& info); | 74 const DownloadPersistentStoreInfo& info, |
| 75 int id); | |
| 118 | 76 |
| 119 // Constructing for a regular download: | 77 // Constructing for a regular download: |
| 120 DownloadItem(DownloadManager* download_manager, | 78 DownloadItem(DownloadManager* download_manager, |
| 121 const DownloadCreateInfo& info, | 79 const DownloadCreateInfo& info, |
| 122 bool is_otr); | 80 bool is_otr); |
| 123 | 81 |
| 124 // Constructing for the "Save Page As..." feature: | 82 // Constructing for the "Save Page As..." feature: |
| 125 DownloadItem(DownloadManager* download_manager, | 83 DownloadItem(DownloadManager* download_manager, |
| 126 const FilePath& path, | 84 const FilePath& path, |
| 127 const GURL& url, | 85 const GURL& url, |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 | 189 |
| 232 // Called when the download is ready to complete. | 190 // Called when the download is ready to complete. |
| 233 // This may perform final rename if necessary and will eventually call | 191 // This may perform final rename if necessary and will eventually call |
| 234 // DownloadItem::Completed(). | 192 // DownloadItem::Completed(). |
| 235 void OnDownloadCompleting(DownloadFileManager* file_manager); | 193 void OnDownloadCompleting(DownloadFileManager* file_manager); |
| 236 | 194 |
| 237 // Called when the file name for the download is renamed to its final name. | 195 // Called when the file name for the download is renamed to its final name. |
| 238 void OnDownloadRenamedToFinalName(const FilePath& full_path); | 196 void OnDownloadRenamedToFinalName(const FilePath& full_path); |
| 239 | 197 |
| 240 // Returns true if this item matches |query|. |query| must be lower-cased. | 198 // Returns true if this item matches |query|. |query| must be lower-cased. |
| 241 bool MatchesQuery(const string16& query) const; | 199 virtual bool MatchesQuery(const string16& query) const OVERRIDE; |
| 242 | 200 |
| 243 // Returns true if the download needs more data. | 201 // Returns true if the download needs more data. |
| 244 bool IsPartialDownload() const; | 202 virtual bool IsPartialDownload() const OVERRIDE; |
| 245 | 203 |
| 246 // Returns true if the download is still receiving data. | 204 // Returns true if the download is still receiving data. |
| 247 bool IsInProgress() const; | 205 virtual bool IsInProgress() const OVERRIDE; |
| 248 | 206 |
| 249 // Returns true if the download has been cancelled or was interrupted. | 207 // Returns true if the download has been cancelled or was interrupted. |
| 250 bool IsCancelled() const; | 208 virtual bool IsCancelled() const OVERRIDE; |
| 251 | 209 |
| 252 // Returns true if the download was interrupted. | 210 // Returns true if the download was interrupted. |
| 253 bool IsInterrupted() const; | 211 virtual bool IsInterrupted() const OVERRIDE; |
| 254 | 212 |
| 255 // Returns true if we have all the data and know the final file name. | 213 // Returns true if we have all the data and know the final file name. |
| 256 bool IsComplete() const; | 214 virtual bool IsComplete() const OVERRIDE; |
| 257 | 215 |
| 258 // Accessors | 216 // Accessors |
| 259 DownloadState state() const { return state_; } | 217 virtual DownloadItemInterface::DownloadState state() const OVERRIDE { |
| 260 const FilePath& full_path() const { return full_path_; } | 218 return state_; |
| 219 } | |
| 220 virtual const FilePath& full_path() const OVERRIDE { return full_path_; } | |
| 261 void set_path_uniquifier(int uniquifier) { | 221 void set_path_uniquifier(int uniquifier) { |
| 262 state_info_.path_uniquifier = uniquifier; | 222 state_info_.path_uniquifier = uniquifier; |
| 263 } | 223 } |
| 264 const GURL& GetURL() const; | 224 virtual const GURL& GetURL() const OVERRIDE; |
| 265 | 225 |
| 266 const std::vector<GURL>& url_chain() const { return url_chain_; } | 226 const std::vector<GURL>& url_chain() const { return url_chain_; } |
| 267 const GURL& original_url() const { return url_chain_.front(); } | 227 virtual const GURL& original_url() const OVERRIDE { |
| 268 const GURL& referrer_url() const { return referrer_url_; } | 228 return url_chain_.front(); |
| 269 std::string suggested_filename() const { return suggested_filename_; } | 229 } |
| 270 std::string content_disposition() const { return content_disposition_; } | 230 virtual const GURL& referrer_url() const OVERRIDE { return referrer_url_; } |
| 271 std::string mime_type() const { return mime_type_; } | 231 virtual std::string suggested_filename() const OVERRIDE { |
| 272 std::string original_mime_type() const { return original_mime_type_; } | 232 return suggested_filename_; |
| 273 std::string referrer_charset() const { return referrer_charset_; } | 233 } |
| 274 int64 total_bytes() const { return total_bytes_; } | 234 virtual std::string content_disposition() const OVERRIDE { |
| 235 return content_disposition_; | |
| 236 } | |
| 237 virtual std::string mime_type() const OVERRIDE { return mime_type_; } | |
| 238 virtual std::string original_mime_type() const OVERRIDE { | |
| 239 return original_mime_type_; | |
| 240 } | |
| 241 virtual std::string referrer_charset() const OVERRIDE { | |
| 242 return referrer_charset_; | |
| 243 } | |
| 244 virtual int64 total_bytes() const OVERRIDE { return total_bytes_; } | |
| 275 void set_total_bytes(int64 total_bytes) { | 245 void set_total_bytes(int64 total_bytes) { |
| 276 total_bytes_ = total_bytes; | 246 total_bytes_ = total_bytes; |
| 277 } | 247 } |
| 278 int64 received_bytes() const { return received_bytes_; } | 248 virtual int64 received_bytes() const OVERRIDE { return received_bytes_; } |
| 279 int32 id() const { return download_id_; } | 249 virtual int32 id() const OVERRIDE { return download_id_; } |
| 280 DownloadId global_id() const; | 250 virtual DownloadId global_id() const OVERRIDE; |
| 281 base::Time start_time() const { return start_time_; } | 251 virtual base::Time start_time() const OVERRIDE { return start_time_; } |
| 282 base::Time end_time() const { return end_time_; } | 252 virtual base::Time end_time() const { return end_time_; } |
| 283 void set_db_handle(int64 handle) { db_handle_ = handle; } | 253 void set_db_handle(int64 handle) { db_handle_ = handle; } |
| 284 int64 db_handle() const { return db_handle_; } | 254 virtual int64 db_handle() const OVERRIDE { return db_handle_; } |
| 285 DownloadManager* download_manager() { return download_manager_; } | 255 virtual DownloadManager* download_manager() OVERRIDE { |
| 286 bool is_paused() const { return is_paused_; } | 256 return download_manager_; |
| 287 bool open_when_complete() const { return open_when_complete_; } | 257 } |
| 258 virtual bool is_paused() const OVERRIDE { return is_paused_; } | |
| 259 virtual bool open_when_complete() const OVERRIDE { | |
| 260 return open_when_complete_; | |
| 261 } | |
| 288 void set_open_when_complete(bool open) { open_when_complete_ = open; } | 262 void set_open_when_complete(bool open) { open_when_complete_ = open; } |
| 289 bool file_externally_removed() const { return file_externally_removed_; } | 263 virtual bool file_externally_removed() const OVERRIDE { |
| 290 SafetyState safety_state() const { return safety_state_; } | 264 return file_externally_removed_; |
| 265 } | |
| 266 virtual DownloadItemInterface::SafetyState safety_state() const OVERRIDE { | |
| 267 return safety_state_; | |
| 268 } | |
| 291 // Why |safety_state_| is not SAFE. | 269 // Why |safety_state_| is not SAFE. |
| 292 DangerType GetDangerType() const; | 270 virtual DownloadItemInterface::DangerType GetDangerType() const OVERRIDE; |
| 293 bool IsDangerous() const; | 271 virtual bool IsDangerous() const OVERRIDE; |
| 294 void MarkFileDangerous(); | 272 void MarkFileDangerous(); |
| 295 void MarkUrlDangerous(); | 273 void MarkUrlDangerous(); |
| 296 | 274 |
| 297 bool auto_opened() { return auto_opened_; } | 275 virtual bool auto_opened() const OVERRIDE { return auto_opened_; } |
| 298 const FilePath& target_name() const { return state_info_.target_name; } | 276 virtual const FilePath& target_name() const OVERRIDE { |
| 299 bool prompt_user_for_save_location() const { | 277 return state_info_.target_name; |
| 278 } | |
| 279 virtual bool prompt_user_for_save_location() const OVERRIDE { | |
| 300 return state_info_.prompt_user_for_save_location; | 280 return state_info_.prompt_user_for_save_location; |
| 301 } | 281 } |
| 302 bool is_otr() const { return is_otr_; } | 282 virtual bool is_otr() const { return is_otr_; } |
| 303 const FilePath& suggested_path() const { return state_info_.suggested_path; } | 283 virtual const FilePath& suggested_path() const OVERRIDE { |
| 304 bool is_temporary() const { return is_temporary_; } | 284 return state_info_.suggested_path; |
| 285 } | |
| 286 virtual bool is_temporary() const OVERRIDE { return is_temporary_; } | |
| 305 void set_opened(bool opened) { opened_ = opened; } | 287 void set_opened(bool opened) { opened_ = opened; } |
| 306 bool opened() const { return opened_; } | 288 virtual bool opened() const OVERRIDE { return opened_; } |
| 307 | 289 |
| 308 InterruptReason last_reason() const { return last_reason_; } | 290 virtual InterruptReason last_reason() const OVERRIDE { return last_reason_; } |
| 309 | 291 |
| 310 DownloadPersistentStoreInfo GetPersistentStoreInfo() const; | 292 DownloadPersistentStoreInfo GetPersistentStoreInfo() const; |
| 311 DownloadStateInfo state_info() const { return state_info_; } | 293 DownloadStateInfo state_info() const { return state_info_; } |
| 312 const DownloadRequestHandle& request_handle() const { | 294 const DownloadRequestHandle& request_handle() const { |
| 313 return request_handle_; | 295 return request_handle_; |
| 314 } | 296 } |
| 315 | 297 |
| 316 // Returns the final target file path for the download. | 298 // Returns the final target file path for the download. |
| 317 FilePath GetTargetFilePath() const; | 299 FilePath GetTargetFilePath() const; |
| 318 | 300 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 | 334 |
| 353 // Called when the entire download operation (including renaming etc) | 335 // Called when the entire download operation (including renaming etc) |
| 354 // is completed. | 336 // is completed. |
| 355 void Completed(); | 337 void Completed(); |
| 356 | 338 |
| 357 // Start/stop sending periodic updates to our observers | 339 // Start/stop sending periodic updates to our observers |
| 358 void StartProgressTimer(); | 340 void StartProgressTimer(); |
| 359 void StopProgressTimer(); | 341 void StopProgressTimer(); |
| 360 | 342 |
| 361 // Call to transition state; all state transitions should go through this. | 343 // Call to transition state; all state transitions should go through this. |
| 362 void TransitionTo(DownloadState new_state); | 344 void TransitionTo(DownloadItemInterface::DownloadState new_state); |
| 363 | 345 |
| 364 // Called when safety_state_ should be recomputed from is_dangerous_file | 346 // Called when safety_state_ should be recomputed from is_dangerous_file |
| 365 // and is_dangerous_url. | 347 // and is_dangerous_url. |
| 366 void UpdateSafetyState(); | 348 void UpdateSafetyState(); |
| 367 | 349 |
| 368 // Helper function to recompute |state_info_.target_name| when | 350 // Helper function to recompute |state_info_.target_name| when |
| 369 // it may have changed. (If it's non-null it should be left alone, | 351 // it may have changed. (If it's non-null it should be left alone, |
| 370 // otherwise updated from |full_path_|.) | 352 // otherwise updated from |full_path_|.) |
| 371 void UpdateTarget(); | 353 void UpdateTarget(); |
| 372 | 354 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 // Current received bytes | 401 // Current received bytes |
| 420 int64 received_bytes_; | 402 int64 received_bytes_; |
| 421 | 403 |
| 422 // Last reason. | 404 // Last reason. |
| 423 InterruptReason last_reason_; | 405 InterruptReason last_reason_; |
| 424 | 406 |
| 425 // Start time for calculating remaining time | 407 // Start time for calculating remaining time |
| 426 base::TimeTicks start_tick_; | 408 base::TimeTicks start_tick_; |
| 427 | 409 |
| 428 // The current state of this download | 410 // The current state of this download |
| 429 DownloadState state_; | 411 DownloadItemInterface::DownloadState state_; |
| 430 | 412 |
| 431 // The views of this item in the download shelf and download tab | 413 // The views of this item in the download shelf and download tab |
| 432 ObserverList<Observer> observers_; | 414 ObserverList<Observer> observers_; |
| 433 | 415 |
| 434 // Time the download was started | 416 // Time the download was started |
| 435 base::Time start_time_; | 417 base::Time start_time_; |
| 436 | 418 |
| 437 // Time the download completed | 419 // Time the download completed |
| 438 base::Time end_time_; | 420 base::Time end_time_; |
| 439 | 421 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 450 bool is_paused_; | 432 bool is_paused_; |
| 451 | 433 |
| 452 // A flag for indicating if the download should be opened at completion. | 434 // A flag for indicating if the download should be opened at completion. |
| 453 bool open_when_complete_; | 435 bool open_when_complete_; |
| 454 | 436 |
| 455 // A flag for indicating if the downloaded file is externally removed. | 437 // A flag for indicating if the downloaded file is externally removed. |
| 456 bool file_externally_removed_; | 438 bool file_externally_removed_; |
| 457 | 439 |
| 458 // Indicates if the download is considered potentially safe or dangerous | 440 // Indicates if the download is considered potentially safe or dangerous |
| 459 // (executable files are typically considered dangerous). | 441 // (executable files are typically considered dangerous). |
| 460 SafetyState safety_state_; | 442 DownloadItemInterface::SafetyState safety_state_; |
| 461 | 443 |
| 462 // True if the download was auto-opened. We set this rather than using | 444 // True if the download was auto-opened. We set this rather than using |
| 463 // an observer as it's frequently possible for the download to be auto opened | 445 // an observer as it's frequently possible for the download to be auto opened |
| 464 // before the observer is added. | 446 // before the observer is added. |
| 465 bool auto_opened_; | 447 bool auto_opened_; |
| 466 | 448 |
| 467 // True if the download was initiated in an incognito window. | 449 // True if the download was initiated in an incognito window. |
| 468 bool is_otr_; | 450 bool is_otr_; |
| 469 | 451 |
| 470 // True if the item was downloaded temporarily. | 452 // True if the item was downloaded temporarily. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 483 // only. | 465 // only. |
| 484 bool open_enabled_; | 466 bool open_enabled_; |
| 485 | 467 |
| 486 // Did the delegate delay calling Complete on this download? | 468 // Did the delegate delay calling Complete on this download? |
| 487 bool delegate_delayed_complete_; | 469 bool delegate_delayed_complete_; |
| 488 | 470 |
| 489 DISALLOW_COPY_AND_ASSIGN(DownloadItem); | 471 DISALLOW_COPY_AND_ASSIGN(DownloadItem); |
| 490 }; | 472 }; |
| 491 | 473 |
| 492 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | 474 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ |
| OLD | NEW |