summaryrefslogtreecommitdiffstats
path: root/device/serial/data_sink_receiver.h
blob: 8bb306e0c1bc8460d747afc07e1a538ffd5a02fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DEVICE_SERIAL_DATA_SINK_RECEIVER_H_
#define DEVICE_SERIAL_DATA_SINK_RECEIVER_H_

#include <queue>

#include "base/callback.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "device/serial/buffer.h"
#include "device/serial/data_stream.mojom.h"
#include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"

namespace device {

class DataSinkReceiver : public base::RefCounted<DataSinkReceiver>,
                         public serial::DataSink {
 public:
  typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReadyCallback;
  typedef base::Callback<void(int32_t error)> CancelCallback;
  typedef base::Callback<void()> ErrorCallback;

  // Constructs a DataSinkReceiver. Whenever the pipe is ready for reading, the
  // |ready_callback| will be called with the ReadOnlyBuffer to read.
  // |ready_callback| will not be called again until the previous ReadOnlyBuffer
  // is destroyed. If a connection error occurs, |error_callback| will be called
  // and the DataSinkReceiver will act as if ShutDown() had been called. If
  // |cancel_callback| is valid, it will be called when the DataSink client
  // requests cancellation of the in-progress read.
  DataSinkReceiver(mojo::InterfaceRequest<serial::DataSink> request,
                   const ReadyCallback& ready_callback,
                   const CancelCallback& cancel_callback,
                   const ErrorCallback& error_callback);

  // Shuts down this DataSinkReceiver. After shut down, |ready_callback|,
  // |cancel_callback| and |error_callback| will never be called.
  void ShutDown();

 private:
  class Buffer;
  class DataFrame;
  friend class base::RefCounted<DataSinkReceiver>;

  ~DataSinkReceiver() override;

  // mojo::InterfaceImpl<serial::DataSink> overrides.
  void Cancel(int32_t error) override;
  void OnData(mojo::Array<uint8_t> data,
              const mojo::Callback<void(uint32_t, int32_t)>& callback) override;
  void ClearError() override;

  void OnConnectionError();

  // Dispatches data to |ready_callback_|.
  void RunReadyCallback();

  // Reports a successful read of |bytes_read|.
  void Done(uint32_t bytes_read);

  // Reports a partially successful or unsuccessful read of |bytes_read|
  // with an error of |error|.
  void DoneWithError(uint32_t bytes_read, int32_t error);

  // Marks |bytes_read| bytes as being read.
  bool DoneInternal(uint32_t bytes_read);

  // Reports an error to the client.
  void ReportError(uint32_t bytes_read, int32_t error);

  // Reports a fatal error to the client and shuts down.
  void DispatchFatalError();

  mojo::Binding<serial::DataSink> binding_;

  // The callback to call when there is data ready to read.
  const ReadyCallback ready_callback_;

  // The callback to call when the client has requested cancellation.
  const CancelCallback cancel_callback_;

  // The callback to call if a fatal error occurs.
  const ErrorCallback error_callback_;

  // The current error that has not been cleared by a ClearError message..
  int32_t current_error_;

  // The buffer passed to |ready_callback_| if one exists. This is not owned,
  // but the Buffer will call Done or DoneWithError before being deleted.
  Buffer* buffer_in_use_;

  // The data we have received from the client that has not been passed to
  // |ready_callback_|.
  std::queue<linked_ptr<DataFrame>> pending_data_buffers_;

  // Whether we have encountered a fatal error and shut down.
  bool shut_down_;

  base::WeakPtrFactory<DataSinkReceiver> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(DataSinkReceiver);
};

}  // namespace device

#endif  // DEVICE_SERIAL_DATA_SINK_RECEIVER_H_