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