OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 // WebSocket live experiment task. |
| 6 // It will try the following scenario. |
| 7 // |
| 8 // - Fetch |http_url| within |url_fetch_deadline_ms| msec. |
| 9 // If failed, the task is aborted (no http reachability) |
| 10 // |
| 11 // - Connect to |url| with WebSocket protocol within |
| 12 // |websocket_onopen_deadline_ms| msec. |
| 13 // Checks WebSocket connection can be established. |
| 14 // |
| 15 // - Send |websocket_hello_message| on the WebSocket connection and |
| 16 // wait it from server within |websocket_hello_echoback_deadline_ms| msec. |
| 17 // Checks message can be sent/received on the WebSocket connection. |
| 18 // |
| 19 // - Keep connection idle at least |websocket_idle_ms| msec. |
| 20 // Checks WebSocket connection keep open in idle state. |
| 21 // |
| 22 // - Wait for some message from server within |
| 23 // |websocket_receive_push_message_deadline_ms| msec, and echo it back. |
| 24 // Checks server can push a message after connection has been idle. |
| 25 // |
| 26 // - Expect that |websocket_bye_message| message arrives within |
| 27 // |websocket_bye_deadline_ms| msec from server. |
| 28 // Checks previous message was sent to the server. |
| 29 // |
| 30 // - Close the connection and wait |websocket_close_deadline_ms| msec |
| 31 // for onclose. |
| 32 // Checks WebSocket connection can be closed normally. |
| 33 |
| 34 #ifndef CHROME_BROWSER_NET_WEBSOCKET_EXPERIMENT_WEBSOCKET_EXPERIMENT_TASK_H_ |
| 35 #define CHROME_BROWSER_NET_WEBSOCKET_EXPERIMENT_WEBSOCKET_EXPERIMENT_TASK_H_ |
| 36 |
| 37 #include <deque> |
| 38 #include <string> |
| 39 |
| 40 #include "base/basictypes.h" |
| 41 #include "base/task.h" |
| 42 #include "base/time.h" |
| 43 #include "chrome/browser/net/url_fetcher.h" |
| 44 #include "googleurl/src/gurl.h" |
| 45 #include "net/base/completion_callback.h" |
| 46 #include "net/base/net_errors.h" |
| 47 #include "net/websockets/websocket.h" |
| 48 |
| 49 class MessageLoop; |
| 50 |
| 51 namespace net { |
| 52 class WebSocket; |
| 53 } // namespace net |
| 54 |
| 55 namespace chrome_browser_net_websocket_experiment { |
| 56 |
| 57 class WebSocketExperimentTask : public URLFetcher::Delegate, |
| 58 public net::WebSocketDelegate { |
| 59 public: |
| 60 enum State { |
| 61 STATE_NONE, |
| 62 STATE_URL_FETCH, |
| 63 STATE_URL_FETCH_COMPLETE, |
| 64 STATE_WEBSOCKET_CONNECT, |
| 65 STATE_WEBSOCKET_CONNECT_COMPLETE, |
| 66 STATE_WEBSOCKET_SEND_HELLO, |
| 67 STATE_WEBSOCKET_RECV_HELLO, |
| 68 STATE_WEBSOCKET_KEEP_IDLE, |
| 69 STATE_WEBSOCKET_KEEP_IDLE_COMPLETE, |
| 70 STATE_WEBSOCKET_RECV_PUSH_MESSAGE, |
| 71 STATE_WEBSOCKET_ECHO_BACK_MESSAGE, |
| 72 STATE_WEBSOCKET_RECV_BYE, |
| 73 STATE_WEBSOCKET_CLOSE, |
| 74 STATE_WEBSOCKET_CLOSE_COMPLETE, |
| 75 }; |
| 76 class Config { |
| 77 public: |
| 78 Config() |
| 79 : url_fetch_deadline_ms(0), |
| 80 websocket_onopen_deadline_ms(0), |
| 81 websocket_hello_echoback_deadline_ms(0), |
| 82 websocket_idle_ms(0), |
| 83 websocket_receive_push_message_deadline_ms(0), |
| 84 websocket_bye_deadline_ms(0), |
| 85 websocket_close_deadline_ms(0) {} |
| 86 GURL url; |
| 87 std::string ws_protocol; |
| 88 std::string ws_origin; |
| 89 std::string ws_location; |
| 90 |
| 91 GURL http_url; |
| 92 |
| 93 int64 url_fetch_deadline_ms; |
| 94 int64 websocket_onopen_deadline_ms; |
| 95 std::string websocket_hello_message; |
| 96 int64 websocket_hello_echoback_deadline_ms; |
| 97 int64 websocket_idle_ms; |
| 98 int64 websocket_receive_push_message_deadline_ms; |
| 99 std::string websocket_bye_message; |
| 100 int64 websocket_bye_deadline_ms; |
| 101 int64 websocket_close_deadline_ms; |
| 102 }; |
| 103 class Context { |
| 104 public: |
| 105 Context(const Config& config, WebSocketExperimentTask* task) |
| 106 : config_(config), task_(task) {} |
| 107 virtual ~Context() {} |
| 108 |
| 109 virtual URLFetcher* CreateURLFetcher(); |
| 110 virtual net::WebSocket* CreateWebSocket(); |
| 111 |
| 112 protected: |
| 113 const Config& config_; |
| 114 WebSocketExperimentTask* task_; |
| 115 |
| 116 private: |
| 117 DISALLOW_COPY_AND_ASSIGN(Context); |
| 118 }; |
| 119 class Result { |
| 120 public: |
| 121 Result() |
| 122 : last_result(net::ERR_UNEXPECTED), |
| 123 last_state(STATE_NONE) {} |
| 124 int last_result; |
| 125 State last_state; |
| 126 |
| 127 base::TimeDelta url_fetch; |
| 128 base::TimeDelta websocket_connect; |
| 129 base::TimeDelta websocket_echo; |
| 130 base::TimeDelta websocket_idle; |
| 131 }; |
| 132 |
| 133 // WebSocketExperimentTask will call |callback| with the last status code |
| 134 // when the task is finished. |
| 135 WebSocketExperimentTask(const Config& config, |
| 136 net::CompletionCallback* callback); |
| 137 virtual ~WebSocketExperimentTask(); |
| 138 |
| 139 void Run(); |
| 140 |
| 141 const Result& GetResult() const { |
| 142 return result_; |
| 143 } |
| 144 |
| 145 // URLFetcher::Delegate method. |
| 146 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 147 const GURL& url, |
| 148 const URLRequestStatus& status, |
| 149 int response_code, |
| 150 const ResponseCookies& cookies, |
| 151 const std::string& data); |
| 152 |
| 153 // net::WebSocketDelegate methods |
| 154 virtual void OnOpen(net::WebSocket* websocket); |
| 155 virtual void OnMessage(net::WebSocket* websocket, const std::string& msg); |
| 156 virtual void OnClose(net::WebSocket* websocket); |
| 157 |
| 158 void SetContext(Context* context); |
| 159 |
| 160 private: |
| 161 void OnTimedOut(); |
| 162 |
| 163 void DoLoop(int result); |
| 164 |
| 165 int DoURLFetch(); |
| 166 int DoURLFetchComplete(int result); |
| 167 int DoWebSocketConnect(); |
| 168 int DoWebSocketConnectComplete(int result); |
| 169 int DoWebSocketSendHello(); |
| 170 int DoWebSocketReceiveHello(int result); |
| 171 int DoWebSocketKeepIdle(); |
| 172 int DoWebSocketKeepIdleComplete(int result); |
| 173 int DoWebSocketReceivePushMessage(int result); |
| 174 int DoWebSocketEchoBackMessage(); |
| 175 int DoWebSocketReceiveBye(int result); |
| 176 int DoWebSocketClose(); |
| 177 int DoWebSocketCloseComplete(int result); |
| 178 void SetTimeout(int64 deadline_ms); |
| 179 void RevokeTimeoutTimer(); |
| 180 void Finish(int result); |
| 181 |
| 182 Config config_; |
| 183 scoped_ptr<Context> context_; |
| 184 Result result_; |
| 185 |
| 186 MessageLoop* message_loop_; |
| 187 ScopedRunnableMethodFactory<WebSocketExperimentTask> method_factory_; |
| 188 net::CompletionCallback* callback_; |
| 189 State next_state_; |
| 190 |
| 191 scoped_ptr<URLFetcher> url_fetcher_; |
| 192 base::TimeTicks url_fetch_start_time_; |
| 193 |
| 194 scoped_refptr<net::WebSocket> websocket_; |
| 195 std::deque<std::string> received_messages_; |
| 196 std::string push_message_; |
| 197 base::TimeTicks websocket_connect_start_time_; |
| 198 base::TimeTicks websocket_echo_start_time_; |
| 199 base::TimeTicks websocket_idle_start_time_; |
| 200 |
| 201 DISALLOW_COPY_AND_ASSIGN(WebSocketExperimentTask); |
| 202 }; |
| 203 |
| 204 } // namespace chrome_browser_net |
| 205 |
| 206 #endif // CHROME_BROWSER_NET_WEBSOCKET_EXPERIMENT_WEBSOCKET_EXPERIMENT_TASK_H_ |
OLD | NEW |