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 "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/weak_ptr.h" | |
10 #include "device/serial/buffer.h" | 11 #include "device/serial/buffer.h" |
11 #include "device/serial/data_stream.mojom.h" | 12 #include "device/serial/data_stream.mojom.h" |
12 #include "mojo/public/cpp/system/data_pipe.h" | 13 #include "mojo/public/cpp/system/data_pipe.h" |
13 | 14 |
14 namespace device { | 15 namespace device { |
15 | 16 |
16 class AsyncWaiter; | |
17 | |
18 // A DataSourceSender is an interface between a source of data and a data pipe. | 17 // A DataSourceSender is an interface between a source of data and a data pipe. |
19 class DataSourceSender : public base::RefCounted<DataSourceSender>, | 18 class DataSourceSender : public base::RefCounted<DataSourceSender>, |
20 public mojo::InterfaceImpl<serial::DataSource> { | 19 public mojo::InterfaceImpl<serial::DataSource> { |
21 public: | 20 public: |
22 typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback; | 21 typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback; |
23 typedef base::Callback<void()> ErrorCallback; | 22 typedef base::Callback<void()> ErrorCallback; |
24 | 23 |
25 // Constructs a DataSourceSender. Whenever the pipe is ready for writing, the | 24 // Constructs a DataSourceSender. Whenever the pipe is ready for writing, the |
26 // |ready_callback| will be called with the WritableBuffer to be filled. | 25 // |ready_callback| will be called with the WritableBuffer to be filled. |
27 // |ready_callback| will not be called again until the previous WritableBuffer | 26 // |ready_callback| will not be called again until the previous WritableBuffer |
28 // is destroyed. If a connection error occurs, |error_callback| will be | 27 // is destroyed. If a connection error occurs, |error_callback| will be |
29 // called and the DataSourceSender will act as if ShutDown() had been called. | 28 // called and the DataSourceSender will act as if ShutDown() had been called. |
30 DataSourceSender(const ReadyCallback& ready_callback, | 29 DataSourceSender(const ReadyCallback& ready_callback, |
31 const ErrorCallback& error_callback); | 30 const ErrorCallback& error_callback); |
32 | 31 |
33 // Shuts down this DataSourceSender. After shut down, |ready_callback| and | 32 // Shuts down this DataSourceSender. After shut down, |ready_callback| and |
34 // |error_callback| will never be called. | 33 // |error_callback| will never be called. |
35 void ShutDown(); | 34 void ShutDown(); |
36 | 35 |
37 private: | 36 private: |
38 friend class base::RefCounted<DataSourceSender>; | 37 friend class base::RefCounted<DataSourceSender>; |
39 class PendingSend; | 38 class PendingSend; |
40 | 39 |
41 virtual ~DataSourceSender(); | 40 virtual ~DataSourceSender(); |
42 | 41 |
43 // mojo::InterfaceImpl<serial::DataSourceSender> overrides. | 42 // mojo::InterfaceImpl<serial::DataSourceSender> overrides. |
44 virtual void Init(mojo::ScopedDataPipeProducerHandle handle) override; | 43 virtual void Init(uint32_t buffer_size) override; |
45 virtual void Resume() override; | 44 virtual void Resume() override; |
45 virtual void AckData(uint32_t bytes_dispatched) override; | |
46 // Invoked in the event of a connection error. Calls DispatchFatalError(). | 46 // Invoked in the event of a connection error. Calls DispatchFatalError(). |
47 virtual void OnConnectionError() override; | 47 virtual void OnConnectionError() override; |
48 | 48 |
49 // Starts waiting for |handle_| to be ready for writes. | 49 // Gets more data to send to the DataSourceClient. |
50 void StartWaiting(); | 50 void GetMoreData(); |
51 | |
52 // Invoked when |handle_| is ready for writes. | |
53 void OnDoneWaiting(MojoResult result); | |
54 | 51 |
55 // Reports a successful write of |bytes_written|. | 52 // Reports a successful write of |bytes_written|. |
56 void Done(uint32_t bytes_written); | 53 void Done(uint32_t bytes_written); |
57 | 54 |
58 // Reports a partially successful or unsuccessful write of |bytes_written| | 55 // Reports a partially successful or unsuccessful write of |bytes_written| |
59 // with an error of |error|. | 56 // with an error of |error|. |
60 void DoneWithError(uint32_t bytes_written, int32_t error); | 57 void DoneWithError(uint32_t bytes_written, int32_t error); |
61 | 58 |
62 // Finishes the two-phase data pipe write. | 59 // Finishes the two-phase data pipe write. |
63 void DoneInternal(uint32_t bytes_written); | 60 void DoneInternal(uint32_t bytes_written); |
64 | 61 |
65 // Reports a fatal error to the client and shuts down. | 62 // Reports a fatal error to the client and shuts down. |
66 void DispatchFatalError(); | 63 void DispatchFatalError(); |
67 | 64 |
68 // The data connection to the data receiver. | 65 // The callback to call when the client is ready for more data. |
69 mojo::ScopedDataPipeProducerHandle handle_; | |
70 | |
71 // The callback to call when |handle_| is ready for more data. | |
72 ReadyCallback ready_callback_; | 66 ReadyCallback ready_callback_; |
73 | 67 |
74 // The callback to call if a fatal error occurs. | 68 // The callback to call if a fatal error occurs. |
75 ErrorCallback error_callback_; | 69 ErrorCallback error_callback_; |
76 | 70 |
77 // The current pending send operation if there is one. | 71 // The current pending send operation if there is one. |
78 scoped_ptr<PendingSend> pending_send_; | 72 scoped_ptr<PendingSend> pending_send_; |
79 | 73 |
80 // A waiter used to wait until |handle_| is writable if we are waiting. | 74 // The number of bytes that have been sent to the client, but have not been |
81 scoped_ptr<AsyncWaiter> waiter_; | 75 // acknowledged. |
raymes
2014/10/17 04:04:45
Please see my other note on the similar comment in
Sam McNally
2014/10/20 05:12:59
Done.
| |
82 | 76 uint32_t available_buffer_capacity_; |
83 // The number of bytes sent to the data receiver. | |
84 uint32_t bytes_sent_; | |
85 | 77 |
86 // Whether we have encountered a fatal error and shut down. | 78 // Whether we have encountered a fatal error and shut down. |
87 bool shut_down_; | 79 bool shut_down_; |
88 | 80 |
81 base::WeakPtrFactory<DataSourceSender> weak_factory_; | |
82 | |
89 DISALLOW_COPY_AND_ASSIGN(DataSourceSender); | 83 DISALLOW_COPY_AND_ASSIGN(DataSourceSender); |
90 }; | 84 }; |
91 | 85 |
92 } // namespace device | 86 } // namespace device |
93 | 87 |
94 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ | 88 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ |
OLD | NEW |