| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> |
| 11 |
| 10 #include "base/callback.h" | 12 #include "base/callback.h" |
| 11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
| 14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 class SequencedTaskRunner; | 18 class SequencedTaskRunner; |
| 18 } | 19 } |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 | 22 |
| 22 // A byte stream is a pipe to transfer bytes between a source and a | 23 // A byte stream is a pipe to transfer bytes between a source and a |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 // evaluation, so setting a null callback will guarantee that the | 56 // evaluation, so setting a null callback will guarantee that the |
| 56 // previous callback will not be executed after setting. | 57 // previous callback will not be executed after setting. |
| 57 // | 58 // |
| 58 // Class methods are virtual to allow mocking for tests; these classes | 59 // Class methods are virtual to allow mocking for tests; these classes |
| 59 // aren't intended to be base classes for other classes. | 60 // aren't intended to be base classes for other classes. |
| 60 // | 61 // |
| 61 // Sample usage (note that this does not show callback usage): | 62 // Sample usage (note that this does not show callback usage): |
| 62 // | 63 // |
| 63 // void OriginatingClass::Initialize() { | 64 // void OriginatingClass::Initialize() { |
| 64 // // Create a stream for sending bytes from IO->FILE threads. | 65 // // Create a stream for sending bytes from IO->FILE threads. |
| 65 // scoped_ptr<ByteStreamWriter> writer; | 66 // std::unique_ptr<ByteStreamWriter> writer; |
| 66 // scoped_ptr<ByteStreamReader> reader; | 67 // std::unique_ptr<ByteStreamReader> reader; |
| 67 // CreateByteStream( | 68 // CreateByteStream( |
| 68 // BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), | 69 // BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), |
| 69 // BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | 70 // BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), |
| 70 // kStreamBufferSize /* e.g. 10240. */, | 71 // kStreamBufferSize /* e.g. 10240. */, |
| 71 // &writer, | 72 // &writer, |
| 72 // &reader); // Presumed passed to FILE thread for reading. | 73 // &reader); // Presumed passed to FILE thread for reading. |
| 73 // | 74 // |
| 74 // // Setup callback for writing. | 75 // // Setup callback for writing. |
| 75 // writer->RegisterCallback(base::Bind(&SpaceAvailable, this)); | 76 // writer->RegisterCallback(base::Bind(&SpaceAvailable, this)); |
| 76 // | 77 // |
| 77 // // Do initial round of writing. | 78 // // Do initial round of writing. |
| 78 // SpaceAvailable(); | 79 // SpaceAvailable(); |
| 79 // } | 80 // } |
| 80 // | 81 // |
| 81 // // May only be run on first argument task runner, in this case the IO | 82 // // May only be run on first argument task runner, in this case the IO |
| 82 // // thread. | 83 // // thread. |
| 83 // void OriginatingClass::SpaceAvailable() { | 84 // void OriginatingClass::SpaceAvailable() { |
| 84 // while (<data available>) { | 85 // while (<data available>) { |
| 85 // scoped_ptr<net::IOBuffer> buffer; | 86 // std::unique_ptr<net::IOBuffer> buffer; |
| 86 // size_t buffer_length; | 87 // size_t buffer_length; |
| 87 // // Create IOBuffer, fill in with data, and set buffer_length. | 88 // // Create IOBuffer, fill in with data, and set buffer_length. |
| 88 // if (!writer->Write(buffer, buffer_length)) { | 89 // if (!writer->Write(buffer, buffer_length)) { |
| 89 // // No more space; return and we'll be called again | 90 // // No more space; return and we'll be called again |
| 90 // // when there is space. | 91 // // when there is space. |
| 91 // return; | 92 // return; |
| 92 // } | 93 // } |
| 93 // } | 94 // } |
| 94 // writer->Close(<operation status>); | 95 // writer->Close(<operation status>); |
| 95 // writer.reset(NULL); | 96 // writer.reset(NULL); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 // with data becoming available (i.e. in the case of that race | 191 // with data becoming available (i.e. in the case of that race |
| 191 // either of the before or after callbacks may be called). | 192 // either of the before or after callbacks may be called). |
| 192 // The callback will not be called after ByteStreamReader destruction. | 193 // The callback will not be called after ByteStreamReader destruction. |
| 193 virtual void RegisterCallback(const base::Closure& sink_callback) = 0; | 194 virtual void RegisterCallback(const base::Closure& sink_callback) = 0; |
| 194 }; | 195 }; |
| 195 | 196 |
| 196 CONTENT_EXPORT void CreateByteStream( | 197 CONTENT_EXPORT void CreateByteStream( |
| 197 scoped_refptr<base::SequencedTaskRunner> input_task_runner, | 198 scoped_refptr<base::SequencedTaskRunner> input_task_runner, |
| 198 scoped_refptr<base::SequencedTaskRunner> output_task_runner, | 199 scoped_refptr<base::SequencedTaskRunner> output_task_runner, |
| 199 size_t buffer_size, | 200 size_t buffer_size, |
| 200 scoped_ptr<ByteStreamWriter>* input, | 201 std::unique_ptr<ByteStreamWriter>* input, |
| 201 scoped_ptr<ByteStreamReader>* output); | 202 std::unique_ptr<ByteStreamReader>* output); |
| 202 | 203 |
| 203 } // namespace content | 204 } // namespace content |
| 204 | 205 |
| 205 #endif // CONTENT_BROWSER_BYTE_STREAM_H_ | 206 #endif // CONTENT_BROWSER_BYTE_STREAM_H_ |
| OLD | NEW |