| 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 |fatal_error_value|. |
| 30 DataReceiver(mojo::InterfacePtr<serial::DataSource> source, |
| 31 uint32_t buffer_size, |
| 32 int32_t fatal_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 or there has been a connection error, this will have no |
| 37 // effect and return false. |
| 38 bool Receive(const ReceiveDataCallback& callback, |
| 39 const ReceiveErrorCallback& error_callback); |
| 40 |
| 41 private: |
| 42 class PendingReceive; |
| 43 struct PendingError; |
| 44 friend class base::RefCounted<DataReceiver>; |
| 45 |
| 46 virtual ~DataReceiver(); |
| 47 |
| 48 // serial::DataSourceClient override. Invoked by the DataSource to report |
| 49 // errors. |
| 50 virtual void OnError(uint32_t bytes_since_last_error, int32_t error) OVERRIDE; |
| 51 |
| 52 // mojo::ErrorHandler override. Calls ShutDown(). |
| 53 virtual void OnConnectionError() OVERRIDE; |
| 54 |
| 55 // Invoked by the PendingReceive to report that the user is done with the |
| 56 // receive buffer, having read |bytes_read| bytes from it. |
| 57 void Done(uint32_t bytes_read); |
| 58 |
| 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 |
| 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, |
| 66 // |waiter_| is used to wait until it is ready. If an error occurs in reading |
| 67 // |handle_|, ShutDown() is called. |
| 68 void ReceiveInternal(); |
| 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, |
| 78 // |fatal_error_value_| will be reported to the user. |
| 79 void ShutDown(); |
| 80 |
| 81 // The control connection to the data source. |
| 82 mojo::InterfacePtr<serial::DataSource> source_; |
| 83 |
| 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. |
| 88 const int32_t fatal_error_value_; |
| 89 |
| 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. |
| 94 bool shut_down_; |
| 95 |
| 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. |
| 100 scoped_ptr<PendingReceive> pending_receive_; |
| 101 |
| 102 // The current pending error if there is one. |
| 103 scoped_ptr<PendingError> pending_error_; |
| 104 |
| 105 base::WeakPtrFactory<DataReceiver> weak_factory_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(DataReceiver); |
| 108 }; |
| 109 |
| 110 } // namespace device |
| 111 |
| 112 #endif // DEVICE_SERIAL_DATA_RECEIVER_H_ |
| OLD | NEW |