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/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/pickle.h" | |
| 27 #include "base/time.h" | 28 #include "base/time.h" |
| 28 #include "content/browser/download/download_id.h" | 29 #include "content/browser/download/download_id.h" |
| 29 #include "content/browser/download/download_request_handle.h" | 30 #include "content/browser/download/download_request_handle.h" |
| 30 #include "content/browser/download/download_state_info.h" | 31 #include "content/browser/download/download_state_info.h" |
| 31 #include "content/browser/download/interrupt_reasons.h" | 32 #include "content/browser/download/interrupt_reasons.h" |
| 32 #include "content/common/content_export.h" | 33 #include "content/common/content_export.h" |
| 33 #include "googleurl/src/gurl.h" | 34 #include "googleurl/src/gurl.h" |
| 34 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
| 35 | 36 |
| 36 class DownloadFileManager; | 37 class DownloadFileManager; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 // Open the file associated with this download (wait for the download to | 140 // Open the file associated with this download (wait for the download to |
| 140 // complete if it is in progress). | 141 // complete if it is in progress). |
| 141 void OpenDownload(); | 142 void OpenDownload(); |
| 142 | 143 |
| 143 // Show the download via the OS shell. | 144 // Show the download via the OS shell. |
| 144 void ShowDownloadInShell(); | 145 void ShowDownloadInShell(); |
| 145 | 146 |
| 146 // Called when the user has validated the download of a dangerous file. | 147 // Called when the user has validated the download of a dangerous file. |
| 147 void DangerousDownloadValidated(); | 148 void DangerousDownloadValidated(); |
| 148 | 149 |
| 149 // Received a new chunk of data | 150 // Received a new chunk of data. |
| 150 void Update(int64 bytes_so_far); | 151 // |bytes_so_far| is the number of bytes received so far. |
| 152 // |hash_state| is the current hash state. | |
| 153 void Update(int64 bytes_so_far, const Pickle& hash_state); | |
|
Randy Smith (Not in Mondays)
2011/11/21 02:10:17
So this makes me concerned, as pickling and unpick
ahendrickson
2011/11/21 20:34:47
FWIW, the size of the pickle payload is around 300
Randy Smith (Not in Mondays)
2011/11/22 17:37:49
That sounds ok. Could you figure out about how of
ahendrickson
2011/11/22 23:46:44
Operations are in bytes
SHA256ContextStr has 3 fi
Randy Smith (Not in Mondays)
2011/11/23 19:25:13
I'm not too worried about the space costs; more ab
ahendrickson
2011/12/20 00:04:13
I downloaded a 50MB file, which took about 10-15 s
Randy Smith (Not in Mondays)
2011/12/20 17:05:40
Cool. What's the comparison? (I.e. what that wou
ahendrickson
2011/12/20 22:06:54
Without the hash state being passed around:
DRH:
Randy Smith (Not in Mondays)
2011/12/21 19:14:49
Agreed. Thanks for the due diligence.
| |
| 151 | 154 |
| 152 // Cancel the download operation. We need to distinguish between cancels at | 155 // Cancel the download operation. We need to distinguish between cancels at |
| 153 // exit (DownloadManager destructor) from user interface initiated cancels | 156 // exit (DownloadManager destructor) from user interface initiated cancels |
| 154 // because at exit, the history system may not exist, and any updates to it | 157 // because at exit, the history system may not exist, and any updates to it |
| 155 // require AddRef'ing the DownloadManager in the destructor which results in | 158 // require AddRef'ing the DownloadManager in the destructor which results in |
| 156 // a DCHECK failure. Set |user_cancel| to false when canceling from at | 159 // a DCHECK failure. Set |user_cancel| to false when canceling from at |
| 157 // exit to prevent this crash. This may result in a difference between the | 160 // exit to prevent this crash. This may result in a difference between the |
| 158 // downloaded file's size on disk, and what the history system's last record | 161 // downloaded file's size on disk, and what the history system's last record |
| 159 // of it is. At worst, we'll end up re-downloading a small portion of the file | 162 // of it is. At worst, we'll end up re-downloading a small portion of the file |
| 160 // when resuming a download (assuming the server supports byte ranges). | 163 // when resuming a download (assuming the server supports byte ranges). |
| 161 void Cancel(bool user_cancel); | 164 void Cancel(bool user_cancel); |
| 162 | 165 |
| 163 // Called by external code (SavePackage) using the DownloadItem interface | 166 // Called by external code (SavePackage) using the DownloadItem interface |
| 164 // to display progress when the DownloadItem should be considered complete. | 167 // to display progress when the DownloadItem should be considered complete. |
| 165 void MarkAsComplete(); | 168 void MarkAsComplete(); |
| 166 | 169 |
| 167 // Called by the delegate after it delayed opening the download in | 170 // Called by the delegate after it delayed opening the download in |
| 168 // DownloadManagerDelegate::ShouldOpenDownload. | 171 // DownloadManagerDelegate::ShouldOpenDownload. |
| 169 void DelayedDownloadOpened(); | 172 void DelayedDownloadOpened(); |
| 170 | 173 |
| 171 // Called when all data has been saved. | 174 // Called when all data has been saved. |
| 172 void OnAllDataSaved(int64 size, const std::string& final_hash); | 175 void OnAllDataSaved(int64 size, const std::string& final_hash); |
| 173 | 176 |
| 174 // Called when the downloaded file is removed. | 177 // Called when the downloaded file is removed. |
| 175 void OnDownloadedFileRemoved(); | 178 void OnDownloadedFileRemoved(); |
| 176 | 179 |
| 177 // Download operation had an error. | 180 // Download operation had an error. |
| 178 // |size| is the amount of data received at interruption. | 181 // |size| is the amount of data received at interruption. |
| 182 // |hash_state| is the current hash state at interruption. | |
| 179 // |reason| is the download interrupt reason code that the operation received. | 183 // |reason| is the download interrupt reason code that the operation received. |
| 180 void Interrupted(int64 size, InterruptReason reason); | 184 void Interrupted(int64 size, |
| 185 const Pickle& hash_state, | |
| 186 InterruptReason reason); | |
| 181 | 187 |
| 182 // Deletes the file from disk and removes the download from the views and | 188 // Deletes the file from disk and removes the download from the views and |
| 183 // history. |user| should be true if this is the result of the user clicking | 189 // history. |user| should be true if this is the result of the user clicking |
| 184 // the discard button, and false if it is being deleted for other reasons like | 190 // the discard button, and false if it is being deleted for other reasons like |
| 185 // browser shutdown. | 191 // browser shutdown. |
| 186 void Delete(DeleteReason reason); | 192 void Delete(DeleteReason reason); |
| 187 | 193 |
| 188 // Removes the download from the views and history. | 194 // Removes the download from the views and history. |
| 189 void Remove(); | 195 void Remove(); |
| 190 | 196 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 const FilePath& target_name() const { return state_info_.target_name; } | 296 const FilePath& target_name() const { return state_info_.target_name; } |
| 291 bool prompt_user_for_save_location() const { | 297 bool prompt_user_for_save_location() const { |
| 292 return state_info_.prompt_user_for_save_location; | 298 return state_info_.prompt_user_for_save_location; |
| 293 } | 299 } |
| 294 bool is_otr() const { return is_otr_; } | 300 bool is_otr() const { return is_otr_; } |
| 295 const FilePath& suggested_path() const { return state_info_.suggested_path; } | 301 const FilePath& suggested_path() const { return state_info_.suggested_path; } |
| 296 bool is_temporary() const { return is_temporary_; } | 302 bool is_temporary() const { return is_temporary_; } |
| 297 void set_opened(bool opened) { opened_ = opened; } | 303 void set_opened(bool opened) { opened_ = opened; } |
| 298 bool opened() const { return opened_; } | 304 bool opened() const { return opened_; } |
| 299 | 305 |
| 306 const std::string& last_modified_time() const { return last_modified_time_; } | |
| 307 const std::string& etag() const { return etag_; } | |
| 308 | |
| 300 InterruptReason last_reason() const { return last_reason_; } | 309 InterruptReason last_reason() const { return last_reason_; } |
| 301 | 310 |
| 302 DownloadPersistentStoreInfo GetPersistentStoreInfo() const; | 311 DownloadPersistentStoreInfo GetPersistentStoreInfo() const; |
| 303 DownloadStateInfo state_info() const { return state_info_; } | 312 DownloadStateInfo state_info() const { return state_info_; } |
| 304 | 313 |
| 305 TabContents* GetTabContents() const; | 314 TabContents* GetTabContents() const; |
| 306 | 315 |
| 307 // Returns the final target file path for the download. | 316 // Returns the final target file path for the download. |
| 308 FilePath GetTargetFilePath() const; | 317 FilePath GetTargetFilePath() const; |
| 309 | 318 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 331 | 340 |
| 332 std::string DebugString(bool verbose) const; | 341 std::string DebugString(bool verbose) const; |
| 333 | 342 |
| 334 void MockDownloadOpenForTesting() { open_enabled_ = false; } | 343 void MockDownloadOpenForTesting() { open_enabled_ = false; } |
| 335 | 344 |
| 336 private: | 345 private: |
| 337 // Construction common to all constructors. |active| should be true for new | 346 // Construction common to all constructors. |active| should be true for new |
| 338 // downloads and false for downloads from the history. | 347 // downloads and false for downloads from the history. |
| 339 void Init(bool active); | 348 void Init(bool active); |
| 340 | 349 |
| 350 // Internal helper for maintaining consistent received and total sizes, and | |
| 351 // hash state. | |
| 352 void UpdateSizeAndHashState(int64 bytes_so_far, const Pickle& hash_state); | |
| 353 | |
| 341 // Internal helper for maintaining consistent received and total sizes. | 354 // Internal helper for maintaining consistent received and total sizes. |
| 342 void UpdateSize(int64 size); | 355 // Should only be called from |UpdateSizeAndHashState| and |OnAllDataSaved|. |
| 356 void UpdateSize(int64 bytes_so_far); | |
| 343 | 357 |
| 344 // Called when the entire download operation (including renaming etc) | 358 // Called when the entire download operation (including renaming etc) |
| 345 // is completed. | 359 // is completed. |
| 346 void Completed(); | 360 void Completed(); |
| 347 | 361 |
| 348 // Call to transition state; all state transitions should go through this. | 362 // Call to transition state; all state transitions should go through this. |
| 349 void TransitionTo(DownloadState new_state); | 363 void TransitionTo(DownloadState new_state); |
| 350 | 364 |
| 351 // Called when safety_state_ should be recomputed from the DangerType of the | 365 // Called when safety_state_ should be recomputed from the DangerType of the |
| 352 // state info. | 366 // state info. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 | 408 |
| 395 // The value of the content type header sent with the downloaded item. It | 409 // The value of the content type header sent with the downloaded item. It |
| 396 // may be different from |mime_type_|, which may be set based on heuristics | 410 // may be different from |mime_type_|, which may be set based on heuristics |
| 397 // which may look at the file extension and first few bytes of the file. | 411 // which may look at the file extension and first few bytes of the file. |
| 398 std::string original_mime_type_; | 412 std::string original_mime_type_; |
| 399 | 413 |
| 400 // The charset of the referring page where the download request comes from. | 414 // The charset of the referring page where the download request comes from. |
| 401 // It's used to construct a suggested filename. | 415 // It's used to construct a suggested filename. |
| 402 std::string referrer_charset_; | 416 std::string referrer_charset_; |
| 403 | 417 |
| 404 // Total bytes expected | 418 // Total bytes expected. |
| 405 int64 total_bytes_; | 419 int64 total_bytes_; |
| 406 | 420 |
| 407 // Current received bytes | 421 // Current received bytes. |
| 408 int64 received_bytes_; | 422 int64 received_bytes_; |
| 409 | 423 |
| 410 // Sha256 hash of the content. This might be empty either because | 424 // Sha256 hash of the content. This might be empty either because |
| 411 // the download isn't done yet or because the hash isn't needed | 425 // the download hasn't started yet or because the hash isn't needed |
| 412 // (ChromeDownloadManagerDelegate::GenerateFileHash() returned false). | 426 // (ChromeDownloadManagerDelegate::GenerateFileHash() returned false). |
| 413 std::string hash_; | 427 std::string hash_; |
| 414 | 428 |
| 429 Pickle hash_state_; | |
| 430 | |
| 431 // Server's time stamp for the file. | |
| 432 std::string last_modified_time_; | |
| 433 | |
| 434 // Server's ETAG for the file. | |
| 435 std::string etag_; | |
| 436 | |
| 415 // Last reason. | 437 // Last reason. |
| 416 InterruptReason last_reason_; | 438 InterruptReason last_reason_; |
| 417 | 439 |
| 418 // Start time for calculating remaining time | 440 // Start time for calculating remaining time. |
| 419 base::TimeTicks start_tick_; | 441 base::TimeTicks start_tick_; |
| 420 | 442 |
| 421 // The current state of this download | 443 // The current state of this download. |
| 422 DownloadState state_; | 444 DownloadState state_; |
| 423 | 445 |
| 424 // The views of this item in the download shelf and download tab | 446 // The views of this item in the download shelf and download tab. |
| 425 ObserverList<Observer> observers_; | 447 ObserverList<Observer> observers_; |
| 426 | 448 |
| 427 // Time the download was started | 449 // Time the download was started. |
| 428 base::Time start_time_; | 450 base::Time start_time_; |
| 429 | 451 |
| 430 // Time the download completed | 452 // Time the download completed. |
| 431 base::Time end_time_; | 453 base::Time end_time_; |
| 432 | 454 |
| 433 // Our persistent store handle | 455 // Our persistent store handle. |
| 434 int64 db_handle_; | 456 int64 db_handle_; |
| 435 | 457 |
| 436 // Our owning object | 458 // Our owning object. |
| 437 DownloadManager* download_manager_; | 459 DownloadManager* download_manager_; |
| 438 | 460 |
| 439 // In progress downloads may be paused by the user, we note it here | 461 // In progress downloads may be paused by the user, we note it here. |
| 440 bool is_paused_; | 462 bool is_paused_; |
| 441 | 463 |
| 442 // A flag for indicating if the download should be opened at completion. | 464 // A flag for indicating if the download should be opened at completion. |
| 443 bool open_when_complete_; | 465 bool open_when_complete_; |
| 444 | 466 |
| 445 // A flag for indicating if the downloaded file is externally removed. | 467 // A flag for indicating if the downloaded file is externally removed. |
| 446 bool file_externally_removed_; | 468 bool file_externally_removed_; |
| 447 | 469 |
| 448 // Indicates if the download is considered potentially safe or dangerous | 470 // Indicates if the download is considered potentially safe or dangerous |
| 449 // (executable files are typically considered dangerous). | 471 // (executable files are typically considered dangerous). |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 473 // only. | 495 // only. |
| 474 bool open_enabled_; | 496 bool open_enabled_; |
| 475 | 497 |
| 476 // Did the delegate delay calling Complete on this download? | 498 // Did the delegate delay calling Complete on this download? |
| 477 bool delegate_delayed_complete_; | 499 bool delegate_delayed_complete_; |
| 478 | 500 |
| 479 DISALLOW_COPY_AND_ASSIGN(DownloadItem); | 501 DISALLOW_COPY_AND_ASSIGN(DownloadItem); |
| 480 }; | 502 }; |
| 481 | 503 |
| 482 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | 504 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ |
| OLD | NEW |