Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_PIPE_PRODUCER_H_ | |
| 6 #define DEVICE_SERIAL_DATA_PIPE_PRODUCER_H_ | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "device/serial/buffer.h" | |
| 11 #include "device/serial/data_pipe.mojom.h" | |
| 12 #include "mojo/public/cpp/system/data_pipe.h" | |
| 13 | |
| 14 namespace device { | |
| 15 | |
| 16 class AsyncWaiter; | |
| 17 | |
| 18 // A wrapper around the producer end of a data pipe. | |
| 19 class DataPipeProducer : public base::RefCounted<DataPipeProducer>, | |
| 20 public mojo::InterfaceImpl<serial::DataPipeProducer> { | |
| 21 public: | |
| 22 typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback; | |
| 23 typedef base::Callback<void()> ErrorCallback; | |
| 24 | |
| 25 // Constructs a DataPipeProducer. Whenever the pipe as ready for writing, the | |
|
raymes
2014/08/05 06:26:44
as->is
Sam McNally
2014/08/05 07:26:33
Done.
| |
| 26 // |ready_callback| will be called with the WritableBuffer to be filled. | |
| 27 // |ready_callback| will not be called again until the previous WritableBuffer | |
| 28 // is destroyed. If a connection error occurs, |error_callback| will be | |
| 29 // called and the DataPipeProducer will act as if Shutdown() had been called. | |
| 30 DataPipeProducer(const ReadyCallback& ready_callback, | |
| 31 const ErrorCallback& error_callback); | |
| 32 | |
| 33 // Shuts down this DataPipeProducer. After shut down, |ready_callback| and | |
| 34 // |error_callback| will never be called. | |
| 35 void Shutdown(); | |
| 36 | |
| 37 private: | |
| 38 class Buffer; | |
| 39 friend class base::RefCounted<DataPipeProducer>; | |
| 40 | |
| 41 enum State { | |
| 42 STATE_UNINITIALIZED, | |
| 43 STATE_WAITING_FOR_SPACE, | |
| 44 STATE_WAITING_FOR_BUFFER, | |
| 45 STATE_PAUSED, | |
| 46 STATE_SHUT_DOWN, | |
| 47 }; | |
| 48 | |
| 49 virtual ~DataPipeProducer(); | |
| 50 | |
| 51 // mojo::InterfaceImpl<serial::DataPipeProducer> overrides. | |
| 52 virtual void Init(mojo::ScopedDataPipeProducerHandle handle) OVERRIDE; | |
| 53 virtual void Resume() OVERRIDE; | |
| 54 virtual void OnConnectionError() OVERRIDE; | |
| 55 | |
| 56 void StartWaiting(); | |
| 57 void OnDoneWaiting(MojoResult result); | |
| 58 | |
| 59 void Done(uint32_t bytes_produced); | |
| 60 void DoneWithError(uint32_t bytes_produced, int32_t error); | |
| 61 bool HandleMojoResult(MojoResult result); | |
| 62 | |
| 63 mojo::ScopedDataPipeProducerHandle handle_; | |
| 64 const ReadyCallback ready_callback_; | |
| 65 const ErrorCallback error_callback_; | |
| 66 scoped_ptr<AsyncWaiter> waiter_; | |
| 67 State state_; | |
| 68 uint32_t bytes_since_last_error_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(DataPipeProducer); | |
| 71 }; | |
| 72 | |
| 73 } // namespace device | |
| 74 | |
| 75 #endif // DEVICE_SERIAL_DATA_PIPE_PRODUCER_H_ | |
| OLD | NEW |