Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_FILE_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ |
| 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ |
| 7 | 7 |
| 8 #include "content/browser/download/download_file.h" | 8 #include "content/browser/download/download_file.h" |
| 9 | 9 |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| 11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <unordered_map> | |
| 15 | 16 |
| 16 #include "base/files/file.h" | 17 #include "base/files/file.h" |
| 17 #include "base/macros.h" | 18 #include "base/macros.h" |
| 18 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
| 21 #include "base/threading/thread_checker.h" | |
| 20 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 21 #include "base/timer/timer.h" | 23 #include "base/timer/timer.h" |
| 22 #include "content/browser/byte_stream.h" | 24 #include "content/browser/byte_stream.h" |
| 23 #include "content/browser/download/base_file.h" | 25 #include "content/browser/download/base_file.h" |
| 24 #include "content/browser/download/rate_estimator.h" | 26 #include "content/browser/download/rate_estimator.h" |
| 25 #include "content/public/browser/download_save_info.h" | 27 #include "content/public/browser/download_save_info.h" |
| 26 #include "net/log/net_log_with_source.h" | 28 #include "net/log/net_log_with_source.h" |
| 27 | 29 |
| 28 namespace content { | 30 namespace content { |
| 29 class ByteStreamReader; | 31 class ByteStreamReader; |
| 30 class DownloadDestinationObserver; | 32 class DownloadDestinationObserver; |
| 31 | 33 |
| 32 class CONTENT_EXPORT DownloadFileImpl : public DownloadFile { | 34 class CONTENT_EXPORT DownloadFileImpl : public DownloadFile { |
| 33 public: | 35 public: |
| 34 // Takes ownership of the object pointed to by |request_handle|. | 36 // Takes ownership of the object pointed to by |request_handle|. |
| 35 // |net_log| will be used for logging the download file's events. | 37 // |net_log| will be used for logging the download file's events. |
| 36 // May be constructed on any thread. All methods besides the constructor | 38 // May be constructed on any thread. All methods besides the constructor |
| 37 // (including destruction) must occur on the FILE thread. | 39 // (including destruction) must occur on the FILE thread. |
| 38 // | 40 // |
| 39 // Note that the DownloadFileImpl automatically reads from the passed in | 41 // Note that the DownloadFileImpl automatically reads from the passed in |
| 40 // stream, and sends updates and status of those reads to the | 42 // stream, and sends updates and status of those reads to the |
| 41 // DownloadDestinationObserver. | 43 // DownloadDestinationObserver. |
| 42 DownloadFileImpl(std::unique_ptr<DownloadSaveInfo> save_info, | 44 DownloadFileImpl(std::unique_ptr<DownloadSaveInfo> save_info, |
| 43 const base::FilePath& default_downloads_directory, | 45 const base::FilePath& default_downloads_directory, |
| 44 std::unique_ptr<ByteStreamReader> byte_stream, | 46 std::unique_ptr<ByteStreamReader> stream_reader, |
| 45 const net::NetLogWithSource& net_log, | 47 const net::NetLogWithSource& net_log, |
| 48 bool is_sparse_file, | |
| 46 base::WeakPtr<DownloadDestinationObserver> observer); | 49 base::WeakPtr<DownloadDestinationObserver> observer); |
| 47 | 50 |
| 48 ~DownloadFileImpl() override; | 51 ~DownloadFileImpl() override; |
| 49 | 52 |
| 50 // DownloadFile functions. | 53 // DownloadFile functions. |
| 51 void Initialize(const InitializeCallback& callback) override; | 54 void Initialize(const InitializeCallback& callback) override; |
| 55 | |
| 56 void AddByteStream(std::unique_ptr<ByteStreamReader> stream_reader, | |
| 57 int64_t offset) override; | |
| 58 | |
| 52 void RenameAndUniquify(const base::FilePath& full_path, | 59 void RenameAndUniquify(const base::FilePath& full_path, |
| 53 const RenameCompletionCallback& callback) override; | 60 const RenameCompletionCallback& callback) override; |
| 54 void RenameAndAnnotate(const base::FilePath& full_path, | 61 void RenameAndAnnotate(const base::FilePath& full_path, |
| 55 const std::string& client_guid, | 62 const std::string& client_guid, |
| 56 const GURL& source_url, | 63 const GURL& source_url, |
| 57 const GURL& referrer_url, | 64 const GURL& referrer_url, |
| 58 const RenameCompletionCallback& callback) override; | 65 const RenameCompletionCallback& callback) override; |
| 59 void Detach() override; | 66 void Detach() override; |
| 60 void Cancel() override; | 67 void Cancel() override; |
| 61 const base::FilePath& FullPath() const override; | 68 const base::FilePath& FullPath() const override; |
| 62 bool InProgress() const override; | 69 bool InProgress() const override; |
| 63 | 70 |
| 64 protected: | 71 protected: |
| 65 // For test class overrides. | 72 // For test class overrides. |
| 73 // Append data to the file. | |
| 74 // On OS level, it will write at current position to the file. | |
| 66 virtual DownloadInterruptReason AppendDataToFile( | 75 virtual DownloadInterruptReason AppendDataToFile( |
| 67 const char* data, size_t data_len); | 76 const char* data, size_t data_len); |
| 68 | 77 |
| 78 // Write data from the offset to the file. | |
| 79 // On OS level, it will seek to the |offset| and write from there. | |
| 80 DownloadInterruptReason WriteDataToFile(int64_t offset, | |
| 81 const char* data, | |
| 82 size_t data_len); | |
| 83 | |
| 69 virtual base::TimeDelta GetRetryDelayForFailedRename(int attempt_number); | 84 virtual base::TimeDelta GetRetryDelayForFailedRename(int attempt_number); |
| 70 | 85 |
| 71 virtual bool ShouldRetryFailedRename(DownloadInterruptReason reason); | 86 virtual bool ShouldRetryFailedRename(DownloadInterruptReason reason); |
| 72 | 87 |
| 73 private: | 88 private: |
| 74 friend class DownloadFileTest; | 89 friend class DownloadFileTest; |
| 75 | 90 |
| 91 // Wrapper of a ByteStreamReader, and the meta data needed to write to a | |
| 92 // slice of the target file. | |
| 93 // | |
| 94 // Does not require the stream reader ready when constructor is called. | |
| 95 // |stream_reader_| can be set later when the network response is handled. | |
| 96 // | |
| 97 // Multiple SourceStreams can concurrently write to the same file sink. | |
| 98 // | |
| 99 // The file IO processing is finished when all SourceStreams are finished. | |
| 100 class CONTENT_EXPORT SourceStream { | |
| 101 public: | |
| 102 SourceStream(int64_t offset, int64_t bytes_written); | |
|
qinmin
2017/02/28 06:56:12
why does this take bytes_written? Shouldn't that a
xingliu
2017/02/28 23:24:21
bytes_written can be serialized/deserialized from/
qinmin
2017/03/01 00:01:12
If we start downloding a new slice, it should come
xingliu
2017/03/01 19:25:07
Done, bytes written now always starts from 0 for a
| |
| 103 ~SourceStream(); | |
| 104 | |
| 105 void SetByteStream(std::unique_ptr<ByteStreamReader> stream_reader); | |
| 106 | |
| 107 // Called when successfully read a buffer from the stream and write it to | |
| 108 // disk. | |
| 109 void OnWriteBytesToDisk(int64_t bytes_write); | |
| 110 | |
| 111 ByteStreamReader* stream_reader() const { return stream_reader_.get(); } | |
| 112 int64_t offset() const { return offset_; } | |
| 113 int64_t bytes_written() const { return bytes_written_; } | |
| 114 bool is_finished() const { return finished_; } | |
| 115 void set_finished(bool finish) { finished_ = finish; } | |
| 116 | |
| 117 private: | |
| 118 // Starting position for the current slice. | |
| 119 int64_t offset_; | |
| 120 | |
| 121 // Number of bytes written to disk for the slice. The initial value may be | |
| 122 // read from download history. | |
| 123 // Will write to disk at (|offset_| + |bytes_received_|). | |
| 124 int64_t bytes_written_; | |
| 125 | |
| 126 // If all the data read from the stream has been successfully write to disk. | |
| 127 bool finished_; | |
| 128 | |
| 129 // The stream through which data comes. | |
| 130 // TODO(rdsmith): Move this into BaseFile; requires using the same | |
| 131 // stream semantics in SavePackage. Alternatively, replace SaveFile | |
| 132 // with DownloadFile and get rid of BaseFile. | |
| 133 std::unique_ptr<ByteStreamReader> stream_reader_; | |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(SourceStream); | |
| 136 }; | |
| 137 | |
| 138 typedef std::unordered_map<int64_t, std::unique_ptr<SourceStream>> | |
| 139 SourceStreams; | |
| 140 | |
| 76 // Options for RenameWithRetryInternal. | 141 // Options for RenameWithRetryInternal. |
| 77 enum RenameOption { | 142 enum RenameOption { |
| 78 UNIQUIFY = 1 << 0, // If there's already a file on disk that conflicts with | 143 UNIQUIFY = 1 << 0, // If there's already a file on disk that conflicts with |
| 79 // |new_path|, try to create a unique file by appending | 144 // |new_path|, try to create a unique file by appending |
| 80 // a uniquifier. | 145 // a uniquifier. |
| 81 ANNOTATE_WITH_SOURCE_INFORMATION = 1 << 1 | 146 ANNOTATE_WITH_SOURCE_INFORMATION = 1 << 1 |
| 82 }; | 147 }; |
| 83 | 148 |
| 84 struct RenameParameters { | 149 struct RenameParameters { |
| 85 RenameParameters(RenameOption option, | 150 RenameParameters(RenameOption option, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 101 // encountered. Used for UMA. | 166 // encountered. Used for UMA. |
| 102 RenameCompletionCallback completion_callback; | 167 RenameCompletionCallback completion_callback; |
| 103 }; | 168 }; |
| 104 | 169 |
| 105 // Rename file_ based on |parameters|. | 170 // Rename file_ based on |parameters|. |
| 106 void RenameWithRetryInternal(std::unique_ptr<RenameParameters> parameters); | 171 void RenameWithRetryInternal(std::unique_ptr<RenameParameters> parameters); |
| 107 | 172 |
| 108 // Send an update on our progress. | 173 // Send an update on our progress. |
| 109 void SendUpdate(); | 174 void SendUpdate(); |
| 110 | 175 |
| 111 // Called when there's some activity on stream_reader_ that needs to be | 176 // Called before the data is written to disk. |
| 177 void WillWriteToDisk(size_t data_len); | |
| 178 | |
| 179 // Called when there's some activity on the byte stream that needs to be | |
| 112 // handled. | 180 // handled. |
| 113 void StreamActive(); | 181 void StreamActive(SourceStream* source_stream); |
| 182 | |
| 183 // Register callback and start to read data from the stream. | |
| 184 void RegisterAndActivateStream(SourceStream* source_stream); | |
| 185 | |
| 186 // Return the total valid bytes received in the target file. | |
| 187 // If the file is a sparse file, return the total number of valid bytes. | |
| 188 // Otherwise, return the current file size. | |
| 189 int64_t TotalBytesReceived() const; | |
| 114 | 190 |
| 115 net::NetLogWithSource net_log_; | 191 net::NetLogWithSource net_log_; |
| 116 | 192 |
| 117 // The base file instance. | 193 // The base file instance. |
| 118 BaseFile file_; | 194 BaseFile file_; |
| 119 | 195 |
| 120 // DownloadSaveInfo provided during construction. Since the DownloadFileImpl | 196 // DownloadSaveInfo provided during construction. Since the DownloadFileImpl |
| 121 // can be created on any thread, this holds the save_info_ until it can be | 197 // can be created on any thread, this holds the save_info_ until it can be |
| 122 // used to initialize file_ on the FILE thread. | 198 // used to initialize file_ on the FILE thread. |
| 123 std::unique_ptr<DownloadSaveInfo> save_info_; | 199 std::unique_ptr<DownloadSaveInfo> save_info_; |
| 124 | 200 |
| 125 // The default directory for creating the download file. | 201 // The default directory for creating the download file. |
| 126 base::FilePath default_download_directory_; | 202 base::FilePath default_download_directory_; |
| 127 | 203 |
| 128 // The stream through which data comes. | 204 // Map of the offset and the source stream that represents the slice |
| 129 // TODO(rdsmith): Move this into BaseFile; requires using the same | 205 // starting from offset. |
| 130 // stream semantics in SavePackage. Alternatively, replace SaveFile | 206 // Must be modified on the same thread that constructs the DownloadFile. |
| 131 // with DownloadFile and get rid of BaseFile. | 207 // Any byte stream should have a SourceStream before added to the download |
| 132 std::unique_ptr<ByteStreamReader> stream_reader_; | 208 // file. |
| 209 // The disk IO is completed when all source streams are finished. | |
| 210 SourceStreams source_streams_; | |
| 133 | 211 |
| 134 // Used to trigger progress updates. | 212 // Used to trigger progress updates. |
| 135 std::unique_ptr<base::RepeatingTimer> update_timer_; | 213 std::unique_ptr<base::RepeatingTimer> update_timer_; |
| 136 | 214 |
| 215 // Set to true when multiple byte streams write to the same file. | |
| 216 // The file may contains null bytes(holes) in between of valid data slices. | |
| 217 bool is_sparse_file_; | |
| 218 | |
| 137 // Statistics | 219 // Statistics |
| 138 size_t bytes_seen_; | 220 size_t bytes_seen_; |
| 139 base::TimeDelta disk_writes_time_; | 221 base::TimeDelta disk_writes_time_; |
| 140 base::TimeTicks download_start_; | 222 base::TimeTicks download_start_; |
| 141 RateEstimator rate_estimator_; | 223 RateEstimator rate_estimator_; |
| 142 | 224 |
| 225 base::ThreadChecker thread_checker_; | |
| 143 base::WeakPtr<DownloadDestinationObserver> observer_; | 226 base::WeakPtr<DownloadDestinationObserver> observer_; |
| 144 | |
| 145 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; | 227 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; |
| 146 | 228 |
| 147 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); | 229 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); |
| 148 }; | 230 }; |
| 149 | 231 |
| 150 } // namespace content | 232 } // namespace content |
| 151 | 233 |
| 152 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ | 234 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ |
| OLD | NEW |