David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 NET_WEBSOCKETS_WEBSOCKET_JOB_H_ |
| 6 | #define NET_WEBSOCKETS_WEBSOCKET_JOB_H_ |
| 7 | |
| 8 | #include <deque> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
David Ghandehari | 00be30f | 2017-04-27 21:30:48 -0700 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
David Ghandehari | 00be30f | 2017-04-27 21:30:48 -0700 | [diff] [blame] | 14 | #include "base/time.h" |
| 15 | #include "base/timer.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 16 | #include "net/base/address_list.h" |
| 17 | #include "net/base/completion_callback.h" |
| 18 | #include "net/socket_stream/socket_stream_job.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 19 | |
| 20 | class GURL; |
| 21 | |
| 22 | namespace net { |
| 23 | |
| 24 | class DrainableIOBuffer; |
| 25 | class SSLInfo; |
| 26 | class WebSocketHandshakeRequestHandler; |
| 27 | class WebSocketHandshakeResponseHandler; |
| 28 | |
| 29 | // WebSocket protocol specific job on SocketStream. |
| 30 | // It captures WebSocket handshake message and handles cookie operations. |
| 31 | // Chrome security policy doesn't allow renderer process (except dev tools) |
| 32 | // see HttpOnly cookies, so it injects cookie header in handshake request and |
| 33 | // strips set-cookie headers in handshake response. |
| 34 | // TODO(ukai): refactor websocket.cc to use this. |
| 35 | class NET_EXPORT WebSocketJob |
| 36 | : public SocketStreamJob, |
David Ghandehari | 175e9c8 | 2017-02-21 15:46:11 -0800 | [diff] [blame] | 37 | public SocketStream::Delegate { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 38 | public: |
| 39 | // This is state of WebSocket, not SocketStream. |
| 40 | enum State { |
| 41 | INITIALIZED = -1, |
| 42 | CONNECTING = 0, |
| 43 | OPEN = 1, |
David Ghandehari | 00be30f | 2017-04-27 21:30:48 -0700 | [diff] [blame] | 44 | SEND_CLOSED = 2, // A Close frame has been sent but not received. |
| 45 | RECV_CLOSED = 3, // Used briefly between receiving a Close frame and |
| 46 | // sending the response. Once the response is sent, the |
| 47 | // state changes to CLOSED. |
| 48 | CLOSE_WAIT = 4, // The Closing Handshake has completed, but the remote |
| 49 | // server has not yet closed the connection. |
| 50 | CLOSED = 5, // The Closing Handshake has completed and the connection |
| 51 | // has been closed; or the connection is failed. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | explicit WebSocketJob(SocketStream::Delegate* delegate); |
| 55 | |
| 56 | static void EnsureInit(); |
| 57 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 58 | State state() const { return state_; } |
| 59 | virtual void Connect() OVERRIDE; |
| 60 | virtual bool SendData(const char* data, int len) OVERRIDE; |
| 61 | virtual void Close() OVERRIDE; |
| 62 | virtual void RestartWithAuth(const AuthCredentials& credentials) OVERRIDE; |
| 63 | virtual void DetachDelegate() OVERRIDE; |
| 64 | |
| 65 | // SocketStream::Delegate methods. |
| 66 | virtual int OnStartOpenConnection( |
| 67 | SocketStream* socket, const CompletionCallback& callback) OVERRIDE; |
| 68 | virtual void OnConnected(SocketStream* socket, |
| 69 | int max_pending_send_allowed) OVERRIDE; |
| 70 | virtual void OnSentData(SocketStream* socket, int amount_sent) OVERRIDE; |
| 71 | virtual void OnReceivedData(SocketStream* socket, |
| 72 | const char* data, |
| 73 | int len) OVERRIDE; |
| 74 | virtual void OnClose(SocketStream* socket) OVERRIDE; |
| 75 | virtual void OnAuthRequired( |
| 76 | SocketStream* socket, AuthChallengeInfo* auth_info) OVERRIDE; |
| 77 | virtual void OnSSLCertificateError(SocketStream* socket, |
| 78 | const SSLInfo& ssl_info, |
| 79 | bool fatal) OVERRIDE; |
| 80 | virtual void OnError(const SocketStream* socket, int error) OVERRIDE; |
| 81 | |
David Ghandehari | 175e9c8 | 2017-02-21 15:46:11 -0800 | [diff] [blame] | 82 | WebSocketHandshakeResponseHandler* GetHandshakeResponse() { |
| 83 | return handshake_response_.get(); |
| 84 | } |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 85 | |
David Ghandehari | 00be30f | 2017-04-27 21:30:48 -0700 | [diff] [blame] | 86 | void SetState(const State new_state) { |
| 87 | DCHECK_GE(new_state, state_); // states should only move forward. |
| 88 | state_ = new_state; |
| 89 | } |
| 90 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 91 | private: |
| 92 | friend class WebSocketThrottle; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 93 | virtual ~WebSocketJob(); |
| 94 | |
| 95 | bool SendHandshakeRequest(const char* data, int len); |
| 96 | void AddCookieHeaderAndSend(); |
| 97 | void LoadCookieCallback(const std::string& cookie); |
| 98 | |
| 99 | void OnSentHandshakeRequest(SocketStream* socket, int amount_sent); |
| 100 | void OnReceivedHandshakeResponse( |
| 101 | SocketStream* socket, const char* data, int len); |
| 102 | void SaveCookiesAndNotifyHeaderComplete(); |
| 103 | void SaveNextCookie(); |
| 104 | void SaveCookieCallback(bool cookie_status); |
| 105 | void DoSendData(); |
| 106 | |
| 107 | GURL GetURLForCookies() const; |
| 108 | |
| 109 | const AddressList& address_list() const; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 110 | void SetWaiting(); |
| 111 | bool IsWaiting() const; |
| 112 | void Wakeup(); |
| 113 | void RetryPendingIO(); |
| 114 | void CompleteIO(int result); |
| 115 | |
| 116 | bool SendDataInternal(const char* data, int length); |
| 117 | void CloseInternal(); |
| 118 | void SendPending(); |
David Ghandehari | 00be30f | 2017-04-27 21:30:48 -0700 | [diff] [blame] | 119 | void CloseTimeout(); |
| 120 | void StopTimer(base::WaitableEvent* timer_stop_event); |
| 121 | |
| 122 | void WaitForSocketClose(); |
| 123 | void DelayedClose(); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 124 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 125 | SocketStream::Delegate* delegate_; |
| 126 | State state_; |
| 127 | bool waiting_; |
| 128 | AddressList addresses_; |
| 129 | CompletionCallback callback_; // for throttling. |
| 130 | |
| 131 | scoped_ptr<WebSocketHandshakeRequestHandler> handshake_request_; |
| 132 | scoped_ptr<WebSocketHandshakeResponseHandler> handshake_response_; |
| 133 | |
| 134 | bool started_to_send_handshake_request_; |
| 135 | size_t handshake_request_sent_; |
| 136 | |
| 137 | std::vector<std::string> response_cookies_; |
| 138 | size_t response_cookies_save_index_; |
| 139 | |
| 140 | std::deque<scoped_refptr<IOBufferWithSize> > send_buffer_queue_; |
| 141 | scoped_refptr<DrainableIOBuffer> current_send_buffer_; |
| 142 | std::vector<char> received_data_after_handshake_; |
| 143 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 144 | std::string challenge_; |
| 145 | |
David Ghandehari | 00be30f | 2017-04-27 21:30:48 -0700 | [diff] [blame] | 146 | base::TimeDelta closing_handshake_timeout_; |
| 147 | scoped_ptr<base::OneShotTimer<WebSocketJob> > close_timer_; |
| 148 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 149 | base::WeakPtrFactory<WebSocketJob> weak_ptr_factory_; |
| 150 | base::WeakPtrFactory<WebSocketJob> weak_ptr_factory_for_send_pending_; |
| 151 | |
| 152 | DISALLOW_COPY_AND_ASSIGN(WebSocketJob); |
| 153 | }; |
| 154 | |
| 155 | } // namespace |
| 156 | |
| 157 | #endif // NET_WEBSOCKETS_WEBSOCKET_JOB_H_ |