OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ | 5 #ifndef DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ |
6 #define DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ | 6 #define DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "device/serial/buffer.h" | 13 #include "device/serial/buffer.h" |
14 #include "device/serial/data_stream.mojom.h" | 14 #include "device/serial/data_stream.mojom.h" |
15 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h" | 15 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h" |
16 | 16 |
17 namespace device { | 17 namespace device { |
18 | 18 |
19 // A DataSourceSender is an interface between a source of data and a | 19 // A DataSourceSender is an interface between a source of data and a |
20 // DataSourceClient. | 20 // DataSourceClient. |
21 class DataSourceSender : public base::RefCounted<DataSourceSender>, | 21 class DataSourceSender : public base::RefCounted<DataSourceSender>, |
22 public mojo::InterfaceImpl<serial::DataSource> { | 22 public serial::DataSource, |
| 23 public mojo::ErrorHandler { |
23 public: | 24 public: |
24 typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback; | 25 typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback; |
25 typedef base::Callback<void()> ErrorCallback; | 26 typedef base::Callback<void()> ErrorCallback; |
26 | 27 |
27 // Constructs a DataSourceSender. Whenever the pipe is ready for writing, the | 28 // 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 be called with the WritableBuffer to be filled. |
29 // |ready_callback| will not be called again until the previous WritableBuffer | 30 // |ready_callback| will not be called again until the previous WritableBuffer |
30 // is destroyed. If a connection error occurs, |error_callback| will be | 31 // 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 // called and the DataSourceSender will act as if ShutDown() had been called. |
32 DataSourceSender(const ReadyCallback& ready_callback, | 33 DataSourceSender(mojo::InterfaceRequest<serial::DataSource> source, |
| 34 mojo::InterfacePtr<serial::DataSourceClient> client, |
| 35 const ReadyCallback& ready_callback, |
33 const ErrorCallback& error_callback); | 36 const ErrorCallback& error_callback); |
34 | 37 |
35 // Shuts down this DataSourceSender. After shut down, |ready_callback| and | 38 // Shuts down this DataSourceSender. After shut down, |ready_callback| and |
36 // |error_callback| will never be called. | 39 // |error_callback| will never be called. |
37 void ShutDown(); | 40 void ShutDown(); |
38 | 41 |
39 private: | 42 private: |
40 friend class base::RefCounted<DataSourceSender>; | 43 friend class base::RefCounted<DataSourceSender>; |
41 class PendingSend; | 44 class PendingSend; |
42 | 45 |
(...skipping 15 matching lines...) Expand all Loading... |
58 // Invoked to pass |data| and |error| obtained in response to | 61 // Invoked to pass |data| and |error| obtained in response to |
59 // |ready_callback_|. | 62 // |ready_callback_|. |
60 void DoneWithError(const std::vector<char>& data, int32_t error); | 63 void DoneWithError(const std::vector<char>& data, int32_t error); |
61 | 64 |
62 // Dispatches |data| to the client. | 65 // Dispatches |data| to the client. |
63 void DoneInternal(const std::vector<char>& data); | 66 void DoneInternal(const std::vector<char>& data); |
64 | 67 |
65 // Reports a fatal error to the client and shuts down. | 68 // Reports a fatal error to the client and shuts down. |
66 void DispatchFatalError(); | 69 void DispatchFatalError(); |
67 | 70 |
| 71 mojo::Binding<serial::DataSource> binding_; |
| 72 mojo::InterfacePtr<serial::DataSourceClient> client_; |
| 73 |
68 // The callback to call when the client is ready for more data. | 74 // The callback to call when the client is ready for more data. |
69 ReadyCallback ready_callback_; | 75 ReadyCallback ready_callback_; |
70 | 76 |
71 // The callback to call if a fatal error occurs. | 77 // The callback to call if a fatal error occurs. |
72 ErrorCallback error_callback_; | 78 ErrorCallback error_callback_; |
73 | 79 |
74 // The current pending send operation if there is one. | 80 // The current pending send operation if there is one. |
75 scoped_ptr<PendingSend> pending_send_; | 81 scoped_ptr<PendingSend> pending_send_; |
76 | 82 |
77 // The number of bytes available for buffering in the client. | 83 // The number of bytes available for buffering in the client. |
78 uint32_t available_buffer_capacity_; | 84 uint32_t available_buffer_capacity_; |
79 | 85 |
80 // Whether sending is paused due to an error. | 86 // Whether sending is paused due to an error. |
81 bool paused_; | 87 bool paused_; |
82 | 88 |
83 // Whether we have encountered a fatal error and shut down. | 89 // Whether we have encountered a fatal error and shut down. |
84 bool shut_down_; | 90 bool shut_down_; |
85 | 91 |
86 base::WeakPtrFactory<DataSourceSender> weak_factory_; | 92 base::WeakPtrFactory<DataSourceSender> weak_factory_; |
87 | 93 |
88 DISALLOW_COPY_AND_ASSIGN(DataSourceSender); | 94 DISALLOW_COPY_AND_ASSIGN(DataSourceSender); |
89 }; | 95 }; |
90 | 96 |
91 } // namespace device | 97 } // namespace device |
92 | 98 |
93 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ | 99 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ |
OLD | NEW |