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 this, |
156 this, | 156 ipc_thread_.message_loop_proxy().get(), |
157 ipc_thread_.message_loop_proxy().get(), | 157 &shutdown_event_); |
158 true, | 158 channel->InitByMode(channel_name_, mode_, true); |
159 &shutdown_event_); | 159 return channel.release(); |
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 = new SyncChannel( | 327 SyncChannel* channel = SyncChannel::Create( |
328 this, ipc_thread().message_loop_proxy().get(), shutdown_event()); | 328 this, ipc_thread().message_loop_proxy().get(), |
329 channel->Init(channel_name(), mode(), create_pipe_now_); | 329 shutdown_event()).release(); |
| 330 channel->InitByMode(channel_name(), mode(), create_pipe_now_); |
330 return channel; | 331 return channel; |
331 } | 332 } |
332 | 333 |
333 bool create_pipe_now_; | 334 bool create_pipe_now_; |
334 }; | 335 }; |
335 | 336 |
336 class TwoStepClient : public Worker { | 337 class TwoStepClient : public Worker { |
337 public: | 338 public: |
338 TwoStepClient(bool create_pipe_now) | 339 TwoStepClient(bool create_pipe_now) |
339 : Worker(Channel::MODE_CLIENT, "simple_client"), | 340 : Worker(Channel::MODE_CLIENT, "simple_client"), |
340 create_pipe_now_(create_pipe_now) { } | 341 create_pipe_now_(create_pipe_now) { } |
341 | 342 |
342 virtual void OnAnswer(int* answer) OVERRIDE { | 343 virtual void OnAnswer(int* answer) OVERRIDE { |
343 *answer = 42; | 344 *answer = 42; |
344 Done(); | 345 Done(); |
345 } | 346 } |
346 | 347 |
347 virtual SyncChannel* CreateChannel() OVERRIDE { | 348 virtual SyncChannel* CreateChannel() OVERRIDE { |
348 SyncChannel* channel = new SyncChannel( | 349 SyncChannel* channel = SyncChannel::Create( |
349 this, ipc_thread().message_loop_proxy().get(), shutdown_event()); | 350 this, ipc_thread().message_loop_proxy().get(), |
350 channel->Init(channel_name(), mode(), create_pipe_now_); | 351 shutdown_event()).release(); |
| 352 channel->InitByMode(channel_name(), mode(), create_pipe_now_); |
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::CreateClient("non_restricted_channel", |
1140 Channel::MODE_CLIENT, | 1142 this, |
1141 this, | 1143 ipc_thread().message_loop_proxy().get(), |
1142 ipc_thread().message_loop_proxy().get(), | 1144 true, |
1143 true, | 1145 shutdown_event()); |
1144 shutdown_event())); | |
1145 | 1146 |
1146 server_->ListenerThread()->message_loop()->PostTask( | 1147 server_->ListenerThread()->message_loop()->PostTask( |
1147 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 2)); | 1148 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 2)); |
1148 sent_ping_event_->Wait(); | 1149 sent_ping_event_->Wait(); |
1149 // Check that the incoming message is *not* dispatched when sending on the | 1150 // Check that the incoming message is *not* dispatched when sending on the |
1150 // non restricted channel. | 1151 // non restricted channel. |
1151 // TODO(piman): there is a possibility of a false positive race condition | 1152 // 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 | 1153 // 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 | 1154 // 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 | 1155 // 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); | 1520 other_channel_->Send(new SyncChannelTestMsg_Done); |
1520 other_channel_.reset(); | 1521 other_channel_.reset(); |
1521 Done(); | 1522 Done(); |
1522 } | 1523 } |
1523 | 1524 |
1524 virtual void Run() OVERRIDE { | 1525 virtual void Run() OVERRIDE { |
1525 channel()->SetRestrictDispatchChannelGroup(group_); | 1526 channel()->SetRestrictDispatchChannelGroup(group_); |
1526 if (is_first()) | 1527 if (is_first()) |
1527 event1_->Signal(); | 1528 event1_->Signal(); |
1528 event2_->Wait(); | 1529 event2_->Wait(); |
1529 other_channel_.reset( | 1530 other_channel_ = SyncChannel::CreateClient( |
1530 new SyncChannel(other_channel_name_, | 1531 other_channel_name_, |
1531 Channel::MODE_CLIENT, | 1532 this, |
1532 this, | 1533 ipc_thread().message_loop_proxy().get(), |
1533 ipc_thread().message_loop_proxy().get(), | 1534 true, |
1534 true, | 1535 shutdown_event()); |
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_.reset( | 1609 server2_channel_ = SyncChannel::CreateClient( |
1610 new SyncChannel("reentrant_reply2", | 1610 "reentrant_reply2", |
1611 Channel::MODE_CLIENT, | 1611 this, |
1612 this, | 1612 ipc_thread().message_loop_proxy().get(), |
1613 ipc_thread().message_loop_proxy().get(), | 1613 true, |
1614 true, | 1614 shutdown_event()); |
1615 shutdown_event())); | |
1616 server_ready_->Signal(); | 1615 server_ready_->Signal(); |
1617 Message* msg = new SyncChannelTestMsg_Reentrant1(); | 1616 Message* msg = new SyncChannelTestMsg_Reentrant1(); |
1618 server2_channel_->Send(msg); | 1617 server2_channel_->Send(msg); |
1619 server2_channel_.reset(); | 1618 server2_channel_.reset(); |
1620 Done(); | 1619 Done(); |
1621 } | 1620 } |
1622 | 1621 |
1623 private: | 1622 private: |
1624 virtual bool OnMessageReceived(const Message& message) OVERRIDE { | 1623 virtual bool OnMessageReceived(const Message& message) OVERRIDE { |
1625 IPC_BEGIN_MESSAGE_MAP(ReentrantReplyServer1, message) | 1624 IPC_BEGIN_MESSAGE_MAP(ReentrantReplyServer1, message) |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1784 } | 1783 } |
1785 | 1784 |
1786 // Windows needs to send an out-of-band secret to verify the client end of the | 1785 // 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. | 1786 // channel. Test that we still connect correctly in that case. |
1788 TEST_F(IPCSyncChannelTest, Verified) { | 1787 TEST_F(IPCSyncChannelTest, Verified) { |
1789 Verified(); | 1788 Verified(); |
1790 } | 1789 } |
1791 | 1790 |
1792 } // namespace | 1791 } // namespace |
1793 } // namespace IPC | 1792 } // namespace IPC |
OLD | NEW |