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

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

Powered by Google App Engine
This is Rietveld 408576698