| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/websockets/websocket_job.h" | 5 #include "net/websockets/websocket_job.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 namespace net { | 38 namespace net { |
| 39 | 39 |
| 40 namespace { | 40 namespace { |
| 41 | 41 |
| 42 class MockSocketStream : public SocketStream { | 42 class MockSocketStream : public SocketStream { |
| 43 public: | 43 public: |
| 44 MockSocketStream(const GURL& url, SocketStream::Delegate* delegate, | 44 MockSocketStream(const GURL& url, SocketStream::Delegate* delegate, |
| 45 URLRequestContext* context, CookieStore* cookie_store) | 45 URLRequestContext* context, CookieStore* cookie_store) |
| 46 : SocketStream(url, delegate, context, cookie_store) {} | 46 : SocketStream(url, delegate, context, cookie_store) {} |
| 47 | 47 |
| 48 virtual void Connect() override {} | 48 void Connect() override {} |
| 49 virtual bool SendData(const char* data, int len) override { | 49 bool SendData(const char* data, int len) override { |
| 50 sent_data_ += std::string(data, len); | 50 sent_data_ += std::string(data, len); |
| 51 return true; | 51 return true; |
| 52 } | 52 } |
| 53 | 53 |
| 54 virtual void Close() override {} | 54 void Close() override {} |
| 55 virtual void RestartWithAuth( | 55 void RestartWithAuth(const AuthCredentials& credentials) override {} |
| 56 const AuthCredentials& credentials) override { | |
| 57 } | |
| 58 | 56 |
| 59 virtual void DetachDelegate() override { | 57 void DetachDelegate() override { delegate_ = NULL; } |
| 60 delegate_ = NULL; | |
| 61 } | |
| 62 | 58 |
| 63 const std::string& sent_data() const { | 59 const std::string& sent_data() const { |
| 64 return sent_data_; | 60 return sent_data_; |
| 65 } | 61 } |
| 66 | 62 |
| 67 protected: | 63 protected: |
| 68 virtual ~MockSocketStream() {} | 64 ~MockSocketStream() override {} |
| 69 | 65 |
| 70 private: | 66 private: |
| 71 std::string sent_data_; | 67 std::string sent_data_; |
| 72 }; | 68 }; |
| 73 | 69 |
| 74 class MockSocketStreamDelegate : public SocketStream::Delegate { | 70 class MockSocketStreamDelegate : public SocketStream::Delegate { |
| 75 public: | 71 public: |
| 76 MockSocketStreamDelegate() | 72 MockSocketStreamDelegate() |
| 77 : amount_sent_(0), allow_all_cookies_(true) {} | 73 : amount_sent_(0), allow_all_cookies_(true) {} |
| 78 void set_allow_all_cookies(bool allow_all_cookies) { | 74 void set_allow_all_cookies(bool allow_all_cookies) { |
| 79 allow_all_cookies_ = allow_all_cookies; | 75 allow_all_cookies_ = allow_all_cookies; |
| 80 } | 76 } |
| 81 virtual ~MockSocketStreamDelegate() {} | 77 ~MockSocketStreamDelegate() override {} |
| 82 | 78 |
| 83 void SetOnStartOpenConnection(const base::Closure& callback) { | 79 void SetOnStartOpenConnection(const base::Closure& callback) { |
| 84 on_start_open_connection_ = callback; | 80 on_start_open_connection_ = callback; |
| 85 } | 81 } |
| 86 void SetOnConnected(const base::Closure& callback) { | 82 void SetOnConnected(const base::Closure& callback) { |
| 87 on_connected_ = callback; | 83 on_connected_ = callback; |
| 88 } | 84 } |
| 89 void SetOnSentData(const base::Closure& callback) { | 85 void SetOnSentData(const base::Closure& callback) { |
| 90 on_sent_data_ = callback; | 86 on_sent_data_ = callback; |
| 91 } | 87 } |
| 92 void SetOnReceivedData(const base::Closure& callback) { | 88 void SetOnReceivedData(const base::Closure& callback) { |
| 93 on_received_data_ = callback; | 89 on_received_data_ = callback; |
| 94 } | 90 } |
| 95 void SetOnClose(const base::Closure& callback) { | 91 void SetOnClose(const base::Closure& callback) { |
| 96 on_close_ = callback; | 92 on_close_ = callback; |
| 97 } | 93 } |
| 98 | 94 |
| 99 virtual int OnStartOpenConnection( | 95 int OnStartOpenConnection(SocketStream* socket, |
| 100 SocketStream* socket, | 96 const CompletionCallback& callback) override { |
| 101 const CompletionCallback& callback) override { | |
| 102 if (!on_start_open_connection_.is_null()) | 97 if (!on_start_open_connection_.is_null()) |
| 103 on_start_open_connection_.Run(); | 98 on_start_open_connection_.Run(); |
| 104 return OK; | 99 return OK; |
| 105 } | 100 } |
| 106 virtual void OnConnected(SocketStream* socket, | 101 void OnConnected(SocketStream* socket, |
| 107 int max_pending_send_allowed) override { | 102 int max_pending_send_allowed) override { |
| 108 if (!on_connected_.is_null()) | 103 if (!on_connected_.is_null()) |
| 109 on_connected_.Run(); | 104 on_connected_.Run(); |
| 110 } | 105 } |
| 111 virtual void OnSentData(SocketStream* socket, | 106 void OnSentData(SocketStream* socket, int amount_sent) override { |
| 112 int amount_sent) override { | |
| 113 amount_sent_ += amount_sent; | 107 amount_sent_ += amount_sent; |
| 114 if (!on_sent_data_.is_null()) | 108 if (!on_sent_data_.is_null()) |
| 115 on_sent_data_.Run(); | 109 on_sent_data_.Run(); |
| 116 } | 110 } |
| 117 virtual void OnReceivedData(SocketStream* socket, | 111 void OnReceivedData(SocketStream* socket, |
| 118 const char* data, int len) override { | 112 const char* data, |
| 113 int len) override { |
| 119 received_data_ += std::string(data, len); | 114 received_data_ += std::string(data, len); |
| 120 if (!on_received_data_.is_null()) | 115 if (!on_received_data_.is_null()) |
| 121 on_received_data_.Run(); | 116 on_received_data_.Run(); |
| 122 } | 117 } |
| 123 virtual void OnClose(SocketStream* socket) override { | 118 void OnClose(SocketStream* socket) override { |
| 124 if (!on_close_.is_null()) | 119 if (!on_close_.is_null()) |
| 125 on_close_.Run(); | 120 on_close_.Run(); |
| 126 } | 121 } |
| 127 virtual bool CanGetCookies(SocketStream* socket, | 122 bool CanGetCookies(SocketStream* socket, const GURL& url) override { |
| 128 const GURL& url) override { | |
| 129 return allow_all_cookies_; | 123 return allow_all_cookies_; |
| 130 } | 124 } |
| 131 virtual bool CanSetCookie(SocketStream* request, | 125 bool CanSetCookie(SocketStream* request, |
| 132 const GURL& url, | 126 const GURL& url, |
| 133 const std::string& cookie_line, | 127 const std::string& cookie_line, |
| 134 CookieOptions* options) override { | 128 CookieOptions* options) override { |
| 135 return allow_all_cookies_; | 129 return allow_all_cookies_; |
| 136 } | 130 } |
| 137 | 131 |
| 138 size_t amount_sent() const { return amount_sent_; } | 132 size_t amount_sent() const { return amount_sent_; } |
| 139 const std::string& received_data() const { return received_data_; } | 133 const std::string& received_data() const { return received_data_; } |
| 140 | 134 |
| 141 private: | 135 private: |
| 142 int amount_sent_; | 136 int amount_sent_; |
| 143 bool allow_all_cookies_; | 137 bool allow_all_cookies_; |
| 144 std::string received_data_; | 138 std::string received_data_; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 if (!result.empty()) { | 173 if (!result.empty()) { |
| 180 result += "; "; | 174 result += "; "; |
| 181 } | 175 } |
| 182 result += entry.cookie_line; | 176 result += entry.cookie_line; |
| 183 } | 177 } |
| 184 } | 178 } |
| 185 return result; | 179 return result; |
| 186 } | 180 } |
| 187 | 181 |
| 188 // CookieStore: | 182 // CookieStore: |
| 189 virtual void SetCookieWithOptionsAsync( | 183 void SetCookieWithOptionsAsync(const GURL& url, |
| 190 const GURL& url, | 184 const std::string& cookie_line, |
| 191 const std::string& cookie_line, | 185 const CookieOptions& options, |
| 192 const CookieOptions& options, | 186 const SetCookiesCallback& callback) override { |
| 193 const SetCookiesCallback& callback) override { | |
| 194 bool result = SetCookieWithOptions(url, cookie_line, options); | 187 bool result = SetCookieWithOptions(url, cookie_line, options); |
| 195 if (!callback.is_null()) | 188 if (!callback.is_null()) |
| 196 callback.Run(result); | 189 callback.Run(result); |
| 197 } | 190 } |
| 198 | 191 |
| 199 virtual void GetCookiesWithOptionsAsync( | 192 void GetCookiesWithOptionsAsync(const GURL& url, |
| 200 const GURL& url, | 193 const CookieOptions& options, |
| 201 const CookieOptions& options, | 194 const GetCookiesCallback& callback) override { |
| 202 const GetCookiesCallback& callback) override { | |
| 203 if (!callback.is_null()) | 195 if (!callback.is_null()) |
| 204 callback.Run(GetCookiesWithOptions(url, options)); | 196 callback.Run(GetCookiesWithOptions(url, options)); |
| 205 } | 197 } |
| 206 | 198 |
| 207 virtual void GetAllCookiesForURLAsync( | 199 void GetAllCookiesForURLAsync( |
| 208 const GURL& url, | 200 const GURL& url, |
| 209 const GetCookieListCallback& callback) override { | 201 const GetCookieListCallback& callback) override { |
| 210 ADD_FAILURE(); | 202 ADD_FAILURE(); |
| 211 } | 203 } |
| 212 | 204 |
| 213 virtual void DeleteCookieAsync(const GURL& url, | 205 void DeleteCookieAsync(const GURL& url, |
| 214 const std::string& cookie_name, | 206 const std::string& cookie_name, |
| 215 const base::Closure& callback) override { | 207 const base::Closure& callback) override { |
| 216 ADD_FAILURE(); | 208 ADD_FAILURE(); |
| 217 } | 209 } |
| 218 | 210 |
| 219 virtual void DeleteAllCreatedBetweenAsync( | 211 void DeleteAllCreatedBetweenAsync(const base::Time& delete_begin, |
| 220 const base::Time& delete_begin, | 212 const base::Time& delete_end, |
| 221 const base::Time& delete_end, | 213 const DeleteCallback& callback) override { |
| 222 const DeleteCallback& callback) override { | |
| 223 ADD_FAILURE(); | 214 ADD_FAILURE(); |
| 224 } | 215 } |
| 225 | 216 |
| 226 virtual void DeleteAllCreatedBetweenForHostAsync( | 217 void DeleteAllCreatedBetweenForHostAsync( |
| 227 const base::Time delete_begin, | 218 const base::Time delete_begin, |
| 228 const base::Time delete_end, | 219 const base::Time delete_end, |
| 229 const GURL& url, | 220 const GURL& url, |
| 230 const DeleteCallback& callback) override { | 221 const DeleteCallback& callback) override { |
| 231 ADD_FAILURE(); | 222 ADD_FAILURE(); |
| 232 } | 223 } |
| 233 | 224 |
| 234 virtual void DeleteSessionCookiesAsync(const DeleteCallback&) override { | 225 void DeleteSessionCookiesAsync(const DeleteCallback&) override { |
| 235 ADD_FAILURE(); | 226 ADD_FAILURE(); |
| 236 } | 227 } |
| 237 | 228 |
| 238 virtual CookieMonster* GetCookieMonster() override { return NULL; } | 229 CookieMonster* GetCookieMonster() override { return NULL; } |
| 239 | 230 |
| 240 const std::vector<Entry>& entries() const { return entries_; } | 231 const std::vector<Entry>& entries() const { return entries_; } |
| 241 | 232 |
| 242 private: | 233 private: |
| 243 friend class base::RefCountedThreadSafe<MockCookieStore>; | 234 friend class base::RefCountedThreadSafe<MockCookieStore>; |
| 244 virtual ~MockCookieStore() {} | 235 ~MockCookieStore() override {} |
| 245 | 236 |
| 246 std::vector<Entry> entries_; | 237 std::vector<Entry> entries_; |
| 247 }; | 238 }; |
| 248 | 239 |
| 249 class MockSSLConfigService : public SSLConfigService { | 240 class MockSSLConfigService : public SSLConfigService { |
| 250 public: | 241 public: |
| 251 virtual void GetSSLConfig(SSLConfig* config) override {} | 242 void GetSSLConfig(SSLConfig* config) override {} |
| 252 | 243 |
| 253 protected: | 244 protected: |
| 254 virtual ~MockSSLConfigService() {} | 245 ~MockSSLConfigService() override {} |
| 255 }; | 246 }; |
| 256 | 247 |
| 257 class MockURLRequestContext : public URLRequestContext { | 248 class MockURLRequestContext : public URLRequestContext { |
| 258 public: | 249 public: |
| 259 explicit MockURLRequestContext(CookieStore* cookie_store) | 250 explicit MockURLRequestContext(CookieStore* cookie_store) |
| 260 : transport_security_state_() { | 251 : transport_security_state_() { |
| 261 set_cookie_store(cookie_store); | 252 set_cookie_store(cookie_store); |
| 262 set_transport_security_state(&transport_security_state_); | 253 set_transport_security_state(&transport_security_state_); |
| 263 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); | 254 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 264 bool include_subdomains = false; | 255 bool include_subdomains = false; |
| 265 transport_security_state_.AddHSTS("upgrademe.com", expiry, | 256 transport_security_state_.AddHSTS("upgrademe.com", expiry, |
| 266 include_subdomains); | 257 include_subdomains); |
| 267 } | 258 } |
| 268 | 259 |
| 269 virtual ~MockURLRequestContext() { | 260 ~MockURLRequestContext() override { AssertNoURLRequests(); } |
| 270 AssertNoURLRequests(); | |
| 271 } | |
| 272 | 261 |
| 273 private: | 262 private: |
| 274 TransportSecurityState transport_security_state_; | 263 TransportSecurityState transport_security_state_; |
| 275 }; | 264 }; |
| 276 | 265 |
| 277 class MockHttpTransactionFactory : public HttpTransactionFactory { | 266 class MockHttpTransactionFactory : public HttpTransactionFactory { |
| 278 public: | 267 public: |
| 279 MockHttpTransactionFactory(NextProto next_proto, | 268 MockHttpTransactionFactory(NextProto next_proto, |
| 280 OrderedSocketData* data, | 269 OrderedSocketData* data, |
| 281 bool enable_websocket_over_spdy) { | 270 bool enable_websocket_over_spdy) { |
| 282 data_ = data; | 271 data_ = data; |
| 283 MockConnect connect_data(SYNCHRONOUS, OK); | 272 MockConnect connect_data(SYNCHRONOUS, OK); |
| 284 data_->set_connect_data(connect_data); | 273 data_->set_connect_data(connect_data); |
| 285 session_deps_.reset(new SpdySessionDependencies(next_proto)); | 274 session_deps_.reset(new SpdySessionDependencies(next_proto)); |
| 286 session_deps_->enable_websocket_over_spdy = enable_websocket_over_spdy; | 275 session_deps_->enable_websocket_over_spdy = enable_websocket_over_spdy; |
| 287 session_deps_->socket_factory->AddSocketDataProvider(data_); | 276 session_deps_->socket_factory->AddSocketDataProvider(data_); |
| 288 http_session_ = | 277 http_session_ = |
| 289 SpdySessionDependencies::SpdyCreateSession(session_deps_.get()); | 278 SpdySessionDependencies::SpdyCreateSession(session_deps_.get()); |
| 290 host_port_pair_.set_host("example.com"); | 279 host_port_pair_.set_host("example.com"); |
| 291 host_port_pair_.set_port(80); | 280 host_port_pair_.set_port(80); |
| 292 spdy_session_key_ = SpdySessionKey(host_port_pair_, | 281 spdy_session_key_ = SpdySessionKey(host_port_pair_, |
| 293 ProxyServer::Direct(), | 282 ProxyServer::Direct(), |
| 294 PRIVACY_MODE_DISABLED); | 283 PRIVACY_MODE_DISABLED); |
| 295 session_ = CreateInsecureSpdySession( | 284 session_ = CreateInsecureSpdySession( |
| 296 http_session_, spdy_session_key_, BoundNetLog()); | 285 http_session_, spdy_session_key_, BoundNetLog()); |
| 297 } | 286 } |
| 298 | 287 |
| 299 virtual int CreateTransaction( | 288 int CreateTransaction(RequestPriority priority, |
| 300 RequestPriority priority, | 289 scoped_ptr<HttpTransaction>* trans) override { |
| 301 scoped_ptr<HttpTransaction>* trans) override { | |
| 302 NOTREACHED(); | 290 NOTREACHED(); |
| 303 return ERR_UNEXPECTED; | 291 return ERR_UNEXPECTED; |
| 304 } | 292 } |
| 305 | 293 |
| 306 virtual HttpCache* GetCache() override { | 294 HttpCache* GetCache() override { |
| 307 NOTREACHED(); | 295 NOTREACHED(); |
| 308 return NULL; | 296 return NULL; |
| 309 } | 297 } |
| 310 | 298 |
| 311 virtual HttpNetworkSession* GetSession() override { | 299 HttpNetworkSession* GetSession() override { return http_session_.get(); } |
| 312 return http_session_.get(); | |
| 313 } | |
| 314 | 300 |
| 315 private: | 301 private: |
| 316 OrderedSocketData* data_; | 302 OrderedSocketData* data_; |
| 317 scoped_ptr<SpdySessionDependencies> session_deps_; | 303 scoped_ptr<SpdySessionDependencies> session_deps_; |
| 318 scoped_refptr<HttpNetworkSession> http_session_; | 304 scoped_refptr<HttpNetworkSession> http_session_; |
| 319 base::WeakPtr<SpdySession> session_; | 305 base::WeakPtr<SpdySession> session_; |
| 320 HostPortPair host_port_pair_; | 306 HostPortPair host_port_pair_; |
| 321 SpdySessionKey spdy_session_key_; | 307 SpdySessionKey spdy_session_key_; |
| 322 }; | 308 }; |
| 323 | 309 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 342 job_->DetachContext(); | 328 job_->DetachContext(); |
| 343 job_->DetachDelegate(); | 329 job_->DetachDelegate(); |
| 344 job_ = NULL; | 330 job_ = NULL; |
| 345 } | 331 } |
| 346 } | 332 } |
| 347 | 333 |
| 348 // SocketStream::Delegate implementation | 334 // SocketStream::Delegate implementation |
| 349 | 335 |
| 350 // OnStartOpenConnection() is not implemented by SocketStreamDispatcherHost | 336 // OnStartOpenConnection() is not implemented by SocketStreamDispatcherHost |
| 351 | 337 |
| 352 virtual void OnConnected(SocketStream* socket, | 338 void OnConnected(SocketStream* socket, |
| 353 int max_pending_send_allowed) override { | 339 int max_pending_send_allowed) override { |
| 354 DeleteJobMaybe(); | 340 DeleteJobMaybe(); |
| 355 } | 341 } |
| 356 | 342 |
| 357 virtual void OnSentData(SocketStream* socket, int amount_sent) override { | 343 void OnSentData(SocketStream* socket, int amount_sent) override { |
| 358 DeleteJobMaybe(); | 344 DeleteJobMaybe(); |
| 359 } | 345 } |
| 360 | 346 |
| 361 virtual void OnReceivedData(SocketStream* socket, | 347 void OnReceivedData(SocketStream* socket, |
| 362 const char* data, | 348 const char* data, |
| 363 int len) override { | 349 int len) override { |
| 364 DeleteJobMaybe(); | 350 DeleteJobMaybe(); |
| 365 } | 351 } |
| 366 | 352 |
| 367 virtual void OnClose(SocketStream* socket) override { DeleteJobMaybe(); } | 353 void OnClose(SocketStream* socket) override { DeleteJobMaybe(); } |
| 368 | 354 |
| 369 virtual void OnAuthRequired(SocketStream* socket, | 355 void OnAuthRequired(SocketStream* socket, |
| 370 AuthChallengeInfo* auth_info) override { | 356 AuthChallengeInfo* auth_info) override { |
| 371 DeleteJobMaybe(); | 357 DeleteJobMaybe(); |
| 372 } | 358 } |
| 373 | 359 |
| 374 virtual void OnSSLCertificateError(SocketStream* socket, | 360 void OnSSLCertificateError(SocketStream* socket, |
| 375 const SSLInfo& ssl_info, | 361 const SSLInfo& ssl_info, |
| 376 bool fatal) override { | 362 bool fatal) override { |
| 377 DeleteJobMaybe(); | 363 DeleteJobMaybe(); |
| 378 } | 364 } |
| 379 | 365 |
| 380 virtual void OnError(const SocketStream* socket, int error) override { | 366 void OnError(const SocketStream* socket, int error) override { |
| 381 DeleteJobMaybe(); | 367 DeleteJobMaybe(); |
| 382 } | 368 } |
| 383 | 369 |
| 384 // CanGetCookies() and CanSetCookies() do not appear to be able to delete the | 370 // CanGetCookies() and CanSetCookies() do not appear to be able to delete the |
| 385 // WebSocketJob object. | 371 // WebSocketJob object. |
| 386 | 372 |
| 387 private: | 373 private: |
| 388 scoped_refptr<WebSocketJob> job_; | 374 scoped_refptr<WebSocketJob> job_; |
| 389 bool delete_next_; | 375 bool delete_next_; |
| 390 }; | 376 }; |
| (...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1285 job()->Connect(); | 1271 job()->Connect(); |
| 1286 SetDeleteNext(); | 1272 SetDeleteNext(); |
| 1287 job()->OnReceivedData( | 1273 job()->OnReceivedData( |
| 1288 socket_.get(), kMinimalResponse, arraysize(kMinimalResponse) - 1); | 1274 socket_.get(), kMinimalResponse, arraysize(kMinimalResponse) - 1); |
| 1289 EXPECT_FALSE(job()); | 1275 EXPECT_FALSE(job()); |
| 1290 } | 1276 } |
| 1291 | 1277 |
| 1292 // TODO(toyoshim): Add tests to verify throttling, SPDY stream limitation. | 1278 // TODO(toyoshim): Add tests to verify throttling, SPDY stream limitation. |
| 1293 // TODO(toyoshim,yutak): Add tests to verify closing handshake. | 1279 // TODO(toyoshim,yutak): Add tests to verify closing handshake. |
| 1294 } // namespace net | 1280 } // namespace net |
| OLD | NEW |