Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: content/browser/download/download_item_impl.h

Issue 1751603002: [Downloads] Rework how hashes are calculated for download files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Try to appease MSVC Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <string> 10 #include <string>
11 11
12 #include "base/callback_forward.h" 12 #include "base/callback_forward.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h" 17 #include "base/observer_list.h"
18 #include "base/time/time.h" 18 #include "base/time/time.h"
19 #include "content/browser/download/download_destination_observer.h"
19 #include "content/browser/download/download_net_log_parameters.h" 20 #include "content/browser/download/download_net_log_parameters.h"
20 #include "content/browser/download/download_request_handle.h" 21 #include "content/browser/download/download_request_handle.h"
21 #include "content/common/content_export.h" 22 #include "content/common/content_export.h"
22 #include "content/public/browser/download_destination_observer.h"
23 #include "content/public/browser/download_interrupt_reasons.h" 23 #include "content/public/browser/download_interrupt_reasons.h"
24 #include "content/public/browser/download_item.h" 24 #include "content/public/browser/download_item.h"
25 #include "net/log/net_log.h" 25 #include "net/log/net_log.h"
26 #include "url/gurl.h" 26 #include "url/gurl.h"
27 27
28 namespace content { 28 namespace content {
29 class DownloadFile; 29 class DownloadFile;
30 class DownloadItemImplDelegate; 30 class DownloadItemImplDelegate;
31 31
32 // See download_item.h for usage. 32 // See download_item.h for usage.
(...skipping 25 matching lines...) Expand all
58 const std::vector<GURL>& url_chain, 58 const std::vector<GURL>& url_chain,
59 const GURL& referrer_url, 59 const GURL& referrer_url,
60 const std::string& mime_type, 60 const std::string& mime_type,
61 const std::string& original_mime_type, 61 const std::string& original_mime_type,
62 const base::Time& start_time, 62 const base::Time& start_time,
63 const base::Time& end_time, 63 const base::Time& end_time,
64 const std::string& etag, 64 const std::string& etag,
65 const std::string& last_modified, 65 const std::string& last_modified,
66 int64_t received_bytes, 66 int64_t received_bytes,
67 int64_t total_bytes, 67 int64_t total_bytes,
68 const std::string& hash,
68 DownloadItem::DownloadState state, 69 DownloadItem::DownloadState state,
69 DownloadDangerType danger_type, 70 DownloadDangerType danger_type,
70 DownloadInterruptReason interrupt_reason, 71 DownloadInterruptReason interrupt_reason,
71 bool opened, 72 bool opened,
72 const net::BoundNetLog& bound_net_log); 73 const net::BoundNetLog& bound_net_log);
73 74
74 // Constructing for a regular download. 75 // Constructing for a regular download.
75 // |bound_net_log| is constructed externally for our use. 76 // |bound_net_log| is constructed externally for our use.
76 // TODO(asanka): Get rid of the DownloadCreateInfo parameter since active 77 // TODO(asanka): Get rid of the DownloadCreateInfo parameter since active
77 // downloads end up at Start() immediately after creation. 78 // downloads end up at Start() immediately after creation.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 ui::PageTransition GetTransitionType() const override; 127 ui::PageTransition GetTransitionType() const override;
127 const std::string& GetLastModifiedTime() const override; 128 const std::string& GetLastModifiedTime() const override;
128 const std::string& GetETag() const override; 129 const std::string& GetETag() const override;
129 bool IsSavePackageDownload() const override; 130 bool IsSavePackageDownload() const override;
130 const base::FilePath& GetFullPath() const override; 131 const base::FilePath& GetFullPath() const override;
131 const base::FilePath& GetTargetFilePath() const override; 132 const base::FilePath& GetTargetFilePath() const override;
132 const base::FilePath& GetForcedFilePath() const override; 133 const base::FilePath& GetForcedFilePath() const override;
133 base::FilePath GetFileNameToReportUser() const override; 134 base::FilePath GetFileNameToReportUser() const override;
134 TargetDisposition GetTargetDisposition() const override; 135 TargetDisposition GetTargetDisposition() const override;
135 const std::string& GetHash() const override; 136 const std::string& GetHash() const override;
136 const std::string& GetHashState() const override;
137 bool GetFileExternallyRemoved() const override; 137 bool GetFileExternallyRemoved() const override;
138 void DeleteFile(const base::Callback<void(bool)>& callback) override; 138 void DeleteFile(const base::Callback<void(bool)>& callback) override;
139 bool IsDangerous() const override; 139 bool IsDangerous() const override;
140 DownloadDangerType GetDangerType() const override; 140 DownloadDangerType GetDangerType() const override;
141 bool TimeRemaining(base::TimeDelta* remaining) const override; 141 bool TimeRemaining(base::TimeDelta* remaining) const override;
142 int64_t CurrentSpeed() const override; 142 int64_t CurrentSpeed() const override;
143 int PercentComplete() const override; 143 int PercentComplete() const override;
144 bool AllDataSaved() const override; 144 bool AllDataSaved() const override;
145 int64_t GetTotalBytes() const override; 145 int64_t GetTotalBytes() const override;
146 int64_t GetReceivedBytes() const override; 146 int64_t GetReceivedBytes() const override;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 DestinationObserverAsWeakPtr(); 198 DestinationObserverAsWeakPtr();
199 199
200 // Get the download's BoundNetLog. 200 // Get the download's BoundNetLog.
201 virtual const net::BoundNetLog& GetBoundNetLog() const; 201 virtual const net::BoundNetLog& GetBoundNetLog() const;
202 202
203 // DownloadItemImpl routines only needed by SavePackage ---------------------- 203 // DownloadItemImpl routines only needed by SavePackage ----------------------
204 204
205 // Called by SavePackage to set the total number of bytes on the item. 205 // Called by SavePackage to set the total number of bytes on the item.
206 virtual void SetTotalBytes(int64_t total_bytes); 206 virtual void SetTotalBytes(int64_t total_bytes);
207 207
208 virtual void OnAllDataSaved(const std::string& final_hash); 208 virtual void OnAllDataSaved(int64_t total_bytes,
209 scoped_ptr<crypto::SecureHash> hash_state);
209 210
210 // Called by SavePackage to display progress when the DownloadItem 211 // Called by SavePackage to display progress when the DownloadItem
211 // should be considered complete. 212 // should be considered complete.
212 virtual void MarkAsComplete(); 213 virtual void MarkAsComplete();
213 214
214 // DownloadDestinationObserver 215 // DownloadDestinationObserver
215 void DestinationUpdate(int64_t bytes_so_far, 216 void DestinationUpdate(int64_t bytes_so_far, int64_t bytes_per_sec) override;
216 int64_t bytes_per_sec, 217 void DestinationError(DownloadInterruptReason reason,
217 const std::string& hash_state) override; 218 int64_t bytes_so_far,
218 void DestinationError(DownloadInterruptReason reason) override; 219 scoped_ptr<crypto::SecureHash> hash_state) override;
219 void DestinationCompleted(const std::string& final_hash) override; 220 void DestinationCompleted(int64_t total_bytes,
221 scoped_ptr<crypto::SecureHash> hash_state) override;
220 222
221 private: 223 private:
222 // Fine grained states of a download. 224 // Fine grained states of a download.
223 // 225 //
224 // New downloads can be created in the following states: 226 // New downloads can be created in the following states:
225 // 227 //
226 // INITIAL_INTERNAL: All active new downloads. 228 // INITIAL_INTERNAL: All active new downloads.
227 // 229 //
228 // COMPLETE_INTERNAL: Downloads restored from persisted state. 230 // COMPLETE_INTERNAL: Downloads restored from persisted state.
229 // CANCELLED_INTERNAL: - do - 231 // CANCELLED_INTERNAL: - do -
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 // Called if the embedder took over opening a download, to indicate that 421 // Called if the embedder took over opening a download, to indicate that
420 // the download has been opened. 422 // the download has been opened.
421 void DelayedDownloadOpened(bool auto_opened); 423 void DelayedDownloadOpened(bool auto_opened);
422 424
423 // Called when the entire download operation (including renaming etc.) 425 // Called when the entire download operation (including renaming etc.)
424 // is completed. 426 // is completed.
425 void Completed(); 427 void Completed();
426 428
427 // Helper routines ----------------------------------------------------------- 429 // Helper routines -----------------------------------------------------------
428 430
429 // Indicate that an error has occurred on the download. 431 // Indicate that an error has occurred on the download. Discards partial
430 void Interrupt(DownloadInterruptReason reason); 432 // state. The interrupted download will not be considered continuable, but may
433 // be restarted.
434 void InterruptAndDiscardPartialState(DownloadInterruptReason reason);
435
436 // Indiates that an error has occurred on the download. The |bytes_so_far| and
437 // |hash_state| should correspond to the state of the DownloadFile. If the
438 // interrupt reason allows, this partial state may be allowed to continue the
439 // interrupted download upon resumption.
440 void InterruptWithPartialState(int64_t bytes_so_far,
441 scoped_ptr<crypto::SecureHash> hash_state,
442 DownloadInterruptReason reason);
443
444 void UpdateProgress(int64_t bytes_so_far, int64_t bytes_per_sec);
445
446 // Set |hash_| based on the assumption that |hash_state_| represents the
447 // partial hash state.
448 void UpdatePrefixHash();
431 449
432 // Destroy the DownloadFile object. If |destroy_file| is true, the file is 450 // Destroy the DownloadFile object. If |destroy_file| is true, the file is
433 // destroyed with it. Otherwise, DownloadFile::Detach() is called before 451 // destroyed with it. Otherwise, DownloadFile::Detach() is called before
434 // object destruction to prevent file destruction. Destroying the file also 452 // object destruction to prevent file destruction. Destroying the file also
435 // resets |current_path_|. 453 // resets |current_path_|.
436 void ReleaseDownloadFile(bool destroy_file); 454 void ReleaseDownloadFile(bool destroy_file);
437 455
438 // Check if a download is ready for completion. The callback provided 456 // Check if a download is ready for completion. The callback provided
439 // may be called at some point in the future if an external entity 457 // may be called at some point in the future if an external entity
440 // state has change s.t. this routine should be checked again. 458 // state has change s.t. this routine should be checked again.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 // The handle to the request information. Used for operations outside the 497 // The handle to the request information. Used for operations outside the
480 // download system. 498 // download system.
481 scoped_ptr<DownloadRequestHandleInterface> request_handle_; 499 scoped_ptr<DownloadRequestHandleInterface> request_handle_;
482 500
483 uint32_t download_id_ = kInvalidId; 501 uint32_t download_id_ = kInvalidId;
484 502
485 // Display name for the download. If this is empty, then the display name is 503 // Display name for the download. If this is empty, then the display name is
486 // considered to be |target_path_.BaseName()|. 504 // considered to be |target_path_.BaseName()|.
487 base::FilePath display_name_; 505 base::FilePath display_name_;
488 506
489 // Full path to the downloaded or downloading file. This is the path to the
490 // physical file, if one exists. The final target path is specified by
491 // |target_path_|. |current_path_| can be empty if the in-progress path hasn't
492 // been determined.
493 base::FilePath current_path_;
494
495 // Target path of an in-progress download. We may be downloading to a 507 // Target path of an in-progress download. We may be downloading to a
496 // temporary or intermediate file (specified by |current_path_|. Once the 508 // temporary or intermediate file (specified by |current_path_|. Once the
497 // download completes, we will rename the file to |target_path_|. 509 // download completes, we will rename the file to |target_path_|.
498 base::FilePath target_path_; 510 base::FilePath target_path_;
499 511
500 // Whether the target should be overwritten, uniquified or prompted for. 512 // Whether the target should be overwritten, uniquified or prompted for.
501 TargetDisposition target_disposition_ = TARGET_DISPOSITION_OVERWRITE; 513 TargetDisposition target_disposition_ = TARGET_DISPOSITION_OVERWRITE;
502 514
503 // The chain of redirects that leading up to and including the final URL. 515 // The chain of redirects that leading up to and including the final URL.
504 std::vector<GURL> url_chain_; 516 std::vector<GURL> url_chain_;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 // which may look at the file extension and first few bytes of the file. 551 // which may look at the file extension and first few bytes of the file.
540 std::string original_mime_type_; 552 std::string original_mime_type_;
541 553
542 // The remote IP address where the download was fetched from. Copied from 554 // The remote IP address where the download was fetched from. Copied from
543 // DownloadCreateInfo::remote_address. 555 // DownloadCreateInfo::remote_address.
544 std::string remote_address_; 556 std::string remote_address_;
545 557
546 // Total bytes expected. 558 // Total bytes expected.
547 int64_t total_bytes_ = 0; 559 int64_t total_bytes_ = 0;
548 560
549 // Current received bytes.
550 int64_t received_bytes_ = 0;
551
552 // Current speed. Calculated by the DownloadFile.
553 int64_t bytes_per_sec_ = 0;
554
555 // Sha256 hash of the content. This might be empty either because
556 // the download isn't done yet or because the hash isn't needed
557 // (ChromeDownloadManagerDelegate::GenerateFileHash() returned false).
558 std::string hash_;
559
560 // A blob containing the state of the hash algorithm. Only valid while the
561 // download is in progress.
562 std::string hash_state_;
563
564 // Server's time stamp for the file.
565 std::string last_modified_time_;
566
567 // Server's ETAG for the file.
568 std::string etag_;
569
570 // Last reason. 561 // Last reason.
571 DownloadInterruptReason last_reason_ = DOWNLOAD_INTERRUPT_REASON_NONE; 562 DownloadInterruptReason last_reason_ = DOWNLOAD_INTERRUPT_REASON_NONE;
572 563
573 // Start time for recording statistics. 564 // Start time for recording statistics.
574 base::TimeTicks start_tick_; 565 base::TimeTicks start_tick_;
575 566
576 // The current state of this download. 567 // The current state of this download.
577 DownloadInternalState state_ = INITIAL_INTERNAL; 568 DownloadInternalState state_ = INITIAL_INTERNAL;
578 569
579 // Current danger type for the download. 570 // Current danger type for the download.
580 DownloadDangerType danger_type_ = DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS; 571 DownloadDangerType danger_type_ = DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS;
581 572
582 // The views of this item in the download shelf and download contents. 573 // The views of this item in the download shelf and download contents.
583 base::ObserverList<Observer> observers_; 574 base::ObserverList<Observer> observers_;
584 575
585 // Time the download was started. 576 // Time the download was started.
586 base::Time start_time_; 577 base::Time start_time_;
587 578
588 // Time the download completed. 579 // Time the download completed.
589 base::Time end_time_; 580 base::Time end_time_;
590 581
591 // Our delegate. 582 // Our delegate.
592 DownloadItemImplDelegate* delegate_ = nullptr; 583 DownloadItemImplDelegate* delegate_ = nullptr;
593 584
594 // In progress downloads may be paused by the user, we note it here. 585 // In progress downloads may be paused by the user, we note it here.
595 bool is_paused_ = false; 586 bool is_paused_ = false;
596 587
597 // The number of times this download has been resumed automatically.
598 int auto_resume_count_ = 0;
599
600 // A flag for indicating if the download should be opened at completion. 588 // A flag for indicating if the download should be opened at completion.
601 bool open_when_complete_ = false; 589 bool open_when_complete_ = false;
602 590
603 // A flag for indicating if the downloaded file is externally removed. 591 // A flag for indicating if the downloaded file is externally removed.
604 bool file_externally_removed_ = false; 592 bool file_externally_removed_ = false;
605 593
606 // True if the download was auto-opened. We set this rather than using 594 // True if the download was auto-opened. We set this rather than using
607 // an observer as it's frequently possible for the download to be auto opened 595 // an observer as it's frequently possible for the download to be auto opened
608 // before the observer is added. 596 // before the observer is added.
609 bool auto_opened_ = false; 597 bool auto_opened_ = false;
610 598
611 // True if the item was downloaded temporarily. 599 // True if the item was downloaded temporarily.
612 bool is_temporary_ = false; 600 bool is_temporary_ = false;
613 601
614 // True if we've saved all the data for the download.
615 bool all_data_saved_ = false;
616
617 // Error return from DestinationError. Stored separately from
618 // last_reason_ so that we can avoid handling destination errors until
619 // after file name determination has occurred.
620 DownloadInterruptReason destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE;
621
622 // Did the user open the item either directly or indirectly (such as by 602 // Did the user open the item either directly or indirectly (such as by
623 // setting always open files of this type)? The shelf also sets this field 603 // setting always open files of this type)? The shelf also sets this field
624 // when the user closes the shelf before the item has been opened but should 604 // when the user closes the shelf before the item has been opened but should
625 // be treated as though the user opened it. 605 // be treated as though the user opened it.
626 bool opened_ = false; 606 bool opened_ = false;
627 607
628 // Did the delegate delay calling Complete on this download? 608 // Did the delegate delay calling Complete on this download?
629 bool delegate_delayed_complete_ = false; 609 bool delegate_delayed_complete_ = false;
630 610
611 // Error return from DestinationError. Stored separately from
612 // last_reason_ so that we can avoid handling destination errors until
613 // after file name determination has occurred.
614 DownloadInterruptReason destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE;
615
616 // The following fields describe the current state of the download file.
617
631 // DownloadFile associated with this download. Note that this 618 // DownloadFile associated with this download. Note that this
632 // pointer may only be used or destroyed on the FILE thread. 619 // pointer may only be used or destroyed on the FILE thread.
633 // This pointer will be non-null only while the DownloadItem is in 620 // This pointer will be non-null only while the DownloadItem is in
634 // the IN_PROGRESS state. 621 // the IN_PROGRESS state.
635 scoped_ptr<DownloadFile> download_file_; 622 scoped_ptr<DownloadFile> download_file_;
636 623
624 // Full path to the downloaded or downloading file. This is the path to the
625 // physical file, if one exists. The final target path is specified by
626 // |target_path_|. |current_path_| can be empty if the in-progress path hasn't
627 // been determined.
628 base::FilePath current_path_;
629
630 // Current received bytes.
631 int64_t received_bytes_ = 0;
632
633 // Current speed. Calculated by the DownloadFile.
634 int64_t bytes_per_sec_ = 0;
635
636 // True if we've saved all the data for the download. If true, then the file
637 // at |current_path_| contains |received_bytes_|, which constitute the
638 // entirety of what we expect to save there. A digest of its contents can be
639 // found at |hash_|.
640 bool all_data_saved_ = false;
641
642 // The number of times this download has been resumed automatically. Will be
643 // reset to 0 if a resumption is performed in response to a Resume() call.
644 int auto_resume_count_ = 0;
645
646 // SHA256 hash of the possibly partial content. The hash is updated each time
647 // the download is interrupted, or when the all the data has been transferred.
648 std::string hash_;
649
650 // In the event of an interruption, the DownloadDestinationObserver interface
651 // exposes the partial hash state. This state can be held by the download item
652 // in case it's needed for resumption.
653 scoped_ptr<crypto::SecureHash> hash_state_;
654
655 // Contents of the Last-Modified header for the most recent server response.
656 std::string last_modified_time_;
657
658 // Server's ETAG for the file.
659 std::string etag_;
660
637 // Net log to use for this download. 661 // Net log to use for this download.
638 const net::BoundNetLog bound_net_log_; 662 const net::BoundNetLog bound_net_log_;
639 663
640 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; 664 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_;
641 665
642 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); 666 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl);
643 }; 667 };
644 668
645 } // namespace content 669 } // namespace content
646 670
647 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ 671 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698