OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/p2p/socket_host_tcp_server.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h" |
6 | 6 |
7 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 7 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
8 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" | 8 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" |
| 9 #include "net/base/completion_callback.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 using ::testing::_; | 13 using ::testing::_; |
13 using ::testing::DeleteArg; | 14 using ::testing::DeleteArg; |
14 using ::testing::DoAll; | 15 using ::testing::DoAll; |
15 using ::testing::Return; | 16 using ::testing::Return; |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 class FakeServerSocket : public net::ServerSocket { | 20 class FakeServerSocket : public net::ServerSocket { |
20 public: | 21 public: |
21 FakeServerSocket() | 22 FakeServerSocket() |
22 : listening_(false), | 23 : listening_(false), |
23 accept_socket_(NULL), | 24 accept_socket_(NULL) { |
24 accept_callback_(NULL) { | |
25 } | 25 } |
26 | 26 |
27 ~FakeServerSocket() { } | 27 virtual ~FakeServerSocket() {} |
28 | 28 |
29 bool listening() { return listening_; } | 29 bool listening() { return listening_; } |
30 | 30 |
31 void AddIncoming(net::StreamSocket* socket) { | 31 void AddIncoming(net::StreamSocket* socket) { |
32 if (accept_callback_) { | 32 if (!accept_callback_.is_null()) { |
33 DCHECK(incoming_sockets_.empty()); | 33 DCHECK(incoming_sockets_.empty()); |
34 accept_socket_->reset(socket); | 34 accept_socket_->reset(socket); |
35 accept_socket_ = NULL; | 35 accept_socket_ = NULL; |
36 net::OldCompletionCallback* cb = accept_callback_; | 36 |
37 accept_callback_ = NULL; | 37 // This copy is necessary because this implementation of ServerSocket |
38 cb->Run(net::OK); | 38 // bases logic on the null-ness of |accept_callback_| in the bound |
| 39 // callback. |
| 40 net::CompletionCallback cb = accept_callback_; |
| 41 accept_callback_.Reset(); |
| 42 cb.Run(net::OK); |
39 } else { | 43 } else { |
40 incoming_sockets_.push_back(socket); | 44 incoming_sockets_.push_back(socket); |
41 } | 45 } |
42 } | 46 } |
43 | 47 |
44 // net::ServerSocket implementation. | 48 // net::ServerSocket implementation. |
45 virtual int Listen(const net::IPEndPoint& address, int backlog) OVERRIDE { | 49 virtual int Listen(const net::IPEndPoint& address, int backlog) OVERRIDE { |
46 local_address_ = address; | 50 local_address_ = address; |
47 listening_ = true; | 51 listening_ = true; |
48 return net::OK; | 52 return net::OK; |
49 } | 53 } |
50 | 54 |
51 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE { | 55 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE { |
52 *address = local_address_; | 56 *address = local_address_; |
53 return net::OK; | 57 return net::OK; |
54 } | 58 } |
55 | 59 |
56 virtual int Accept(scoped_ptr<net::StreamSocket>* socket, | 60 virtual int Accept(scoped_ptr<net::StreamSocket>* socket, |
57 net::OldCompletionCallback* callback) OVERRIDE { | 61 const net::CompletionCallback& callback) OVERRIDE { |
58 DCHECK(socket); | 62 DCHECK(socket); |
59 if (!incoming_sockets_.empty()) { | 63 if (!incoming_sockets_.empty()) { |
60 socket->reset(incoming_sockets_.front()); | 64 socket->reset(incoming_sockets_.front()); |
61 incoming_sockets_.pop_front(); | 65 incoming_sockets_.pop_front(); |
62 return net::OK; | 66 return net::OK; |
63 } else { | 67 } else { |
64 accept_socket_ = socket; | 68 accept_socket_ = socket; |
65 accept_callback_ = callback; | 69 accept_callback_ = callback; |
66 return net::ERR_IO_PENDING; | 70 return net::ERR_IO_PENDING; |
67 } | 71 } |
68 } | 72 } |
69 | 73 |
70 private: | 74 private: |
71 bool listening_; | 75 bool listening_; |
72 | 76 |
73 net::IPEndPoint local_address_; | 77 net::IPEndPoint local_address_; |
74 | 78 |
75 scoped_ptr<net::StreamSocket>* accept_socket_; | 79 scoped_ptr<net::StreamSocket>* accept_socket_; |
76 net::OldCompletionCallback* accept_callback_; | 80 net::CompletionCallback accept_callback_; |
77 | 81 |
78 std::list<net::StreamSocket*> incoming_sockets_; | 82 std::list<net::StreamSocket*> incoming_sockets_; |
79 }; | 83 }; |
80 | 84 |
81 } // namespace | 85 } // namespace |
82 | 86 |
83 namespace content { | 87 namespace content { |
84 | 88 |
85 class P2PSocketHostTcpServerTest : public testing::Test { | 89 class P2PSocketHostTcpServerTest : public testing::Test { |
86 protected: | 90 protected: |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 EXPECT_EQ(incoming1, GetSocketFormTcpSocketHost( | 160 EXPECT_EQ(incoming1, GetSocketFormTcpSocketHost( |
157 reinterpret_cast<P2PSocketHostTcp*>(new_host1.get()))); | 161 reinterpret_cast<P2PSocketHostTcp*>(new_host1.get()))); |
158 scoped_ptr<P2PSocketHost> new_host2( | 162 scoped_ptr<P2PSocketHost> new_host2( |
159 socket_host_->AcceptIncomingTcpConnection(addr2, kAcceptedSocketId2)); | 163 socket_host_->AcceptIncomingTcpConnection(addr2, kAcceptedSocketId2)); |
160 ASSERT_TRUE(new_host2.get() != NULL); | 164 ASSERT_TRUE(new_host2.get() != NULL); |
161 EXPECT_EQ(incoming2, GetSocketFormTcpSocketHost( | 165 EXPECT_EQ(incoming2, GetSocketFormTcpSocketHost( |
162 reinterpret_cast<P2PSocketHostTcp*>(new_host2.get()))); | 166 reinterpret_cast<P2PSocketHostTcp*>(new_host2.get()))); |
163 } | 167 } |
164 | 168 |
165 } // namespace content | 169 } // namespace content |
OLD | NEW |