OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ipc/ipc_sync_channel.h" | 5 #include "ipc/ipc_sync_channel.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 OnDouble(in, &result); | 144 OnDouble(in, &result); |
145 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result); | 145 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result); |
146 Send(reply_msg); | 146 Send(reply_msg); |
147 } | 147 } |
148 | 148 |
149 virtual void OnNestedTestMsg(Message* reply_msg) { | 149 virtual void OnNestedTestMsg(Message* reply_msg) { |
150 NOTREACHED(); | 150 NOTREACHED(); |
151 } | 151 } |
152 | 152 |
153 virtual SyncChannel* CreateChannel() { | 153 virtual SyncChannel* CreateChannel() { |
154 return new SyncChannel(channel_name_, | 154 scoped_ptr<SyncChannel> channel = SyncChannel::Create( |
155 mode_, | 155 channel_name_, mode_, this, ipc_thread_.message_loop_proxy().get(), |
156 this, | 156 true, &shutdown_event_); |
157 ipc_thread_.message_loop_proxy().get(), | 157 return channel.release(); |
158 true, | |
159 &shutdown_event_); | |
160 } | 158 } |
161 | 159 |
162 base::Thread* ListenerThread() { | 160 base::Thread* ListenerThread() { |
163 return overrided_thread_ ? overrided_thread_ : &listener_thread_; | 161 return overrided_thread_ ? overrided_thread_ : &listener_thread_; |
164 } | 162 } |
165 | 163 |
166 const base::Thread& ipc_thread() const { return ipc_thread_; } | 164 const base::Thread& ipc_thread() const { return ipc_thread_; } |
167 | 165 |
168 private: | 166 private: |
169 // Called on the listener thread to create the sync channel. | 167 // Called on the listener thread to create the sync channel. |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 explicit TwoStepServer(bool create_pipe_now) | 315 explicit TwoStepServer(bool create_pipe_now) |
318 : Worker(Channel::MODE_SERVER, "simpler_server"), | 316 : Worker(Channel::MODE_SERVER, "simpler_server"), |
319 create_pipe_now_(create_pipe_now) { } | 317 create_pipe_now_(create_pipe_now) { } |
320 | 318 |
321 virtual void Run() OVERRIDE { | 319 virtual void Run() OVERRIDE { |
322 SendAnswerToLife(false, true); | 320 SendAnswerToLife(false, true); |
323 Done(); | 321 Done(); |
324 } | 322 } |
325 | 323 |
326 virtual SyncChannel* CreateChannel() OVERRIDE { | 324 virtual SyncChannel* CreateChannel() OVERRIDE { |
327 SyncChannel* channel = new SyncChannel( | 325 SyncChannel* channel = |
328 this, ipc_thread().message_loop_proxy().get(), shutdown_event()); | 326 SyncChannel::Create(channel_name(), mode(), this, |
329 channel->Init(channel_name(), mode(), create_pipe_now_); | 327 ipc_thread().message_loop_proxy().get(), |
| 328 create_pipe_now_, |
| 329 shutdown_event()).release(); |
330 return channel; | 330 return channel; |
331 } | 331 } |
332 | 332 |
333 bool create_pipe_now_; | 333 bool create_pipe_now_; |
334 }; | 334 }; |
335 | 335 |
336 class TwoStepClient : public Worker { | 336 class TwoStepClient : public Worker { |
337 public: | 337 public: |
338 TwoStepClient(bool create_pipe_now) | 338 TwoStepClient(bool create_pipe_now) |
339 : Worker(Channel::MODE_CLIENT, "simple_client"), | 339 : Worker(Channel::MODE_CLIENT, "simple_client"), |
340 create_pipe_now_(create_pipe_now) { } | 340 create_pipe_now_(create_pipe_now) { } |
341 | 341 |
342 virtual void OnAnswer(int* answer) OVERRIDE { | 342 virtual void OnAnswer(int* answer) OVERRIDE { |
343 *answer = 42; | 343 *answer = 42; |
344 Done(); | 344 Done(); |
345 } | 345 } |
346 | 346 |
347 virtual SyncChannel* CreateChannel() OVERRIDE { | 347 virtual SyncChannel* CreateChannel() OVERRIDE { |
348 SyncChannel* channel = new SyncChannel( | 348 SyncChannel* channel = |
349 this, ipc_thread().message_loop_proxy().get(), shutdown_event()); | 349 SyncChannel::Create(channel_name(), mode(), this, |
350 channel->Init(channel_name(), mode(), create_pipe_now_); | 350 ipc_thread().message_loop_proxy().get(), |
| 351 create_pipe_now_, |
| 352 shutdown_event()).release(); |
351 return channel; | 353 return channel; |
352 } | 354 } |
353 | 355 |
354 bool create_pipe_now_; | 356 bool create_pipe_now_; |
355 }; | 357 }; |
356 | 358 |
357 void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) { | 359 void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) { |
358 std::vector<Worker*> workers; | 360 std::vector<Worker*> workers; |
359 workers.push_back(new TwoStepServer(create_server_pipe_now)); | 361 workers.push_back(new TwoStepServer(create_server_pipe_now)); |
360 workers.push_back(new TwoStepClient(create_client_pipe_now)); | 362 workers.push_back(new TwoStepClient(create_client_pipe_now)); |
(...skipping 767 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1128 | 1130 |
1129 server_->ListenerThread()->message_loop()->PostTask( | 1131 server_->ListenerThread()->message_loop()->PostTask( |
1130 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 1)); | 1132 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 1)); |
1131 sent_ping_event_->Wait(); | 1133 sent_ping_event_->Wait(); |
1132 Send(new SyncChannelTestMsg_NoArgs); | 1134 Send(new SyncChannelTestMsg_NoArgs); |
1133 if (ping_ == 1) | 1135 if (ping_ == 1) |
1134 ++*success_; | 1136 ++*success_; |
1135 else | 1137 else |
1136 LOG(ERROR) << "Send failed to dispatch incoming message on same channel"; | 1138 LOG(ERROR) << "Send failed to dispatch incoming message on same channel"; |
1137 | 1139 |
1138 non_restricted_channel_.reset( | 1140 non_restricted_channel_ = |
1139 new SyncChannel("non_restricted_channel", | 1141 SyncChannel::Create("non_restricted_channel", |
1140 Channel::MODE_CLIENT, | 1142 IPC::Channel::MODE_CLIENT, |
1141 this, | 1143 this, |
1142 ipc_thread().message_loop_proxy().get(), | 1144 ipc_thread().message_loop_proxy().get(), |
1143 true, | 1145 true, |
1144 shutdown_event())); | 1146 shutdown_event()); |
1145 | 1147 |
1146 server_->ListenerThread()->message_loop()->PostTask( | 1148 server_->ListenerThread()->message_loop()->PostTask( |
1147 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 2)); | 1149 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 2)); |
1148 sent_ping_event_->Wait(); | 1150 sent_ping_event_->Wait(); |
1149 // Check that the incoming message is *not* dispatched when sending on the | 1151 // Check that the incoming message is *not* dispatched when sending on the |
1150 // non restricted channel. | 1152 // non restricted channel. |
1151 // TODO(piman): there is a possibility of a false positive race condition | 1153 // TODO(piman): there is a possibility of a false positive race condition |
1152 // here, if the message that was posted on the server-side end of the pipe | 1154 // here, if the message that was posted on the server-side end of the pipe |
1153 // is not visible yet on the client side, but I don't know how to solve this | 1155 // is not visible yet on the client side, but I don't know how to solve this |
1154 // without hooking into the internals of SyncChannel. I haven't seen it in | 1156 // without hooking into the internals of SyncChannel. I haven't seen it in |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1519 other_channel_->Send(new SyncChannelTestMsg_Done); | 1521 other_channel_->Send(new SyncChannelTestMsg_Done); |
1520 other_channel_.reset(); | 1522 other_channel_.reset(); |
1521 Done(); | 1523 Done(); |
1522 } | 1524 } |
1523 | 1525 |
1524 virtual void Run() OVERRIDE { | 1526 virtual void Run() OVERRIDE { |
1525 channel()->SetRestrictDispatchChannelGroup(group_); | 1527 channel()->SetRestrictDispatchChannelGroup(group_); |
1526 if (is_first()) | 1528 if (is_first()) |
1527 event1_->Signal(); | 1529 event1_->Signal(); |
1528 event2_->Wait(); | 1530 event2_->Wait(); |
1529 other_channel_.reset( | 1531 other_channel_ = |
1530 new SyncChannel(other_channel_name_, | 1532 SyncChannel::Create(other_channel_name_, |
1531 Channel::MODE_CLIENT, | 1533 IPC::Channel::MODE_CLIENT, |
1532 this, | 1534 this, |
1533 ipc_thread().message_loop_proxy().get(), | 1535 ipc_thread().message_loop_proxy().get(), |
1534 true, | 1536 true, |
1535 shutdown_event())); | 1537 shutdown_event()); |
1536 other_channel_->SetRestrictDispatchChannelGroup(group_); | 1538 other_channel_->SetRestrictDispatchChannelGroup(group_); |
1537 if (!is_first()) { | 1539 if (!is_first()) { |
1538 event1_->Signal(); | 1540 event1_->Signal(); |
1539 return; | 1541 return; |
1540 } | 1542 } |
1541 *success_ = 0; | 1543 *success_ = 0; |
1542 int value = 0; | 1544 int value = 0; |
1543 OnPingTTL(3, &value); | 1545 OnPingTTL(3, &value); |
1544 *success_ += (value == 3); | 1546 *success_ += (value == 3); |
1545 OnPingTTL(4, &value); | 1547 OnPingTTL(4, &value); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1599 // it will send another message to Server2. While sending that second message it | 1601 // it will send another message to Server2. While sending that second message it |
1600 // will receive a reply from Server1 with the unblock flag. | 1602 // will receive a reply from Server1 with the unblock flag. |
1601 | 1603 |
1602 class ReentrantReplyServer1 : public Worker { | 1604 class ReentrantReplyServer1 : public Worker { |
1603 public: | 1605 public: |
1604 ReentrantReplyServer1(WaitableEvent* server_ready) | 1606 ReentrantReplyServer1(WaitableEvent* server_ready) |
1605 : Worker("reentrant_reply1", Channel::MODE_SERVER), | 1607 : Worker("reentrant_reply1", Channel::MODE_SERVER), |
1606 server_ready_(server_ready) { } | 1608 server_ready_(server_ready) { } |
1607 | 1609 |
1608 virtual void Run() OVERRIDE { | 1610 virtual void Run() OVERRIDE { |
1609 server2_channel_.reset( | 1611 server2_channel_ = |
1610 new SyncChannel("reentrant_reply2", | 1612 SyncChannel::Create("reentrant_reply2", |
1611 Channel::MODE_CLIENT, | 1613 IPC::Channel::MODE_CLIENT, |
1612 this, | 1614 this, |
1613 ipc_thread().message_loop_proxy().get(), | 1615 ipc_thread().message_loop_proxy().get(), |
1614 true, | 1616 true, |
1615 shutdown_event())); | 1617 shutdown_event()); |
1616 server_ready_->Signal(); | 1618 server_ready_->Signal(); |
1617 Message* msg = new SyncChannelTestMsg_Reentrant1(); | 1619 Message* msg = new SyncChannelTestMsg_Reentrant1(); |
1618 server2_channel_->Send(msg); | 1620 server2_channel_->Send(msg); |
1619 server2_channel_.reset(); | 1621 server2_channel_.reset(); |
1620 Done(); | 1622 Done(); |
1621 } | 1623 } |
1622 | 1624 |
1623 private: | 1625 private: |
1624 virtual bool OnMessageReceived(const Message& message) OVERRIDE { | 1626 virtual bool OnMessageReceived(const Message& message) OVERRIDE { |
1625 IPC_BEGIN_MESSAGE_MAP(ReentrantReplyServer1, message) | 1627 IPC_BEGIN_MESSAGE_MAP(ReentrantReplyServer1, message) |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1784 } | 1786 } |
1785 | 1787 |
1786 // Windows needs to send an out-of-band secret to verify the client end of the | 1788 // Windows needs to send an out-of-band secret to verify the client end of the |
1787 // channel. Test that we still connect correctly in that case. | 1789 // channel. Test that we still connect correctly in that case. |
1788 TEST_F(IPCSyncChannelTest, Verified) { | 1790 TEST_F(IPCSyncChannelTest, Verified) { |
1789 Verified(); | 1791 Verified(); |
1790 } | 1792 } |
1791 | 1793 |
1792 } // namespace | 1794 } // namespace |
1793 } // namespace IPC | 1795 } // namespace IPC |
OLD | NEW |