| 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 "content/browser/renderer_host/websocket_dispatcher_host.h" | 5 #include "content/browser/renderer_host/websocket_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 gone_hosts_.push_back(routing_id); | 110 gone_hosts_.push_back(routing_id); |
| 111 } | 111 } |
| 112 | 112 |
| 113 base::WeakPtr<WebSocketDispatcherHostTest> GetWeakPtr() { | 113 base::WeakPtr<WebSocketDispatcherHostTest> GetWeakPtr() { |
| 114 return weak_ptr_factory_.GetWeakPtr(); | 114 return weak_ptr_factory_.GetWeakPtr(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 protected: | 117 protected: |
| 118 // Adds |n| connections. Returns true if succeeded. | 118 // Adds |n| connections. Returns true if succeeded. |
| 119 bool AddMultipleChannels(int number_of_channels) { | 119 bool AddMultipleChannels(int number_of_channels) { |
| 120 GURL socket_url("ws://example.com/test"); | |
| 121 std::vector<std::string> requested_protocols; | |
| 122 url::Origin origin(GURL("http://example.com")); | |
| 123 int render_frame_id = -3; | |
| 124 | |
| 125 for (int i = 0; i < number_of_channels; ++i) { | 120 for (int i = 0; i < number_of_channels; ++i) { |
| 126 int routing_id = next_routing_id_++; | 121 int routing_id = next_routing_id_++; |
| 127 WebSocketHostMsg_AddChannelRequest message( | 122 |
| 128 routing_id, | 123 WebSocketHostMsg_AddChannelRequest_Params params; |
| 129 socket_url, | 124 params.socket_url = GURL("ws://example.com/test"); |
| 130 requested_protocols, | 125 params.origin = url::Origin(GURL("http://example.com")); |
| 131 origin, | 126 params.first_party_for_cookies = GURL("http://example.com"); |
| 132 "", | 127 params.user_agent_override = ""; |
| 133 render_frame_id); | 128 params.render_frame_id = -3; |
| 129 |
| 130 WebSocketHostMsg_AddChannelRequest message(routing_id, params); |
| 134 if (!dispatcher_host_->OnMessageReceived(message)) | 131 if (!dispatcher_host_->OnMessageReceived(message)) |
| 135 return false; | 132 return false; |
| 136 } | 133 } |
| 137 | 134 |
| 138 return true; | 135 return true; |
| 139 } | 136 } |
| 140 | 137 |
| 141 // Adds and cancels |n| connections. Returns true if succeeded. | 138 // Adds and cancels |n| connections. Returns true if succeeded. |
| 142 bool AddAndCancelMultipleChannels(int number_of_channels) { | 139 bool AddAndCancelMultipleChannels(int number_of_channels) { |
| 143 GURL socket_url("ws://example.com/test"); | |
| 144 std::vector<std::string> requested_protocols; | |
| 145 url::Origin origin(GURL("http://example.com")); | |
| 146 int render_frame_id = -3; | |
| 147 | |
| 148 for (int i = 0; i < number_of_channels; ++i) { | 140 for (int i = 0; i < number_of_channels; ++i) { |
| 149 int routing_id = next_routing_id_++; | 141 int routing_id = next_routing_id_++; |
| 142 |
| 143 WebSocketHostMsg_AddChannelRequest_Params params; |
| 144 params.socket_url = GURL("ws://example.com/test"); |
| 145 params.origin = url::Origin(GURL("http://example.com")); |
| 146 params.first_party_for_cookies = GURL("http://example.com"); |
| 147 params.user_agent_override = ""; |
| 148 params.render_frame_id = -3; |
| 149 |
| 150 WebSocketHostMsg_AddChannelRequest messageAddChannelRequest( | 150 WebSocketHostMsg_AddChannelRequest messageAddChannelRequest( |
| 151 routing_id, | 151 routing_id, params); |
| 152 socket_url, | |
| 153 requested_protocols, | |
| 154 origin, | |
| 155 "", | |
| 156 render_frame_id); | |
| 157 if (!dispatcher_host_->OnMessageReceived(messageAddChannelRequest)) | 152 if (!dispatcher_host_->OnMessageReceived(messageAddChannelRequest)) |
| 158 return false; | 153 return false; |
| 159 | 154 |
| 160 WebSocketMsg_DropChannel messageDropChannel( | 155 WebSocketMsg_DropChannel messageDropChannel( |
| 161 routing_id, false, net::kWebSocketErrorAbnormalClosure, ""); | 156 routing_id, false, net::kWebSocketErrorAbnormalClosure, ""); |
| 162 if (!dispatcher_host_->OnMessageReceived(messageDropChannel)) | 157 if (!dispatcher_host_->OnMessageReceived(messageDropChannel)) |
| 163 return false; | 158 return false; |
| 164 } | 159 } |
| 165 | 160 |
| 166 return true; | 161 return true; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 IPC::Message message; | 210 IPC::Message message; |
| 216 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); | 211 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); |
| 217 } | 212 } |
| 218 | 213 |
| 219 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { | 214 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { |
| 220 EXPECT_EQ(kMagicRenderProcessId, dispatcher_host_->render_process_id()); | 215 EXPECT_EQ(kMagicRenderProcessId, dispatcher_host_->render_process_id()); |
| 221 } | 216 } |
| 222 | 217 |
| 223 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { | 218 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { |
| 224 int routing_id = 123; | 219 int routing_id = 123; |
| 225 GURL socket_url("ws://example.com/test"); | |
| 226 std::vector<std::string> requested_protocols; | 220 std::vector<std::string> requested_protocols; |
| 227 requested_protocols.push_back("hello"); | 221 requested_protocols.push_back("hello"); |
| 228 url::Origin origin(GURL("http://example.com")); | 222 |
| 229 int render_frame_id = -2; | 223 WebSocketHostMsg_AddChannelRequest_Params params; |
| 230 WebSocketHostMsg_AddChannelRequest message( | 224 params.socket_url = GURL("ws://example.com/test"); |
| 231 routing_id, socket_url, requested_protocols, origin, "", render_frame_id); | 225 params.requested_protocols = requested_protocols; |
| 226 params.origin = url::Origin(GURL("http://example.com")); |
| 227 params.first_party_for_cookies = GURL("http://example.com"); |
| 228 params.user_agent_override = ""; |
| 229 params.render_frame_id = -2; |
| 230 |
| 231 WebSocketHostMsg_AddChannelRequest message(routing_id, params); |
| 232 | 232 |
| 233 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); | 233 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); |
| 234 | 234 |
| 235 ASSERT_EQ(1U, mock_hosts_.size()); | 235 ASSERT_EQ(1U, mock_hosts_.size()); |
| 236 MockWebSocketHost* host = mock_hosts_[0]; | 236 MockWebSocketHost* host = mock_hosts_[0]; |
| 237 | 237 |
| 238 ASSERT_EQ(1U, host->received_messages_.size()); | 238 ASSERT_EQ(1U, host->received_messages_.size()); |
| 239 const IPC::Message& forwarded_message = host->received_messages_[0]; | 239 const IPC::Message& forwarded_message = host->received_messages_[0]; |
| 240 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | 240 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
| 241 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 241 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| 242 } | 242 } |
| 243 | 243 |
| 244 TEST_F(WebSocketDispatcherHostTest, SendFrameButNoHostYet) { | 244 TEST_F(WebSocketDispatcherHostTest, SendFrameButNoHostYet) { |
| 245 int routing_id = 123; | 245 int routing_id = 123; |
| 246 std::vector<char> data; | 246 std::vector<char> data; |
| 247 WebSocketMsg_SendFrame message( | 247 WebSocketMsg_SendFrame message( |
| 248 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); | 248 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); |
| 249 | 249 |
| 250 // Expected to be ignored. | 250 // Expected to be ignored. |
| 251 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(message)); | 251 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(message)); |
| 252 | 252 |
| 253 EXPECT_EQ(0U, mock_hosts_.size()); | 253 EXPECT_EQ(0U, mock_hosts_.size()); |
| 254 } | 254 } |
| 255 | 255 |
| 256 TEST_F(WebSocketDispatcherHostTest, SendFrame) { | 256 TEST_F(WebSocketDispatcherHostTest, SendFrame) { |
| 257 int routing_id = 123; | 257 int routing_id = 123; |
| 258 | 258 |
| 259 GURL socket_url("ws://example.com/test"); | |
| 260 std::vector<std::string> requested_protocols; | 259 std::vector<std::string> requested_protocols; |
| 261 requested_protocols.push_back("hello"); | 260 requested_protocols.push_back("hello"); |
| 262 url::Origin origin(GURL("http://example.com")); | 261 |
| 263 int render_frame_id = -2; | 262 WebSocketHostMsg_AddChannelRequest_Params params; |
| 264 WebSocketHostMsg_AddChannelRequest add_channel_message( | 263 params.socket_url = GURL("ws://example.com/test"); |
| 265 routing_id, socket_url, requested_protocols, origin, "", render_frame_id); | 264 params.requested_protocols = requested_protocols; |
| 265 params.origin = url::Origin(GURL("http://example.com")); |
| 266 params.first_party_for_cookies = GURL("http://example.com"); |
| 267 params.user_agent_override = ""; |
| 268 params.render_frame_id = -2; |
| 269 |
| 270 WebSocketHostMsg_AddChannelRequest add_channel_message(routing_id, params); |
| 266 | 271 |
| 267 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message)); | 272 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message)); |
| 268 | 273 |
| 269 std::vector<char> data; | 274 std::vector<char> data; |
| 270 WebSocketMsg_SendFrame send_frame_message( | 275 WebSocketMsg_SendFrame send_frame_message( |
| 271 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); | 276 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); |
| 272 | 277 |
| 273 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(send_frame_message)); | 278 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(send_frame_message)); |
| 274 | 279 |
| 275 ASSERT_EQ(1U, mock_hosts_.size()); | 280 ASSERT_EQ(1U, mock_hosts_.size()); |
| 276 MockWebSocketHost* host = mock_hosts_[0]; | 281 MockWebSocketHost* host = mock_hosts_[0]; |
| 277 | 282 |
| 278 ASSERT_EQ(2U, host->received_messages_.size()); | 283 ASSERT_EQ(2U, host->received_messages_.size()); |
| 279 { | 284 { |
| 280 const IPC::Message& forwarded_message = host->received_messages_[0]; | 285 const IPC::Message& forwarded_message = host->received_messages_[0]; |
| 281 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | 286 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
| 282 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 287 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| 283 } | 288 } |
| 284 { | 289 { |
| 285 const IPC::Message& forwarded_message = host->received_messages_[1]; | 290 const IPC::Message& forwarded_message = host->received_messages_[1]; |
| 286 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); | 291 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); |
| 287 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 292 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| 288 } | 293 } |
| 289 } | 294 } |
| 290 | 295 |
| 291 TEST_F(WebSocketDispatcherHostTest, Destruct) { | 296 TEST_F(WebSocketDispatcherHostTest, Destruct) { |
| 292 WebSocketHostMsg_AddChannelRequest message1( | 297 WebSocketHostMsg_AddChannelRequest_Params params1; |
| 293 123, GURL("ws://example.com/test"), std::vector<std::string>(), | 298 params1.socket_url = GURL("ws://example.com/test"); |
| 294 url::Origin(GURL("http://example.com")), "", -1); | 299 params1.requested_protocols = std::vector<std::string>(); |
| 295 WebSocketHostMsg_AddChannelRequest message2( | 300 params1.origin = url::Origin(GURL("http://example.com")); |
| 296 456, GURL("ws://example.com/test2"), std::vector<std::string>(), | 301 params1.first_party_for_cookies = GURL("http://example.com"); |
| 297 url::Origin(GURL("http://example.com")), "", -1); | 302 params1.user_agent_override = ""; |
| 303 params1.render_frame_id = -1; |
| 304 |
| 305 WebSocketHostMsg_AddChannelRequest message1(123, params1); |
| 306 |
| 307 WebSocketHostMsg_AddChannelRequest_Params params2; |
| 308 params2.socket_url = GURL("ws://example.com/test2"); |
| 309 params2.requested_protocols = std::vector<std::string>(); |
| 310 params2.origin = url::Origin(GURL("http://example.com")); |
| 311 params2.first_party_for_cookies = GURL("http://example.com"); |
| 312 params2.user_agent_override = ""; |
| 313 params2.render_frame_id = -1; |
| 314 |
| 315 WebSocketHostMsg_AddChannelRequest message2(456, params2); |
| 298 | 316 |
| 299 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message1)); | 317 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message1)); |
| 300 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message2)); | 318 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message2)); |
| 301 | 319 |
| 302 ASSERT_EQ(2u, mock_hosts_.size()); | 320 ASSERT_EQ(2u, mock_hosts_.size()); |
| 303 | 321 |
| 304 mock_hosts_.clear(); | 322 mock_hosts_.clear(); |
| 305 dispatcher_host_ = NULL; | 323 dispatcher_host_ = NULL; |
| 306 | 324 |
| 307 ASSERT_EQ(2u, gone_hosts_.size()); | 325 ASSERT_EQ(2u, gone_hosts_.size()); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 ASSERT_TRUE(AddMultipleChannels(1)); | 426 ASSERT_TRUE(AddMultipleChannels(1)); |
| 409 | 427 |
| 410 EXPECT_EQ(1, dispatcher_host_->num_pending_connections()); | 428 EXPECT_EQ(1, dispatcher_host_->num_pending_connections()); |
| 411 EXPECT_EQ(255, dispatcher_host_->num_failed_connections()); | 429 EXPECT_EQ(255, dispatcher_host_->num_failed_connections()); |
| 412 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | 430 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); |
| 413 } | 431 } |
| 414 | 432 |
| 415 // This is a regression test for https://crrev.com/998173003/. | 433 // This is a regression test for https://crrev.com/998173003/. |
| 416 TEST_F(WebSocketDispatcherHostTest, InvalidScheme) { | 434 TEST_F(WebSocketDispatcherHostTest, InvalidScheme) { |
| 417 int routing_id = 123; | 435 int routing_id = 123; |
| 418 GURL socket_url("http://example.com/test"); | 436 |
| 419 std::vector<std::string> requested_protocols; | 437 std::vector<std::string> requested_protocols; |
| 420 requested_protocols.push_back("hello"); | 438 requested_protocols.push_back("hello"); |
| 421 url::Origin origin(GURL("http://example.com")); | 439 |
| 422 int render_frame_id = -2; | 440 WebSocketHostMsg_AddChannelRequest_Params params; |
| 423 WebSocketHostMsg_AddChannelRequest message( | 441 params.socket_url = GURL("http://example.com/test"); |
| 424 routing_id, socket_url, requested_protocols, origin, "", render_frame_id); | 442 params.requested_protocols = requested_protocols; |
| 443 params.origin = url::Origin(GURL("http://example.com")); |
| 444 params.first_party_for_cookies = GURL("http://example.com"); |
| 445 params.user_agent_override = ""; |
| 446 params.render_frame_id = -2; |
| 447 |
| 448 WebSocketHostMsg_AddChannelRequest message(routing_id, params); |
| 425 | 449 |
| 426 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); | 450 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); |
| 427 | 451 |
| 428 ASSERT_EQ(1U, mock_hosts_.size()); | 452 ASSERT_EQ(1U, mock_hosts_.size()); |
| 429 MockWebSocketHost* host = mock_hosts_[0]; | 453 MockWebSocketHost* host = mock_hosts_[0]; |
| 430 | 454 |
| 431 // Tests that WebSocketHost::OnMessageReceived() doesn't cause a crash and | 455 // Tests that WebSocketHost::OnMessageReceived() doesn't cause a crash and |
| 432 // the connection with an invalid scheme fails here. | 456 // the connection with an invalid scheme fails here. |
| 433 // We call WebSocketHost::OnMessageReceived() here explicitly because | 457 // We call WebSocketHost::OnMessageReceived() here explicitly because |
| 434 // MockWebSocketHost does not call WebSocketHost::OnMessageReceived() for | 458 // MockWebSocketHost does not call WebSocketHost::OnMessageReceived() for |
| 435 // WebSocketHostMsg_AddChannelRequest. | 459 // WebSocketHostMsg_AddChannelRequest. |
| 436 host->WebSocketHost::OnMessageReceived(message); | 460 host->WebSocketHost::OnMessageReceived(message); |
| 437 | 461 |
| 438 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); | 462 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); |
| 439 EXPECT_EQ(1, dispatcher_host_->num_failed_connections()); | 463 EXPECT_EQ(1, dispatcher_host_->num_failed_connections()); |
| 440 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | 464 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); |
| 441 } | 465 } |
| 442 | 466 |
| 443 } // namespace | 467 } // namespace |
| 444 } // namespace content | 468 } // namespace content |
| OLD | NEW |