| 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 <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <queue> | |
| 12 | |
| 13 #include "base/callback.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/linked_ptr.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/weak_ptr.h" | |
| 18 #include "device/serial/buffer.h" | |
| 19 #include "device/serial/data_stream.mojom.h" | |
| 20 #include "mojo/public/cpp/bindings/binding.h" | |
| 21 #include "mojo/public/cpp/system/data_pipe.h" | |
| 22 | |
| 23 namespace device { | |
| 24 | |
| 25 // A DataReceiver receives data from a DataSource. | |
| 26 class DataReceiver : public base::RefCounted<DataReceiver>, | |
| 27 public serial::DataSourceClient { | |
| 28 public: | |
| 29 typedef base::Callback<void(std::unique_ptr<ReadOnlyBuffer>)> | |
| 30 ReceiveDataCallback; | |
| 31 typedef base::Callback<void(int32_t error)> ReceiveErrorCallback; | |
| 32 | |
| 33 // Constructs a DataReceiver to receive data from |source|, using a buffer | |
| 34 // size of |buffer_size|, with connection errors reported as | |
| 35 // |fatal_error_value|. | |
| 36 DataReceiver(mojo::InterfacePtr<serial::DataSource> source, | |
| 37 mojo::InterfaceRequest<serial::DataSourceClient> client, | |
| 38 uint32_t buffer_size, | |
| 39 int32_t fatal_error_value); | |
| 40 | |
| 41 // Begins a receive operation. If this returns true, exactly one of |callback| | |
| 42 // and |error_callback| will eventually be called. If there is already a | |
| 43 // receive in progress or there has been a connection error, this will have no | |
| 44 // effect and return false. | |
| 45 bool Receive(const ReceiveDataCallback& callback, | |
| 46 const ReceiveErrorCallback& error_callback); | |
| 47 | |
| 48 private: | |
| 49 class PendingReceive; | |
| 50 struct DataFrame; | |
| 51 friend class base::RefCounted<DataReceiver>; | |
| 52 | |
| 53 ~DataReceiver() override; | |
| 54 | |
| 55 // serial::DataSourceClient overrides. | |
| 56 // Invoked by the DataSource to report errors. | |
| 57 void OnError(int32_t error) override; | |
| 58 // Invoked by the DataSource transmit data. | |
| 59 void OnData(mojo::Array<uint8_t> data) override; | |
| 60 | |
| 61 // mojo error handler | |
| 62 void OnConnectionError(); | |
| 63 | |
| 64 // Invoked by the PendingReceive to report that the user is done with the | |
| 65 // receive buffer, having read |bytes_read| bytes from it. | |
| 66 void Done(uint32_t bytes_read); | |
| 67 | |
| 68 // The implementation of Receive(). If a |pending_error_| is ready to | |
| 69 // dispatch, it does so. Otherwise, this attempts to read from |handle_| and | |
| 70 // dispatches the contents to |pending_receive_|. If |handle_| is not ready, | |
| 71 // |waiter_| is used to wait until it is ready. If an error occurs in reading | |
| 72 // |handle_|, ShutDown() is called. | |
| 73 void ReceiveInternal(); | |
| 74 | |
| 75 // Called when we encounter a fatal error. If a receive is in progress, | |
| 76 // |fatal_error_value_| will be reported to the user. | |
| 77 void ShutDown(); | |
| 78 | |
| 79 // The control connection to the data source. | |
| 80 mojo::InterfacePtr<serial::DataSource> source_; | |
| 81 mojo::Binding<serial::DataSourceClient> client_; | |
| 82 | |
| 83 // The error value to report in the event of a fatal error. | |
| 84 const int32_t fatal_error_value_; | |
| 85 | |
| 86 // Whether we have encountered a fatal error and shut down. | |
| 87 bool shut_down_; | |
| 88 | |
| 89 // The current pending receive operation if there is one. | |
| 90 std::unique_ptr<PendingReceive> pending_receive_; | |
| 91 | |
| 92 // The queue of pending data frames (data or an error) received from the | |
| 93 // DataSource. | |
| 94 std::queue<linked_ptr<DataFrame>> pending_data_frames_; | |
| 95 | |
| 96 base::WeakPtrFactory<DataReceiver> weak_factory_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(DataReceiver); | |
| 99 }; | |
| 100 | |
| 101 } // namespace device | |
| 102 | |
| 103 #endif // DEVICE_SERIAL_DATA_RECEIVER_H_ | |
| OLD | NEW |