| 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 #ifndef DEVICE_SERIAL_DATA_RECEIVER_H_ | 5 #ifndef DEVICE_SERIAL_DATA_RECEIVER_H_ |
| 6 #define DEVICE_SERIAL_DATA_RECEIVER_H_ | 6 #define DEVICE_SERIAL_DATA_RECEIVER_H_ |
| 7 | 7 |
| 8 #include <queue> |
| 9 |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/memory/linked_ptr.h" |
| 9 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 11 #include "device/serial/buffer.h" | 14 #include "device/serial/buffer.h" |
| 12 #include "device/serial/data_stream.mojom.h" | 15 #include "device/serial/data_stream.mojom.h" |
| 13 #include "mojo/public/cpp/system/data_pipe.h" | 16 #include "mojo/public/cpp/system/data_pipe.h" |
| 14 | 17 |
| 15 namespace device { | 18 namespace device { |
| 16 | 19 |
| 17 class AsyncWaiter; | |
| 18 | |
| 19 // A DataReceiver receives data from a DataSource. | 20 // A DataReceiver receives data from a DataSource. |
| 20 class DataReceiver : public base::RefCounted<DataReceiver>, | 21 class DataReceiver : public base::RefCounted<DataReceiver>, |
| 21 public serial::DataSourceClient, | 22 public serial::DataSourceClient, |
| 22 public mojo::ErrorHandler { | 23 public mojo::ErrorHandler { |
| 23 public: | 24 public: |
| 24 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback; | 25 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback; |
| 25 typedef base::Callback<void(int32_t error)> ReceiveErrorCallback; | 26 typedef base::Callback<void(int32_t error)> ReceiveErrorCallback; |
| 26 | 27 |
| 27 // Constructs a DataReceiver to receive data from |source|, using a data | 28 // Constructs a DataReceiver to receive data from |source|, using a buffer |
| 28 // pipe with a buffer size of |buffer_size|, with connection errors reported | 29 // size of |buffer_size|, with connection errors reported as |
| 29 // as |fatal_error_value|. | 30 // |fatal_error_value|. |
| 30 DataReceiver(mojo::InterfacePtr<serial::DataSource> source, | 31 DataReceiver(mojo::InterfacePtr<serial::DataSource> source, |
| 31 uint32_t buffer_size, | 32 uint32_t buffer_size, |
| 32 int32_t fatal_error_value); | 33 int32_t fatal_error_value); |
| 33 | 34 |
| 34 // Begins a receive operation. If this returns true, exactly one of |callback| | 35 // 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 // and |error_callback| will eventually be called. If there is already a |
| 36 // receive in progress or there has been a connection error, this will have no | 37 // receive in progress or there has been a connection error, this will have no |
| 37 // effect and return false. | 38 // effect and return false. |
| 38 bool Receive(const ReceiveDataCallback& callback, | 39 bool Receive(const ReceiveDataCallback& callback, |
| 39 const ReceiveErrorCallback& error_callback); | 40 const ReceiveErrorCallback& error_callback); |
| 40 | 41 |
| 41 private: | 42 private: |
| 42 class PendingReceive; | 43 class PendingReceive; |
| 44 struct PendingData; |
| 43 struct PendingError; | 45 struct PendingError; |
| 44 friend class base::RefCounted<DataReceiver>; | 46 friend class base::RefCounted<DataReceiver>; |
| 45 | 47 |
| 46 virtual ~DataReceiver(); | 48 virtual ~DataReceiver(); |
| 47 | 49 |
| 48 // serial::DataSourceClient override. Invoked by the DataSource to report | 50 // serial::DataSourceClient overrides. |
| 49 // errors. | 51 // Invoked by the DataSource to report errors. |
| 50 virtual void OnError(uint32_t bytes_since_last_error, int32_t error) override; | 52 virtual void OnError(int32_t error) override; |
| 53 // Invoked by the DataSource transmit data. |
| 54 virtual void OnData(mojo::Array<uint8_t> data) override; |
| 51 | 55 |
| 52 // mojo::ErrorHandler override. Calls ShutDown(). | 56 // mojo::ErrorHandler override. Calls ShutDown(). |
| 53 virtual void OnConnectionError() override; | 57 virtual void OnConnectionError() override; |
| 54 | 58 |
| 55 // Invoked by the PendingReceive to report that the user is done with the | 59 // Invoked by the PendingReceive to report that the user is done with the |
| 56 // receive buffer, having read |bytes_read| bytes from it. | 60 // receive buffer, having read |bytes_read| bytes from it. |
| 57 void Done(uint32_t bytes_read); | 61 void Done(uint32_t bytes_read); |
| 58 | 62 |
| 59 // Invoked when |handle_| is ready to read. Unless an error has occurred, this | |
| 60 // calls ReceiveInternal(). | |
| 61 void OnDoneWaiting(MojoResult result); | |
| 62 | |
| 63 // The implementation of Receive(). If a |pending_error_| is ready to | 63 // The implementation of Receive(). If a |pending_error_| is ready to |
| 64 // dispatch, it does so. Otherwise, this attempts to read from |handle_| and | 64 // dispatch, it does so. Otherwise, this attempts to read from |handle_| and |
| 65 // dispatches the contents to |pending_receive_|. If |handle_| is not ready, | 65 // dispatches the contents to |pending_receive_|. If |handle_| is not ready, |
| 66 // |waiter_| is used to wait until it is ready. If an error occurs in reading | 66 // |waiter_| is used to wait until it is ready. If an error occurs in reading |
| 67 // |handle_|, ShutDown() is called. | 67 // |handle_|, ShutDown() is called. |
| 68 void ReceiveInternal(); | 68 void ReceiveInternal(); |
| 69 | 69 |
| 70 // We may have been notified of an error that occurred at some future point in | |
| 71 // the stream. We should never be able to read past the point at which the | |
| 72 // error occurred until we have dealt with the error and called Resume() on | |
| 73 // the DataSource. If this has occurred, something bad has happened on the | |
| 74 // service side, so we shut down. | |
| 75 bool CheckErrorNotInReadRange(uint32_t num_bytes); | |
| 76 | |
| 77 // Called when we encounter a fatal error. If a receive is in progress, | 70 // Called when we encounter a fatal error. If a receive is in progress, |
| 78 // |fatal_error_value_| will be reported to the user. | 71 // |fatal_error_value_| will be reported to the user. |
| 79 void ShutDown(); | 72 void ShutDown(); |
| 80 | 73 |
| 81 // The control connection to the data source. | 74 // The control connection to the data source. |
| 82 mojo::InterfacePtr<serial::DataSource> source_; | 75 mojo::InterfacePtr<serial::DataSource> source_; |
| 83 | 76 |
| 84 // The data connection to the data source. | |
| 85 mojo::ScopedDataPipeConsumerHandle handle_; | |
| 86 | |
| 87 // The error value to report in the event of a fatal error. | 77 // The error value to report in the event of a fatal error. |
| 88 const int32_t fatal_error_value_; | 78 const int32_t fatal_error_value_; |
| 89 | 79 |
| 90 // The number of bytes received from the data source. | |
| 91 uint32_t bytes_received_; | |
| 92 | |
| 93 // Whether we have encountered a fatal error and shut down. | 80 // Whether we have encountered a fatal error and shut down. |
| 94 bool shut_down_; | 81 bool shut_down_; |
| 95 | 82 |
| 96 // A waiter used to wait until |handle_| is readable if we are waiting. | |
| 97 scoped_ptr<AsyncWaiter> waiter_; | |
| 98 | |
| 99 // The current pending receive operation if there is one. | 83 // The current pending receive operation if there is one. |
| 100 scoped_ptr<PendingReceive> pending_receive_; | 84 scoped_ptr<PendingReceive> pending_receive_; |
| 101 | 85 |
| 102 // The current pending error if there is one. | 86 // The current pending error if there is one. |
| 103 scoped_ptr<PendingError> pending_error_; | 87 scoped_ptr<PendingError> pending_error_; |
| 104 | 88 |
| 89 // The queue of pending data buffers received from the DataSource. |
| 90 std::queue<linked_ptr<PendingData>> pending_data_buffers_; |
| 91 |
| 105 base::WeakPtrFactory<DataReceiver> weak_factory_; | 92 base::WeakPtrFactory<DataReceiver> weak_factory_; |
| 106 | 93 |
| 107 DISALLOW_COPY_AND_ASSIGN(DataReceiver); | 94 DISALLOW_COPY_AND_ASSIGN(DataReceiver); |
| 108 }; | 95 }; |
| 109 | 96 |
| 110 } // namespace device | 97 } // namespace device |
| 111 | 98 |
| 112 #endif // DEVICE_SERIAL_DATA_RECEIVER_H_ | 99 #endif // DEVICE_SERIAL_DATA_RECEIVER_H_ |
| OLD | NEW |