Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / device / serial / data_receiver.h
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 <queue>
9
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 "mojo/public/cpp/system/data_pipe.h"
17
18 namespace device {
19
20 // A DataReceiver receives data from a DataSource.
21 class DataReceiver : public base::RefCounted<DataReceiver>,
22                      public serial::DataSourceClient,
23                      public mojo::ErrorHandler {
24  public:
25   typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback;
26   typedef base::Callback<void(int32_t error)> ReceiveErrorCallback;
27
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                uint32_t buffer_size,
33                int32_t fatal_error_value);
34
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);
41
42  private:
43   class PendingReceive;
44   struct DataFrame;
45   friend class base::RefCounted<DataReceiver>;
46
47   ~DataReceiver() override;
48
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;
54
55   // mojo::ErrorHandler override. Calls ShutDown().
56   void OnConnectionError() override;
57
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);
61
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();
68
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();
72
73   // The control connection to the data source.
74   mojo::InterfacePtr<serial::DataSource> source_;
75
76   // The error value to report in the event of a fatal error.
77   const int32_t fatal_error_value_;
78
79   // Whether we have encountered a fatal error and shut down.
80   bool shut_down_;
81
82   // The current pending receive operation if there is one.
83   scoped_ptr<PendingReceive> pending_receive_;
84
85   // The queue of pending data frames (data or an error) received from the
86   // DataSource.
87   std::queue<linked_ptr<DataFrame>> pending_data_frames_;
88
89   base::WeakPtrFactory<DataReceiver> weak_factory_;
90
91   DISALLOW_COPY_AND_ASSIGN(DataReceiver);
92 };
93
94 }  // namespace device
95
96 #endif  // DEVICE_SERIAL_DATA_RECEIVER_H_