| 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/download/byte_stream.h" | 5 #include "content/browser/download/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/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 class ByteStreamReaderImpl; | 26 class ByteStreamReaderImpl; |
| 27 | 27 |
| 28 // A poor man's weak pointer; a RefCountedThreadSafe boolean that can be | 28 // A poor man's weak pointer; a RefCountedThreadSafe boolean that can be |
| 29 // cleared in an object destructor and accessed to check for object | 29 // cleared in an object destructor and accessed to check for object |
| 30 // existence. We can't use weak pointers because they're tightly tied to | 30 // existence. We can't use weak pointers because they're tightly tied to |
| 31 // threads rather than task runners. | 31 // threads rather than task runners. |
| 32 // TODO(rdsmith): A better solution would be extending weak pointers | 32 // TODO(rdsmith): A better solution would be extending weak pointers |
| 33 // to support SequencedTaskRunners. | 33 // to support SequencedTaskRunners. |
| 34 struct LifetimeFlag : public base::RefCountedThreadSafe<LifetimeFlag> { | 34 struct LifetimeFlag : public base::RefCountedThreadSafe<LifetimeFlag> { |
| 35 public: | 35 public: |
| 36 LifetimeFlag() : is_alive_(true) { } | 36 LifetimeFlag() : is_alive(true) { } |
| 37 bool is_alive_; | 37 bool is_alive; |
| 38 | 38 |
| 39 protected: | 39 protected: |
| 40 friend class base::RefCountedThreadSafe<LifetimeFlag>; | 40 friend class base::RefCountedThreadSafe<LifetimeFlag>; |
| 41 virtual ~LifetimeFlag() { } | 41 virtual ~LifetimeFlag() { } |
| 42 | 42 |
| 43 private: | 43 private: |
| 44 DISALLOW_COPY_AND_ASSIGN(LifetimeFlag); | 44 DISALLOW_COPY_AND_ASSIGN(LifetimeFlag); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 // For both ByteStreamWriterImpl and ByteStreamReaderImpl, Construction and | 47 // For both ByteStreamWriterImpl and ByteStreamReaderImpl, Construction and |
| 48 // SetPeer may happen anywhere; all other operations on each class must | 48 // SetPeer may happen anywhere; all other operations on each class must |
| 49 // happen in the context of their SequencedTaskRunner. | 49 // happen in the context of their SequencedTaskRunner. |
| 50 class ByteStreamWriterImpl : public content::ByteStreamWriter { | 50 class ByteStreamWriterImpl : public content::ByteStreamWriter { |
| 51 public: | 51 public: |
| 52 ByteStreamWriterImpl(scoped_refptr<base::SequencedTaskRunner> task_runner, | 52 ByteStreamWriterImpl(scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 53 scoped_refptr<LifetimeFlag> lifetime_flag, | 53 scoped_refptr<LifetimeFlag> lifetime_flag, |
| 54 size_t buffer_size); | 54 size_t buffer_size); |
| 55 virtual ~ByteStreamWriterImpl(); | 55 virtual ~ByteStreamWriterImpl(); |
| 56 | 56 |
| 57 // Must be called before any operations are performed. | 57 // Must be called before any operations are performed. |
| 58 void SetPeer(ByteStreamReaderImpl* peer, | 58 void SetPeer(ByteStreamReaderImpl* peer, |
| 59 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, | 59 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, |
| 60 scoped_refptr<LifetimeFlag> peer_lifetime_flag); | 60 scoped_refptr<LifetimeFlag> peer_lifetime_flag); |
| 61 | 61 |
| 62 // Overridden from ByteStreamWriter. | 62 // Overridden from ByteStreamWriter. |
| 63 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, | 63 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, |
| 64 size_t byte_count) OVERRIDE; | 64 size_t byte_count) OVERRIDE; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 scoped_refptr<base::SequencedTaskRunner> task_runner, | 185 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 186 scoped_refptr<LifetimeFlag> lifetime_flag, | 186 scoped_refptr<LifetimeFlag> lifetime_flag, |
| 187 size_t buffer_size) | 187 size_t buffer_size) |
| 188 : total_buffer_size_(buffer_size), | 188 : total_buffer_size_(buffer_size), |
| 189 my_task_runner_(task_runner), | 189 my_task_runner_(task_runner), |
| 190 my_lifetime_flag_(lifetime_flag), | 190 my_lifetime_flag_(lifetime_flag), |
| 191 input_contents_size_(0), | 191 input_contents_size_(0), |
| 192 output_size_used_(0), | 192 output_size_used_(0), |
| 193 peer_(NULL) { | 193 peer_(NULL) { |
| 194 DCHECK(my_lifetime_flag_.get()); | 194 DCHECK(my_lifetime_flag_.get()); |
| 195 my_lifetime_flag_->is_alive_ = true; | 195 my_lifetime_flag_->is_alive = true; |
| 196 } | 196 } |
| 197 | 197 |
| 198 ByteStreamWriterImpl::~ByteStreamWriterImpl() { | 198 ByteStreamWriterImpl::~ByteStreamWriterImpl() { |
| 199 my_lifetime_flag_->is_alive_ = false; | 199 my_lifetime_flag_->is_alive = false; |
| 200 } | 200 } |
| 201 | 201 |
| 202 void ByteStreamWriterImpl::SetPeer( | 202 void ByteStreamWriterImpl::SetPeer( |
| 203 ByteStreamReaderImpl* peer, | 203 ByteStreamReaderImpl* peer, |
| 204 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, | 204 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, |
| 205 scoped_refptr<LifetimeFlag> peer_lifetime_flag) { | 205 scoped_refptr<LifetimeFlag> peer_lifetime_flag) { |
| 206 peer_ = peer; | 206 peer_ = peer; |
| 207 peer_task_runner_ = peer_task_runner; | 207 peer_task_runner_ = peer_task_runner; |
| 208 peer_lifetime_flag_ = peer_lifetime_flag; | 208 peer_lifetime_flag_ = peer_lifetime_flag; |
| 209 } | 209 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 232 const base::Closure& source_callback) { | 232 const base::Closure& source_callback) { |
| 233 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); | 233 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); |
| 234 space_available_callback_ = source_callback; | 234 space_available_callback_ = source_callback; |
| 235 } | 235 } |
| 236 | 236 |
| 237 // static | 237 // static |
| 238 void ByteStreamWriterImpl::UpdateWindow( | 238 void ByteStreamWriterImpl::UpdateWindow( |
| 239 scoped_refptr<LifetimeFlag> lifetime_flag, ByteStreamWriterImpl* target, | 239 scoped_refptr<LifetimeFlag> lifetime_flag, ByteStreamWriterImpl* target, |
| 240 size_t bytes_consumed) { | 240 size_t bytes_consumed) { |
| 241 // If the target object isn't alive anymore, we do nothing. | 241 // If the target object isn't alive anymore, we do nothing. |
| 242 if (!lifetime_flag->is_alive_) return; | 242 if (!lifetime_flag->is_alive) return; |
| 243 | 243 |
| 244 target->UpdateWindowInternal(bytes_consumed); | 244 target->UpdateWindowInternal(bytes_consumed); |
| 245 } | 245 } |
| 246 | 246 |
| 247 void ByteStreamWriterImpl::UpdateWindowInternal(size_t bytes_consumed) { | 247 void ByteStreamWriterImpl::UpdateWindowInternal(size_t bytes_consumed) { |
| 248 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); | 248 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); |
| 249 DCHECK_GE(output_size_used_, bytes_consumed); | 249 DCHECK_GE(output_size_used_, bytes_consumed); |
| 250 output_size_used_ -= bytes_consumed; | 250 output_size_used_ -= bytes_consumed; |
| 251 | 251 |
| 252 // Callback if we were above the limit and we're now <= to it. | 252 // Callback if we were above the limit and we're now <= to it. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 scoped_refptr<LifetimeFlag> lifetime_flag, | 290 scoped_refptr<LifetimeFlag> lifetime_flag, |
| 291 size_t buffer_size) | 291 size_t buffer_size) |
| 292 : total_buffer_size_(buffer_size), | 292 : total_buffer_size_(buffer_size), |
| 293 my_task_runner_(task_runner), | 293 my_task_runner_(task_runner), |
| 294 my_lifetime_flag_(lifetime_flag), | 294 my_lifetime_flag_(lifetime_flag), |
| 295 received_status_(false), | 295 received_status_(false), |
| 296 status_(content::DOWNLOAD_INTERRUPT_REASON_NONE), | 296 status_(content::DOWNLOAD_INTERRUPT_REASON_NONE), |
| 297 unreported_consumed_bytes_(0), | 297 unreported_consumed_bytes_(0), |
| 298 peer_(NULL) { | 298 peer_(NULL) { |
| 299 DCHECK(my_lifetime_flag_.get()); | 299 DCHECK(my_lifetime_flag_.get()); |
| 300 my_lifetime_flag_->is_alive_ = true; | 300 my_lifetime_flag_->is_alive = true; |
| 301 } | 301 } |
| 302 | 302 |
| 303 ByteStreamReaderImpl::~ByteStreamReaderImpl() { | 303 ByteStreamReaderImpl::~ByteStreamReaderImpl() { |
| 304 my_lifetime_flag_->is_alive_ = false; | 304 my_lifetime_flag_->is_alive = false; |
| 305 } | 305 } |
| 306 | 306 |
| 307 void ByteStreamReaderImpl::SetPeer( | 307 void ByteStreamReaderImpl::SetPeer( |
| 308 ByteStreamWriterImpl* peer, | 308 ByteStreamWriterImpl* peer, |
| 309 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, | 309 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, |
| 310 scoped_refptr<LifetimeFlag> peer_lifetime_flag) { | 310 scoped_refptr<LifetimeFlag> peer_lifetime_flag) { |
| 311 peer_ = peer; | 311 peer_ = peer; |
| 312 peer_task_runner_ = peer_task_runner; | 312 peer_task_runner_ = peer_task_runner; |
| 313 peer_lifetime_flag_ = peer_lifetime_flag; | 313 peer_lifetime_flag_ = peer_lifetime_flag; |
| 314 } | 314 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 | 349 |
| 350 // static | 350 // static |
| 351 void ByteStreamReaderImpl::TransferData( | 351 void ByteStreamReaderImpl::TransferData( |
| 352 scoped_refptr<LifetimeFlag> object_lifetime_flag, | 352 scoped_refptr<LifetimeFlag> object_lifetime_flag, |
| 353 ByteStreamReaderImpl* target, | 353 ByteStreamReaderImpl* target, |
| 354 scoped_ptr<ContentVector> transfer_buffer, | 354 scoped_ptr<ContentVector> transfer_buffer, |
| 355 size_t buffer_size, | 355 size_t buffer_size, |
| 356 bool source_complete, | 356 bool source_complete, |
| 357 content::DownloadInterruptReason status) { | 357 content::DownloadInterruptReason status) { |
| 358 // If our target is no longer alive, do nothing. | 358 // If our target is no longer alive, do nothing. |
| 359 if (!object_lifetime_flag->is_alive_) return; | 359 if (!object_lifetime_flag->is_alive) return; |
| 360 | 360 |
| 361 target->TransferDataInternal( | 361 target->TransferDataInternal( |
| 362 transfer_buffer.Pass(), buffer_size, source_complete, status); | 362 transfer_buffer.Pass(), buffer_size, source_complete, status); |
| 363 } | 363 } |
| 364 | 364 |
| 365 void ByteStreamReaderImpl::TransferDataInternal( | 365 void ByteStreamReaderImpl::TransferDataInternal( |
| 366 scoped_ptr<ContentVector> transfer_buffer, | 366 scoped_ptr<ContentVector> transfer_buffer, |
| 367 size_t buffer_size, | 367 size_t buffer_size, |
| 368 bool source_complete, | 368 bool source_complete, |
| 369 content::DownloadInterruptReason status) { | 369 content::DownloadInterruptReason status) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 ByteStreamReaderImpl* out = new ByteStreamReaderImpl( | 431 ByteStreamReaderImpl* out = new ByteStreamReaderImpl( |
| 432 output_task_runner, output_flag, buffer_size); | 432 output_task_runner, output_flag, buffer_size); |
| 433 | 433 |
| 434 in->SetPeer(out, output_task_runner, output_flag); | 434 in->SetPeer(out, output_task_runner, output_flag); |
| 435 out->SetPeer(in, input_task_runner, input_flag); | 435 out->SetPeer(in, input_task_runner, input_flag); |
| 436 input->reset(in); | 436 input->reset(in); |
| 437 output->reset(out); | 437 output->reset(out); |
| 438 } | 438 } |
| 439 | 439 |
| 440 } // namespace content | 440 } // namespace content |
| OLD | NEW |