| 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_DOWNLOAD_BYTE_STREAM_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ |
| 6 #define CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 // responsibility of the callers to avoid use-after-free references. | 51 // responsibility of the callers to avoid use-after-free references. |
| 52 // This may be done by any of several mechanisms, including weak | 52 // This may be done by any of several mechanisms, including weak |
| 53 // pointers, scoped_refptr references, or calling the registration | 53 // pointers, scoped_refptr references, or calling the registration |
| 54 // function with a null callback from a destructor. To enable the null | 54 // function with a null callback from a destructor. To enable the null |
| 55 // callback strategy, callbacks will not be stored between retrieval and | 55 // callback strategy, callbacks will not be stored between retrieval and |
| 56 // evaluation, so setting a null callback will guarantee that the | 56 // evaluation, so setting a null callback will guarantee that the |
| 57 // previous callback will not be executed after setting. | 57 // previous callback will not be executed after setting. |
| 58 // | 58 // |
| 59 // Class methods are virtual to allow mocking for tests; these classes | 59 // Class methods are virtual to allow mocking for tests; these classes |
| 60 // aren't intended to be base classes for other classes. | 60 // aren't intended to be base classes for other classes. |
| 61 // |
| 62 // Sample usage (note that this does not show callback usage): |
| 63 // |
| 64 // void OriginatingClass::Initialize() { |
| 65 // // Create a stream for sending bytes from IO->FILE threads. |
| 66 // scoped_ptr<ByteStreamWriter> writer; |
| 67 // scoped_ptr<ByteStreamReader> reader; |
| 68 // content::CreateByteStream( |
| 69 // BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), |
| 70 // BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), |
| 71 // kStreamBufferSize /* e.g. 10240. */, |
| 72 // &writer, |
| 73 // &reader); // Presumed passed to FILE thread for reading. |
| 74 // |
| 75 // // Setup callback for writing. |
| 76 // writer->RegisterCallback(base::Bind(&SpaceAvailable, this)); |
| 77 // |
| 78 // // Do initial round of writing. |
| 79 // SpaceAvailable(); |
| 80 // } |
| 81 // |
| 82 // // May only be run on first argument task runner, in this case the IO |
| 83 // // thread. |
| 84 // void OriginatingClass::SpaceAvailable() { |
| 85 // while (<data available>) { |
| 86 // scoped_ptr<net::IOBuffer> buffer; |
| 87 // size_t buffer_length; |
| 88 // // Create IOBuffer, fill in with data, and set buffer_length. |
| 89 // if (!writer->Write(buffer, buffer_length)) { |
| 90 // // No more space; return and we'll be called again |
| 91 // // when there is space. |
| 92 // return; |
| 93 // } |
| 94 // } |
| 95 // writer->Close(<operation status>); |
| 96 // writer.reset(NULL); |
| 97 // } |
| 98 // |
| 99 // // On File thread; containing class setup not shown. |
| 100 // |
| 101 // void ReceivingClass::Initialize() { |
| 102 // // Initialization |
| 103 // reader->RegisterCallback(base::Bind(&DataAvailable, obj)); |
| 104 // } |
| 105 // |
| 106 // // Called whenever there's something to read. |
| 107 // void ReceivingClass::DataAvailable() { |
| 108 // scoped_refptr<net::IOBuffer> data; |
| 109 // size_t length = 0; |
| 110 // |
| 111 // while (content::ByteStreamReader::STREAM_HAS_DATA == |
| 112 // (state = reader->Read(&data, &length))) { |
| 113 // // Process |data|. |
| 114 // } |
| 115 // |
| 116 // if (content::ByteStreamReader::STREAM_COMPLETE == state) { |
| 117 // content::DownloadInterruptReason status = reader->GetStatus(); |
| 118 // // Process error or successful completion in |status|. |
| 119 // } |
| 120 // |
| 121 // // if |state| is STREAM_EMPTY, we're done for now; we'll be called |
| 122 // // again when there's more data. |
| 123 // } |
| 61 class CONTENT_EXPORT ByteStreamWriter { | 124 class CONTENT_EXPORT ByteStreamWriter { |
| 62 public: | 125 public: |
| 63 virtual ~ByteStreamWriter() = 0; | 126 virtual ~ByteStreamWriter() = 0; |
| 64 | 127 |
| 65 // Always adds the data passed into the ByteStream. Returns true | 128 // Always adds the data passed into the ByteStream. Returns true |
| 66 // if more data may be added without exceeding the class limit | 129 // if more data may be added without exceeding the class limit |
| 67 // on data. Takes ownership of |buffer|. | 130 // on data. Takes ownership of |buffer|. |
| 68 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, | 131 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, |
| 69 size_t byte_count) = 0; | 132 size_t byte_count) = 0; |
| 70 | 133 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 CONTENT_EXPORT void CreateByteStream( | 180 CONTENT_EXPORT void CreateByteStream( |
| 118 scoped_refptr<base::SequencedTaskRunner> input_task_runner, | 181 scoped_refptr<base::SequencedTaskRunner> input_task_runner, |
| 119 scoped_refptr<base::SequencedTaskRunner> output_task_runner, | 182 scoped_refptr<base::SequencedTaskRunner> output_task_runner, |
| 120 size_t buffer_size, | 183 size_t buffer_size, |
| 121 scoped_ptr<ByteStreamWriter>* input, | 184 scoped_ptr<ByteStreamWriter>* input, |
| 122 scoped_ptr<ByteStreamReader>* output); | 185 scoped_ptr<ByteStreamReader>* output); |
| 123 | 186 |
| 124 } // namespace content | 187 } // namespace content |
| 125 | 188 |
| 126 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ | 189 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ |
| OLD | NEW |