Vibration API: convert implementation to java mojo-service.
[chromium-blink-merge.git] / device / serial / data_receiver.h
blob774d3958753bde9966d7725d5757aea0fa812ec9
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_
8 #include <queue>
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"
18 namespace device {
20 // A DataReceiver receives data from a DataSource.
21 class DataReceiver : public base::RefCounted<DataReceiver>,
22 public serial::DataSourceClient {
23 public:
24 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback;
25 typedef base::Callback<void(int32_t error)> ReceiveErrorCallback;
27 // Constructs a DataReceiver to receive data from |source|, using a buffer
28 // size of |buffer_size|, with connection errors reported as
29 // |fatal_error_value|.
30 DataReceiver(mojo::InterfacePtr<serial::DataSource> source,
31 mojo::InterfaceRequest<serial::DataSourceClient> client,
32 uint32_t buffer_size,
33 int32_t fatal_error_value);
35 // Begins a receive operation. If this returns true, exactly one of |callback|
36 // and |error_callback| will eventually be called. If there is already a
37 // receive in progress or there has been a connection error, this will have no
38 // effect and return false.
39 bool Receive(const ReceiveDataCallback& callback,
40 const ReceiveErrorCallback& error_callback);
42 private:
43 class PendingReceive;
44 struct DataFrame;
45 friend class base::RefCounted<DataReceiver>;
47 ~DataReceiver() override;
49 // serial::DataSourceClient overrides.
50 // Invoked by the DataSource to report errors.
51 void OnError(int32_t error) override;
52 // Invoked by the DataSource transmit data.
53 void OnData(mojo::Array<uint8_t> data) override;
55 // mojo error handler
56 void OnConnectionError();
58 // Invoked by the PendingReceive to report that the user is done with the
59 // receive buffer, having read |bytes_read| bytes from it.
60 void Done(uint32_t bytes_read);
62 // The implementation of Receive(). If a |pending_error_| is ready to
63 // dispatch, it does so. Otherwise, this attempts to read from |handle_| and
64 // dispatches the contents to |pending_receive_|. If |handle_| is not ready,
65 // |waiter_| is used to wait until it is ready. If an error occurs in reading
66 // |handle_|, ShutDown() is called.
67 void ReceiveInternal();
69 // Called when we encounter a fatal error. If a receive is in progress,
70 // |fatal_error_value_| will be reported to the user.
71 void ShutDown();
73 // The control connection to the data source.
74 mojo::InterfacePtr<serial::DataSource> source_;
75 mojo::Binding<serial::DataSourceClient> client_;
77 // The error value to report in the event of a fatal error.
78 const int32_t fatal_error_value_;
80 // Whether we have encountered a fatal error and shut down.
81 bool shut_down_;
83 // The current pending receive operation if there is one.
84 scoped_ptr<PendingReceive> pending_receive_;
86 // The queue of pending data frames (data or an error) received from the
87 // DataSource.
88 std::queue<linked_ptr<DataFrame>> pending_data_frames_;
90 base::WeakPtrFactory<DataReceiver> weak_factory_;
92 DISALLOW_COPY_AND_ASSIGN(DataReceiver);
95 } // namespace device
97 #endif // DEVICE_SERIAL_DATA_RECEIVER_H_