Chromium Code Reviews| Index: device/serial/data_pipe_producer.h |
| diff --git a/device/serial/data_pipe_producer.h b/device/serial/data_pipe_producer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5a7d5c2dc7aa19ce227bc511084944e5fdc25ffe |
| --- /dev/null |
| +++ b/device/serial/data_pipe_producer.h |
| @@ -0,0 +1,75 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef DEVICE_SERIAL_DATA_PIPE_PRODUCER_H_ |
| +#define DEVICE_SERIAL_DATA_PIPE_PRODUCER_H_ |
| + |
| +#include "base/callback.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "device/serial/buffer.h" |
| +#include "device/serial/data_pipe.mojom.h" |
| +#include "mojo/public/cpp/system/data_pipe.h" |
| + |
| +namespace device { |
| + |
| +class AsyncWaiter; |
| + |
| +// A wrapper around the producer end of a data pipe. |
| +class DataPipeProducer : public base::RefCounted<DataPipeProducer>, |
| + public mojo::InterfaceImpl<serial::DataPipeProducer> { |
| + public: |
| + typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback; |
| + typedef base::Callback<void()> ErrorCallback; |
| + |
| + // 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.
|
| + // |ready_callback| will be called with the WritableBuffer to be filled. |
| + // |ready_callback| will not be called again until the previous WritableBuffer |
| + // is destroyed. If a connection error occurs, |error_callback| will be |
| + // called and the DataPipeProducer will act as if Shutdown() had been called. |
| + DataPipeProducer(const ReadyCallback& ready_callback, |
| + const ErrorCallback& error_callback); |
| + |
| + // Shuts down this DataPipeProducer. After shut down, |ready_callback| and |
| + // |error_callback| will never be called. |
| + void Shutdown(); |
| + |
| + private: |
| + class Buffer; |
| + friend class base::RefCounted<DataPipeProducer>; |
| + |
| + enum State { |
| + STATE_UNINITIALIZED, |
| + STATE_WAITING_FOR_SPACE, |
| + STATE_WAITING_FOR_BUFFER, |
| + STATE_PAUSED, |
| + STATE_SHUT_DOWN, |
| + }; |
| + |
| + virtual ~DataPipeProducer(); |
| + |
| + // mojo::InterfaceImpl<serial::DataPipeProducer> overrides. |
| + virtual void Init(mojo::ScopedDataPipeProducerHandle handle) OVERRIDE; |
| + virtual void Resume() OVERRIDE; |
| + virtual void OnConnectionError() OVERRIDE; |
| + |
| + void StartWaiting(); |
| + void OnDoneWaiting(MojoResult result); |
| + |
| + void Done(uint32_t bytes_produced); |
| + void DoneWithError(uint32_t bytes_produced, int32_t error); |
| + bool HandleMojoResult(MojoResult result); |
| + |
| + mojo::ScopedDataPipeProducerHandle handle_; |
| + const ReadyCallback ready_callback_; |
| + const ErrorCallback error_callback_; |
| + scoped_ptr<AsyncWaiter> waiter_; |
| + State state_; |
| + uint32_t bytes_since_last_error_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DataPipeProducer); |
| +}; |
| + |
| +} // namespace device |
| + |
| +#endif // DEVICE_SERIAL_DATA_PIPE_PRODUCER_H_ |