Chromium Code Reviews| 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 <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "content/browser/renderer_host/websocket_host.h" | 13 #include "content/browser/renderer_host/websocket_host.h" |
| 13 #include "content/common/websocket.h" | 14 #include "content/common/websocket.h" |
| 14 #include "content/common/websocket_messages.h" | 15 #include "content/common/websocket_messages.h" |
| 15 #include "ipc/ipc_message.h" | 16 #include "ipc/ipc_message.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 18 #include "url/origin.h" | 19 #include "url/origin.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // This number is unlikely to occur by chance. | 24 // This number is unlikely to occur by chance. |
| 24 static const int kMagicRenderProcessId = 506116062; | 25 static const int kMagicRenderProcessId = 506116062; |
| 25 | 26 |
| 27 class WebSocketDispatcherHostTest; | |
| 28 | |
| 26 // A mock of WebsocketHost which records received messages. | 29 // A mock of WebsocketHost which records received messages. |
| 27 class MockWebSocketHost : public WebSocketHost { | 30 class MockWebSocketHost : public WebSocketHost { |
| 28 public: | 31 public: |
| 29 MockWebSocketHost(int routing_id, | 32 MockWebSocketHost(int routing_id, |
| 30 WebSocketDispatcherHost* dispatcher, | 33 WebSocketDispatcherHost* dispatcher, |
| 31 net::URLRequestContext* url_request_context) | 34 net::URLRequestContext* url_request_context, |
| 32 : WebSocketHost(routing_id, dispatcher, url_request_context) { | 35 WebSocketDispatcherHostTest* owner) |
| 36 : WebSocketHost(routing_id, dispatcher, url_request_context), | |
| 37 owner_(owner) { | |
| 33 } | 38 } |
| 34 | 39 |
| 35 virtual ~MockWebSocketHost() {} | 40 virtual ~MockWebSocketHost() {} |
| 36 | 41 |
| 37 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE{ | 42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| 38 received_messages_.push_back(message); | 43 received_messages_.push_back(message); |
| 39 return true; | 44 return true; |
| 40 } | 45 } |
| 41 | 46 |
| 47 virtual void GoAway() OVERRIDE; | |
| 48 | |
| 42 std::vector<IPC::Message> received_messages_; | 49 std::vector<IPC::Message> received_messages_; |
| 50 WebSocketDispatcherHostTest* owner_; | |
| 43 }; | 51 }; |
| 44 | 52 |
| 45 class WebSocketDispatcherHostTest : public ::testing::Test { | 53 class WebSocketDispatcherHostTest : public ::testing::Test { |
| 46 public: | 54 public: |
| 47 WebSocketDispatcherHostTest() { | 55 WebSocketDispatcherHostTest() |
| 56 : on_destruction_(false) { | |
| 48 dispatcher_host_ = new WebSocketDispatcherHost( | 57 dispatcher_host_ = new WebSocketDispatcherHost( |
| 49 kMagicRenderProcessId, | 58 kMagicRenderProcessId, |
| 50 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, | 59 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, |
| 51 base::Unretained(this)), | 60 base::Unretained(this)), |
| 52 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, | 61 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, |
| 53 base::Unretained(this))); | 62 base::Unretained(this))); |
| 54 } | 63 } |
| 55 | 64 |
| 56 virtual ~WebSocketDispatcherHostTest() {} | 65 virtual ~WebSocketDispatcherHostTest() { |
| 66 on_destruction_ = true; | |
|
Adam Rice
2014/07/28 04:29:22
This doesn't seem safe to me. How about changing t
yhirano
2014/07/28 05:08:52
Done.
| |
| 67 } | |
| 68 | |
| 69 void GoAway(int routing_id) { | |
| 70 if (!on_destruction_) | |
| 71 gone_hosts_.push_back(routing_id); | |
| 72 } | |
| 57 | 73 |
| 58 protected: | 74 protected: |
| 59 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; | 75 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; |
| 60 | 76 |
| 61 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of | 77 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of |
| 62 // them. | 78 // them. |
| 63 std::vector<MockWebSocketHost*> mock_hosts_; | 79 std::vector<MockWebSocketHost*> mock_hosts_; |
| 80 std::vector<int> gone_hosts_; | |
| 81 bool on_destruction_; | |
| 64 | 82 |
| 65 private: | 83 private: |
| 66 net::URLRequestContext* OnGetRequestContext() { | 84 net::URLRequestContext* OnGetRequestContext() { |
| 67 return NULL; | 85 return NULL; |
| 68 } | 86 } |
| 69 | 87 |
| 70 WebSocketHost* CreateWebSocketHost(int routing_id) { | 88 WebSocketHost* CreateWebSocketHost(int routing_id) { |
| 71 MockWebSocketHost* host = | 89 MockWebSocketHost* host = |
| 72 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL); | 90 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL, this); |
| 73 mock_hosts_.push_back(host); | 91 mock_hosts_.push_back(host); |
| 74 return host; | 92 return host; |
| 75 } | 93 } |
| 76 }; | 94 }; |
| 77 | 95 |
| 96 void MockWebSocketHost::GoAway() { | |
| 97 owner_->GoAway(routing_id()); | |
| 98 } | |
| 99 | |
| 78 TEST_F(WebSocketDispatcherHostTest, Construct) { | 100 TEST_F(WebSocketDispatcherHostTest, Construct) { |
| 79 // Do nothing. | 101 // Do nothing. |
| 80 } | 102 } |
| 81 | 103 |
| 82 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { | 104 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { |
| 83 IPC::Message message; | 105 IPC::Message message; |
| 84 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); | 106 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); |
| 85 } | 107 } |
| 86 | 108 |
| 87 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { | 109 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | 171 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
| 150 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 172 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| 151 } | 173 } |
| 152 { | 174 { |
| 153 const IPC::Message& forwarded_message = host->received_messages_[1]; | 175 const IPC::Message& forwarded_message = host->received_messages_[1]; |
| 154 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); | 176 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); |
| 155 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 177 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| 156 } | 178 } |
| 157 } | 179 } |
| 158 | 180 |
| 181 TEST_F(WebSocketDispatcherHostTest, Destruct) { | |
| 182 WebSocketHostMsg_AddChannelRequest message1( | |
| 183 123, GURL("ws://example.com/test"), std::vector<std::string>(), | |
| 184 url::Origin("http://example.com"), -1); | |
| 185 WebSocketHostMsg_AddChannelRequest message2( | |
| 186 456, GURL("ws://example.com/test2"), std::vector<std::string>(), | |
| 187 url::Origin("http://example.com"), -1); | |
| 188 | |
| 189 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message1)); | |
| 190 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message2)); | |
| 191 | |
| 192 ASSERT_EQ(2u, mock_hosts_.size()); | |
| 193 | |
| 194 mock_hosts_.clear(); | |
| 195 dispatcher_host_ = NULL; | |
| 196 | |
| 197 ASSERT_EQ(2u, gone_hosts_.size()); | |
| 198 // The gone_hosts_ ordering is not predictable because it depends on the | |
| 199 // hash_map ordering. | |
| 200 std::sort(gone_hosts_.begin(), gone_hosts_.end()); | |
| 201 EXPECT_EQ(123, gone_hosts_[0]); | |
| 202 EXPECT_EQ(456, gone_hosts_[1]); | |
| 203 } | |
| 204 | |
| 159 } // namespace | 205 } // namespace |
| 160 } // namespace content | 206 } // namespace content |
| OLD | NEW |