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 "content/public/browser/download_interrupt_reasons.h" | |
17 #include "net/base/io_buffer.h" | |
18 | |
19 namespace base { | |
20 class SequencedTaskRunner; | |
21 } | |
22 | |
23 namespace content { | |
24 | |
25 // A byte stream is a pipe to transfer bytes between a source and a | |
26 // sink, which may be on different threads. It is intended to be the | |
27 // only connection between source and sink; they need have no | |
28 // awareness of each other aside from the byte stream. The source and | |
29 // the sink have different interfaces to a byte stream, |ByteStreamInput| | |
30 // and |ByteStreamOutput|. A pair of connected interfaces is generated by | |
31 // calling |CreateByteStream|. | |
32 // | |
33 // The source adds bytes to the bytestream via |ByteStreamInput::AddData| | |
34 // and the sync retrieves bytes already written via |ByteStreamOutput::GetData|. | |
35 // | |
36 // When the source has no more data to add, it will call | |
37 // |ByteStreamInput::SourceComplete| to indicate that. Errors at the source | |
38 // are indicated to the sync via a non-DOWNLOAD_INTERRUPT_REASON_NONE code. | |
39 // | |
40 // Normally the source is not managed after the relationship is setup; | |
41 // it is expected to provide data and then close itself. If an error | |
42 // occurs on the sink, it is not signalled to the source via this | |
43 // mechanism; instead, the source will write data until it exausts the | |
44 // available space. Instead, it is the responsibility of the sink, | |
45 // usually through notifying whatever controller setup the | |
46 // relationship, to signal the source in some other fashion. | |
47 // | |
48 // Callback lifetime management: No lifetime management is done in this | |
49 // class to prevent registered callbacks from being called after any | |
50 // objects to which they may refer have been destroyed. It is the | |
51 // responsibility of the callers to avoid use-after-free references. | |
52 // This may be done by any of several mechanisms, including weak | |
53 // pointers, scoped_refptr references, or calling the registration | |
54 // function with a null callback from a destructor. To enable the null | |
55 // callback strategy, callbacks will always be evaluated immediately | |
56 // before execution. | |
57 // | |
58 // Class methods are virtual to allow mocking for tests; these classes | |
59 // aren't intended to be base classes for other classes. | |
60 class CONTENT_EXPORT ByteStreamInput { | |
61 public: | |
willchan no longer on Chromium
2012/05/07 23:08:51
I'm a bit surprised this interface isn't more like
Randy Smith (Not in Mondays)
2012/05/07 23:26:01
I'm happy to switch over to Write/Close; that's a
Randy Smith (Not in Mondays)
2012/05/08 05:27:04
A point in support of keeping the callback separat
willchan no longer on Chromium
2012/05/10 20:31:39
I'm surprised there's no use case. What happens wh
willchan no longer on Chromium
2012/05/10 20:31:39
We avoid this problem in net::Socket code by disal
| |
62 virtual ~ByteStreamInput() = 0; | |
63 | |
64 // Always adds the data passed into the ByteStream. Returns true | |
65 // if more data may be added without exceeding the class limit | |
66 // on data. Takes ownership of |buffer|. | |
67 virtual bool AddData(scoped_refptr<net::IOBuffer> buffer, | |
68 size_t byte_count) = 0; | |
69 | |
70 // Signal that all data that is going to be sent, has been sent, | |
71 // and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| should be | |
72 // passed for successful completion. | |
73 virtual void SourceComplete(DownloadInterruptReason status) = 0; | |
74 | |
75 // Register a callback to be called | |
76 // when the stream transitions from full to having space available. | |
77 // The callback will always be called on the task runner associated | |
78 // with the ByteStreamInput. | |
79 // This callback will only be called if a call to AddData has previously | |
80 // returned false (i.e. the ByteStream has been filled). | |
81 // Multiple calls to this function are supported, but they may result | |
82 // in dispatched source callbacks never arriving if they race with | |
83 // the callback update. | |
84 // |empty_percentage| is an integer in the range 0-100 that specifies how | |
85 // much of the space in the pipe must be available before the source | |
86 // callback is called. If it is 0, the callback is called as soon as there | |
87 // is any space in the pipe; if 100, the pipe must be completely empty | |
88 // before it is called. | |
89 virtual void RegisterCallback(base::Closure source_callback) = 0; | |
90 }; | |
91 | |
92 class CONTENT_EXPORT ByteStreamOutput { | |
93 public: | |
94 typedef enum { STREAM_EMPTY, STREAM_HAS_DATA, STREAM_COMPLETE } StreamState; | |
95 | |
96 virtual ~ByteStreamOutput() = 0; | |
97 | |
98 // Returns STREAM_EMPTY if there is no data on the ByteStream and | |
99 // SourceComplete() has not been called, and STREAM_COMPLETE if there | |
100 // is no data on the ByteStream and SourceComplete() has been called. | |
101 // If there is data on the ByteStream, returns STREAM_HAS_DATA | |
102 // and fills in |*data| with a pointer to the data, and |*length| | |
103 // with its length. | |
104 virtual StreamState GetData(scoped_refptr<net::IOBuffer>* data, | |
105 size_t* length) = 0; | |
106 | |
107 // Only valid to call if GetData() has returned STREAM_COMPLETE. | |
108 virtual DownloadInterruptReason GetSourceResult() const = 0; | |
109 | |
110 // Register a callback to be called | |
111 // when data is added or the source completes. | |
112 // The callback will be always be called on the owning task runner. | |
113 // Multiple calls to this function are supported, but they may result | |
114 // in dispatched sink callbacks never arriving if they race with | |
115 // the callback update. | |
116 virtual void RegisterCallback(base::Closure sink_callback) = 0; | |
117 | |
118 // Statistics. On the output side of the byte stream to indicate | |
willchan no longer on Chromium
2012/05/07 23:08:51
Why all these stats? Can't this be done in a compo
Randy Smith (Not in Mondays)
2012/05/07 23:26:01
Reasonable. I'll make the change. The reason for
| |
119 // that they track only completed transfers, i.e. ones that have gone | |
120 // all the way through the pipe. | |
121 virtual size_t NumSourceCallbacks() const = 0; | |
122 virtual size_t NumSinkCallbacks() const = 0; | |
123 virtual size_t BytesRead() const = 0; | |
124 virtual size_t BuffersRead() const = 0; | |
125 | |
126 // Time between any space becoming available for writing in the | |
127 // stream and triggering the source. | |
128 virtual base::TimeDelta TotalSourceTriggerWaitTime() const = 0; | |
129 | |
130 // Time between any data becoming available for reading in the | |
131 // stream and triggering the sink. | |
132 virtual base::TimeDelta TotalSinkTriggerWaitTime() const = 0; | |
133 }; | |
134 | |
135 CONTENT_EXPORT void CreateByteStream( | |
136 scoped_ptr<ByteStreamInput>* input, | |
137 scoped_ptr<ByteStreamOutput>* output, | |
138 scoped_refptr<base::SequencedTaskRunner> input_task_runner, | |
139 scoped_refptr<base::SequencedTaskRunner> output_task_runner, | |
140 size_t buffer_size); | |
141 | |
142 } // namespace content | |
143 | |
144 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ | |
OLD | NEW |