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