[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 1 | // Copyright (c) 2010 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_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 12 | #include "base/string16.h" |
[email protected] | b4384a798 | 2010-03-17 07:29:54 | [diff] [blame] | 13 | #include "net/base/address_list.h" |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 14 | #include "net/base/completion_callback.h" |
| 15 | #include "net/socket_stream/socket_stream_job.h" |
| 16 | |
| 17 | class GURL; |
| 18 | |
| 19 | namespace net { |
| 20 | |
[email protected] | 3985966a | 2010-06-01 03:11:16 | [diff] [blame] | 21 | class DrainableIOBuffer; |
| 22 | class WebSocketFrameHandler; |
[email protected] | 817fc50 | 2010-06-03 08:58:08 | [diff] [blame] | 23 | class WebSocketHandshakeRequestHandler; |
| 24 | class WebSocketHandshakeResponseHandler; |
[email protected] | 3985966a | 2010-06-01 03:11:16 | [diff] [blame] | 25 | |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 26 | // WebSocket protocol specific job on SocketStream. |
| 27 | // It captures WebSocket handshake message and handles cookie operations. |
[email protected] | b4384a798 | 2010-03-17 07:29:54 | [diff] [blame] | 28 | // Chrome security policy doesn't allow renderer process (except dev tools) |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 29 | // see HttpOnly cookies, so it injects cookie header in handshake request and |
| 30 | // strips set-cookie headers in handshake response. |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 31 | // TODO(ukai): refactor websocket.cc to use this. |
| 32 | class WebSocketJob : public SocketStreamJob, public SocketStream::Delegate { |
| 33 | public: |
| 34 | // This is state of WebSocket, not SocketStream. |
| 35 | enum State { |
| 36 | INITIALIZED = -1, |
| 37 | CONNECTING = 0, |
| 38 | OPEN = 1, |
[email protected] | 3985966a | 2010-06-01 03:11:16 | [diff] [blame] | 39 | CLOSING = 2, |
| 40 | CLOSED = 3, |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 41 | }; |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 42 | |
| 43 | explicit WebSocketJob(SocketStream::Delegate* delegate); |
| 44 | |
[email protected] | 4b3c95dd | 2011-01-07 23:02:11 | [diff] [blame] | 45 | static void EnsureInit(); |
| 46 | |
[email protected] | b4384a798 | 2010-03-17 07:29:54 | [diff] [blame] | 47 | State state() const { return state_; } |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 48 | virtual void Connect(); |
| 49 | virtual bool SendData(const char* data, int len); |
| 50 | virtual void Close(); |
| 51 | virtual void RestartWithAuth( |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 52 | const string16& username, |
| 53 | const string16& password); |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 54 | virtual void DetachDelegate(); |
| 55 | |
| 56 | // SocketStream::Delegate methods. |
[email protected] | b4384a798 | 2010-03-17 07:29:54 | [diff] [blame] | 57 | virtual int OnStartOpenConnection( |
| 58 | SocketStream* socket, CompletionCallback* callback); |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 59 | virtual void OnConnected( |
| 60 | SocketStream* socket, int max_pending_send_allowed); |
| 61 | virtual void OnSentData( |
| 62 | SocketStream* socket, int amount_sent); |
| 63 | virtual void OnReceivedData( |
| 64 | SocketStream* socket, const char* data, int len); |
| 65 | virtual void OnClose(SocketStream* socket); |
| 66 | virtual void OnAuthRequired( |
| 67 | SocketStream* socket, AuthChallengeInfo* auth_info); |
| 68 | virtual void OnError( |
| 69 | const SocketStream* socket, int error); |
| 70 | |
| 71 | private: |
[email protected] | b4384a798 | 2010-03-17 07:29:54 | [diff] [blame] | 72 | friend class WebSocketThrottle; |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 73 | friend class WebSocketJobTest; |
| 74 | virtual ~WebSocketJob(); |
| 75 | |
| 76 | bool SendHandshakeRequest(const char* data, int len); |
| 77 | void AddCookieHeaderAndSend(); |
| 78 | void OnCanGetCookiesCompleted(int policy); |
| 79 | |
| 80 | void OnSentHandshakeRequest(SocketStream* socket, int amount_sent); |
| 81 | void OnReceivedHandshakeResponse( |
| 82 | SocketStream* socket, const char* data, int len); |
| 83 | void SaveCookiesAndNotifyHeaderComplete(); |
| 84 | void SaveNextCookie(); |
| 85 | void OnCanSetCookieCompleted(int policy); |
| 86 | |
| 87 | GURL GetURLForCookies() const; |
| 88 | |
[email protected] | b4384a798 | 2010-03-17 07:29:54 | [diff] [blame] | 89 | const AddressList& address_list() const; |
| 90 | void SetWaiting(); |
| 91 | bool IsWaiting() const; |
| 92 | void Wakeup(); |
[email protected] | d11a34ba9 | 2010-03-23 06:40:20 | [diff] [blame] | 93 | void DoCallback(); |
[email protected] | b4384a798 | 2010-03-17 07:29:54 | [diff] [blame] | 94 | |
[email protected] | 3985966a | 2010-06-01 03:11:16 | [diff] [blame] | 95 | void SendPending(); |
| 96 | |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 97 | SocketStream::Delegate* delegate_; |
| 98 | State state_; |
[email protected] | b4384a798 | 2010-03-17 07:29:54 | [diff] [blame] | 99 | bool waiting_; |
| 100 | AddressList addresses_; |
| 101 | CompletionCallback* callback_; // for throttling. |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 102 | |
[email protected] | 817fc50 | 2010-06-03 08:58:08 | [diff] [blame] | 103 | scoped_ptr<WebSocketHandshakeRequestHandler> handshake_request_; |
| 104 | scoped_ptr<WebSocketHandshakeResponseHandler> handshake_response_; |
| 105 | |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 106 | size_t handshake_request_sent_; |
| 107 | |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 108 | std::vector<std::string> response_cookies_; |
| 109 | size_t response_cookies_save_index_; |
| 110 | |
| 111 | CompletionCallbackImpl<WebSocketJob> can_get_cookies_callback_; |
| 112 | CompletionCallbackImpl<WebSocketJob> can_set_cookie_callback_; |
| 113 | |
[email protected] | 3985966a | 2010-06-01 03:11:16 | [diff] [blame] | 114 | scoped_ptr<WebSocketFrameHandler> send_frame_handler_; |
| 115 | scoped_refptr<DrainableIOBuffer> current_buffer_; |
| 116 | scoped_ptr<WebSocketFrameHandler> receive_frame_handler_; |
| 117 | |
[email protected] | 6a2c3677 | 2010-03-01 02:37:13 | [diff] [blame] | 118 | DISALLOW_COPY_AND_ASSIGN(WebSocketJob); |
| 119 | }; |
| 120 | |
| 121 | } // namespace |
| 122 | |
| 123 | #endif // NET_WEBSOCKETS_WEBSOCKET_JOB_H_ |