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 #include "content/browser/byte_stream.h" | 5 #include "content/browser/byte_stream.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 virtual ~ByteStreamWriterImpl(); | 48 virtual ~ByteStreamWriterImpl(); |
49 | 49 |
50 // Must be called before any operations are performed. | 50 // Must be called before any operations are performed. |
51 void SetPeer(ByteStreamReaderImpl* peer, | 51 void SetPeer(ByteStreamReaderImpl* peer, |
52 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, | 52 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, |
53 scoped_refptr<LifetimeFlag> peer_lifetime_flag); | 53 scoped_refptr<LifetimeFlag> peer_lifetime_flag); |
54 | 54 |
55 // Overridden from ByteStreamWriter. | 55 // Overridden from ByteStreamWriter. |
56 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, | 56 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, |
57 size_t byte_count) OVERRIDE; | 57 size_t byte_count) OVERRIDE; |
| 58 virtual void Flush() OVERRIDE; |
58 virtual void Close(DownloadInterruptReason status) OVERRIDE; | 59 virtual void Close(DownloadInterruptReason status) OVERRIDE; |
59 virtual void RegisterCallback(const base::Closure& source_callback) OVERRIDE; | 60 virtual void RegisterCallback(const base::Closure& source_callback) OVERRIDE; |
60 | 61 |
61 // PostTask target from |ByteStreamReaderImpl::MaybeUpdateInput|. | 62 // PostTask target from |ByteStreamReaderImpl::MaybeUpdateInput|. |
62 static void UpdateWindow(scoped_refptr<LifetimeFlag> lifetime_flag, | 63 static void UpdateWindow(scoped_refptr<LifetimeFlag> lifetime_flag, |
63 ByteStreamWriterImpl* target, | 64 ByteStreamWriterImpl* target, |
64 size_t bytes_consumed); | 65 size_t bytes_consumed); |
65 | 66 |
66 private: | 67 private: |
67 // Called from UpdateWindow when object existence has been validated. | 68 // Called from UpdateWindow when object existence has been validated. |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 input_contents_.push_back(std::make_pair(buffer, byte_count)); | 209 input_contents_.push_back(std::make_pair(buffer, byte_count)); |
209 input_contents_size_ += byte_count; | 210 input_contents_size_ += byte_count; |
210 | 211 |
211 // Arbitrarily, we buffer to a third of the total size before sending. | 212 // Arbitrarily, we buffer to a third of the total size before sending. |
212 if (input_contents_size_ > total_buffer_size_ / kFractionBufferBeforeSending) | 213 if (input_contents_size_ > total_buffer_size_ / kFractionBufferBeforeSending) |
213 PostToPeer(false, DOWNLOAD_INTERRUPT_REASON_NONE); | 214 PostToPeer(false, DOWNLOAD_INTERRUPT_REASON_NONE); |
214 | 215 |
215 return (input_contents_size_ + output_size_used_ <= total_buffer_size_); | 216 return (input_contents_size_ + output_size_used_ <= total_buffer_size_); |
216 } | 217 } |
217 | 218 |
| 219 void ByteStreamWriterImpl::Flush() { |
| 220 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); |
| 221 if (input_contents_size_ > 0) |
| 222 PostToPeer(false, DOWNLOAD_INTERRUPT_REASON_NONE); |
| 223 } |
| 224 |
218 void ByteStreamWriterImpl::Close( | 225 void ByteStreamWriterImpl::Close( |
219 DownloadInterruptReason status) { | 226 DownloadInterruptReason status) { |
220 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); | 227 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); |
221 PostToPeer(true, status); | 228 PostToPeer(true, status); |
222 } | 229 } |
223 | 230 |
224 void ByteStreamWriterImpl::RegisterCallback( | 231 void ByteStreamWriterImpl::RegisterCallback( |
225 const base::Closure& source_callback) { | 232 const base::Closure& source_callback) { |
226 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); | 233 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); |
227 space_available_callback_ = source_callback; | 234 space_available_callback_ = source_callback; |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 ByteStreamReaderImpl* out = new ByteStreamReaderImpl( | 432 ByteStreamReaderImpl* out = new ByteStreamReaderImpl( |
426 output_task_runner, output_flag, buffer_size); | 433 output_task_runner, output_flag, buffer_size); |
427 | 434 |
428 in->SetPeer(out, output_task_runner, output_flag); | 435 in->SetPeer(out, output_task_runner, output_flag); |
429 out->SetPeer(in, input_task_runner, input_flag); | 436 out->SetPeer(in, input_task_runner, input_flag); |
430 input->reset(in); | 437 input->reset(in); |
431 output->reset(out); | 438 output->reset(out); |
432 } | 439 } |
433 | 440 |
434 } // namespace content | 441 } // namespace content |
OLD | NEW |