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

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

Issue 10074001: Initial implementation of the ByteStream refactor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Checkpoint and merge to LKGR. Created 8 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/byte_stream.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 |ReleaseContents|.
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.
81 // Takes ownership of the passed buffer.
82 virtual bool AddData(scoped_refptr<net::IOBuffer> buffer, size_t byte_count);
83
84 // Signal that all data that is going to be sent, has been sent,
85 // and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| will be
86 // passed for successful completion.
87 virtual void SourceComplete(DownloadInterruptReason status);
88
89 // Probe whether the stream is full (== has more data than the configured
90 // buffer size).
91 virtual bool IsFull() const;
92
93 // Register a callback to be called on the specified TaskRunner
94 // when the stream transitions from full to having space available.
95 // This callback will only be called if a call to AddData has returned
96 // false.
97 // Multiple calls to this function are supported, but they may result
98 // in dispatched source callbacks never arriving if they race with
99 // the callback update.
100 // |empty_percentage| is an integer in the range 0-100 that specifies how
101 // much of the space in the pipe must be available before the source
102 // callback is called. If it is 0, the callback is called as soon as there
103 // is any space in the pipe; if 100, the pipe must be completely empty
104 // before it is called.
105 virtual void RegisterSourceCallback(
106 scoped_refptr<base::TaskRunner> source_task_runner,
107 ByteStreamCallback source_callback,
108 int empty_percentage);
109
110 // **** Sink interface
111
112 // Returns STREAM_EMPTY if there is no data on the ByteStream and
113 // SourceComplete() has not been called, and STREAM_COMPLETE if there
114 // is no data on the ByteStream and SourceComplete() has been called.
115 // If there is data on the ByteStream, returns STREAM_HAS_DATA
116 // and fills in |*data| with a pointer to the data, and |*length|
117 // with its length.
118 virtual StreamState GetData(scoped_refptr<net::IOBuffer>* data,
119 size_t* length);
120
121 // Only valid to call if GetData() has returned STREAM_COMPLETE.
122 virtual DownloadInterruptReason GetSourceResult() const;
123
124 // Register a callback to be called on the specified TaskRunner
125 // on any transition out of the state (no data, source not complete).
126 // I.e. the callback will be called if there is no data in the
127 // stream and either any data is added or the source completes.
128 // Multiple calls to this function are supported, but they may result
129 // in dispatched sink callbacks never arriving if they race with
130 // the callback update.
131 // |full_percentage| is an integer in the range 0-100 that specifies how
132 // much of the space in the pipe must be used before the sink
133 // callback is called. If it is 0, the callback is called as soon as there
134 // is any data in the pipe; if 100, the pipe must be completely full
135 // before it is called.
136 virtual void RegisterSinkCallback(
137 scoped_refptr<base::TaskRunner> sink_task_runner,
138 ByteStreamCallback sink_callback,
139 int full_percentage);
140
141 // **** Statistics
142 virtual size_t num_source_callbacks() const { return num_source_callbacks_; }
143 virtual size_t num_sink_callbacks() const { return num_sink_callbacks_; }
144 virtual size_t bytes_read() const { return bytes_read_; }
145 virtual size_t buffers_read() const { return buffers_read_; }
146 virtual base::TimeDelta source_trigger_wait_time() const {
147 return source_trigger_wait_time_;
148 }
149 virtual base::TimeDelta sink_trigger_wait_time() const {
150 return sink_trigger_wait_time_;
151 }
152
153 protected:
154 virtual ~ByteStream();
155
156 private:
157 friend class base::RefCountedThreadSafe<ByteStream>;
158
159 typedef std::deque<std::pair<scoped_refptr<net::IOBuffer>,size_t> >
160 ContentVector;
161
162 void RunSinkCallback(scoped_refptr<base::TaskRunner> target_runner);
163 void RunSourceCallback(scoped_refptr<base::TaskRunner> target_runner);
164
165 // Must be acquired for all accesses other than construction/destruction.
166 mutable base::Lock lock_;
167
168 // Size at which we start pushing back.
169 size_t buffer_size_;
170
171 // Current data. Note that ByteSTream owns and is responsible for
172 // deletion of all memory pointed to by this member. We do not use
173 // scoped_array<> because that has no copy constructor and STL
174 // containers require copy constructors.
175 ContentVector contents_;
176
177 // Total number of bytes held. Redundant with (and must be kept in
178 // sync with) the sum of the individual ContentVector elements.
179 size_t data_size_;
180
181 // Completion status
182 bool is_complete_;
183 DownloadInterruptReason source_status_;
184
185 // Source callback; to be called on transition from
186 // a state in which |data_size_ >= buffer_size_| to a state in which
187 // |data_size_ < buffer_size_|.
188 scoped_refptr<base::TaskRunner> source_task_runner_;
189 ByteStreamCallback source_callback_;
190 int empty_percentage_;
191
192 // Sink callback; to be called on transition from empty to
193 // non-empty data_.
194 scoped_refptr<base::TaskRunner> sink_task_runner_;
195 ByteStreamCallback sink_callback_;
196 int full_percentage_;
197
198 // Stats
199 size_t num_source_callbacks_;
200 size_t num_sink_callbacks_;
201 size_t bytes_read_;
202 size_t buffers_read_;
203 base::Time last_non_empty_time_;
204 base::Time last_non_full_time_;
205 base::TimeDelta sink_trigger_wait_time_;
206 base::TimeDelta source_trigger_wait_time_;
207
208 DISALLOW_COPY_AND_ASSIGN(ByteStream);
209 };
210
211 } // namespace content
212
213 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_
OLDNEW
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/byte_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698