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