| OLD | NEW |
| (Empty) |
| 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 | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "net/base/address_list.h" | |
| 14 #include "net/base/completion_callback.h" | |
| 15 #include "net/socket_stream/socket_stream_job.h" | |
| 16 #include "net/spdy/spdy_header_block.h" | |
| 17 #include "net/spdy/spdy_websocket_stream.h" | |
| 18 | |
| 19 class GURL; | |
| 20 | |
| 21 namespace net { | |
| 22 | |
| 23 class DrainableIOBuffer; | |
| 24 class SSLInfo; | |
| 25 class WebSocketHandshakeRequestHandler; | |
| 26 class WebSocketHandshakeResponseHandler; | |
| 27 | |
| 28 // WebSocket protocol specific job on SocketStream. | |
| 29 // It captures WebSocket handshake message and handles cookie operations. | |
| 30 // Chrome security policy doesn't allow renderer process (except dev tools) | |
| 31 // see HttpOnly cookies, so it injects cookie header in handshake request and | |
| 32 // strips set-cookie headers in handshake response. | |
| 33 // TODO(ukai): refactor websocket.cc to use this. | |
| 34 class NET_EXPORT WebSocketJob | |
| 35 : public SocketStreamJob, | |
| 36 public SocketStream::Delegate, | |
| 37 public SpdyWebSocketStream::Delegate { | |
| 38 public: | |
| 39 // This is state of WebSocket, not SocketStream. | |
| 40 enum State { | |
| 41 INITIALIZED = -1, | |
| 42 CONNECTING = 0, | |
| 43 OPEN = 1, | |
| 44 CLOSING = 2, | |
| 45 CLOSED = 3, | |
| 46 }; | |
| 47 | |
| 48 explicit WebSocketJob(SocketStream::Delegate* delegate); | |
| 49 | |
| 50 static void EnsureInit(); | |
| 51 | |
| 52 State state() const { return state_; } | |
| 53 void Connect() override; | |
| 54 bool SendData(const char* data, int len) override; | |
| 55 void Close() override; | |
| 56 void RestartWithAuth(const AuthCredentials& credentials) override; | |
| 57 void DetachDelegate() override; | |
| 58 | |
| 59 // SocketStream::Delegate methods. | |
| 60 int OnStartOpenConnection(SocketStream* socket, | |
| 61 const CompletionCallback& callback) override; | |
| 62 void OnConnected(SocketStream* socket, int max_pending_send_allowed) override; | |
| 63 void OnSentData(SocketStream* socket, int amount_sent) override; | |
| 64 void OnReceivedData(SocketStream* socket, const char* data, int len) override; | |
| 65 void OnClose(SocketStream* socket) override; | |
| 66 void OnAuthRequired(SocketStream* socket, | |
| 67 AuthChallengeInfo* auth_info) override; | |
| 68 void OnSSLCertificateError(SocketStream* socket, | |
| 69 const SSLInfo& ssl_info, | |
| 70 bool fatal) override; | |
| 71 void OnError(const SocketStream* socket, int error) override; | |
| 72 | |
| 73 // SpdyWebSocketStream::Delegate methods. | |
| 74 void OnCreatedSpdyStream(int status) override; | |
| 75 void OnSentSpdyHeaders() override; | |
| 76 void OnSpdyResponseHeadersUpdated( | |
| 77 const SpdyHeaderBlock& response_headers) override; | |
| 78 void OnSentSpdyData(size_t bytes_sent) override; | |
| 79 void OnReceivedSpdyData(scoped_ptr<SpdyBuffer> buffer) override; | |
| 80 void OnCloseSpdyStream() override; | |
| 81 | |
| 82 private: | |
| 83 friend class WebSocketThrottle; | |
| 84 friend class WebSocketJobTest; | |
| 85 ~WebSocketJob() override; | |
| 86 | |
| 87 bool SendHandshakeRequest(const char* data, int len); | |
| 88 void AddCookieHeaderAndSend(); | |
| 89 void LoadCookieCallback(const std::string& cookie); | |
| 90 | |
| 91 void OnSentHandshakeRequest(SocketStream* socket, int amount_sent); | |
| 92 // Parses received data into handshake_response_. When finished receiving the | |
| 93 // response, calls SaveCookiesAndNotifyHeadersComplete(). | |
| 94 void OnReceivedHandshakeResponse( | |
| 95 SocketStream* socket, const char* data, int len); | |
| 96 // Saves received cookies to the cookie store, and then notifies the | |
| 97 // delegate_ of completion of handshake. | |
| 98 void SaveCookiesAndNotifyHeadersComplete(); | |
| 99 void SaveNextCookie(); | |
| 100 void OnCookieSaved(bool cookie_status); | |
| 101 // Clears variables for handling cookies, rebuilds handshake string excluding | |
| 102 // cookies, and then pass the handshake string to delegate_. | |
| 103 void NotifyHeadersComplete(); | |
| 104 void DoSendData(); | |
| 105 | |
| 106 GURL GetURLForCookies() const; | |
| 107 | |
| 108 const AddressList& address_list() const; | |
| 109 int TrySpdyStream(); | |
| 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(); | |
| 119 | |
| 120 SocketStream::Delegate* delegate_; | |
| 121 State state_; | |
| 122 bool waiting_; | |
| 123 AddressList addresses_; | |
| 124 CompletionCallback callback_; // for throttling. | |
| 125 | |
| 126 scoped_ptr<WebSocketHandshakeRequestHandler> handshake_request_; | |
| 127 scoped_ptr<WebSocketHandshakeResponseHandler> handshake_response_; | |
| 128 | |
| 129 bool started_to_send_handshake_request_; | |
| 130 size_t handshake_request_sent_; | |
| 131 | |
| 132 std::vector<std::string> response_cookies_; | |
| 133 size_t response_cookies_save_index_; | |
| 134 | |
| 135 std::deque<scoped_refptr<IOBufferWithSize> > send_buffer_queue_; | |
| 136 scoped_refptr<DrainableIOBuffer> current_send_buffer_; | |
| 137 std::vector<char> received_data_after_handshake_; | |
| 138 | |
| 139 int spdy_protocol_version_; | |
| 140 scoped_ptr<SpdyWebSocketStream> spdy_websocket_stream_; | |
| 141 std::string challenge_; | |
| 142 | |
| 143 bool save_next_cookie_running_; | |
| 144 bool callback_pending_; | |
| 145 | |
| 146 base::WeakPtrFactory<WebSocketJob> weak_ptr_factory_; | |
| 147 base::WeakPtrFactory<WebSocketJob> weak_ptr_factory_for_send_pending_; | |
| 148 | |
| 149 DISALLOW_COPY_AND_ASSIGN(WebSocketJob); | |
| 150 }; | |
| 151 | |
| 152 } // namespace | |
| 153 | |
| 154 #endif // NET_WEBSOCKETS_WEBSOCKET_JOB_H_ | |
| OLD | NEW |