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

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

Issue 2712713007: Make DownloadFileImpl handle multiple byte streams. (Closed)
Patch Set: Remove the AppendDataToFile call, fix the browsertest. 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,
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.
66 virtual DownloadInterruptReason AppendDataToFile( 73 // Write data from the offset to the file.
67 const char* data, size_t data_len); 74 // On OS level, it will seek to the |offset| and write from there.
75 virtual DownloadInterruptReason WriteDataToFile(int64_t offset,
76 const char* data,
77 size_t data_len);
68 78
69 virtual base::TimeDelta GetRetryDelayForFailedRename(int attempt_number); 79 virtual base::TimeDelta GetRetryDelayForFailedRename(int attempt_number);
70 80
71 virtual bool ShouldRetryFailedRename(DownloadInterruptReason reason); 81 virtual bool ShouldRetryFailedRename(DownloadInterruptReason reason);
72 82
73 private: 83 private:
74 friend class DownloadFileTest; 84 friend class DownloadFileTest;
75 85
86 // Wrapper of a ByteStreamReader, and the meta data needed to write to a
87 // slice of the target file.
88 //
89 // Does not require the stream reader ready when constructor is called.
90 // |stream_reader_| can be set later when the network response is handled.
91 //
92 // Multiple SourceStreams can concurrently write to the same file sink.
93 //
94 // The file IO processing is finished when all SourceStreams are finished.
95 class CONTENT_EXPORT SourceStream {
96 public:
97 SourceStream(int64_t offset, int64_t length);
98 ~SourceStream();
99
100 void SetByteStream(std::unique_ptr<ByteStreamReader> stream_reader);
101
102 // Called after successfully writing a buffer to disk.
103 void OnWriteBytesToDisk(int64_t bytes_write);
104
105 ByteStreamReader* stream_reader() const { return stream_reader_.get(); }
106 int64_t offset() const { return offset_; }
107 int64_t length() const { return length_; }
108 int64_t bytes_written() const { return bytes_written_; }
109 bool is_finished() const { return finished_; }
110 void set_finished(bool finish) { finished_ = finish; }
111
112 private:
113 // Starting position for the stream to write to disk.
114 int64_t offset_;
115
116 // The maximum length to write to the disk. If set to 0, keep writing until
117 // the stream depletes.
118 int64_t length_;
119
120 // Number of bytes written to disk from the stream.
121 // Next write position is (|offset_| + |bytes_written_|).
122 int64_t bytes_written_;
123
124 // If all the data read from the stream has been successfully written to
125 // disk.
126 bool finished_;
127
128 // The stream through which data comes.
129 std::unique_ptr<ByteStreamReader> stream_reader_;
130
131 DISALLOW_COPY_AND_ASSIGN(SourceStream);
132 };
133
134 typedef std::unordered_map<int64_t, std::unique_ptr<SourceStream>>
135 SourceStreams;
136
76 // Options for RenameWithRetryInternal. 137 // Options for RenameWithRetryInternal.
77 enum RenameOption { 138 enum RenameOption {
78 UNIQUIFY = 1 << 0, // If there's already a file on disk that conflicts with 139 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 140 // |new_path|, try to create a unique file by appending
80 // a uniquifier. 141 // a uniquifier.
81 ANNOTATE_WITH_SOURCE_INFORMATION = 1 << 1 142 ANNOTATE_WITH_SOURCE_INFORMATION = 1 << 1
82 }; 143 };
83 144
84 struct RenameParameters { 145 struct RenameParameters {
85 RenameParameters(RenameOption option, 146 RenameParameters(RenameOption option,
(...skipping 15 matching lines...) Expand all
101 // encountered. Used for UMA. 162 // encountered. Used for UMA.
102 RenameCompletionCallback completion_callback; 163 RenameCompletionCallback completion_callback;
103 }; 164 };
104 165
105 // Rename file_ based on |parameters|. 166 // Rename file_ based on |parameters|.
106 void RenameWithRetryInternal(std::unique_ptr<RenameParameters> parameters); 167 void RenameWithRetryInternal(std::unique_ptr<RenameParameters> parameters);
107 168
108 // Send an update on our progress. 169 // Send an update on our progress.
109 void SendUpdate(); 170 void SendUpdate();
110 171
111 // Called when there's some activity on stream_reader_ that needs to be 172 // Called before the data is written to disk.
173 void WillWriteToDisk(size_t data_len);
174
175 // Called when there's some activity on the byte stream that needs to be
112 // handled. 176 // handled.
113 void StreamActive(); 177 void StreamActive(SourceStream* source_stream);
178
179 // Register callback and start to read data from the stream.
180 void RegisterAndActivateStream(SourceStream* source_stream);
181
182 // Return the total valid bytes received in the target file.
183 // If the file is a sparse file, return the total number of valid bytes.
184 // Otherwise, return the current file size.
185 int64_t TotalBytesReceived() const;
114 186
115 net::NetLogWithSource net_log_; 187 net::NetLogWithSource net_log_;
116 188
117 // The base file instance. 189 // The base file instance.
118 BaseFile file_; 190 BaseFile file_;
119 191
120 // DownloadSaveInfo provided during construction. Since the DownloadFileImpl 192 // DownloadSaveInfo provided during construction. Since the DownloadFileImpl
121 // can be created on any thread, this holds the save_info_ until it can be 193 // can be created on any thread, this holds the save_info_ until it can be
122 // used to initialize file_ on the FILE thread. 194 // used to initialize file_ on the FILE thread.
123 std::unique_ptr<DownloadSaveInfo> save_info_; 195 std::unique_ptr<DownloadSaveInfo> save_info_;
124 196
125 // The default directory for creating the download file. 197 // The default directory for creating the download file.
126 base::FilePath default_download_directory_; 198 base::FilePath default_download_directory_;
127 199
128 // The stream through which data comes. 200 // Map of the offset and the source stream that represents the slice
129 // TODO(rdsmith): Move this into BaseFile; requires using the same 201 // starting from offset.
130 // stream semantics in SavePackage. Alternatively, replace SaveFile 202 // Must be created on the same thread that constructs the DownloadFile.
131 // with DownloadFile and get rid of BaseFile. 203 // Should not add or remove elements after creation.
132 std::unique_ptr<ByteStreamReader> stream_reader_; 204 // Any byte stream should have a SourceStream before added to the download
205 // file.
206 // The disk IO is completed when all source streams are finished.
207 SourceStreams source_streams_;
133 208
134 // Used to trigger progress updates. 209 // Used to trigger progress updates.
135 std::unique_ptr<base::RepeatingTimer> update_timer_; 210 std::unique_ptr<base::RepeatingTimer> update_timer_;
136 211
212 // Set to true when multiple byte streams write to the same file.
213 // The file may contain null bytes(holes) in between of valid data slices.
214 // TODO(xingliu): Pass a slice info vector to determine if the file is sparse.
215 bool is_sparse_file_;
216
137 // Statistics 217 // Statistics
138 size_t bytes_seen_; 218 size_t bytes_seen_;
139 base::TimeDelta disk_writes_time_; 219 base::TimeDelta disk_writes_time_;
140 base::TimeTicks download_start_; 220 base::TimeTicks download_start_;
141 RateEstimator rate_estimator_; 221 RateEstimator rate_estimator_;
142 222
143 base::WeakPtr<DownloadDestinationObserver> observer_; 223 base::WeakPtr<DownloadDestinationObserver> observer_;
144
145 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; 224 base::WeakPtrFactory<DownloadFileImpl> weak_factory_;
146 225
147 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); 226 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl);
148 }; 227 };
149 228
150 } // namespace content 229 } // namespace content
151 230
152 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ 231 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_
OLDNEW
« no previous file with comments | « content/browser/download/download_file_factory.cc ('k') | content/browser/download/download_file_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698