blob: d9a6e719b912f63b4356fb3d42e4847da6d25d42 [file] [log] [blame]
[email protected]6a2c36772010-03-01 02:37:131// 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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]6a2c36772010-03-01 02:37:138
9#include <string>
10#include <vector>
11
[email protected]13c8a092010-07-29 06:15:4412#include "base/string16.h"
[email protected]b4384a7982010-03-17 07:29:5413#include "net/base/address_list.h"
[email protected]6a2c36772010-03-01 02:37:1314#include "net/base/completion_callback.h"
15#include "net/socket_stream/socket_stream_job.h"
16
17class GURL;
18
19namespace net {
20
[email protected]3985966a2010-06-01 03:11:1621class DrainableIOBuffer;
22class WebSocketFrameHandler;
[email protected]817fc502010-06-03 08:58:0823class WebSocketHandshakeRequestHandler;
24class WebSocketHandshakeResponseHandler;
[email protected]3985966a2010-06-01 03:11:1625
[email protected]6a2c36772010-03-01 02:37:1326// WebSocket protocol specific job on SocketStream.
27// It captures WebSocket handshake message and handles cookie operations.
[email protected]b4384a7982010-03-17 07:29:5428// Chrome security policy doesn't allow renderer process (except dev tools)
[email protected]6a2c36772010-03-01 02:37:1329// see HttpOnly cookies, so it injects cookie header in handshake request and
30// strips set-cookie headers in handshake response.
[email protected]6a2c36772010-03-01 02:37:1331// TODO(ukai): refactor websocket.cc to use this.
32class 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]3985966a2010-06-01 03:11:1639 CLOSING = 2,
40 CLOSED = 3,
[email protected]6a2c36772010-03-01 02:37:1341 };
[email protected]6a2c36772010-03-01 02:37:1342
43 explicit WebSocketJob(SocketStream::Delegate* delegate);
44
[email protected]4b3c95dd2011-01-07 23:02:1145 static void EnsureInit();
46
[email protected]b4384a7982010-03-17 07:29:5447 State state() const { return state_; }
[email protected]6a2c36772010-03-01 02:37:1348 virtual void Connect();
49 virtual bool SendData(const char* data, int len);
50 virtual void Close();
51 virtual void RestartWithAuth(
[email protected]13c8a092010-07-29 06:15:4452 const string16& username,
53 const string16& password);
[email protected]6a2c36772010-03-01 02:37:1354 virtual void DetachDelegate();
55
56 // SocketStream::Delegate methods.
[email protected]b4384a7982010-03-17 07:29:5457 virtual int OnStartOpenConnection(
58 SocketStream* socket, CompletionCallback* callback);
[email protected]6a2c36772010-03-01 02:37:1359 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]b4384a7982010-03-17 07:29:5472 friend class WebSocketThrottle;
[email protected]6a2c36772010-03-01 02:37:1373 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]b4384a7982010-03-17 07:29:5489 const AddressList& address_list() const;
90 void SetWaiting();
91 bool IsWaiting() const;
92 void Wakeup();
[email protected]d11a34ba92010-03-23 06:40:2093 void DoCallback();
[email protected]b4384a7982010-03-17 07:29:5494
[email protected]3985966a2010-06-01 03:11:1695 void SendPending();
96
[email protected]6a2c36772010-03-01 02:37:1397 SocketStream::Delegate* delegate_;
98 State state_;
[email protected]b4384a7982010-03-17 07:29:5499 bool waiting_;
100 AddressList addresses_;
101 CompletionCallback* callback_; // for throttling.
[email protected]6a2c36772010-03-01 02:37:13102
[email protected]817fc502010-06-03 08:58:08103 scoped_ptr<WebSocketHandshakeRequestHandler> handshake_request_;
104 scoped_ptr<WebSocketHandshakeResponseHandler> handshake_response_;
105
[email protected]6a2c36772010-03-01 02:37:13106 size_t handshake_request_sent_;
107
[email protected]6a2c36772010-03-01 02:37:13108 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]3985966a2010-06-01 03:11:16114 scoped_ptr<WebSocketFrameHandler> send_frame_handler_;
115 scoped_refptr<DrainableIOBuffer> current_buffer_;
116 scoped_ptr<WebSocketFrameHandler> receive_frame_handler_;
117
[email protected]6a2c36772010-03-01 02:37:13118 DISALLOW_COPY_AND_ASSIGN(WebSocketJob);
119};
120
121} // namespace
122
123#endif // NET_WEBSOCKETS_WEBSOCKET_JOB_H_