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 #include "content/browser/download/byte_stream.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/sequenced_task_runner.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 typedef std::deque<std::pair<scoped_refptr<net::IOBuffer>,size_t> > |
| 16 ContentVector; |
| 17 |
| 18 class ByteStreamOutputImpl; |
| 19 |
| 20 // A poor man's weak pointer; a RefCountedThreadSafe boolean that can be |
| 21 // cleared in an object destructor and accessed to check for object |
| 22 // existence. We can't use weak pointers because they're tightly tied to |
| 23 // threads rather than task runners. |
| 24 struct LifetimeFlag : public base::RefCountedThreadSafe<LifetimeFlag> { |
| 25 public: |
| 26 LifetimeFlag() : is_alive_(true) { } |
| 27 bool is_alive_; |
| 28 protected: |
| 29 friend class base::RefCountedThreadSafe<LifetimeFlag>; |
| 30 virtual ~LifetimeFlag() { } |
| 31 }; |
| 32 |
| 33 // For both ByteStreamInputImpl and ByteStreamOutputImpl, Construction and |
| 34 // SetPeer may happen anywhere; all other operations on each class must |
| 35 // happen in the context of their SequencedTaskRunner. |
| 36 class ByteStreamInputImpl : public content::ByteStreamInput { |
| 37 public: |
| 38 ByteStreamInputImpl(scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 39 scoped_refptr<LifetimeFlag> lifetime_flag, |
| 40 size_t buffer_size); |
| 41 virtual ~ByteStreamInputImpl(); |
| 42 |
| 43 // Must be called before any operations are performed. |
| 44 void SetPeer(ByteStreamOutputImpl* peer, |
| 45 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, |
| 46 scoped_refptr<LifetimeFlag> peer_lifetime_flag); |
| 47 |
| 48 // Overridden from ByteStreamInput. |
| 49 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, |
| 50 size_t byte_count) OVERRIDE; |
| 51 virtual void Close(content::DownloadInterruptReason status) OVERRIDE; |
| 52 virtual void RegisterCallback(base::Closure source_callback) OVERRIDE; |
| 53 |
| 54 // PostTask target from |ByteStreamOutputImpl::MaybeUpdateInput|. |
| 55 static void UpdateWindow(scoped_refptr<LifetimeFlag> lifetime_flag, |
| 56 ByteStreamInputImpl* target, |
| 57 size_t bytes_consumed); |
| 58 |
| 59 private: |
| 60 void MaybePostToPeer(); |
| 61 |
| 62 const size_t total_buffer_size_; |
| 63 |
| 64 // All data objects in this class are only valid to access on |
| 65 // this task runner except as otherwise noted. |
| 66 scoped_refptr<base::SequencedTaskRunner> my_task_runner_; |
| 67 |
| 68 // True while this object is alive. |
| 69 scoped_refptr<LifetimeFlag> my_lifetime_flag_; |
| 70 |
| 71 base::Closure space_available_callback_; |
| 72 ContentVector input_contents_; |
| 73 size_t input_contents_size_; |
| 74 |
| 75 // ** Peer information. |
| 76 |
| 77 scoped_refptr<base::SequencedTaskRunner> peer_task_runner_; |
| 78 |
| 79 // How much we've sent to the output that for flow control purposes we |
| 80 // must assume hasn't been read yet. |
| 81 size_t output_size_used_; |
| 82 |
| 83 // Only valid to access on peer_task_runner_. |
| 84 scoped_refptr<LifetimeFlag> peer_lifetime_flag_; |
| 85 |
| 86 // Only valid to access on peer_task_runner_ if |
| 87 // |*peer_lifetime_flag_ == true| |
| 88 ByteStreamOutputImpl* peer_; |
| 89 }; |
| 90 |
| 91 class ByteStreamOutputImpl : public content::ByteStreamOutput { |
| 92 public: |
| 93 ByteStreamOutputImpl(scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 94 scoped_refptr<LifetimeFlag> lifetime_flag, |
| 95 size_t buffer_size); |
| 96 virtual ~ByteStreamOutputImpl(); |
| 97 |
| 98 // Must be called before any operations are performed. |
| 99 void SetPeer(ByteStreamInputImpl* peer, |
| 100 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, |
| 101 scoped_refptr<LifetimeFlag> peer_lifetime_flag); |
| 102 |
| 103 // Overridden from ByteStreamOutput. |
| 104 virtual StreamState Read(scoped_refptr<net::IOBuffer>* data, |
| 105 size_t* length) OVERRIDE; |
| 106 virtual content::DownloadInterruptReason GetStatus() const OVERRIDE; |
| 107 virtual void RegisterCallback(base::Closure sink_callback) OVERRIDE; |
| 108 |
| 109 // PostTask target from |ByteStreamInputImpl::MaybePostToPeer| and |
| 110 // |ByteStreamInputImpl::Close|. |
| 111 // Receive data from our peer. |
| 112 // static because it may be called after the object it is targeting |
| 113 // has been destroyed. It may not access |*target| |
| 114 // if |*object_lifetime_flag| is false. |
| 115 static void TransferData( |
| 116 scoped_refptr<LifetimeFlag> object_lifetime_flag, |
| 117 ByteStreamOutputImpl* target, |
| 118 scoped_ptr<ContentVector> xfer_buffer, |
| 119 size_t xfer_buffer_bytes, |
| 120 bool source_complete, |
| 121 content::DownloadInterruptReason status); |
| 122 |
| 123 private: |
| 124 void MaybeUpdateInput(); |
| 125 |
| 126 const size_t total_buffer_size_; |
| 127 |
| 128 scoped_refptr<base::SequencedTaskRunner> my_task_runner_; |
| 129 |
| 130 // True while this object is alive. |
| 131 scoped_refptr<LifetimeFlag> my_lifetime_flag_; |
| 132 |
| 133 ContentVector available_contents_; |
| 134 size_t available_contents_size_; |
| 135 |
| 136 bool received_status_; |
| 137 content::DownloadInterruptReason status_; |
| 138 |
| 139 base::Closure data_available_callback_; |
| 140 |
| 141 // Time of last point at which data in stream transitioned from full |
| 142 // to non-full. Nulled when a callback is sent. |
| 143 base::Time last_non_full_time_; |
| 144 |
| 145 // ** Peer information |
| 146 |
| 147 scoped_refptr<base::SequencedTaskRunner> peer_task_runner_; |
| 148 |
| 149 // How much has been removed from this class that we haven't told |
| 150 // the input about yet. |
| 151 size_t unreported_consumed_bytes_; |
| 152 |
| 153 // Only valid to access on peer_task_runner_. |
| 154 scoped_refptr<LifetimeFlag> peer_lifetime_flag_; |
| 155 |
| 156 // Only valid to access on peer_task_runner_ if |
| 157 // |*peer_lifetime_flag_ == true| |
| 158 ByteStreamInputImpl* peer_; |
| 159 }; |
| 160 |
| 161 ByteStreamInputImpl::ByteStreamInputImpl( |
| 162 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 163 scoped_refptr<LifetimeFlag> lifetime_flag, |
| 164 size_t buffer_size) |
| 165 : total_buffer_size_(buffer_size), |
| 166 my_task_runner_(task_runner), |
| 167 my_lifetime_flag_(lifetime_flag), |
| 168 input_contents_size_(0), |
| 169 output_size_used_(0), |
| 170 peer_(NULL) { |
| 171 DCHECK(my_lifetime_flag_.get()); |
| 172 my_lifetime_flag_->is_alive_ = true; |
| 173 } |
| 174 |
| 175 ByteStreamInputImpl::~ByteStreamInputImpl() { |
| 176 my_lifetime_flag_->is_alive_ = false; |
| 177 } |
| 178 |
| 179 void ByteStreamInputImpl::SetPeer( |
| 180 ByteStreamOutputImpl* peer, |
| 181 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, |
| 182 scoped_refptr<LifetimeFlag> peer_lifetime_flag) { |
| 183 peer_ = peer; |
| 184 peer_task_runner_ = peer_task_runner; |
| 185 peer_lifetime_flag_ = peer_lifetime_flag; |
| 186 } |
| 187 |
| 188 bool ByteStreamInputImpl::Write( |
| 189 scoped_refptr<net::IOBuffer> buffer, size_t byte_count) { |
| 190 input_contents_.push_back(std::make_pair(buffer, byte_count)); |
| 191 input_contents_size_ += byte_count; |
| 192 |
| 193 MaybePostToPeer(); |
| 194 |
| 195 return (input_contents_size_ + output_size_used_ <= total_buffer_size_); |
| 196 } |
| 197 |
| 198 void ByteStreamInputImpl::Close( |
| 199 content::DownloadInterruptReason status) { |
| 200 // Make sure to flush everything we have with the source notification. |
| 201 scoped_ptr<ContentVector> xfer_buffer; |
| 202 size_t buffer_size = 0; |
| 203 if (0 != input_contents_size_) { |
| 204 xfer_buffer.reset(new ContentVector); |
| 205 xfer_buffer->swap(input_contents_); |
| 206 buffer_size = input_contents_size_; |
| 207 output_size_used_ += input_contents_size_; |
| 208 input_contents_size_ = 0; |
| 209 } |
| 210 peer_task_runner_->PostTask( |
| 211 FROM_HERE, base::Bind( |
| 212 &ByteStreamOutputImpl::TransferData, |
| 213 peer_lifetime_flag_, |
| 214 peer_, |
| 215 base::Passed(xfer_buffer.Pass()), |
| 216 buffer_size, |
| 217 true /* Source complete. */, |
| 218 status)); |
| 219 } |
| 220 |
| 221 void ByteStreamInputImpl::RegisterCallback( |
| 222 base::Closure source_callback) { |
| 223 space_available_callback_ = source_callback; |
| 224 } |
| 225 |
| 226 // static |
| 227 void ByteStreamInputImpl::UpdateWindow( |
| 228 scoped_refptr<LifetimeFlag> lifetime_flag, ByteStreamInputImpl* target, |
| 229 size_t bytes_consumed) { |
| 230 |
| 231 // If the target object isn't alive anymore, we do nothing. |
| 232 if (!lifetime_flag->is_alive_) return; |
| 233 |
| 234 DCHECK_GE(target->output_size_used_, bytes_consumed); |
| 235 target->output_size_used_ -= bytes_consumed; |
| 236 |
| 237 if (!target->space_available_callback_.is_null()) |
| 238 target->space_available_callback_.Run(); |
| 239 } |
| 240 |
| 241 // Decide whether or not we've bufferred enough for a transfer. |
| 242 // For right now "enough" will be "anything". |
| 243 void ByteStreamInputImpl::MaybePostToPeer() { |
| 244 // Arbitrarily, we buffer to a third of the total size before sending. |
| 245 if (input_contents_size_ > total_buffer_size_ / 3) { |
| 246 scoped_ptr<ContentVector> xfer_buffer(new ContentVector); |
| 247 xfer_buffer->swap(input_contents_); |
| 248 size_t buffer_size = input_contents_size_; |
| 249 output_size_used_ += input_contents_size_; |
| 250 input_contents_size_ = 0; |
| 251 |
| 252 peer_task_runner_->PostTask( |
| 253 FROM_HERE, base::Bind( |
| 254 &ByteStreamOutputImpl::TransferData, |
| 255 peer_lifetime_flag_, |
| 256 peer_, |
| 257 base::Passed(xfer_buffer.Pass()), |
| 258 buffer_size, |
| 259 false /* Source not complete. */, |
| 260 content::DOWNLOAD_INTERRUPT_REASON_NONE)); |
| 261 } |
| 262 } |
| 263 |
| 264 ByteStreamOutputImpl::ByteStreamOutputImpl( |
| 265 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 266 scoped_refptr<LifetimeFlag> lifetime_flag, |
| 267 size_t buffer_size) |
| 268 : total_buffer_size_(buffer_size), |
| 269 my_task_runner_(task_runner), |
| 270 my_lifetime_flag_(lifetime_flag), |
| 271 available_contents_size_(0), |
| 272 received_status_(false), |
| 273 status_(content::DOWNLOAD_INTERRUPT_REASON_NONE), |
| 274 unreported_consumed_bytes_(0), |
| 275 peer_(NULL) { |
| 276 DCHECK(my_lifetime_flag_.get()); |
| 277 my_lifetime_flag_->is_alive_ = true; |
| 278 } |
| 279 |
| 280 ByteStreamOutputImpl::~ByteStreamOutputImpl() { |
| 281 my_lifetime_flag_->is_alive_ = false; |
| 282 } |
| 283 |
| 284 void ByteStreamOutputImpl::SetPeer( |
| 285 ByteStreamInputImpl* peer, |
| 286 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, |
| 287 scoped_refptr<LifetimeFlag> peer_lifetime_flag) { |
| 288 peer_ = peer; |
| 289 peer_task_runner_ = peer_task_runner; |
| 290 peer_lifetime_flag_ = peer_lifetime_flag; |
| 291 } |
| 292 |
| 293 ByteStreamOutputImpl::StreamState |
| 294 ByteStreamOutputImpl::Read(scoped_refptr<net::IOBuffer>* data, |
| 295 size_t* length) { |
| 296 if (available_contents_.size()) { |
| 297 *data = available_contents_.front().first; |
| 298 *length = available_contents_.front().second; |
| 299 available_contents_.pop_front(); |
| 300 DCHECK_GE(available_contents_size_, *length); |
| 301 available_contents_size_ -= *length; |
| 302 unreported_consumed_bytes_ += *length; |
| 303 |
| 304 MaybeUpdateInput(); |
| 305 return STREAM_HAS_DATA; |
| 306 } |
| 307 if (received_status_) { |
| 308 return STREAM_COMPLETE; |
| 309 } |
| 310 return STREAM_EMPTY; |
| 311 } |
| 312 |
| 313 content::DownloadInterruptReason |
| 314 ByteStreamOutputImpl::GetStatus() const { |
| 315 DCHECK(received_status_); |
| 316 return status_; |
| 317 } |
| 318 |
| 319 void ByteStreamOutputImpl::RegisterCallback(base::Closure sink_callback) { |
| 320 data_available_callback_ = sink_callback; |
| 321 } |
| 322 |
| 323 // static |
| 324 void ByteStreamOutputImpl::TransferData( |
| 325 scoped_refptr<LifetimeFlag> object_lifetime_flag, |
| 326 ByteStreamOutputImpl* target, |
| 327 scoped_ptr<ContentVector> xfer_buffer, |
| 328 size_t buffer_size, |
| 329 bool source_complete, |
| 330 content::DownloadInterruptReason status) { |
| 331 // If our target is no longer alive, do nothing. |
| 332 if (!object_lifetime_flag->is_alive_) return; |
| 333 |
| 334 if (xfer_buffer.get()) { |
| 335 target->available_contents_.insert(target->available_contents_.end(), |
| 336 xfer_buffer->begin(), |
| 337 xfer_buffer->end()); |
| 338 target->available_contents_size_ += buffer_size; |
| 339 } |
| 340 |
| 341 if (source_complete) { |
| 342 target->received_status_ = true; |
| 343 target->status_ = status; |
| 344 } |
| 345 |
| 346 if (!target->data_available_callback_.is_null()) |
| 347 target->data_available_callback_.Run(); |
| 348 } |
| 349 |
| 350 // Decide whether or not to send the input a window update. |
| 351 // Currently we do that whenever we've got unreported consumption |
| 352 // greater than 1/3 of total size. |
| 353 void ByteStreamOutputImpl::MaybeUpdateInput() { |
| 354 if (unreported_consumed_bytes_ > total_buffer_size_ / 3) { |
| 355 peer_task_runner_->PostTask( |
| 356 FROM_HERE, base::Bind( |
| 357 &ByteStreamInputImpl::UpdateWindow, |
| 358 peer_lifetime_flag_, |
| 359 peer_, |
| 360 unreported_consumed_bytes_)); |
| 361 unreported_consumed_bytes_ = 0; |
| 362 } |
| 363 } |
| 364 |
| 365 } // namespace |
| 366 |
| 367 namespace content { |
| 368 |
| 369 ByteStreamOutput::~ByteStreamOutput() { } |
| 370 |
| 371 ByteStreamInput::~ByteStreamInput() { } |
| 372 |
| 373 void CreateByteStream( |
| 374 scoped_ptr<ByteStreamInput>* input, |
| 375 scoped_ptr<ByteStreamOutput>* output, |
| 376 scoped_refptr<base::SequencedTaskRunner> input_task_runner, |
| 377 scoped_refptr<base::SequencedTaskRunner> output_task_runner, |
| 378 size_t buffer_size) { |
| 379 scoped_refptr<LifetimeFlag> input_flag(new LifetimeFlag()); |
| 380 scoped_refptr<LifetimeFlag> output_flag(new LifetimeFlag()); |
| 381 |
| 382 ByteStreamInputImpl* in = new ByteStreamInputImpl( |
| 383 input_task_runner, input_flag, buffer_size); |
| 384 ByteStreamOutputImpl* out = new ByteStreamOutputImpl( |
| 385 output_task_runner, output_flag, buffer_size); |
| 386 |
| 387 in->SetPeer(out, output_task_runner, output_flag); |
| 388 out->SetPeer(in, input_task_runner, input_flag); |
| 389 input->reset(in); |
| 390 output->reset(out); |
| 391 return; |
| 392 } |
| 393 |
| 394 } // namespace content |
OLD | NEW |