OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ | |
6 #define CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ | |
7 | |
8 #include <set> | |
9 #include <utility> | |
10 #include <deque> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/synchronization/lock.h" | |
15 #include "content/public/browser/download_interrupt_reasons.h" | |
16 #include "net/base/io_buffer.h" | |
17 | |
18 namespace base { | |
19 class SequencedTaskRunner; | |
20 } | |
21 | |
22 namespace content { | |
23 | |
24 // TODO(zork): Move this class out of content/browser/download | |
25 // crbug.com/180833 | |
26 // | |
27 // A byte stream is a pipe to transfer bytes between a source and a | |
28 // sink, which may be on different threads. It is intended to be the | |
29 // only connection between source and sink; they need have no | |
30 // direct awareness of each other aside from the byte stream. The source and | |
31 // the sink have different interfaces to a byte stream, |ByteStreamWriter| | |
32 // and |ByteStreamReader|. A pair of connected interfaces is generated by | |
33 // calling |CreateByteStream|. | |
34 // | |
35 // The source adds bytes to the bytestream via |ByteStreamWriter::Write| | |
36 // and the sink retrieves bytes already written via |ByteStreamReader::Read|. | |
37 // | |
38 // When the source has no more data to add, it will call | |
39 // |ByteStreamWriter::Close| to indicate that. Errors at the source | |
40 // are indicated to the sink via a non-DOWNLOAD_INTERRUPT_REASON_NONE code. | |
41 // | |
42 // Normally the source is not managed after the relationship is setup; | |
43 // it is expected to provide data and then close itself. If an error | |
44 // occurs on the sink, it is not signalled to the source via this | |
45 // mechanism; instead, the source will write data until it exausts the | |
46 // available space. If the source needs to be aware of errors occuring | |
47 // on the sink, this must be signalled in some other fashion (usually | |
48 // through whatever controller setup the relationship). | |
49 // | |
50 // Callback lifetime management: No lifetime management is done in this | |
51 // class to prevent registered callbacks from being called after any | |
52 // objects to which they may refer have been destroyed. It is the | |
53 // responsibility of the callers to avoid use-after-free references. | |
54 // This may be done by any of several mechanisms, including weak | |
55 // pointers, scoped_refptr references, or calling the registration | |
56 // function with a null callback from a destructor. To enable the null | |
57 // callback strategy, callbacks will not be stored between retrieval and | |
58 // evaluation, so setting a null callback will guarantee that the | |
59 // previous callback will not be executed after setting. | |
60 // | |
61 // Class methods are virtual to allow mocking for tests; these classes | |
62 // aren't intended to be base classes for other classes. | |
63 // | |
64 // Sample usage (note that this does not show callback usage): | |
65 // | |
66 // void OriginatingClass::Initialize() { | |
67 // // Create a stream for sending bytes from IO->FILE threads. | |
68 // scoped_ptr<ByteStreamWriter> writer; | |
69 // scoped_ptr<ByteStreamReader> reader; | |
70 // CreateByteStream( | |
71 // BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), | |
72 // BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | |
73 // kStreamBufferSize /* e.g. 10240. */, | |
74 // &writer, | |
75 // &reader); // Presumed passed to FILE thread for reading. | |
76 // | |
77 // // Setup callback for writing. | |
78 // writer->RegisterCallback(base::Bind(&SpaceAvailable, this)); | |
79 // | |
80 // // Do initial round of writing. | |
81 // SpaceAvailable(); | |
82 // } | |
83 // | |
84 // // May only be run on first argument task runner, in this case the IO | |
85 // // thread. | |
86 // void OriginatingClass::SpaceAvailable() { | |
87 // while (<data available>) { | |
88 // scoped_ptr<net::IOBuffer> buffer; | |
89 // size_t buffer_length; | |
90 // // Create IOBuffer, fill in with data, and set buffer_length. | |
91 // if (!writer->Write(buffer, buffer_length)) { | |
92 // // No more space; return and we'll be called again | |
93 // // when there is space. | |
94 // return; | |
95 // } | |
96 // } | |
97 // writer->Close(<operation status>); | |
98 // writer.reset(NULL); | |
99 // } | |
100 // | |
101 // // On File thread; containing class setup not shown. | |
102 // | |
103 // void ReceivingClass::Initialize() { | |
104 // // Initialization | |
105 // reader->RegisterCallback(base::Bind(&DataAvailable, obj)); | |
106 // } | |
107 // | |
108 // // Called whenever there's something to read. | |
109 // void ReceivingClass::DataAvailable() { | |
110 // scoped_refptr<net::IOBuffer> data; | |
111 // size_t length = 0; | |
112 // | |
113 // while (ByteStreamReader::STREAM_HAS_DATA == | |
114 // (state = reader->Read(&data, &length))) { | |
115 // // Process |data|. | |
116 // } | |
117 // | |
118 // if (ByteStreamReader::STREAM_COMPLETE == state) { | |
119 // DownloadInterruptReason status = reader->GetStatus(); | |
120 // // Process error or successful completion in |status|. | |
121 // } | |
122 // | |
123 // // if |state| is STREAM_EMPTY, we're done for now; we'll be called | |
124 // // again when there's more data. | |
125 // } | |
126 class CONTENT_EXPORT ByteStreamWriter { | |
127 public: | |
128 // Inverse of the fraction of the stream buffer that must be full before | |
129 // a notification is sent to paired Reader that there's more data. | |
130 static const int kFractionBufferBeforeSending; | |
131 | |
132 virtual ~ByteStreamWriter() = 0; | |
133 | |
134 // Always adds the data passed into the ByteStream. Returns true | |
135 // if more data may be added without exceeding the class limit | |
136 // on data. Takes ownership of |buffer|. | |
137 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, | |
138 size_t byte_count) = 0; | |
139 | |
140 // Signal that all data that is going to be sent, has been sent, | |
141 // and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| should be | |
142 // passed for successful completion. | |
143 virtual void Close(DownloadInterruptReason status) = 0; | |
144 | |
145 // Register a callback to be called when the stream transitions from | |
146 // full to having space available. The callback will always be | |
147 // called on the task runner associated with the ByteStreamWriter. | |
148 // This callback will only be called if a call to Write has previously | |
149 // returned false (i.e. the ByteStream has been filled). | |
150 // Multiple calls to this function are supported, though note that it | |
151 // is the callers responsibility to handle races with space becoming | |
152 // available (i.e. in the case of that race either of the before | |
153 // or after callbacks may be called). | |
154 // The callback will not be called after ByteStreamWriter destruction. | |
155 virtual void RegisterCallback(const base::Closure& source_callback) = 0; | |
156 }; | |
157 | |
158 class CONTENT_EXPORT ByteStreamReader { | |
159 public: | |
160 // Inverse of the fraction of the stream buffer that must be empty before | |
161 // a notification is send to paired Writer that there's more room. | |
162 static const int kFractionReadBeforeWindowUpdate; | |
163 | |
164 enum StreamState { STREAM_EMPTY, STREAM_HAS_DATA, STREAM_COMPLETE }; | |
165 | |
166 virtual ~ByteStreamReader() = 0; | |
167 | |
168 // Returns STREAM_EMPTY if there is no data on the ByteStream and | |
169 // Close() has not been called, and STREAM_COMPLETE if there | |
170 // is no data on the ByteStream and Close() has been called. | |
171 // If there is data on the ByteStream, returns STREAM_HAS_DATA | |
172 // and fills in |*data| with a pointer to the data, and |*length| | |
173 // with its length. | |
174 virtual StreamState Read(scoped_refptr<net::IOBuffer>* data, | |
175 size_t* length) = 0; | |
176 | |
177 // Only valid to call if Read() has returned STREAM_COMPLETE. | |
178 virtual DownloadInterruptReason GetStatus() const = 0; | |
179 | |
180 // Register a callback to be called when data is added or the source | |
181 // completes. The callback will be always be called on the owning | |
182 // task runner. Multiple calls to this function are supported, | |
183 // though note that it is the callers responsibility to handle races | |
184 // with data becoming available (i.e. in the case of that race | |
185 // either of the before or after callbacks may be called). | |
186 // The callback will not be called after ByteStreamReader destruction. | |
187 virtual void RegisterCallback(const base::Closure& sink_callback) = 0; | |
188 }; | |
189 | |
190 CONTENT_EXPORT void CreateByteStream( | |
191 scoped_refptr<base::SequencedTaskRunner> input_task_runner, | |
192 scoped_refptr<base::SequencedTaskRunner> output_task_runner, | |
193 size_t buffer_size, | |
194 scoped_ptr<ByteStreamWriter>* input, | |
195 scoped_ptr<ByteStreamReader>* output); | |
196 | |
197 } // namespace content | |
198 | |
199 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ | |
OLD | NEW |