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.
5 #ifndef DEVICE_SERIAL_DATA_RECEIVER_H_
6 #define DEVICE_SERIAL_DATA_RECEIVER_H_
10 #include "base/callback.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "device/serial/buffer.h"
15 #include "device/serial/data_stream.mojom.h"
16 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
20 // A DataReceiver receives data from a DataSource.
21 class DataReceiver
: public base::RefCounted
<DataReceiver
>,
22 public serial::DataSourceClient
,
23 public mojo::ErrorHandler
{
25 typedef base::Callback
<void(scoped_ptr
<ReadOnlyBuffer
>)> ReceiveDataCallback
;
26 typedef base::Callback
<void(int32_t error
)> ReceiveErrorCallback
;
28 // Constructs a DataReceiver to receive data from |source|, using a buffer
29 // size of |buffer_size|, with connection errors reported as
30 // |fatal_error_value|.
31 DataReceiver(mojo::InterfacePtr
<serial::DataSource
> source
,
32 mojo::InterfaceRequest
<serial::DataSourceClient
> client
,
34 int32_t fatal_error_value
);
36 // Begins a receive operation. If this returns true, exactly one of |callback|
37 // and |error_callback| will eventually be called. If there is already a
38 // receive in progress or there has been a connection error, this will have no
39 // effect and return false.
40 bool Receive(const ReceiveDataCallback
& callback
,
41 const ReceiveErrorCallback
& error_callback
);
46 friend class base::RefCounted
<DataReceiver
>;
48 ~DataReceiver() override
;
50 // serial::DataSourceClient overrides.
51 // Invoked by the DataSource to report errors.
52 void OnError(int32_t error
) override
;
53 // Invoked by the DataSource transmit data.
54 void OnData(mojo::Array
<uint8_t> data
) override
;
56 // mojo::ErrorHandler override. Calls ShutDown().
57 void OnConnectionError() override
;
59 // Invoked by the PendingReceive to report that the user is done with the
60 // receive buffer, having read |bytes_read| bytes from it.
61 void Done(uint32_t bytes_read
);
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();
70 // Called when we encounter a fatal error. If a receive is in progress,
71 // |fatal_error_value_| will be reported to the user.
74 // The control connection to the data source.
75 mojo::InterfacePtr
<serial::DataSource
> source_
;
76 mojo::Binding
<serial::DataSourceClient
> client_
;
78 // The error value to report in the event of a fatal error.
79 const int32_t fatal_error_value_
;
81 // Whether we have encountered a fatal error and shut down.
84 // The current pending receive operation if there is one.
85 scoped_ptr
<PendingReceive
> pending_receive_
;
87 // The queue of pending data frames (data or an error) received from the
89 std::queue
<linked_ptr
<DataFrame
>> pending_data_frames_
;
91 base::WeakPtrFactory
<DataReceiver
> weak_factory_
;
93 DISALLOW_COPY_AND_ASSIGN(DataReceiver
);
98 #endif // DEVICE_SERIAL_DATA_RECEIVER_H_