| 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> |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 const net::NetLogWithSource& net_log, | 51 const net::NetLogWithSource& net_log, |
| 52 bool is_sparse_file, | 52 bool is_sparse_file, |
| 53 base::WeakPtr<DownloadDestinationObserver> observer); | 53 base::WeakPtr<DownloadDestinationObserver> observer); |
| 54 | 54 |
| 55 ~DownloadFileImpl() override; | 55 ~DownloadFileImpl() override; |
| 56 | 56 |
| 57 // DownloadFile functions. | 57 // DownloadFile functions. |
| 58 void Initialize(const InitializeCallback& callback) override; | 58 void Initialize(const InitializeCallback& callback) override; |
| 59 | 59 |
| 60 void AddByteStream(std::unique_ptr<ByteStreamReader> stream_reader, | 60 void AddByteStream(std::unique_ptr<ByteStreamReader> stream_reader, |
| 61 int64_t offset) override; | 61 int64_t offset, |
| 62 int64_t length) override; |
| 62 | 63 |
| 63 void RenameAndUniquify(const base::FilePath& full_path, | 64 void RenameAndUniquify(const base::FilePath& full_path, |
| 64 const RenameCompletionCallback& callback) override; | 65 const RenameCompletionCallback& callback) override; |
| 65 void RenameAndAnnotate(const base::FilePath& full_path, | 66 void RenameAndAnnotate(const base::FilePath& full_path, |
| 66 const std::string& client_guid, | 67 const std::string& client_guid, |
| 67 const GURL& source_url, | 68 const GURL& source_url, |
| 68 const GURL& referrer_url, | 69 const GURL& referrer_url, |
| 69 const RenameCompletionCallback& callback) override; | 70 const RenameCompletionCallback& callback) override; |
| 70 void Detach() override; | 71 void Detach() override; |
| 71 void Cancel() override; | 72 void Cancel() override; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 87 private: | 88 private: |
| 88 friend class DownloadFileTest; | 89 friend class DownloadFileTest; |
| 89 | 90 |
| 90 // Wrapper of a ByteStreamReader, and the meta data needed to write to a | 91 // Wrapper of a ByteStreamReader, and the meta data needed to write to a |
| 91 // slice of the target file. | 92 // slice of the target file. |
| 92 // | 93 // |
| 93 // Does not require the stream reader ready when constructor is called. | 94 // Does not require the stream reader ready when constructor is called. |
| 94 // |stream_reader_| can be set later when the network response is handled. | 95 // |stream_reader_| can be set later when the network response is handled. |
| 95 // | 96 // |
| 96 // Multiple SourceStreams can concurrently write to the same file sink. | 97 // Multiple SourceStreams can concurrently write to the same file sink. |
| 97 // | |
| 98 // The file IO processing is finished when all SourceStreams are finished. | |
| 99 class CONTENT_EXPORT SourceStream { | 98 class CONTENT_EXPORT SourceStream { |
| 100 public: | 99 public: |
| 101 SourceStream(int64_t offset, int64_t length); | 100 SourceStream(int64_t offset, |
| 101 int64_t length, |
| 102 std::unique_ptr<ByteStreamReader> stream_reader); |
| 102 ~SourceStream(); | 103 ~SourceStream(); |
| 103 | 104 |
| 104 void SetByteStream(std::unique_ptr<ByteStreamReader> stream_reader); | |
| 105 | |
| 106 // Called after successfully writing a buffer to disk. | 105 // Called after successfully writing a buffer to disk. |
| 107 void OnWriteBytesToDisk(int64_t bytes_write); | 106 void OnWriteBytesToDisk(int64_t bytes_write); |
| 108 | 107 |
| 109 ByteStreamReader* stream_reader() const { return stream_reader_.get(); } | 108 ByteStreamReader* stream_reader() const { return stream_reader_.get(); } |
| 110 int64_t offset() const { return offset_; } | 109 int64_t offset() const { return offset_; } |
| 111 int64_t length() const { return length_; } | 110 int64_t length() const { return length_; } |
| 112 void set_length(int64_t length) { length_ = length; } | 111 void set_length(int64_t length) { length_ = length; } |
| 113 int64_t bytes_written() const { return bytes_written_; } | 112 int64_t bytes_written() const { return bytes_written_; } |
| 114 bool is_finished() const { return finished_; } | 113 bool is_finished() const { return finished_; } |
| 115 void set_finished(bool finish) { finished_ = finish; } | 114 void set_finished(bool finish) { finished_ = finish; } |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 // DownloadSaveInfo provided during construction. Since the DownloadFileImpl | 220 // DownloadSaveInfo provided during construction. Since the DownloadFileImpl |
| 222 // can be created on any thread, this holds the save_info_ until it can be | 221 // can be created on any thread, this holds the save_info_ until it can be |
| 223 // used to initialize file_ on the FILE thread. | 222 // used to initialize file_ on the FILE thread. |
| 224 std::unique_ptr<DownloadSaveInfo> save_info_; | 223 std::unique_ptr<DownloadSaveInfo> save_info_; |
| 225 | 224 |
| 226 // The default directory for creating the download file. | 225 // The default directory for creating the download file. |
| 227 base::FilePath default_download_directory_; | 226 base::FilePath default_download_directory_; |
| 228 | 227 |
| 229 // Map of the offset and the source stream that represents the slice | 228 // Map of the offset and the source stream that represents the slice |
| 230 // starting from offset. | 229 // starting from offset. |
| 231 // Must be created on the same thread that constructs the DownloadFile. | |
| 232 // Should not add or remove elements after creation. | |
| 233 // Any byte stream should have a SourceStream before added to the download | |
| 234 // file. | |
| 235 // The disk IO is completed when all source streams are finished. | |
| 236 SourceStreams source_streams_; | 230 SourceStreams source_streams_; |
| 237 | 231 |
| 238 // Used to trigger progress updates. | 232 // Used to trigger progress updates. |
| 239 std::unique_ptr<base::RepeatingTimer> update_timer_; | 233 std::unique_ptr<base::RepeatingTimer> update_timer_; |
| 240 | 234 |
| 241 // Set to true when multiple byte streams write to the same file. | 235 // Set to true when multiple byte streams write to the same file. |
| 242 // The file may contain null bytes(holes) in between of valid data slices. | 236 // The file may contain null bytes(holes) in between of valid data slices. |
| 243 // TODO(xingliu): Remove this variable. We can use size of |received_slices_| | 237 // TODO(xingliu): Remove this variable. We can use size of |received_slices_| |
| 244 // to determine if the file is sparse | 238 // to determine if the file is sparse |
| 245 bool is_sparse_file_; | 239 bool is_sparse_file_; |
| 246 | 240 |
| 247 // Statistics | 241 // Statistics |
| 248 size_t bytes_seen_; | 242 size_t bytes_seen_; |
| 249 base::TimeDelta disk_writes_time_; | 243 base::TimeDelta disk_writes_time_; |
| 250 base::TimeTicks download_start_; | 244 base::TimeTicks download_start_; |
| 251 RateEstimator rate_estimator_; | 245 RateEstimator rate_estimator_; |
| 252 | 246 |
| 253 std::vector<DownloadItem::ReceivedSlice> received_slices_; | 247 std::vector<DownloadItem::ReceivedSlice> received_slices_; |
| 254 | 248 |
| 255 base::WeakPtr<DownloadDestinationObserver> observer_; | 249 base::WeakPtr<DownloadDestinationObserver> observer_; |
| 256 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; | 250 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; |
| 257 | 251 |
| 258 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); | 252 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); |
| 259 }; | 253 }; |
| 260 | 254 |
| 261 } // namespace content | 255 } // namespace content |
| 262 | 256 |
| 263 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ | 257 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ |
| OLD | NEW |