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 MessageFramer; | |
| 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<MessageFramer> 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 MessageFramer { | |
| 372 public: | |
| 373 explicit MessageFramer(scoped_refptr<net::GrowableIOBuffer> input_buffer); | |
|
mark a. foltz
2014/08/28 21:45:22
Document |input_buffer|.
Kevin M
2014/08/28 23:33:32
Done.
| |
| 374 ~MessageFramer(); | |
| 375 | |
| 376 // The number of bytes requested from the incoming data stream. | |
|
mark a. foltz
2014/08/28 21:45:22
The number of requested from |input_buffer| to com
Kevin M
2014/08/28 23:33:32
An improvement, thanks.
| |
| 377 size_t BytesRequested(); | |
| 378 | |
| 379 // Constructs a packet with the serialized form of |message_proto| in | |
|
mark a. foltz
2014/08/28 21:45:22
Shorten first sentence to "Serializes |message_pro
Kevin M
2014/08/28 23:33:32
Done.
| |
| 380 // |message_data|. | |
| 381 // Returns true if the message was serialized successfully, false otherwise. | |
| 382 static bool Serialize(const CastMessage& message_proto, | |
| 383 std::string* message_data); | |
| 384 | |
| 385 // Reads bytes from an incoming data buffer and returns CastMessage | |
|
mark a. foltz
2014/08/28 21:45:22
Reads bytes from |input_buffer| and returns a new
Kevin M
2014/08/28 23:33:32
Done.
| |
| 386 // objects once they are fully read. | |
| 387 // | |
| 388 // num_bytes: the number of bytes received by a read operation. | |
| 389 // Value must be <= BytesRequested(). | |
| 390 // message_length: size of the deserialized message object, in bytes. For | |
|
mark a. foltz
2014/08/28 21:45:22
What is *message_length set to if no message was p
mark a. foltz
2014/08/28 21:45:22
The usual format I have seen is to use pipes to de
mark a. foltz
2014/08/28 21:45:22
The size of the parsed CastMessage (if any) ...
I
Kevin M
2014/08/28 23:33:32
Done.
Kevin M
2014/08/28 23:33:32
Done.
Kevin M
2014/08/28 23:33:32
Zero, now. :)
| |
| 391 // logging purposes. | |
| 392 // error: the status of the ingest operation. | |
|
mark a. foltz
2014/08/28 21:45:22
What is this set to if no error occurred?
Kevin M
2014/08/28 23:33:32
Done.
| |
| 393 // return value: a pointer to a parsed CastMessage if a message was received | |
| 394 // in its entirety, NULL otherwise. | |
| 395 scoped_ptr<CastMessage> Ingest(size_t num_bytes, | |
| 396 size_t* message_length, | |
| 397 ChannelError* error); | |
| 398 | |
| 399 // Message header struct. If fields are added, be sure to update | |
| 400 // header_size(). Protected to allow use of *_size() methods in unit tests. | |
| 401 // | |
| 402 // MessageHeader is publicly visible for testing purposes. | |
| 403 struct MessageHeader { | |
| 404 MessageHeader(); | |
| 405 // Sets the message size. | |
| 406 void SetMessageSize(size_t message_size); | |
| 407 // Prepends this header to |str|. | |
| 408 void PrependToString(std::string* str); | |
| 409 // Reads |header| from the bytes specified by |data|. | |
| 410 static void Deserialize(char* data, MessageHeader* header); | |
| 411 // Size (in bytes) of the message header. | |
| 412 static size_t header_size(); | |
| 413 // Maximum size (in bytes) of a message payload on the wire (does not | |
| 414 // include header). | |
| 415 static size_t max_message_size(); | |
| 416 std::string ToString(); | |
| 417 // The size of the following protocol message in bytes, in host byte order. | |
| 418 size_t message_size; | |
| 419 }; | |
| 420 | |
| 421 private: | |
| 422 enum MessageElement { HEADER, BODY }; | |
| 423 | |
| 424 // Prepares the framer for ingesting a new message. | |
| 425 void Reset(); | |
| 426 | |
| 427 // The element of the message that will be read on the next call to Ingest(). | |
| 428 MessageElement current_element_; | |
| 429 | |
| 430 // Total size of the message, in bytes (head + body). | |
| 431 size_t packet_bytes_read_; | |
|
mark a. foltz
2014/08/28 21:45:22
message_size_?
Kevin M
2014/08/28 23:33:32
This is a count of the partial bytes received so f
| |
| 432 | |
| 433 // Size of the body alone, in bytes. | |
| 434 size_t body_size_; | |
| 435 | |
| 436 // Input buffer which carries message data read from the socket. | |
|
mark a. foltz
2014/08/28 21:45:22
You might mention that the caller of this class sh
Kevin M
2014/08/28 23:33:32
Done.
| |
| 437 scoped_refptr<net::GrowableIOBuffer> input_buffer_; | |
| 438 | |
| 439 DISALLOW_COPY_AND_ASSIGN(MessageFramer); | |
| 440 }; | |
| 404 } // namespace cast_channel | 441 } // namespace cast_channel |
| 405 } // namespace core_api | 442 } // namespace core_api |
| 406 } // namespace extensions | 443 } // namespace extensions |
| 407 | 444 |
| 408 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 445 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
| OLD | NEW |