Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
| 6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 class TransportSecurityState; | 31 class TransportSecurityState; |
| 32 } | 32 } |
| 33 | 33 |
| 34 namespace extensions { | 34 namespace extensions { |
| 35 namespace core_api { | 35 namespace core_api { |
| 36 namespace cast_channel { | 36 namespace cast_channel { |
| 37 | 37 |
| 38 class CastMessage; | 38 class CastMessage; |
| 39 class Logger; | 39 class Logger; |
| 40 struct LastErrors; | 40 struct LastErrors; |
| 41 class PacketFramer; | |
| 41 | 42 |
| 42 // This class implements a channel between Chrome and a Cast device using a TCP | 43 // This class implements a channel between Chrome and a Cast device using a TCP |
| 43 // socket with SSL. The channel may authenticate that the receiver is a genuine | 44 // socket with SSL. The channel may authenticate that the receiver is a genuine |
| 44 // Cast device. All CastSocket objects must be used only on the IO thread. | 45 // Cast device. All CastSocket objects must be used only on the IO thread. |
| 45 // | 46 // |
| 46 // NOTE: Not called "CastChannel" to reduce confusion with the generated API | 47 // NOTE: Not called "CastChannel" to reduce confusion with the generated API |
| 47 // code. | 48 // code. |
| 48 class CastSocket : public ApiResource { | 49 class CastSocket : public ApiResource { |
| 49 public: | 50 public: |
| 50 // Object to be informed of incoming messages and errors. The CastSocket that | 51 // Object to be informed of incoming messages and errors. The CastSocket that |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 | 149 |
| 149 // Internal read states. | 150 // Internal read states. |
| 150 enum ReadState { | 151 enum ReadState { |
| 151 READ_STATE_NONE, | 152 READ_STATE_NONE, |
| 152 READ_STATE_READ, | 153 READ_STATE_READ, |
| 153 READ_STATE_READ_COMPLETE, | 154 READ_STATE_READ_COMPLETE, |
| 154 READ_STATE_DO_CALLBACK, | 155 READ_STATE_DO_CALLBACK, |
| 155 READ_STATE_ERROR, | 156 READ_STATE_ERROR, |
| 156 }; | 157 }; |
| 157 | 158 |
| 158 protected: | |
| 159 // Message header struct. If fields are added, be sure to update | |
| 160 // header_size(). Protected to allow use of *_size() methods in unit tests. | |
| 161 struct MessageHeader { | |
| 162 MessageHeader(); | |
| 163 // Sets the message size. | |
| 164 void SetMessageSize(size_t message_size); | |
| 165 // Prepends this header to |str|. | |
| 166 void PrependToString(std::string* str); | |
| 167 // Reads |header| from the beginning of |buffer|. | |
| 168 static void ReadFromIOBuffer(net::GrowableIOBuffer* buffer, | |
| 169 MessageHeader* header); | |
| 170 // Size (in bytes) of the message header. | |
| 171 static uint32 header_size() { return sizeof(uint32); } | |
| 172 | |
| 173 // Maximum size (in bytes) of a message payload on the wire (does not | |
| 174 // include header). | |
| 175 static uint32 max_message_size() { return 65536; } | |
| 176 | |
| 177 std::string ToString(); | |
| 178 // The size of the following protocol message in bytes, in host byte order. | |
| 179 uint32 message_size; | |
| 180 }; | |
| 181 | |
| 182 private: | 159 private: |
| 183 friend class ApiResourceManager<CastSocket>; | 160 friend class ApiResourceManager<CastSocket>; |
| 184 friend class CastSocketTest; | 161 friend class CastSocketTest; |
| 185 friend class TestCastSocket; | 162 friend class TestCastSocket; |
| 186 | 163 |
| 187 static const char* service_name() { return "CastSocketManager"; } | 164 static const char* service_name() { return "CastSocketManager"; } |
| 188 | 165 |
| 189 // Creates an instance of TCPClientSocket. | 166 // Creates an instance of TCPClientSocket. |
| 190 virtual scoped_ptr<net::TCPClientSocket> CreateTcpSocket(); | 167 virtual scoped_ptr<net::TCPClientSocket> CreateTcpSocket(); |
| 191 // Creates an instance of SSLClientSocket with the given underlying |socket|. | 168 // Creates an instance of SSLClientSocket with the given underlying |socket|. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 ///////////////////////////////////////////////////////////////////////////// | 236 ///////////////////////////////////////////////////////////////////////////// |
| 260 | 237 |
| 261 // Runs the external connection callback and resets it. | 238 // Runs the external connection callback and resets it. |
| 262 void DoConnectCallback(int result); | 239 void DoConnectCallback(int result); |
| 263 // Adds |message| to the write queue and starts the write loop if needed. | 240 // Adds |message| to the write queue and starts the write loop if needed. |
| 264 void SendCastMessageInternal(const CastMessage& message, | 241 void SendCastMessageInternal(const CastMessage& message, |
| 265 const net::CompletionCallback& callback); | 242 const net::CompletionCallback& callback); |
| 266 void PostTaskToStartConnectLoop(int result); | 243 void PostTaskToStartConnectLoop(int result); |
| 267 void PostTaskToStartReadLoop(); | 244 void PostTaskToStartReadLoop(); |
| 268 void StartReadLoop(); | 245 void StartReadLoop(); |
| 269 // Parses the contents of header_read_buffer_ and sets current_message_size_ | |
| 270 // to the size of the body of the message. | |
| 271 bool ProcessHeader(); | |
| 272 // Parses the contents of body_read_buffer_ and sets current_message_ to | |
| 273 // the message received. | |
| 274 bool ProcessBody(); | |
| 275 // Closes socket, signaling the delegate that |error| has occurred. | 246 // Closes socket, signaling the delegate that |error| has occurred. |
| 276 void CloseWithError(); | 247 void CloseWithError(); |
| 277 // Frees resources and cancels pending callbacks. |ready_state_| will be set | 248 // Frees resources and cancels pending callbacks. |ready_state_| will be set |
| 278 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already | 249 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already |
| 279 // READY_STATE_CLOSED. | 250 // READY_STATE_CLOSED. |
| 280 void CloseInternal(); | 251 void CloseInternal(); |
| 281 // Runs pending callbacks that are passed into us to notify API clients that | 252 // Runs pending callbacks that are passed into us to notify API clients that |
| 282 // pending operations will fail because the socket has been closed. | 253 // pending operations will fail because the socket has been closed. |
| 283 void RunPendingCallbacksOnClose(); | 254 void RunPendingCallbacksOnClose(); |
| 284 // Serializes the content of message_proto (with a header) to |message_data|. | 255 // Serializes the content of message_proto (with a header) to |message_data|. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 301 int channel_id_; | 272 int channel_id_; |
| 302 | 273 |
| 303 // The IP endpoint that the the channel is connected to. | 274 // The IP endpoint that the the channel is connected to. |
| 304 net::IPEndPoint ip_endpoint_; | 275 net::IPEndPoint ip_endpoint_; |
| 305 // Receiver authentication requested for the channel. | 276 // Receiver authentication requested for the channel. |
| 306 ChannelAuthType channel_auth_; | 277 ChannelAuthType channel_auth_; |
| 307 // Delegate to inform of incoming messages and errors. | 278 // Delegate to inform of incoming messages and errors. |
| 308 Delegate* delegate_; | 279 Delegate* delegate_; |
| 309 | 280 |
| 310 // IOBuffer for reading the message header. | 281 // IOBuffer for reading the message header. |
| 311 scoped_refptr<net::GrowableIOBuffer> header_read_buffer_; | 282 scoped_refptr<net::GrowableIOBuffer> read_buffer_; |
| 312 // IOBuffer for reading the message body. | 283 scoped_ptr<PacketFramer> framer_; |
| 313 scoped_refptr<net::GrowableIOBuffer> body_read_buffer_; | |
| 314 // IOBuffer to currently read into. | |
| 315 scoped_refptr<net::GrowableIOBuffer> current_read_buffer_; | |
| 316 // The number of bytes in the current message body. | |
| 317 uint32 current_message_size_; | |
| 318 // Last message received on the socket. | |
| 319 scoped_ptr<CastMessage> current_message_; | |
| 320 | 284 |
| 321 // The NetLog for this service. | 285 // The NetLog for this service. |
| 322 net::NetLog* net_log_; | 286 net::NetLog* net_log_; |
| 323 // The NetLog source for this service. | 287 // The NetLog source for this service. |
| 324 net::NetLog::Source net_log_source_; | 288 net::NetLog::Source net_log_source_; |
| 325 | 289 |
| 326 // Logger used to track multiple CastSockets. Does NOT own this object. | 290 // Logger used to track multiple CastSockets. Does NOT own this object. |
| 327 scoped_refptr<Logger> logger_; | 291 scoped_refptr<Logger> logger_; |
| 328 | 292 |
| 329 // CertVerifier is owned by us but should be deleted AFTER SSLClientSocket | 293 // CertVerifier is owned by us but should be deleted AFTER SSLClientSocket |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 348 // Callback invoked by |connect_timeout_timer_| to cancel the connection. | 312 // Callback invoked by |connect_timeout_timer_| to cancel the connection. |
| 349 base::CancelableClosure connect_timeout_callback_; | 313 base::CancelableClosure connect_timeout_callback_; |
| 350 // Duration to wait before timing out. | 314 // Duration to wait before timing out. |
| 351 base::TimeDelta connect_timeout_; | 315 base::TimeDelta connect_timeout_; |
| 352 // Timer invoked when the connection has timed out. | 316 // Timer invoked when the connection has timed out. |
| 353 scoped_ptr<base::Timer> connect_timeout_timer_; | 317 scoped_ptr<base::Timer> connect_timeout_timer_; |
| 354 // Set when a timeout is triggered and the connection process has | 318 // Set when a timeout is triggered and the connection process has |
| 355 // canceled. | 319 // canceled. |
| 356 bool is_canceled_; | 320 bool is_canceled_; |
| 357 | 321 |
| 322 scoped_ptr<CastMessage> current_message_; | |
| 323 | |
| 358 // Connection flow state machine state. | 324 // Connection flow state machine state. |
| 359 ConnectionState connect_state_; | 325 ConnectionState connect_state_; |
| 360 // Write flow state machine state. | 326 // Write flow state machine state. |
| 361 WriteState write_state_; | 327 WriteState write_state_; |
| 362 // Read flow state machine state. | 328 // Read flow state machine state. |
| 363 ReadState read_state_; | 329 ReadState read_state_; |
| 364 // The last error encountered by the channel. | 330 // The last error encountered by the channel. |
| 365 ChannelError error_state_; | 331 ChannelError error_state_; |
| 366 // The current status of the channel. | 332 // The current status of the channel. |
| 367 ReadyState ready_state_; | 333 ReadyState ready_state_; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 394 std::queue<WriteRequest> write_queue_; | 360 std::queue<WriteRequest> write_queue_; |
| 395 | 361 |
| 396 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync); | 362 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync); |
| 397 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); | 363 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); |
| 398 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError); | 364 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError); |
| 399 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); | 365 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); |
| 400 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); | 366 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); |
| 401 DISALLOW_COPY_AND_ASSIGN(CastSocket); | 367 DISALLOW_COPY_AND_ASSIGN(CastSocket); |
| 402 }; | 368 }; |
| 403 | 369 |
| 370 // Class for constructing and parsing CastMessage packet data. | |
| 371 class PacketFramer { | |
|
mark a. foltz
2014/08/26 20:37:03
nit: The underlying network protocol (i.e. TCP) pr
Kevin M
2014/08/27 01:14:03
Done.
| |
| 372 public: | |
| 373 explicit PacketFramer(scoped_refptr<net::GrowableIOBuffer> buffer); | |
|
mark a. foltz
2014/08/26 20:37:03
s/buffer/input_buffer/?
Kevin M
2014/08/27 01:14:03
Done.
| |
| 374 ~PacketFramer(); | |
| 375 | |
| 376 // The number of bytes to be read from a socket and provided via Ingest(...). | |
|
mark a. foltz
2014/08/26 20:37:02
nit: The framer should be independent of the mecha
Kevin M
2014/08/27 01:14:03
Done.
| |
| 377 size_t BytesRequested(); | |
| 378 | |
| 379 // Constructs a packet with the serialized form of |message_proto| in | |
| 380 // |message_data|. | |
| 381 // Returns true if the message was serialized successfully, false otherwise. | |
| 382 static bool Serialize(const CastMessage& message_proto, | |
|
mark a. foltz
2014/08/26 20:37:03
It seems like the caller would want to pass in a b
Kevin M
2014/08/27 01:14:04
Hmmm. My understanding is that this approach would
| |
| 383 std::string* message_data); | |
| 384 | |
| 385 // Collects data from an incoming data stream and assembles CastMessage | |
| 386 // objects. | |
|
mark a. foltz
2014/08/26 20:37:02
This documentation is a little confusing since it
Kevin M
2014/08/27 01:14:03
It LGTM, but I tweaked the wording a bit.
| |
| 387 // | |
| 388 // num_bytes: the number of bytes received by a read operation. | |
|
mark a. foltz
2014/08/26 20:37:03
The framer could keep track of the previous offset
Kevin M
2014/08/27 01:14:04
Socket reads don't not advance the offset; either
| |
| 389 // Value must be <= BytesRequested(). | |
|
mark a. foltz
2014/08/26 20:37:03
What happens if this isn't true?
Kevin M
2014/08/27 01:14:03
CHECK failure, because it upsets one of the assume
| |
| 390 // message: a pointer to the deserialized message object. | |
| 391 // message_length: size of the deserialized message object, in bytes. For | |
| 392 // logging purposes. | |
| 393 // error: the status of the ingest operation. | |
| 394 // return value: true if a CastMessage was fully parsed, false otherwise. | |
| 395 bool Ingest(uint32 num_bytes, | |
|
mark a. foltz
2014/08/26 20:37:03
Perhaps this would be
size_t ParseBytes(CastMes
mark a. foltz
2014/08/26 20:37:03
s/uint32/size_t/?
Kevin M
2014/08/27 01:14:04
How about this?
A size_t return value would still
Kevin M
2014/08/27 01:14:04
Done.
| |
| 396 CastMessage* message, | |
| 397 size_t* message_length, | |
| 398 ChannelError* error); | |
| 399 | |
| 400 // Message header struct. If fields are added, be sure to update | |
| 401 // header_size(). Protected to allow use of *_size() methods in unit tests. | |
| 402 // | |
| 403 // MessageHeader is public visiblefor testing purposes. | |
|
mark a. foltz
2014/08/26 20:37:02
Need space before 'for'
Kevin M
2014/08/27 01:14:04
Done.
| |
| 404 struct MessageHeader { | |
| 405 MessageHeader(); | |
| 406 // Sets the message size. | |
| 407 void SetMessageSize(size_t message_size); | |
| 408 // Prepends this header to |str|. | |
| 409 void PrependToString(std::string* str); | |
| 410 // Reads |header| from the bytes specified by |data|. | |
| 411 static void Deserialize(char* data, MessageHeader* header); | |
| 412 // Size (in bytes) of the message header. | |
| 413 static uint32 header_size(); | |
|
mark a. foltz
2014/08/26 20:37:02
Let's use size_t consistently in this class and do
Kevin M
2014/08/27 01:14:04
Done.
| |
| 414 // Maximum size (in bytes) of a message payload on the wire (does not | |
| 415 // include header). | |
| 416 static uint32 max_message_size(); | |
| 417 std::string ToString(); | |
| 418 // The size of the following protocol message in bytes, in host byte order. | |
| 419 uint32 message_size; | |
| 420 }; | |
| 421 | |
| 422 private: | |
| 423 enum PacketElement { HEADER, BODY }; | |
|
mark a. foltz
2014/08/26 20:37:03
MessageElement?
Kevin M
2014/08/27 01:14:03
Done.
| |
| 424 | |
| 425 // Prepares the framer for receiving the next packet. | |
|
mark a. foltz
2014/08/26 20:37:03
The framer doesn't receive packets directly?
Kevin M
2014/08/27 01:14:04
Done.
| |
| 426 void Reset(); | |
| 427 | |
| 428 PacketElement current_element_; | |
|
mark a. foltz
2014/08/26 20:37:03
Please add documentation for each of the class fie
Kevin M
2014/08/27 01:14:04
Done.
| |
| 429 size_t packet_bytes_read_; | |
|
mark a. foltz
2014/08/26 20:37:03
message_bytes_read_?
Kevin M
2014/08/27 01:14:04
Done.
| |
| 430 size_t message_size_; | |
| 431 scoped_refptr<net::GrowableIOBuffer> buffer_; | |
| 432 | |
| 433 DISALLOW_COPY_AND_ASSIGN(PacketFramer); | |
| 434 }; | |
| 404 } // namespace cast_channel | 435 } // namespace cast_channel |
| 405 } // namespace core_api | 436 } // namespace core_api |
| 406 } // namespace extensions | 437 } // namespace extensions |
| 407 | 438 |
| 408 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 439 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
| OLD | NEW |