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 #pragma once | |
8 | |
9 #include <set> | |
10 #include <utility> | |
11 #include <deque> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/synchronization/lock.h" | |
16 #include "base/task_runner.h" | |
17 #include "content/public/browser/download_interrupt_reasons.h" | |
18 #include "net/base/io_buffer.h" | |
19 | |
20 namespace content { | |
21 | |
22 // ByteStream is a class which implements the concept of a pipe between | |
23 // a source and a sink, which may be on different threads. It is intended | |
24 // to be the only connection between source and sink; they need have | |
25 // no awareness of each other aside from the ByteStream. Each of | |
26 // source and sink will maintain a reference on the ByteStream while | |
27 // they are in existence. | |
28 // | |
29 // The source will add bytes to the bytestream via |AddData| and the | |
30 // sync will retrieve the data already written via |GetData|. | |
31 // | |
32 // When the source has no more data to add, it will call | |
33 // SourceComplete to indicate that and release its reference. Errors | |
34 // at the source are indicated to the sync via a | |
35 // non-DOWNLOAD_INTERRUPT_REASON_NONE code. | |
36 // | |
37 // Normally the source is not managed after the relationship is setup; | |
38 // it is expected to provide data and then close itself. If an error | |
39 // occurs on the sink, it is not signalled to the source via this | |
40 // mechanism; instead, the source will write data until it exausts the | |
41 // available space. Instead, it is the responsibility of the sink, | |
42 // usually through notifying whatever controller setup the | |
43 // relationship, to signal the source in some other fashion. | |
44 // | |
45 // Callback lifetime management: No lifetime management is done in this | |
46 // class to prevent registered callbacks from being called after any | |
47 // objects to which they may refer have been destroyed. It is the | |
48 // responsibility of the callers to avoid use-after-free references. | |
49 // This may be done by any of several mechanisms, including weak | |
50 // pointers, scoped_refptr references, or calling the registration | |
51 // function with a null callback from a destructor. To avoid PostTask | |
52 // races, the callback will always be evaluated on the thread on which | |
53 // it is going to be executed immediately before execution. | |
54 // | |
55 // Class methods are virtual and destructor is protected to allow mocking | |
56 // for tests; this class isn't intended to be a base class for other classes. | |
57 class CONTENT_EXPORT ByteStream | |
58 : public base::RefCountedThreadSafe<ByteStream> { | |
59 public: | |
60 typedef enum { STREAM_EMPTY, STREAM_HAS_DATA, STREAM_COMPLETE } StreamState; | |
61 typedef base::Closure ByteStreamCallback; | |
62 | |
63 static const size_t kDefaultBufferSize = 100*1024; | |
64 | |
65 ByteStream(); | |
66 | |
67 // *** Configuration | |
68 | |
69 // |buffer_size| specifies the advisory limit on the amount of data | |
70 // the ByteStream can hold. If, at the end of an AddData() call, the | |
71 // total memory held by a ByteStream is greater than |buffer_size|, | |
72 // AddData() will return false. This has no other effect. | |
73 // The default buffer size is kDefaultBufferSize. | |
74 virtual void SetBufferSize(size_t buffer_size); | |
75 | |
76 // **** Source interface | |
77 | |
78 // Always adds the data passed into the ByteStream. Returns true | |
79 // if more data may be added without exceeding the class limit | |
80 // on data. Takes ownership of |buffer|. | |
81 virtual bool AddData(scoped_refptr<net::IOBuffer> buffer, size_t byte_count); | |
82 | |
83 // Signal that all data that is going to be sent, has been sent, | |
84 // and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| should be | |
85 // passed for successful completion. | |
86 virtual void SourceComplete(DownloadInterruptReason status); | |
87 | |
88 // Probe whether the stream is full (== has more data than the configured | |
89 // buffer size). | |
90 virtual bool IsFull() const; | |
91 | |
92 // Register a callback to be called on the specified TaskRunner | |
93 // when the stream transitions from full to having space available. | |
94 // This callback will only be called if a call to AddData has previously | |
95 // returned false (i.e. the ByteStream has been filled). | |
96 // Multiple calls to this function are supported, but they may result | |
97 // in dispatched source callbacks never arriving if they race with | |
98 // the callback update. | |
99 // |empty_percentage| is an integer in the range 0-100 that specifies how | |
100 // much of the space in the pipe must be available before the source | |
101 // callback is called. If it is 0, the callback is called as soon as there | |
102 // is any space in the pipe; if 100, the pipe must be completely empty | |
103 // before it is called. | |
104 virtual void RegisterSourceCallback( | |
105 scoped_refptr<base::TaskRunner> source_task_runner, | |
106 ByteStreamCallback source_callback, | |
107 int empty_percentage); | |
108 | |
109 // **** Sink interface | |
110 | |
111 // Returns STREAM_EMPTY if there is no data on the ByteStream and | |
112 // SourceComplete() has not been called, and STREAM_COMPLETE if there | |
113 // is no data on the ByteStream and SourceComplete() has been called. | |
114 // If there is data on the ByteStream, returns STREAM_HAS_DATA | |
115 // and fills in |*data| with a pointer to the data, and |*length| | |
116 // with its length. | |
117 virtual StreamState GetData(scoped_refptr<net::IOBuffer>* data, | |
118 size_t* length); | |
119 | |
120 // Only valid to call if GetData() has returned STREAM_COMPLETE. | |
121 virtual DownloadInterruptReason GetSourceResult() const; | |
122 | |
123 // Register a callback to be called on the specified TaskRunner | |
124 // when data is added or the source completes. | |
125 // Multiple calls to this function are supported, but they may result | |
126 // in dispatched sink callbacks never arriving if they race with | |
127 // the callback update. | |
128 // |full_percentage| is an integer in the range 0-100 that specifies how | |
129 // much of the space in the pipe must be filled before the sink | |
130 // callback is called. If it is 0, the callback is called as soon as there | |
131 // is any data in the pipe; if 100, the pipe must be completely full | |
132 // before it is called. | |
133 virtual void RegisterSinkCallback( | |
134 scoped_refptr<base::TaskRunner> sink_task_runner, | |
135 ByteStreamCallback sink_callback, | |
136 int full_percentage); | |
137 | |
138 // **** Statistics | |
139 virtual size_t num_source_callbacks() const { return num_source_callbacks_; } | |
willchan no longer on Chromium
2012/04/27 21:22:29
There's some chromium-dev thread on unix_hacker_na
| |
140 virtual size_t num_sink_callbacks() const { return num_sink_callbacks_; } | |
141 virtual size_t bytes_read() const { return bytes_read_; } | |
142 virtual size_t buffers_read() const { return buffers_read_; } | |
143 virtual base::TimeDelta source_trigger_wait_time() const { | |
144 return source_trigger_wait_time_; | |
145 } | |
146 virtual base::TimeDelta sink_trigger_wait_time() const { | |
147 return sink_trigger_wait_time_; | |
148 } | |
149 | |
150 protected: | |
151 virtual ~ByteStream(); | |
152 | |
153 private: | |
154 friend class base::RefCountedThreadSafe<ByteStream>; | |
155 | |
156 typedef std::deque<std::pair<scoped_refptr<net::IOBuffer>,size_t> > | |
157 ContentVector; | |
158 | |
159 void RunSinkCallback(scoped_refptr<base::TaskRunner> target_runner); | |
160 void RunSourceCallback(scoped_refptr<base::TaskRunner> target_runner); | |
161 | |
162 // Must be acquired for all accesses other than construction/destruction. | |
163 mutable base::Lock lock_; | |
164 | |
165 // Size at which we start pushing back. | |
166 size_t buffer_size_; | |
167 | |
168 // Current data. Note that ByteSTream owns and is responsible for | |
169 // deletion of all memory pointed to by this member. We do not use | |
170 // scoped_array<> because that has no copy constructor and STL | |
171 // containers require copy constructors. | |
172 ContentVector contents_; | |
173 | |
174 // Total number of bytes held. Redundant with (and must be kept in | |
175 // sync with) the sum of the individual ContentVector elements. | |
176 size_t data_size_; | |
177 | |
178 // Completion status | |
179 bool is_complete_; | |
180 DownloadInterruptReason source_status_; | |
181 | |
182 // Source callback; to be called on transition from | |
183 // a state in which |data_size_ >= buffer_size_| to a state in which | |
184 // |data_size_ < buffer_size_|. | |
185 scoped_refptr<base::TaskRunner> source_task_runner_; | |
186 ByteStreamCallback source_callback_; | |
187 int empty_percentage_; | |
188 | |
189 // Sink callback; to be called on transition from empty to | |
190 // non-empty data_. | |
191 scoped_refptr<base::TaskRunner> sink_task_runner_; | |
192 ByteStreamCallback sink_callback_; | |
193 int full_percentage_; | |
194 | |
195 // Stats | |
196 size_t num_source_callbacks_; | |
197 size_t num_sink_callbacks_; | |
198 size_t bytes_read_; | |
199 size_t buffers_read_; | |
200 base::Time last_non_empty_time_; | |
201 base::Time last_non_full_time_; | |
202 base::TimeDelta sink_trigger_wait_time_; | |
203 base::TimeDelta source_trigger_wait_time_; | |
204 | |
205 DISALLOW_COPY_AND_ASSIGN(ByteStream); | |
206 }; | |
207 | |
208 } // namespace content | |
209 | |
210 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ | |
OLD | NEW |