Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / device / serial / data_sink_receiver.h
blob733b0a1b56b5284ff17fdba98b30ab679a546680
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_SINK_RECEIVER_H_
6 #define DEVICE_SERIAL_DATA_SINK_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/bindings/binding.h"
17 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
19 namespace device {
21 class DataSinkReceiver : public base::RefCounted<DataSinkReceiver>,
22 public serial::DataSink {
23 public:
24 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReadyCallback;
25 typedef base::Callback<void(int32_t error)> CancelCallback;
26 typedef base::Callback<void()> ErrorCallback;
28 // Constructs a DataSinkReceiver. Whenever the pipe is ready for reading, the
29 // |ready_callback| will be called with the ReadOnlyBuffer to read.
30 // |ready_callback| will not be called again until the previous ReadOnlyBuffer
31 // is destroyed. If a connection error occurs, |error_callback| will be called
32 // and the DataSinkReceiver will act as if ShutDown() had been called. If
33 // |cancel_callback| is valid, it will be called when the DataSink client
34 // requests cancellation of the in-progress read.
35 DataSinkReceiver(mojo::InterfaceRequest<serial::DataSink> request,
36 const ReadyCallback& ready_callback,
37 const CancelCallback& cancel_callback,
38 const ErrorCallback& error_callback);
40 // Shuts down this DataSinkReceiver. After shut down, |ready_callback|,
41 // |cancel_callback| and |error_callback| will never be called.
42 void ShutDown();
44 private:
45 class Buffer;
46 class DataFrame;
47 friend class base::RefCounted<DataSinkReceiver>;
49 ~DataSinkReceiver() override;
51 // mojo::InterfaceImpl<serial::DataSink> overrides.
52 void Cancel(int32_t error) override;
53 void OnData(mojo::Array<uint8_t> data,
54 const mojo::Callback<void(uint32_t, int32_t)>& callback) override;
55 void ClearError() override;
57 void OnConnectionError();
59 // Dispatches data to |ready_callback_|.
60 void RunReadyCallback();
62 // Reports a successful read of |bytes_read|.
63 void Done(uint32_t bytes_read);
65 // Reports a partially successful or unsuccessful read of |bytes_read|
66 // with an error of |error|.
67 void DoneWithError(uint32_t bytes_read, int32_t error);
69 // Marks |bytes_read| bytes as being read.
70 bool DoneInternal(uint32_t bytes_read);
72 // Reports an error to the client.
73 void ReportError(uint32_t bytes_read, int32_t error);
75 // Reports a fatal error to the client and shuts down.
76 void DispatchFatalError();
78 mojo::Binding<serial::DataSink> binding_;
80 // The callback to call when there is data ready to read.
81 const ReadyCallback ready_callback_;
83 // The callback to call when the client has requested cancellation.
84 const CancelCallback cancel_callback_;
86 // The callback to call if a fatal error occurs.
87 const ErrorCallback error_callback_;
89 // The current error that has not been cleared by a ClearError message..
90 int32_t current_error_;
92 // The buffer passed to |ready_callback_| if one exists. This is not owned,
93 // but the Buffer will call Done or DoneWithError before being deleted.
94 Buffer* buffer_in_use_;
96 // The data we have received from the client that has not been passed to
97 // |ready_callback_|.
98 std::queue<linked_ptr<DataFrame>> pending_data_buffers_;
100 // Whether we have encountered a fatal error and shut down.
101 bool shut_down_;
103 base::WeakPtrFactory<DataSinkReceiver> weak_factory_;
105 DISALLOW_COPY_AND_ASSIGN(DataSinkReceiver);
108 } // namespace device
110 #endif // DEVICE_SERIAL_DATA_SINK_RECEIVER_H_