Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Unified Diff: device/serial/data_source_sender.h

Issue 437933002: Add data pipe wrappers to be used to implement serial receive. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serial-buffer
Patch Set: address comments Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/serial/data_source_sender.h
diff --git a/device/serial/data_source_sender.h b/device/serial/data_source_sender.h
new file mode 100644
index 0000000000000000000000000000000000000000..a050cafbca0ed73971aec03791ba7765388e4fd8
--- /dev/null
+++ b/device/serial/data_source_sender.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_SOURCE_SENDER_H_
+#define DEVICE_SERIAL_DATA_SOURCE_SENDER_H_
+
+#include "base/callback.h"
+#include "base/memory/ref_counted.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 DataSourceSender is an interface between a source of data and a data pipe.
+class DataSourceSender : public base::RefCounted<DataSourceSender>,
+ public mojo::InterfaceImpl<serial::DataSource> {
+ public:
+ typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback;
+ typedef base::Callback<void()> ErrorCallback;
+
+ // Constructs a DataSourceSender. Whenever the pipe is ready for writing, the
+ // |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 DataSourceSender will act as if Shutdown() had been called.
+ DataSourceSender(const ReadyCallback& ready_callback,
+ const ErrorCallback& error_callback);
+
+ // Shuts down this DataSourceSender. After shut down, |ready_callback| and
+ // |error_callback| will never be called.
+ void Shutdown();
+
+ private:
+ class Buffer;
+ friend class base::RefCounted<DataSourceSender>;
+
+ enum State {
+ STATE_UNINITIALIZED,
+ STATE_WAITING_FOR_SPACE,
+ STATE_WAITING_FOR_BUFFER,
+ STATE_PAUSED,
+ STATE_SHUT_DOWN,
+ };
+
+ virtual ~DataSourceSender();
+
+ // mojo::InterfaceImpl<serial::DataSourceSender> 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(DataSourceSender);
+};
+
+} // namespace device
+
+#endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_

Powered by Google App Engine
This is Rietveld 408576698