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

Unified Diff: extensions/browser/api/cast_channel/cast_transport.h

Issue 555283002: Create new class "CastTransport", which encapsulates the message read and write event loops. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed code review feedback. Created 6 years, 3 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: extensions/browser/api/cast_channel/cast_transport.h
diff --git a/extensions/browser/api/cast_channel/cast_transport.h b/extensions/browser/api/cast_channel/cast_transport.h
new file mode 100644
index 0000000000000000000000000000000000000000..59ad3ea3110ef39791da4303348f60c8e38caa9d
--- /dev/null
+++ b/extensions/browser/api/cast_channel/cast_transport.h
@@ -0,0 +1,188 @@
+// 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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_
+#define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_
+
+#include <queue>
+#include <string>
+
+#include "base/memory/ref_counted.h"
+#include "base/threading/thread_checker.h"
+#include "extensions/common/api/cast_channel.h"
+#include "net/base/completion_callback.h"
+
+namespace net {
+class DrainableIOBuffer;
+class IPEndPoint;
+class IOBuffer;
+class DrainableIOBuffer;
+class GrowableIOBuffer;
+} // namespace net
+
+namespace extensions {
+namespace core_api {
+namespace cast_channel {
+class CastMessage;
+struct LastErrors;
+class Logger;
+class MessageFramer;
+
+// TODO(kmarshall): Migrate CastSocket to new interface.
+// Skeletal placeholder for a future implementation of CastSocket.
+// New to this interface are read/write calls that operate on simple buffers.
+// Methods that operate on CastMessage/MessageInfo objects will be removed.
+class CastSocketInterface {
+ public:
+ CastSocketInterface() {}
+ virtual ~CastSocketInterface() {}
+
+ virtual int Write(net::IOBuffer* buffer,
+ size_t size,
+ const net::CompletionCallback& callback) = 0;
+ virtual int Read(net::IOBuffer* buf,
+ int buf_len,
+ const net::CompletionCallback& callback) = 0;
+ virtual void CloseWithError(ChannelError error) = 0;
+ virtual const net::IPEndPoint& ip_endpoint() const = 0;
+ virtual ChannelAuthType channel_auth() const = 0;
+ virtual int id() const = 0;
+};
+
+// Manager class for reading and writing messages to/from a CastSocket.
+class CastTransport {
+ public:
+ // Object to be informed of incoming messages and errors.
+ class Delegate {
+ public:
+ // An error occurred on the channel. |last_errors| contains the last errors
+ // logged for the channel from the implementation.
+ virtual void OnError(const CastSocketInterface* socket,
+ ChannelError error_state,
+ const LastErrors& last_errors) = 0;
+ // A message was received on the channel.
+ virtual void OnMessage(const CastSocketInterface* socket,
+ const CastMessage& message) = 0;
+
+ protected:
+ virtual ~Delegate() {}
+ };
+
+ CastTransport(CastSocketInterface* socket,
+ Delegate* read_delegate,
+ scoped_refptr<Logger> logger);
+ virtual ~CastTransport();
+
+ // Sends a CastMessage to |socket_|.
+ // |message|: The message to send.
+ // |callback|: Callback to be invoked when the write operation has finished.
+ void SendMessage(const CastMessage& message,
+ const net::CompletionCallback& callback);
+
+ // Starts reading messages from |socket_|.
+ void StartReadLoop();
+
+ // Internal write states.
+ enum WriteState {
+ WRITE_STATE_NONE,
+ WRITE_STATE_WRITE,
+ WRITE_STATE_WRITE_COMPLETE,
+ WRITE_STATE_DO_CALLBACK,
+ WRITE_STATE_ERROR,
+ };
+
+ // Internal read states.
+ enum ReadState {
+ READ_STATE_NONE,
+ READ_STATE_READ,
+ READ_STATE_READ_COMPLETE,
+ READ_STATE_DO_CALLBACK,
+ READ_STATE_ERROR,
+ };
+
+ private:
+ // Holds a message to be written to the socket. |callback| is invoked when the
+ // message is fully written or an error occurrs.
+ struct WriteRequest {
+ explicit WriteRequest(const std::string& namespace_,
+ const std::string& payload,
+ const net::CompletionCallback& callback);
+ ~WriteRequest();
+
+ // Namespace of the serialized message.
+ std::string message_namespace;
+ // Write completion callback, invoked when the operation has completed or
+ // failed.
+ net::CompletionCallback callback;
+ // Buffer with outgoing data.
+ scoped_refptr<net::DrainableIOBuffer> io_buffer;
+ };
+
+ // Terminates all in-flight write callbacks with error code ERR_FAILED.
+ void FlushWriteQueue();
+
+ // Main method that performs write flow state transitions.
+ void OnWriteResult(int result);
+
+ // Each of the below Do* method is executed in the corresponding
+ // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
+ // DowriteComplete is called, and so on.
+ int DoWrite();
+ int DoWriteComplete(int result);
+ int DoWriteCallback();
+ int DoWriteError(int result);
+
+ // Main method that performs write flow state transitions.
+ void OnReadResult(int result);
+
+ // Each of the below Do* method is executed in the corresponding
+ // write state. For example when write state is READ_STATE_READ_COMPLETE
+ // DoReadComplete is called, and so on.
+ int DoRead();
+ int DoReadComplete(int result);
+ int DoReadCallback();
+ int DoReadError(int result);
+
+ void SetReadState(ReadState read_state);
+ void SetWriteState(WriteState write_state);
+ void SetErrorState(ChannelError error_state);
+
+ // Queue of pending writes. The message at the front of the queue is the one
+ // being written.
+ std::queue<WriteRequest> write_queue_;
+
+ // Buffer used for read operations. Reused for every read.
+ scoped_refptr<net::GrowableIOBuffer> read_buffer_;
+
+ // Constructs and parses the wire representation of message frames.
+ scoped_ptr<MessageFramer> framer_;
+
+ // Last message received on the socket.
+ scoped_ptr<CastMessage> current_message_;
+
+ // Socket used for I/O operations.
+ CastSocketInterface* socket_;
miu 2014/09/15 19:08:36 Should be: CastSocketInterface* const socket_;
Kevin M 2014/09/16 21:23:10 Done.
miu 2014/09/16 21:39:53 ? Doesn't seem to be in patch set 5.
Kevin M 2014/09/16 21:45:59 Didn't catch that git cl upload failed (oops, bad
+
+ // Methods for communicating message receipt and error status to client code.
+ Delegate* read_delegate_;
+
+ // Write flow state machine state.
+ WriteState write_state_;
+
+ // Read flow state machine state.
+ ReadState read_state_;
+
+ // Most recent error that occurred during read or write operation, if any.
+ ChannelError error_state_;
+
+ scoped_refptr<Logger> logger_;
+ base::ThreadChecker thread_checker_;
+
+ DISALLOW_COPY_AND_ASSIGN(CastTransport);
+};
+} // namespace cast_channel
+} // namespace core_api
+} // namespace extensions
+
+#endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_
« no previous file with comments | « extensions/browser/api/cast_channel/cast_message_util.cc ('k') | extensions/browser/api/cast_channel/cast_transport.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698