| Index: device/serial/data_receiver.h
|
| diff --git a/device/serial/data_receiver.h b/device/serial/data_receiver.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..efd2a1e9ed6225bb4d9c54b89c33bb038d26ec90
|
| --- /dev/null
|
| +++ b/device/serial/data_receiver.h
|
| @@ -0,0 +1,85 @@
|
| +// 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_RECEIVER_H_
|
| +#define DEVICE_SERIAL_DATA_RECEIVER_H_
|
| +
|
| +#include "base/callback.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/memory/weak_ptr.h"
|
| +#include "device/serial/buffer.h"
|
| +#include "device/serial/data_stream.mojom.h"
|
| +#include "mojo/public/cpp/system/data_pipe.h"
|
| +
|
| +namespace device {
|
| +
|
| +class AsyncWaiter;
|
| +
|
| +// A DataReceiver receives data from a DataSource.
|
| +class DataReceiver : public base::RefCounted<DataReceiver>,
|
| + public serial::DataSourceClient,
|
| + public mojo::ErrorHandler {
|
| + public:
|
| + typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback;
|
| + typedef base::Callback<void(int32_t error)> ReceiveErrorCallback;
|
| +
|
| + // Constructs a DataReceiver to receive data from |source|, using a data
|
| + // pipe with a buffer size of |buffer_size|, with connection errors reported
|
| + // as |connection_error_value|.
|
| + DataReceiver(mojo::InterfacePtr<serial::DataSource> source,
|
| + uint32_t buffer_size,
|
| + int32_t connection_error_value);
|
| +
|
| + // Begins a receive operation. If this returns true, exactly one of |callback|
|
| + // and |error_callback| will eventually be called. If there is already a
|
| + // receive in progress, |callback| or |error_callback| is invalid or there has
|
| + // been a connection error, this will have no effect and return false.
|
| + bool Receive(const ReceiveDataCallback& callback,
|
| + const ReceiveErrorCallback& error_callback);
|
| +
|
| + private:
|
| + class Buffer;
|
| + friend class base::RefCounted<DataReceiver>;
|
| +
|
| + enum State {
|
| + STATE_IDLE,
|
| + STATE_WAITING_FOR_DATA,
|
| + STATE_WAITING_FOR_BUFFER,
|
| + STATE_PAUSED,
|
| + STATE_SHUT_DOWN,
|
| + };
|
| +
|
| + virtual ~DataReceiver();
|
| + void Done(uint32_t bytes_consumed);
|
| + void OnDoneWaiting(MojoResult result);
|
| +
|
| + void ReceiveInternal();
|
| + void DispatchData(const void* data, uint32_t num_bytes);
|
| + void DispatchError(int32_t error);
|
| +
|
| + // serial::DataSourceClient override.
|
| + virtual void OnError(uint32_t bytes_since_last_error, int32_t error) OVERRIDE;
|
| +
|
| + // mojo::ErrorHandler override.
|
| + virtual void OnConnectionError() OVERRIDE;
|
| +
|
| + mojo::InterfacePtr<serial::DataSource> source_;
|
| + const int32_t connection_error_value_;
|
| + mojo::ScopedDataPipeConsumerHandle handle_;
|
| + ReceiveDataCallback receive_callback_;
|
| + ReceiveErrorCallback receive_error_callback_;
|
| + uint32_t bytes_received_;
|
| + bool pending_error_;
|
| + uint32_t error_offset_;
|
| + int32_t error_;
|
| + State state_;
|
| + scoped_ptr<AsyncWaiter> waiter_;
|
| + base::WeakPtrFactory<DataReceiver> weak_factory_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(DataReceiver);
|
| +};
|
| +
|
| +} // namespace device
|
| +
|
| +#endif // DEVICE_SERIAL_DATA_RECEIVER_H_
|
|
|