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 // direct 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::Write| | |
34 // and the sink retrieves bytes already written via |ByteStreamOutput::Read|. | |
35 // | |
36 // When the source has no more data to add, it will call | |
37 // |ByteStreamInput::Close| to indicate that. Errors at the source | |
38 // are indicated to the sink 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. If the source needs to be aware of errors occuring | |
45 // on the sink, this must be signalled in some other fashion (usually | |
46 // through whatever controller setup the relationship). | |
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 not be stored between retrieval and | |
56 // evaluation, so setting a null callback will guarantee that the | |
57 // previous callback will not be executed after setting. | |
58 // | |
59 // Class methods are virtual to allow mocking for tests; these classes | |
60 // aren't intended to be base classes for other classes. | |
61 class CONTENT_EXPORT ByteStreamInput { | |
62 public: | |
63 virtual ~ByteStreamInput() = 0; | |
64 | |
65 // Always adds the data passed into the ByteStream. Returns true | |
66 // if more data may be added without exceeding the class limit | |
67 // on data. Takes ownership of |buffer|. | |
68 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, | |
69 size_t byte_count) = 0; | |
70 | |
71 // Signal that all data that is going to be sent, has been sent, | |
72 // and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| should be | |
73 // passed for successful completion. | |
74 virtual void Close(DownloadInterruptReason status) = 0; | |
benjhayden
2012/05/18 14:21:06
Did you want to templatize the error type?
Randy Smith (Not in Mondays)
2012/05/18 21:55:15
I've played around a little bit and concluded that
| |
75 | |
76 // Register a callback to be called when the stream transitions from | |
77 // full to having space available. The callback will always be | |
78 // called on the task runner associated with the ByteStreamInput. | |
79 // This callback will only be called if a call to Write has previously | |
80 // returned false (i.e. the ByteStream has been filled). | |
81 // Multiple calls to this function are supported, though note that it | |
82 // is the callers responsibility to handle races with space becoming | |
83 // available (i.e. in the case of that race either of the before | |
84 // or after callbacks may be called). | |
85 virtual void RegisterCallback(const base::Closure& source_callback) = 0; | |
86 }; | |
87 | |
88 class CONTENT_EXPORT ByteStreamOutput { | |
89 public: | |
90 enum StreamState { STREAM_EMPTY, STREAM_HAS_DATA, STREAM_COMPLETE }; | |
91 | |
92 virtual ~ByteStreamOutput() = 0; | |
93 | |
94 // Returns STREAM_EMPTY if there is no data on the ByteStream and | |
95 // Close() has not been called, and STREAM_COMPLETE if there | |
96 // is no data on the ByteStream and Close() has been called. | |
97 // If there is data on the ByteStream, returns STREAM_HAS_DATA | |
98 // and fills in |*data| with a pointer to the data, and |*length| | |
99 // with its length. | |
100 virtual StreamState Read(scoped_refptr<net::IOBuffer>* data, | |
101 size_t* length) = 0; | |
102 | |
103 // Only valid to call if Read() has returned STREAM_COMPLETE. | |
104 virtual DownloadInterruptReason GetStatus() const = 0; | |
105 | |
106 // Register a callback to be called when data is added or the source | |
107 // completes. The callback will be always be called on the owning | |
108 // task runner. Multiple calls to this function are supported, | |
109 // though note that it is the callers responsibility to handle races | |
110 // with data becoming available (i.e. in the case of that race | |
111 // either of the before or after callbacks may be called). | |
112 virtual void RegisterCallback(const base::Closure& sink_callback) = 0; | |
113 }; | |
114 | |
115 CONTENT_EXPORT void CreateByteStream( | |
116 scoped_refptr<base::SequencedTaskRunner> input_task_runner, | |
117 scoped_refptr<base::SequencedTaskRunner> output_task_runner, | |
118 size_t buffer_size, | |
119 scoped_ptr<ByteStreamInput>* input, | |
120 scoped_ptr<ByteStreamOutput>* output); | |
121 | |
122 } // namespace content | |
123 | |
124 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ | |
OLD | NEW |