| 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 #ifndef DEVICE_SERIAL_DATA_RECEIVER_H_ |
| 6 #define DEVICE_SERIAL_DATA_RECEIVER_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "device/serial/buffer.h" |
| 12 #include "device/serial/data_stream.mojom.h" |
| 13 #include "mojo/public/cpp/system/data_pipe.h" |
| 14 |
| 15 namespace device { |
| 16 |
| 17 class AsyncWaiter; |
| 18 |
| 19 // A DataReceiver receives data from a DataSource. |
| 20 class DataReceiver : public base::RefCounted<DataReceiver>, |
| 21 public serial::DataSourceClient, |
| 22 public mojo::ErrorHandler { |
| 23 public: |
| 24 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback; |
| 25 typedef base::Callback<void(int32_t error)> ReceiveErrorCallback; |
| 26 |
| 27 // Constructs a DataReceiver to receive data from |source|, using a data |
| 28 // pipe with a buffer size of |buffer_size|, with connection errors reported |
| 29 // as |connection_error_value|. |
| 30 DataReceiver(mojo::InterfacePtr<serial::DataSource> source, |
| 31 uint32_t buffer_size, |
| 32 int32_t connection_error_value); |
| 33 |
| 34 // Begins a receive operation. If this returns true, exactly one of |callback| |
| 35 // and |error_callback| will eventually be called. If there is already a |
| 36 // receive in progress, |callback| or |error_callback| is invalid or there has |
| 37 // been a connection error, this will have no effect and return false. |
| 38 bool Receive(const ReceiveDataCallback& callback, |
| 39 const ReceiveErrorCallback& error_callback); |
| 40 |
| 41 private: |
| 42 class Buffer; |
| 43 friend class base::RefCounted<DataReceiver>; |
| 44 |
| 45 enum State { |
| 46 STATE_IDLE, |
| 47 STATE_WAITING_FOR_DATA, |
| 48 STATE_WAITING_FOR_BUFFER, |
| 49 STATE_PAUSED, |
| 50 STATE_SHUT_DOWN, |
| 51 }; |
| 52 |
| 53 virtual ~DataReceiver(); |
| 54 void Done(uint32_t bytes_consumed); |
| 55 void OnDoneWaiting(MojoResult result); |
| 56 |
| 57 void ReceiveInternal(); |
| 58 void RetryReceive(); |
| 59 void DispatchData(const void* data, uint32_t num_bytes); |
| 60 void DispatchError(int32_t error); |
| 61 |
| 62 // serial::DataSourceClient override. |
| 63 virtual void OnError(uint32_t bytes_since_last_error, int32_t error) OVERRIDE; |
| 64 |
| 65 // mojo::ErrorHandler override. |
| 66 virtual void OnConnectionError() OVERRIDE; |
| 67 |
| 68 mojo::InterfacePtr<serial::DataSource> source_; |
| 69 const int32_t connection_error_value_; |
| 70 mojo::ScopedDataPipeConsumerHandle handle_; |
| 71 ReceiveDataCallback receive_callback_; |
| 72 ReceiveErrorCallback receive_error_callback_; |
| 73 uint32_t bytes_since_last_error_; |
| 74 bool pending_error_; |
| 75 uint32_t error_offset_; |
| 76 int32_t error_; |
| 77 State state_; |
| 78 scoped_ptr<AsyncWaiter> waiter_; |
| 79 base::WeakPtrFactory<DataReceiver> weak_factory_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(DataReceiver); |
| 82 }; |
| 83 |
| 84 } // namespace device |
| 85 |
| 86 #endif // DEVICE_SERIAL_DATA_RECEIVER_H_ |
| OLD | NEW |