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

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

Issue 2712713007: Make DownloadFileImpl handle multiple byte streams. (Closed)
Patch Set: Work on feedback. 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:
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,
asanka 2017/03/02 19:37:43 The meaning of a bool doesn't directly convey a me
xingliu 2017/03/02 23:06:15 This will change soon in next CL that we will pass
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.
asanka 2017/03/02 19:37:43 Why though? Is there a case where the stream reade
xingliu 2017/03/02 23:06:15 Yes, the byte stream will come later for parallel
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 length);
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.
asanka 2017/03/02 19:37:43 "Called after successfully writing a buffer to dis
xingliu 2017/03/02 23:06:15 Done.
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 length() const { return length_; }
114 int64_t bytes_written() const { return bytes_written_; }
115 bool is_finished() const { return finished_; }
116 void set_finished(bool finish) { finished_ = finish; }
117
118 private:
119 // Starting position for the stream to write to disk.
120 int64_t offset_;
121
122 // The maximum length to write to the disk. If set to 0, keep writing until
123 // the stream depletes.
124 int64_t length_;
125
126 // Number of bytes written to disk from the stream.
127 // Will write to disk at (|offset_| + |bytes_written_|).
asanka 2017/03/02 19:37:43 "Next write position is ..."
xingliu 2017/03/02 23:06:15 Done.
128 int64_t bytes_written_;
129
130 // If all the data read from the stream has been successfully write to disk.
David Trainor- moved to gerrit 2017/03/02 18:24:50 write -> written
xingliu 2017/03/02 19:21:21 Done.
131 bool finished_;
132
133 // The stream through which data comes.
134 // TODO(rdsmith): Move this into BaseFile; requires using the same
135 // stream semantics in SavePackage. Alternatively, replace SaveFile
136 // with DownloadFile and get rid of BaseFile.
asanka 2017/03/02 19:37:43 Remove this TODO. Doesn't apply anymore.
xingliu 2017/03/02 23:06:15 Done.
137 std::unique_ptr<ByteStreamReader> stream_reader_;
138
139 DISALLOW_COPY_AND_ASSIGN(SourceStream);
140 };
141
142 typedef std::unordered_map<int64_t, std::unique_ptr<SourceStream>>
143 SourceStreams;
144
76 // Options for RenameWithRetryInternal. 145 // Options for RenameWithRetryInternal.
77 enum RenameOption { 146 enum RenameOption {
78 UNIQUIFY = 1 << 0, // If there's already a file on disk that conflicts with 147 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 148 // |new_path|, try to create a unique file by appending
80 // a uniquifier. 149 // a uniquifier.
81 ANNOTATE_WITH_SOURCE_INFORMATION = 1 << 1 150 ANNOTATE_WITH_SOURCE_INFORMATION = 1 << 1
82 }; 151 };
83 152
84 struct RenameParameters { 153 struct RenameParameters {
85 RenameParameters(RenameOption option, 154 RenameParameters(RenameOption option,
(...skipping 15 matching lines...) Expand all
101 // encountered. Used for UMA. 170 // encountered. Used for UMA.
102 RenameCompletionCallback completion_callback; 171 RenameCompletionCallback completion_callback;
103 }; 172 };
104 173
105 // Rename file_ based on |parameters|. 174 // Rename file_ based on |parameters|.
106 void RenameWithRetryInternal(std::unique_ptr<RenameParameters> parameters); 175 void RenameWithRetryInternal(std::unique_ptr<RenameParameters> parameters);
107 176
108 // Send an update on our progress. 177 // Send an update on our progress.
109 void SendUpdate(); 178 void SendUpdate();
110 179
111 // Called when there's some activity on stream_reader_ that needs to be 180 // Called before the data is written to disk.
181 void WillWriteToDisk(size_t data_len);
182
183 // Called when there's some activity on the byte stream that needs to be
112 // handled. 184 // handled.
113 void StreamActive(); 185 void StreamActive(SourceStream* source_stream);
186
187 // Register callback and start to read data from the stream.
188 void RegisterAndActivateStream(SourceStream* source_stream);
189
190 // Return the total valid bytes received in the target file.
191 // If the file is a sparse file, return the total number of valid bytes.
192 // Otherwise, return the current file size.
193 int64_t TotalBytesReceived() const;
114 194
115 net::NetLogWithSource net_log_; 195 net::NetLogWithSource net_log_;
116 196
117 // The base file instance. 197 // The base file instance.
118 BaseFile file_; 198 BaseFile file_;
119 199
120 // DownloadSaveInfo provided during construction. Since the DownloadFileImpl 200 // DownloadSaveInfo provided during construction. Since the DownloadFileImpl
121 // can be created on any thread, this holds the save_info_ until it can be 201 // can be created on any thread, this holds the save_info_ until it can be
122 // used to initialize file_ on the FILE thread. 202 // used to initialize file_ on the FILE thread.
123 std::unique_ptr<DownloadSaveInfo> save_info_; 203 std::unique_ptr<DownloadSaveInfo> save_info_;
124 204
125 // The default directory for creating the download file. 205 // The default directory for creating the download file.
126 base::FilePath default_download_directory_; 206 base::FilePath default_download_directory_;
127 207
128 // The stream through which data comes. 208 // Map of the offset and the source stream that represents the slice
129 // TODO(rdsmith): Move this into BaseFile; requires using the same 209 // starting from offset.
130 // stream semantics in SavePackage. Alternatively, replace SaveFile 210 // Must be modified on the same thread that constructs the DownloadFile.
131 // with DownloadFile and get rid of BaseFile. 211 // Any byte stream should have a SourceStream before added to the download
132 std::unique_ptr<ByteStreamReader> stream_reader_; 212 // file.
213 // The disk IO is completed when all source streams are finished.
214 SourceStreams source_streams_;
133 215
134 // Used to trigger progress updates. 216 // Used to trigger progress updates.
135 std::unique_ptr<base::RepeatingTimer> update_timer_; 217 std::unique_ptr<base::RepeatingTimer> update_timer_;
136 218
219 // Set to true when multiple byte streams write to the same file.
220 // The file may contains null bytes(holes) in between of valid data slices.
221 bool is_sparse_file_;
David Trainor- moved to gerrit 2017/03/02 18:24:50 Something to consider, could we rely on source_str
xingliu 2017/03/02 19:21:20 Added a TODO here. We will probably pass a slice
asanka 2017/03/02 19:37:43 +1. Also I don't think it makes things any simpler
xingliu 2017/03/02 23:06:15 Added a TODO that we will use slice info later.
222
137 // Statistics 223 // Statistics
138 size_t bytes_seen_; 224 size_t bytes_seen_;
139 base::TimeDelta disk_writes_time_; 225 base::TimeDelta disk_writes_time_;
140 base::TimeTicks download_start_; 226 base::TimeTicks download_start_;
141 RateEstimator rate_estimator_; 227 RateEstimator rate_estimator_;
142 228
229 base::ThreadChecker thread_checker_;
143 base::WeakPtr<DownloadDestinationObserver> observer_; 230 base::WeakPtr<DownloadDestinationObserver> observer_;
144
145 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; 231 base::WeakPtrFactory<DownloadFileImpl> weak_factory_;
146 232
147 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); 233 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl);
148 }; 234 };
149 235
150 } // namespace content 236 } // namespace content
151 237
152 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ 238 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698