| 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 // is indicated to the sink via an int passed to the Close() method and returned |
| 38 // from the GetStatus() method. Source and sink must agree on the interpretation |
| 39 // of this int. |
| 38 // | 40 // |
| 39 // Normally the source is not managed after the relationship is setup; | 41 // 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 | 42 // 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 | 43 // 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 | 44 // mechanism; instead, the source will write data until it exausts the |
| 43 // available space. If the source needs to be aware of errors occuring | 45 // 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 | 46 // on the sink, this must be signalled in some other fashion (usually |
| 45 // through whatever controller setup the relationship). | 47 // through whatever controller setup the relationship). |
| 46 // | 48 // |
| 47 // Callback lifetime management: No lifetime management is done in this | 49 // 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() { | 108 // void ReceivingClass::DataAvailable() { |
| 107 // scoped_refptr<net::IOBuffer> data; | 109 // scoped_refptr<net::IOBuffer> data; |
| 108 // size_t length = 0; | 110 // size_t length = 0; |
| 109 // | 111 // |
| 110 // while (ByteStreamReader::STREAM_HAS_DATA == | 112 // while (ByteStreamReader::STREAM_HAS_DATA == |
| 111 // (state = reader->Read(&data, &length))) { | 113 // (state = reader->Read(&data, &length))) { |
| 112 // // Process |data|. | 114 // // Process |data|. |
| 113 // } | 115 // } |
| 114 // | 116 // |
| 115 // if (ByteStreamReader::STREAM_COMPLETE == state) { | 117 // if (ByteStreamReader::STREAM_COMPLETE == state) { |
| 116 // DownloadInterruptReason status = reader->GetStatus(); | 118 // int status = reader->GetStatus(); |
| 117 // // Process error or successful completion in |status|. | 119 // // Process error or successful completion in |status|. |
| 118 // } | 120 // } |
| 119 // | 121 // |
| 120 // // if |state| is STREAM_EMPTY, we're done for now; we'll be called | 122 // // if |state| is STREAM_EMPTY, we're done for now; we'll be called |
| 121 // // again when there's more data. | 123 // // again when there's more data. |
| 122 // } | 124 // } |
| 123 class CONTENT_EXPORT ByteStreamWriter { | 125 class CONTENT_EXPORT ByteStreamWriter { |
| 124 public: | 126 public: |
| 125 // Inverse of the fraction of the stream buffer that must be full before | 127 // 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. | 128 // a notification is sent to paired Reader that there's more data. |
| 127 static const int kFractionBufferBeforeSending; | 129 static const int kFractionBufferBeforeSending; |
| 128 | 130 |
| 129 virtual ~ByteStreamWriter() = 0; | 131 virtual ~ByteStreamWriter() = 0; |
| 130 | 132 |
| 131 // Always adds the data passed into the ByteStream. Returns true | 133 // Always adds the data passed into the ByteStream. Returns true |
| 132 // if more data may be added without exceeding the class limit | 134 // if more data may be added without exceeding the class limit |
| 133 // on data. Takes ownership of |buffer|. | 135 // on data. Takes ownership of |buffer|. |
| 134 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, | 136 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, |
| 135 size_t byte_count) = 0; | 137 size_t byte_count) = 0; |
| 136 | 138 |
| 137 // Flushes contents buffered in this writer to the corresponding reader | 139 // Flushes contents buffered in this writer to the corresponding reader |
| 138 // regardless if buffer filling rate is greater than | 140 // regardless if buffer filling rate is greater than |
| 139 // kFractionBufferBeforeSending or not. Does nothing if there's no contents | 141 // kFractionBufferBeforeSending or not. Does nothing if there's no contents |
| 140 // buffered. | 142 // buffered. |
| 141 virtual void Flush() = 0; | 143 virtual void Flush() = 0; |
| 142 | 144 |
| 143 // Signal that all data that is going to be sent, has been sent, | 145 // Signal that all data that is going to be sent, has been sent, |
| 144 // and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| should be | 146 // and provide a status. |
| 145 // passed for successful completion. | 147 virtual void Close(int status) = 0; |
| 146 virtual void Close(DownloadInterruptReason status) = 0; | |
| 147 | 148 |
| 148 // Register a callback to be called when the stream transitions from | 149 // Register a callback to be called when the stream transitions from |
| 149 // full to having space available. The callback will always be | 150 // full to having space available. The callback will always be |
| 150 // called on the task runner associated with the ByteStreamWriter. | 151 // called on the task runner associated with the ByteStreamWriter. |
| 151 // This callback will only be called if a call to Write has previously | 152 // This callback will only be called if a call to Write has previously |
| 152 // returned false (i.e. the ByteStream has been filled). | 153 // returned false (i.e. the ByteStream has been filled). |
| 153 // Multiple calls to this function are supported, though note that it | 154 // Multiple calls to this function are supported, though note that it |
| 154 // is the callers responsibility to handle races with space becoming | 155 // is the callers responsibility to handle races with space becoming |
| 155 // available (i.e. in the case of that race either of the before | 156 // available (i.e. in the case of that race either of the before |
| 156 // or after callbacks may be called). | 157 // or after callbacks may be called). |
| (...skipping 14 matching lines...) Expand all Loading... |
| 171 // Returns STREAM_EMPTY if there is no data on the ByteStream and | 172 // Returns STREAM_EMPTY if there is no data on the ByteStream and |
| 172 // Close() has not been called, and STREAM_COMPLETE if there | 173 // Close() has not been called, and STREAM_COMPLETE if there |
| 173 // is no data on the ByteStream and Close() has been called. | 174 // is no data on the ByteStream and Close() has been called. |
| 174 // If there is data on the ByteStream, returns STREAM_HAS_DATA | 175 // If there is data on the ByteStream, returns STREAM_HAS_DATA |
| 175 // and fills in |*data| with a pointer to the data, and |*length| | 176 // and fills in |*data| with a pointer to the data, and |*length| |
| 176 // with its length. | 177 // with its length. |
| 177 virtual StreamState Read(scoped_refptr<net::IOBuffer>* data, | 178 virtual StreamState Read(scoped_refptr<net::IOBuffer>* data, |
| 178 size_t* length) = 0; | 179 size_t* length) = 0; |
| 179 | 180 |
| 180 // Only valid to call if Read() has returned STREAM_COMPLETE. | 181 // Only valid to call if Read() has returned STREAM_COMPLETE. |
| 181 virtual DownloadInterruptReason GetStatus() const = 0; | 182 virtual int GetStatus() const = 0; |
| 182 | 183 |
| 183 // Register a callback to be called when data is added or the source | 184 // Register a callback to be called when data is added or the source |
| 184 // completes. The callback will be always be called on the owning | 185 // completes. The callback will be always be called on the owning |
| 185 // task runner. Multiple calls to this function are supported, | 186 // task runner. Multiple calls to this function are supported, |
| 186 // though note that it is the callers responsibility to handle races | 187 // though note that it is the callers responsibility to handle races |
| 187 // with data becoming available (i.e. in the case of that race | 188 // with data becoming available (i.e. in the case of that race |
| 188 // either of the before or after callbacks may be called). | 189 // either of the before or after callbacks may be called). |
| 189 // The callback will not be called after ByteStreamReader destruction. | 190 // The callback will not be called after ByteStreamReader destruction. |
| 190 virtual void RegisterCallback(const base::Closure& sink_callback) = 0; | 191 virtual void RegisterCallback(const base::Closure& sink_callback) = 0; |
| 191 }; | 192 }; |
| 192 | 193 |
| 193 CONTENT_EXPORT void CreateByteStream( | 194 CONTENT_EXPORT void CreateByteStream( |
| 194 scoped_refptr<base::SequencedTaskRunner> input_task_runner, | 195 scoped_refptr<base::SequencedTaskRunner> input_task_runner, |
| 195 scoped_refptr<base::SequencedTaskRunner> output_task_runner, | 196 scoped_refptr<base::SequencedTaskRunner> output_task_runner, |
| 196 size_t buffer_size, | 197 size_t buffer_size, |
| 197 scoped_ptr<ByteStreamWriter>* input, | 198 scoped_ptr<ByteStreamWriter>* input, |
| 198 scoped_ptr<ByteStreamReader>* output); | 199 scoped_ptr<ByteStreamReader>* output); |
| 199 | 200 |
| 200 } // namespace content | 201 } // namespace content |
| 201 | 202 |
| 202 #endif // CONTENT_BROWSER_BYTE_STREAM_H_ | 203 #endif // CONTENT_BROWSER_BYTE_STREAM_H_ |
| OLD | NEW |