Move GN build files for cacheinvalidation into the main tree.
[chromium-blink-merge.git] / device / serial / data_source_sender.h
blob2de7ce64d6acae2df2395405d80e015a2bf12307
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_SOURCE_SENDER_H_
6 #define DEVICE_SERIAL_DATA_SOURCE_SENDER_H_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "device/serial/buffer.h"
14 #include "device/serial/data_stream.mojom.h"
15 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
17 namespace device {
19 // A DataSourceSender is an interface between a source of data and a
20 // DataSourceClient.
21 class DataSourceSender : public base::RefCounted<DataSourceSender>,
22 public serial::DataSource {
23 public:
24 typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback;
25 typedef base::Callback<void()> ErrorCallback;
27 // Constructs a DataSourceSender. Whenever the pipe is ready for writing, the
28 // |ready_callback| will be called with the WritableBuffer to be filled.
29 // |ready_callback| will not be called again until the previous WritableBuffer
30 // is destroyed. If a connection error occurs, |error_callback| will be
31 // called and the DataSourceSender will act as if ShutDown() had been called.
32 DataSourceSender(mojo::InterfaceRequest<serial::DataSource> source,
33 mojo::InterfacePtr<serial::DataSourceClient> client,
34 const ReadyCallback& ready_callback,
35 const ErrorCallback& error_callback);
37 // Shuts down this DataSourceSender. After shut down, |ready_callback| and
38 // |error_callback| will never be called.
39 void ShutDown();
41 private:
42 friend class base::RefCounted<DataSourceSender>;
43 class PendingSend;
45 ~DataSourceSender() override;
47 // mojo::InterfaceImpl<serial::DataSourceSender> overrides.
48 void Init(uint32_t buffer_size) override;
49 void Resume() override;
50 void ReportBytesReceived(uint32_t bytes_sent) override;
51 // mojo error handler. Calls DispatchFatalError().
52 void OnConnectionError();
54 // Gets more data to send to the DataSourceClient.
55 void GetMoreData();
57 // Invoked to pass |data| obtained in response to |ready_callback_|.
58 void Done(const std::vector<char>& data);
60 // Invoked to pass |data| and |error| obtained in response to
61 // |ready_callback_|.
62 void DoneWithError(const std::vector<char>& data, int32_t error);
64 // Dispatches |data| to the client.
65 void DoneInternal(const std::vector<char>& data);
67 // Reports a fatal error to the client and shuts down.
68 void DispatchFatalError();
70 mojo::Binding<serial::DataSource> binding_;
71 mojo::InterfacePtr<serial::DataSourceClient> client_;
73 // The callback to call when the client is ready for more data.
74 ReadyCallback ready_callback_;
76 // The callback to call if a fatal error occurs.
77 ErrorCallback error_callback_;
79 // The current pending send operation if there is one.
80 scoped_ptr<PendingSend> pending_send_;
82 // The number of bytes available for buffering in the client.
83 uint32_t available_buffer_capacity_;
85 // Whether sending is paused due to an error.
86 bool paused_;
88 // Whether we have encountered a fatal error and shut down.
89 bool shut_down_;
91 base::WeakPtrFactory<DataSourceSender> weak_factory_;
93 DISALLOW_COPY_AND_ASSIGN(DataSourceSender);
96 } // namespace device
98 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_