[M73 Dev][EFL] Disable VizDisplayCompositor for EFL port
[platform/framework/web/chromium-efl.git] / components / cast_channel / cast_transport.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_
6 #define COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_
7
8 #include <string>
9
10 #include "base/containers/queue.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/sequence_checker.h"
14 #include "base/threading/thread_checker.h"
15 #include "components/cast_channel/cast_channel_enum.h"
16 #include "components/cast_channel/logger.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/completion_once_callback.h"
19 #include "net/base/ip_endpoint.h"
20
21 namespace net {
22 class DrainableIOBuffer;
23 class DrainableIOBuffer;
24 class GrowableIOBuffer;
25 class IOBuffer;
26 class Socket;
27 }  // namespace net
28
29 namespace cast_channel {
30 class CastMessage;
31 class MessageFramer;
32
33 class CastTransport {
34  public:
35   virtual ~CastTransport() {}
36
37   // Object to be informed of incoming messages and read errors.
38   class Delegate {
39    public:
40     virtual ~Delegate() {}
41
42     // Called once Transport is successfully initialized and started.
43     // Owned read delegates are Start()ed automatically.
44     virtual void Start() = 0;
45
46     // An error occurred on the channel.
47     // The caller is responsible for closing |socket| if an error occurred.
48     virtual void OnError(ChannelError error_state) = 0;
49
50     // A message was received on the channel.
51     virtual void OnMessage(const CastMessage& message) = 0;
52   };
53
54   // Sends a CastMessage to |socket_|.
55   // |message|: The message to send.
56   // |callback|: Callback to be invoked when the write operation has finished.
57   // Virtual for testing.
58   virtual void SendMessage(const CastMessage& message,
59                            const net::CompletionCallback& callback) = 0;
60
61   // Initializes the reading state machine and starts reading from the
62   // underlying socket.
63   // Virtual for testing.
64   virtual void Start() = 0;
65
66   // Changes the delegate for processing read events. Pending reads remain
67   // in-flight.
68   // Ownership of the pointee of |delegate| is assumed by the transport.
69   // Prior delegates are deleted automatically.
70   virtual void SetReadDelegate(std::unique_ptr<Delegate> delegate) = 0;
71 };
72
73 // Manager class for reading and writing messages to/from a socket.
74 class CastTransportImpl : public CastTransport {
75  public:
76   using ChannelError = ::cast_channel::ChannelError;
77
78   // Interface to read/write data from a socket to ease unit-testing.
79   class Channel {
80    public:
81     virtual ~Channel() {}
82     virtual void Read(net::IOBuffer* buffer,
83                       int bytes,
84                       net::CompletionOnceCallback callback) = 0;
85     virtual void Write(net::IOBuffer* buffer,
86                        int bytes,
87                        net::CompletionOnceCallback callback) = 0;
88   };
89
90   // Adds a CastMessage read/write layer to a socket.
91   // Message read events are propagated to the owner via |read_delegate|.
92   // |vlog_prefix| sets the prefix used for all VLOGged output.
93   // |channel| and |logger| must all out-live the
94   // CastTransportImpl instance.
95   // |read_delegate| is owned by this CastTransportImpl object.
96   CastTransportImpl(Channel* channel,
97                     int channel_id,
98                     const net::IPEndPoint& ip_endpoint_,
99                     scoped_refptr<Logger> logger);
100
101   ~CastTransportImpl() override;
102
103   // CastTransport interface.
104   void SendMessage(const CastMessage& message,
105                    const net::CompletionCallback& callback) override;
106   void Start() override;
107   void SetReadDelegate(std::unique_ptr<Delegate> delegate) override;
108
109  private:
110   // Holds a message to be written to the socket. |callback| is invoked when the
111   // message is fully written or an error occurrs.
112   struct WriteRequest {
113     explicit WriteRequest(const std::string& namespace_,
114                           const std::string& payload,
115                           const net::CompletionCallback& callback);
116     WriteRequest(const WriteRequest& other);
117     ~WriteRequest();
118
119     // Namespace of the serialized message.
120     std::string message_namespace;
121     // Write completion callback, invoked when the operation has completed or
122     // failed.
123     net::CompletionCallback callback;
124     // Buffer with outgoing data.
125     scoped_refptr<net::DrainableIOBuffer> io_buffer;
126   };
127
128   static bool IsTerminalReadState(ReadState read_state);
129   static bool IsTerminalWriteState(WriteState write_state);
130
131   void SetReadState(ReadState read_state);
132   void SetWriteState(WriteState write_state);
133   void SetErrorState(ChannelError error_state);
134
135   // Terminates all in-flight write callbacks with error code ERR_FAILED.
136   void FlushWriteQueue();
137
138   // Main method that performs write flow state transitions.
139   void OnWriteResult(int result);
140
141   // Each of the below Do* method is executed in the corresponding
142   // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
143   // DowriteComplete is called, and so on.
144   int DoWrite();
145   int DoWriteComplete(int result);
146   int DoWriteCallback();
147   int DoWriteHandleError(int result);
148
149   // Main method that performs write flow state transitions.
150   void OnReadResult(int result);
151
152   // Each of the below Do* method is executed in the corresponding
153   // write state. For example when read state is READ_STATE_READ_COMPLETE
154   // DoReadComplete is called, and so on.
155   int DoRead();
156   int DoReadComplete(int result);
157   int DoReadCallback();
158   int DoReadHandleError(int result);
159
160   // Indicates that the transport object is started and may receive and send
161   // messages.
162   bool started_;
163
164   // Queue of pending writes. The message at the front of the queue is the one
165   // being written.
166   base::queue<WriteRequest> write_queue_;
167
168   // Buffer used for read operations. Reused for every read.
169   scoped_refptr<net::GrowableIOBuffer> read_buffer_;
170
171   // Constructs and parses the wire representation of message frames.
172   std::unique_ptr<MessageFramer> framer_;
173
174   // Last message received on the socket.
175   std::unique_ptr<CastMessage> current_message_;
176
177   // Channel used for I/O operations.
178   Channel* const channel_;
179
180   // Methods for communicating message receipt and error status to client code.
181   std::unique_ptr<Delegate> delegate_;
182
183   // Write flow state machine state.
184   WriteState write_state_;
185
186   // Read flow state machine state.
187   ReadState read_state_;
188
189   // The last error encountered by the channel.
190   ChannelError error_state_;
191
192   // Connection metadata for logging purposes.
193   // Socket ID assigned by ApiResourceManager.
194   int channel_id_;
195
196   // IP address of the remote end.
197   const net::IPEndPoint ip_endpoint_;
198
199   // Accumulates details of events and errors, for debugging purposes.
200   scoped_refptr<Logger> logger_;
201
202   SEQUENCE_CHECKER(sequence_checker_);
203
204   DISALLOW_COPY_AND_ASSIGN(CastTransportImpl);
205 };
206 }  // namespace cast_channel
207
208 #endif  // COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_