Chromium Code Reviews| 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 "base/task_runner.h" | |
| 17 #include "content/public/browser/download_interrupt_reasons.h" | |
| 18 #include "net/base/io_buffer.h" | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 // ByteStream is a class which implements the concept of a pipe between | |
| 23 // a source and a sink, which may be on different threads. It is intended | |
| 24 // to be the only connection between source and sink; they need have | |
| 25 // no awareness of each other aside from the ByteStream. Each of | |
| 26 // source and sink will maintain a reference on the ByteStream while | |
| 27 // they are in existence. | |
| 28 // | |
| 29 // The source will add bytes to the bytestream via |AddData| and the | |
| 30 // sync will retrieve the data already written via |ReleaseContents|. | |
| 31 // | |
| 32 // When the source has no more data to add, it will call | |
| 33 // SourceComplete to indicate that and release its reference. Errors | |
| 34 // at the source are indicated to the sync via a | |
| 35 // non-DOWNLOAD_INTERRUPT_REASON_NONE code. | |
| 36 // | |
| 37 // Normally the source is not managed after the relationship is setup; | |
| 38 // it is expected to provide data and then close itself. If an error | |
| 39 // occurs on the sink, it is not signalled to the source via this | |
| 40 // mechanism; instead, the source will write data until it exausts the | |
| 41 // available space. Instead, it is the responsibility of the sink, | |
| 42 // usually through notifying whatever controller setup the | |
| 43 // relationship, to signal the source in some other fashion. | |
| 44 // | |
| 45 // Callback lifetime management: No lifetime management is done in this | |
| 46 // class to prevent registered callbacks from being called after any | |
| 47 // objects to which they may refer have been destroyed. It is the | |
| 48 // responsibility of the callers to avoid use-after-free references. | |
| 49 // This may be done by any of several mechanisms, including weak | |
| 50 // pointers, scoped_refptr references, or calling the registration | |
| 51 // function with a null callback from a destructor. To avoid PostTask | |
| 52 // races, the callback will always be evaluated on the thread on which | |
| 53 // it is going to be executed immediately before execution. | |
| 54 | |
| 55 class CONTENT_EXPORT ByteStream | |
| 56 : public base::RefCountedThreadSafe<ByteStream> { | |
| 57 public: | |
| 58 typedef enum { STREAM_EMPTY, STREAM_NON_EMPTY, STREAM_COMPLETE } StreamState; | |
|
ahendrickson
2012/04/16 15:14:27
STREAM_NON_EMPTY -> STREAM_HAS_DATA ?
Randy Smith (Not in Mondays)
2012/04/18 19:10:38
Done.
| |
| 59 typedef base::Closure ByteStreamCallback; | |
| 60 | |
| 61 static const size_t kDefaultBufferSize = 100*1024; | |
| 62 | |
| 63 ByteStream(); | |
| 64 | |
| 65 // |buffer_size| specifies the advisory limit on the amount of data | |
| 66 // the ByteStream can hold. If, at the end of an AddData() call, the | |
| 67 // total memory held by a ByteStream is greater than |buffer_size|, | |
| 68 // AddData() will return false. This has no other effect. | |
| 69 // The default buffer size is kDefaultBufferSize. | |
| 70 void SetBufferSize(size_t buffer_size); | |
| 71 | |
| 72 // **** Source interface | |
| 73 | |
| 74 // Always adds the data passed into the ByteStream. Returns true | |
| 75 // if more data may be added without exceeding the class limit | |
| 76 // on data. | |
| 77 // Takes ownership of the passed buffer. | |
| 78 bool AddData(scoped_refptr<net::IOBuffer> buffer, size_t byte_count); | |
| 79 | |
| 80 // Signal that all data that is going to be sent, has been sent, | |
| 81 // and provide a status. | |
|
ahendrickson
2012/04/16 15:14:27
Indicate what status should be provided in the nor
Randy Smith (Not in Mondays)
2012/04/18 19:10:38
Done.
| |
| 82 void SourceComplete(DownloadInterruptReason status); | |
| 83 | |
| 84 // Probe whether the stream is full (== has more data than the configured | |
| 85 // buffer size). | |
| 86 bool IsFull(); | |
| 87 | |
| 88 // Register a callback to be called on the specified TaskRunner | |
| 89 // when the stream transitions from full to having space available. | |
| 90 // This callback will only be called if a call to AddData returns | |
| 91 // false. | |
| 92 // Multiple calls to this function are supported, but they may result | |
| 93 // in dispatched source callbacks never arriving if they race with | |
| 94 // the callback update. | |
| 95 void RegisterSourceCallback( | |
|
ahendrickson
2012/04/16 15:14:27
I think this needs a better name. RegisterSourceC
Randy Smith (Not in Mondays)
2012/04/18 19:10:38
I'd prefer not, mostly because there isn't really
| |
| 96 scoped_refptr<base::TaskRunner> source_task_runner, | |
| 97 ByteStreamCallback source_callback); | |
| 98 | |
| 99 // **** Sink interface | |
| 100 | |
| 101 // Returns STREAM_EMPTY if there is no data on the ByteStream and | |
| 102 // SourceComplete() has not been called, and STREAM_COMPLETE if there | |
| 103 // is no data on the ByteStream and SourceComplete() has been called. | |
| 104 // If there is data on the ByteStream, returns STREAM_NON_EMPTY | |
| 105 // and fills in |*data| with a pointer to the data, and |*length| | |
| 106 // with its length. | |
| 107 StreamState GetData(scoped_refptr<net::IOBuffer>* data, size_t* length); | |
| 108 | |
| 109 // Only valid to call if GetData() has returned STREAM_COMPLETE. | |
|
ahendrickson
2012/04/16 15:14:27
What will this return if called too early?
Randy Smith (Not in Mondays)
2012/04/18 19:10:38
Undefined. I don't want to promise to assert, but
| |
| 110 DownloadInterruptReason GetSourceResult(); | |
| 111 | |
| 112 // Register a callback to be called on the specified TaskRunner | |
| 113 // on any transition out of the state (no data, source not complete). | |
| 114 // I.e. the callback will be called if there is no data in the | |
| 115 // stream and either any data is added or the source completes. | |
| 116 // Multiple calls to this function are supported, but they may result | |
| 117 // in dispatched sink callbacks never arriving if they race with | |
| 118 // the callback update. | |
| 119 void RegisterSinkCallback(scoped_refptr<base::TaskRunner> sink_task_runner, | |
|
ahendrickson
2012/04/16 15:14:27
RegisterDataAvailableCallback?
Randy Smith (Not in Mondays)
2012/04/18 19:10:38
But this is also called when there's just a result
| |
| 120 ByteStreamCallback sink_callback); | |
| 121 | |
| 122 private: | |
| 123 friend class base::RefCountedThreadSafe<ByteStream>; | |
| 124 | |
| 125 typedef std::deque<std::pair<scoped_refptr<net::IOBuffer>,size_t> > | |
| 126 ContentVector; | |
| 127 | |
| 128 virtual ~ByteStream(); | |
| 129 | |
| 130 void RunSinkCallback(scoped_refptr<base::TaskRunner> target_runner); | |
| 131 void RunSourceCallback(scoped_refptr<base::TaskRunner> target_runner); | |
| 132 | |
| 133 // Must be acquired for all accesses other than construction/destruction. | |
| 134 base::Lock lock_; | |
| 135 | |
| 136 // Size at which we start pushing back. | |
| 137 size_t buffer_size_; | |
| 138 | |
| 139 // Current data. Note that ByteSTream owns and is responsible for | |
| 140 // deletion of all memory pointed to by this member. We do not use | |
| 141 // scoped_array<> because that has no copy constructor and STL | |
| 142 // containers require copy constructors. | |
| 143 ContentVector contents_; | |
| 144 | |
| 145 // Total number of bytes held. Redundant with (and must be kept in | |
| 146 // sync with) the sum of the individual ContentVector elements. | |
| 147 size_t data_size_; | |
| 148 | |
| 149 // Completion status | |
| 150 bool is_complete_; | |
| 151 DownloadInterruptReason source_status_; | |
| 152 | |
| 153 // Source callback; to be called on transition from | |
| 154 // a state in which |data_size_ >= buffer_size_| to a state in which | |
| 155 // |data_size_ < buffer_size_|. | |
| 156 scoped_refptr<base::TaskRunner> source_task_runner_; | |
| 157 ByteStreamCallback source_callback_; | |
| 158 | |
| 159 // Sink callback; to be called on transition from empty to | |
| 160 // non-empty data_. | |
| 161 scoped_refptr<base::TaskRunner> sink_task_runner_; | |
| 162 ByteStreamCallback sink_callback_; | |
| 163 }; | |
| 164 | |
| 165 } // namespace content | |
| 166 | |
| 167 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ | |
| OLD | NEW |