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

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

Issue 2712713007: Make DownloadFileImpl handle multiple byte streams. (Closed)
Patch Set: Export the new class for linking on windows. Created 3 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_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:
36 // Wrapper of a ByteStreamReader, and the meta data needed to write to a
37 // slice of the target file.
38 //
39 // Does not require the stream reader ready when constructor is called.
40 // |stream_reader_| can be set later when the network response is handled.
41 //
42 // Multiple SourceStreams can concurrently write to the same file sink.
43 //
44 // The file IO processing is finished when all SourceStreams are finished.
45 class CONTENT_EXPORT SourceStream {
qinmin 2017/02/27 18:55:29 does this needs to be public?
xingliu 2017/02/28 00:57:06 Makes sense, change it to private. The unit test
46 public:
47 SourceStream(int64_t offset, int64_t bytes_received);
48 ~SourceStream();
49
50 void SetByteStream(std::unique_ptr<ByteStreamReader> stream_reader);
51
52 // Called when successfully read a buffer from the stream and write it to
53 // disk.
54 void OnWriteBytesToDisk(int64_t bytes_write);
55
56 ByteStreamReader* stream_reader() const { return stream_reader_.get(); }
57 int64_t offset() const { return offset_; }
58 int64_t bytes_received() const { return bytes_received_; }
qinmin 2017/02/27 18:55:29 I think byte_received is misleading for a stream,
xingliu 2017/02/28 00:57:06 Done, changed to bytes_written.
59 bool is_finished() const { return finished_; }
60 void set_finished(bool finish) { finished_ = finish; }
61
62 private:
63 // Starting position for the current slice.
64 int64_t offset_;
65
66 // Number of bytes received for the slice. Only get updated after bytes
67 // written to disk. The initial value may be read from download history.
68 // Will write to disk at (|offset_| + |bytes_received_|).
69 int64_t bytes_received_;
70
71 // If all the data read from the stream has been successfully write to disk.
72 bool finished_;
73
74 // The stream through which data comes.
75 // TODO(rdsmith): Move this into BaseFile; requires using the same
76 // stream semantics in SavePackage. Alternatively, replace SaveFile
77 // with DownloadFile and get rid of BaseFile.
78 std::unique_ptr<ByteStreamReader> stream_reader_;
79
80 DISALLOW_COPY_AND_ASSIGN(SourceStream);
81 };
82
83 typedef std::unordered_map<int64_t, std::unique_ptr<SourceStream>>
84 SourceStreams;
85
34 // Takes ownership of the object pointed to by |request_handle|. 86 // Takes ownership of the object pointed to by |request_handle|.
35 // |net_log| will be used for logging the download file's events. 87 // |net_log| will be used for logging the download file's events.
36 // May be constructed on any thread. All methods besides the constructor 88 // May be constructed on any thread. All methods besides the constructor
37 // (including destruction) must occur on the FILE thread. 89 // (including destruction) must occur on the FILE thread.
38 // 90 //
39 // Note that the DownloadFileImpl automatically reads from the passed in 91 // Note that the DownloadFileImpl automatically reads from the passed in
40 // stream, and sends updates and status of those reads to the 92 // stream, and sends updates and status of those reads to the
41 // DownloadDestinationObserver. 93 // DownloadDestinationObserver.
42 DownloadFileImpl(std::unique_ptr<DownloadSaveInfo> save_info, 94 DownloadFileImpl(std::unique_ptr<DownloadSaveInfo> save_info,
43 const base::FilePath& default_downloads_directory, 95 const base::FilePath& default_downloads_directory,
44 std::unique_ptr<ByteStreamReader> byte_stream, 96 std::unique_ptr<ByteStreamReader> stream_reader,
45 const net::NetLogWithSource& net_log, 97 const net::NetLogWithSource& net_log,
98 bool is_sparse_file,
46 base::WeakPtr<DownloadDestinationObserver> observer); 99 base::WeakPtr<DownloadDestinationObserver> observer);
47 100
48 ~DownloadFileImpl() override; 101 ~DownloadFileImpl() override;
49 102
50 // DownloadFile functions. 103 // DownloadFile functions.
51 void Initialize(const InitializeCallback& callback) override; 104 void Initialize(const InitializeCallback& callback) override;
105
106 void AddByteStream(std::unique_ptr<ByteStreamReader> stream_reader,
107 int64_t offset) override;
108
52 void RenameAndUniquify(const base::FilePath& full_path, 109 void RenameAndUniquify(const base::FilePath& full_path,
53 const RenameCompletionCallback& callback) override; 110 const RenameCompletionCallback& callback) override;
54 void RenameAndAnnotate(const base::FilePath& full_path, 111 void RenameAndAnnotate(const base::FilePath& full_path,
55 const std::string& client_guid, 112 const std::string& client_guid,
56 const GURL& source_url, 113 const GURL& source_url,
57 const GURL& referrer_url, 114 const GURL& referrer_url,
58 const RenameCompletionCallback& callback) override; 115 const RenameCompletionCallback& callback) override;
59 void Detach() override; 116 void Detach() override;
60 void Cancel() override; 117 void Cancel() override;
61 const base::FilePath& FullPath() const override; 118 const base::FilePath& FullPath() const override;
62 bool InProgress() const override; 119 bool InProgress() const override;
63 120
64 protected: 121 protected:
65 // For test class overrides. 122 // For test class overrides.
123 // Append data to the file.
124 // On OS level, it will write at current position to the file.
66 virtual DownloadInterruptReason AppendDataToFile( 125 virtual DownloadInterruptReason AppendDataToFile(
67 const char* data, size_t data_len); 126 const char* data, size_t data_len);
68 127
128 // Write data from the offset to the file.
129 // On OS level, it will seek to the |offset| and write from there.
130 DownloadInterruptReason WriteDataToFile(int64_t offset,
131 const char* data,
132 size_t data_len);
133
69 virtual base::TimeDelta GetRetryDelayForFailedRename(int attempt_number); 134 virtual base::TimeDelta GetRetryDelayForFailedRename(int attempt_number);
70 135
71 virtual bool ShouldRetryFailedRename(DownloadInterruptReason reason); 136 virtual bool ShouldRetryFailedRename(DownloadInterruptReason reason);
72 137
73 private: 138 private:
74 friend class DownloadFileTest; 139 friend class DownloadFileTest;
75 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
(...skipping 22 matching lines...) Expand all
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698