OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "mojo/data_pipe_utils/data_pipe_drainer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 |
| 9 namespace mojo { |
| 10 namespace common { |
| 11 |
| 12 DataPipeDrainer::DataPipeDrainer(Client* client, |
| 13 mojo::ScopedDataPipeConsumerHandle source) |
| 14 : client_(client), source_(source.Pass()), weak_factory_(this) { |
| 15 DCHECK(client_); |
| 16 ReadData(); |
| 17 } |
| 18 |
| 19 DataPipeDrainer::~DataPipeDrainer() {} |
| 20 |
| 21 void DataPipeDrainer::ReadData() { |
| 22 const void* buffer = nullptr; |
| 23 uint32_t num_bytes = 0; |
| 24 MojoResult rv = BeginReadDataRaw(source_.get(), &buffer, &num_bytes, |
| 25 MOJO_READ_DATA_FLAG_NONE); |
| 26 if (rv == MOJO_RESULT_OK) { |
| 27 client_->OnDataAvailable(buffer, num_bytes); |
| 28 EndReadDataRaw(source_.get(), num_bytes); |
| 29 WaitForData(); |
| 30 } else if (rv == MOJO_RESULT_SHOULD_WAIT) { |
| 31 WaitForData(); |
| 32 } else if (rv == MOJO_RESULT_FAILED_PRECONDITION) { |
| 33 client_->OnDataComplete(); |
| 34 } else { |
| 35 DCHECK(false) << "Unhandled MojoResult: " << rv; |
| 36 } |
| 37 } |
| 38 |
| 39 void DataPipeDrainer::WaitForData() { |
| 40 handle_watcher_.Start( |
| 41 source_.get(), MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, |
| 42 base::Bind(&DataPipeDrainer::WaitComplete, weak_factory_.GetWeakPtr())); |
| 43 } |
| 44 |
| 45 void DataPipeDrainer::WaitComplete(MojoResult result) { |
| 46 ReadData(); |
| 47 } |
| 48 |
| 49 } // namespace common |
| 50 } // namespace mojo |
OLD | NEW |