Chromium Code Reviews| 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..1a6cc735c6910232ee5a098a2cae6322e14d406b |
| --- /dev/null |
| +++ b/extensions/browser/api/cast_channel/cast_transport.h |
| @@ -0,0 +1,181 @@ |
| +// 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 CastSocketPlaceholder { |
| + public: |
| + CastSocketPlaceholder() {} |
| + virtual ~CastSocketPlaceholder() {} |
| + |
| + 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 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 { |
|
mark a. foltz
2014/09/11 06:47:45
This is a clean API and a vast improvement. Thank
Kevin M
2014/09/11 18:07:58
:)
|
| + 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 CastSocketPlaceholder* socket, |
| + ChannelError error_state, |
| + const LastErrors& last_errors) = 0; |
| + // A message was received on the channel. |
| + virtual void OnMessage(const CastSocketPlaceholder* socket, |
| + const CastMessage& message) = 0; |
| + |
| + protected: |
| + virtual ~Delegate() {} |
| + }; |
| + |
| + CastTransport(CastSocketPlaceholder* 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 FlushPendingCallbacks(); |
|
mark a. foltz
2014/09/11 06:47:45
Maybe FlushWriteQueue()?
Kevin M
2014/09/11 18:07:58
Done.
|
| + |
| + ///////////////////////////////////////////////////////////////////////////// |
| + // Following methods work together to implement write flow. |
| + // |
| + // Main method that performs write flow state transitions. |
| + void DoWriteLoop(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); |
| + ///////////////////////////////////////////////////////////////////////////// |
| + |
| + ///////////////////////////////////////////////////////////////////////////// |
| + // Following methods work together to implement read flow. |
| + // |
| + // Main method that performs write flow state transitions. |
| + void DoReadLoop(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. |
| + CastSocketPlaceholder* socket_; |
| + // 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_; |
| +}; |
| +} // namespace cast_channel |
| +} // namespace core_api |
| +} // namespace extensions |
| + |
| +#endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_ |