Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_BYTE_STREAM_H_ | 5 #ifndef CONTENT_BROWSER_BYTE_STREAM_H_ |
| 6 #define CONTENT_BROWSER_BYTE_STREAM_H_ | 6 #define CONTENT_BROWSER_BYTE_STREAM_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "content/public/browser/download_interrupt_reasons.h" | 15 #include "content/common/content_export.h" |
| 16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 class SequencedTaskRunner; | 19 class SequencedTaskRunner; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 | 23 |
| 24 // A byte stream is a pipe to transfer bytes between a source and a | 24 // A byte stream is a pipe to transfer bytes between a source and a |
| 25 // sink, which may be on different threads. It is intended to be the | 25 // sink, which may be on different threads. It is intended to be the |
| 26 // only connection between source and sink; they need have no | 26 // only connection between source and sink; they need have no |
| 27 // direct awareness of each other aside from the byte stream. The source and | 27 // direct awareness of each other aside from the byte stream. The source and |
| 28 // the sink have different interfaces to a byte stream, |ByteStreamWriter| | 28 // the sink have different interfaces to a byte stream, |ByteStreamWriter| |
| 29 // and |ByteStreamReader|. A pair of connected interfaces is generated by | 29 // and |ByteStreamReader|. A pair of connected interfaces is generated by |
| 30 // calling |CreateByteStream|. | 30 // calling |CreateByteStream|. |
| 31 // | 31 // |
| 32 // The source adds bytes to the bytestream via |ByteStreamWriter::Write| | 32 // The source adds bytes to the bytestream via |ByteStreamWriter::Write| |
| 33 // and the sink retrieves bytes already written via |ByteStreamReader::Read|. | 33 // and the sink retrieves bytes already written via |ByteStreamReader::Read|. |
| 34 // | 34 // |
| 35 // When the source has no more data to add, it will call | 35 // When the source has no more data to add, it will call |
| 36 // |ByteStreamWriter::Close| to indicate that. Errors at the source | 36 // |ByteStreamWriter::Close| to indicate that. Operation status at the source |
| 37 // are indicated to the sink via a non-DOWNLOAD_INTERRUPT_REASON_NONE code. | 37 // are indicated to the sink via the GetStatus() method. |
|
Randy Smith (Not in Mondays)
2013/07/31 22:34:42
nit: I think I'd like to put in a bit more detail,
tyoshino (SeeGerritForStatus)
2013/08/01 03:46:41
Thanks, done.
| |
| 38 // | 38 // |
| 39 // Normally the source is not managed after the relationship is setup; | 39 // Normally the source is not managed after the relationship is setup; |
| 40 // it is expected to provide data and then close itself. If an error | 40 // it is expected to provide data and then close itself. If an error |
| 41 // occurs on the sink, it is not signalled to the source via this | 41 // occurs on the sink, it is not signalled to the source via this |
| 42 // mechanism; instead, the source will write data until it exausts the | 42 // mechanism; instead, the source will write data until it exausts the |
| 43 // available space. If the source needs to be aware of errors occuring | 43 // available space. If the source needs to be aware of errors occuring |
| 44 // on the sink, this must be signalled in some other fashion (usually | 44 // on the sink, this must be signalled in some other fashion (usually |
| 45 // through whatever controller setup the relationship). | 45 // through whatever controller setup the relationship). |
| 46 // | 46 // |
| 47 // Callback lifetime management: No lifetime management is done in this | 47 // Callback lifetime management: No lifetime management is done in this |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 // void ReceivingClass::DataAvailable() { | 106 // void ReceivingClass::DataAvailable() { |
| 107 // scoped_refptr<net::IOBuffer> data; | 107 // scoped_refptr<net::IOBuffer> data; |
| 108 // size_t length = 0; | 108 // size_t length = 0; |
| 109 // | 109 // |
| 110 // while (ByteStreamReader::STREAM_HAS_DATA == | 110 // while (ByteStreamReader::STREAM_HAS_DATA == |
| 111 // (state = reader->Read(&data, &length))) { | 111 // (state = reader->Read(&data, &length))) { |
| 112 // // Process |data|. | 112 // // Process |data|. |
| 113 // } | 113 // } |
| 114 // | 114 // |
| 115 // if (ByteStreamReader::STREAM_COMPLETE == state) { | 115 // if (ByteStreamReader::STREAM_COMPLETE == state) { |
| 116 // DownloadInterruptReason status = reader->GetStatus(); | 116 // int status = reader->GetStatus(); |
| 117 // // Process error or successful completion in |status|. | 117 // // Process error or successful completion in |status|. |
| 118 // } | 118 // } |
| 119 // | 119 // |
| 120 // // if |state| is STREAM_EMPTY, we're done for now; we'll be called | 120 // // if |state| is STREAM_EMPTY, we're done for now; we'll be called |
| 121 // // again when there's more data. | 121 // // again when there's more data. |
| 122 // } | 122 // } |
| 123 class CONTENT_EXPORT ByteStreamWriter { | 123 class CONTENT_EXPORT ByteStreamWriter { |
| 124 public: | 124 public: |
| 125 // Inverse of the fraction of the stream buffer that must be full before | 125 // Inverse of the fraction of the stream buffer that must be full before |
| 126 // a notification is sent to paired Reader that there's more data. | 126 // a notification is sent to paired Reader that there's more data. |
| 127 static const int kFractionBufferBeforeSending; | 127 static const int kFractionBufferBeforeSending; |
| 128 | 128 |
| 129 virtual ~ByteStreamWriter() = 0; | 129 virtual ~ByteStreamWriter() = 0; |
| 130 | 130 |
| 131 // Always adds the data passed into the ByteStream. Returns true | 131 // Always adds the data passed into the ByteStream. Returns true |
| 132 // if more data may be added without exceeding the class limit | 132 // if more data may be added without exceeding the class limit |
| 133 // on data. Takes ownership of |buffer|. | 133 // on data. Takes ownership of |buffer|. |
| 134 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, | 134 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, |
| 135 size_t byte_count) = 0; | 135 size_t byte_count) = 0; |
| 136 | 136 |
| 137 // Signal that all data that is going to be sent, has been sent, | 137 // Signal that all data that is going to be sent, has been sent, |
| 138 // and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| should be | 138 // and provide a status. |
| 139 // passed for successful completion. | 139 virtual void Close(int status) = 0; |
| 140 virtual void Close(DownloadInterruptReason status) = 0; | |
| 141 | 140 |
| 142 // Register a callback to be called when the stream transitions from | 141 // Register a callback to be called when the stream transitions from |
| 143 // full to having space available. The callback will always be | 142 // full to having space available. The callback will always be |
| 144 // called on the task runner associated with the ByteStreamWriter. | 143 // called on the task runner associated with the ByteStreamWriter. |
| 145 // This callback will only be called if a call to Write has previously | 144 // This callback will only be called if a call to Write has previously |
| 146 // returned false (i.e. the ByteStream has been filled). | 145 // returned false (i.e. the ByteStream has been filled). |
| 147 // Multiple calls to this function are supported, though note that it | 146 // Multiple calls to this function are supported, though note that it |
| 148 // is the callers responsibility to handle races with space becoming | 147 // is the callers responsibility to handle races with space becoming |
| 149 // available (i.e. in the case of that race either of the before | 148 // available (i.e. in the case of that race either of the before |
| 150 // or after callbacks may be called). | 149 // or after callbacks may be called). |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 165 // Returns STREAM_EMPTY if there is no data on the ByteStream and | 164 // Returns STREAM_EMPTY if there is no data on the ByteStream and |
| 166 // Close() has not been called, and STREAM_COMPLETE if there | 165 // Close() has not been called, and STREAM_COMPLETE if there |
| 167 // is no data on the ByteStream and Close() has been called. | 166 // is no data on the ByteStream and Close() has been called. |
| 168 // If there is data on the ByteStream, returns STREAM_HAS_DATA | 167 // If there is data on the ByteStream, returns STREAM_HAS_DATA |
| 169 // and fills in |*data| with a pointer to the data, and |*length| | 168 // and fills in |*data| with a pointer to the data, and |*length| |
| 170 // with its length. | 169 // with its length. |
| 171 virtual StreamState Read(scoped_refptr<net::IOBuffer>* data, | 170 virtual StreamState Read(scoped_refptr<net::IOBuffer>* data, |
| 172 size_t* length) = 0; | 171 size_t* length) = 0; |
| 173 | 172 |
| 174 // Only valid to call if Read() has returned STREAM_COMPLETE. | 173 // Only valid to call if Read() has returned STREAM_COMPLETE. |
| 175 virtual DownloadInterruptReason GetStatus() const = 0; | 174 virtual int GetStatus() const = 0; |
| 176 | 175 |
| 177 // Register a callback to be called when data is added or the source | 176 // Register a callback to be called when data is added or the source |
| 178 // completes. The callback will be always be called on the owning | 177 // completes. The callback will be always be called on the owning |
| 179 // task runner. Multiple calls to this function are supported, | 178 // task runner. Multiple calls to this function are supported, |
| 180 // though note that it is the callers responsibility to handle races | 179 // though note that it is the callers responsibility to handle races |
| 181 // with data becoming available (i.e. in the case of that race | 180 // with data becoming available (i.e. in the case of that race |
| 182 // either of the before or after callbacks may be called). | 181 // either of the before or after callbacks may be called). |
| 183 // The callback will not be called after ByteStreamReader destruction. | 182 // The callback will not be called after ByteStreamReader destruction. |
| 184 virtual void RegisterCallback(const base::Closure& sink_callback) = 0; | 183 virtual void RegisterCallback(const base::Closure& sink_callback) = 0; |
| 185 }; | 184 }; |
| 186 | 185 |
| 187 CONTENT_EXPORT void CreateByteStream( | 186 CONTENT_EXPORT void CreateByteStream( |
| 188 scoped_refptr<base::SequencedTaskRunner> input_task_runner, | 187 scoped_refptr<base::SequencedTaskRunner> input_task_runner, |
| 189 scoped_refptr<base::SequencedTaskRunner> output_task_runner, | 188 scoped_refptr<base::SequencedTaskRunner> output_task_runner, |
| 190 size_t buffer_size, | 189 size_t buffer_size, |
| 191 scoped_ptr<ByteStreamWriter>* input, | 190 scoped_ptr<ByteStreamWriter>* input, |
| 192 scoped_ptr<ByteStreamReader>* output); | 191 scoped_ptr<ByteStreamReader>* output); |
| 193 | 192 |
| 194 } // namespace content | 193 } // namespace content |
| 195 | 194 |
| 196 #endif // CONTENT_BROWSER_BYTE_STREAM_H_ | 195 #endif // CONTENT_BROWSER_BYTE_STREAM_H_ |
| OLD | NEW |