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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 CHECK(is_shutdown_); | 63 CHECK(is_shutdown_); |
64 } | 64 } |
65 void AddRef() { } | 65 void AddRef() { } |
66 void Release() { } | 66 void Release() { } |
67 virtual bool Send(Message* msg) OVERRIDE { return channel_->Send(msg); } | 67 virtual bool Send(Message* msg) OVERRIDE { return channel_->Send(msg); } |
68 bool SendWithTimeout(Message* msg, int timeout_ms) { | 68 bool SendWithTimeout(Message* msg, int timeout_ms) { |
69 return channel_->SendWithTimeout(msg, timeout_ms); | 69 return channel_->SendWithTimeout(msg, timeout_ms); |
70 } | 70 } |
71 void WaitForChannelCreation() { channel_created_->Wait(); } | 71 void WaitForChannelCreation() { channel_created_->Wait(); } |
72 void CloseChannel() { | 72 void CloseChannel() { |
73 DCHECK(MessageLoop::current() == ListenerThread()->message_loop()); | 73 DCHECK(base::MessageLoop::current() == ListenerThread()->message_loop()); |
74 channel_->Close(); | 74 channel_->Close(); |
75 } | 75 } |
76 void Start() { | 76 void Start() { |
77 StartThread(&listener_thread_, MessageLoop::TYPE_DEFAULT); | 77 StartThread(&listener_thread_, base::MessageLoop::TYPE_DEFAULT); |
78 ListenerThread()->message_loop()->PostTask( | 78 ListenerThread()->message_loop() |
79 FROM_HERE, base::Bind(&Worker::OnStart, this)); | 79 ->PostTask(FROM_HERE, base::Bind(&Worker::OnStart, this)); |
agl
2013/04/30 17:38:49
This specific change is just formatting and is mak
xhwang
2013/04/30 17:46:54
Done.
| |
80 } | 80 } |
81 void Shutdown() { | 81 void Shutdown() { |
82 // The IPC thread needs to outlive SyncChannel. We can't do this in | 82 // The IPC thread needs to outlive SyncChannel. We can't do this in |
83 // ~Worker(), since that'll reset the vtable pointer (to Worker's), which | 83 // ~Worker(), since that'll reset the vtable pointer (to Worker's), which |
84 // may result in a race conditions. See http://crbug.com/25841. | 84 // may result in a race conditions. See http://crbug.com/25841. |
85 WaitableEvent listener_done(false, false), ipc_done(false, false); | 85 WaitableEvent listener_done(false, false), ipc_done(false, false); |
86 ListenerThread()->message_loop()->PostTask( | 86 ListenerThread()->message_loop()->PostTask( |
87 FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown1, this, | 87 FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown1, this, |
88 &listener_done, &ipc_done)); | 88 &listener_done, &ipc_done)); |
89 listener_done.Wait(); | 89 listener_done.Wait(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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. |
170 void OnStart() { | 170 void OnStart() { |
171 // Link ipc_thread_, listener_thread_ and channel_ altogether. | 171 // Link ipc_thread_, listener_thread_ and channel_ altogether. |
172 StartThread(&ipc_thread_, MessageLoop::TYPE_IO); | 172 StartThread(&ipc_thread_, base::MessageLoop::TYPE_IO); |
173 channel_.reset(CreateChannel()); | 173 channel_.reset(CreateChannel()); |
174 channel_created_->Signal(); | 174 channel_created_->Signal(); |
175 Run(); | 175 Run(); |
176 } | 176 } |
177 | 177 |
178 void OnListenerThreadShutdown1(WaitableEvent* listener_event, | 178 void OnListenerThreadShutdown1(WaitableEvent* listener_event, |
179 WaitableEvent* ipc_event) { | 179 WaitableEvent* ipc_event) { |
180 // SyncChannel needs to be destructed on the thread that it was created on. | 180 // SyncChannel needs to be destructed on the thread that it was created on. |
181 channel_.reset(); | 181 channel_.reset(); |
182 | 182 |
(...skipping 23 matching lines...) Expand all Loading... | |
206 IPC_BEGIN_MESSAGE_MAP(Worker, message) | 206 IPC_BEGIN_MESSAGE_MAP(Worker, message) |
207 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay) | 207 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay) |
208 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife, | 208 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife, |
209 OnAnswerDelay) | 209 OnAnswerDelay) |
210 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String, | 210 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String, |
211 OnNestedTestMsg) | 211 OnNestedTestMsg) |
212 IPC_END_MESSAGE_MAP() | 212 IPC_END_MESSAGE_MAP() |
213 return true; | 213 return true; |
214 } | 214 } |
215 | 215 |
216 void StartThread(base::Thread* thread, MessageLoop::Type type) { | 216 void StartThread(base::Thread* thread, base::MessageLoop::Type type) { |
217 base::Thread::Options options; | 217 base::Thread::Options options; |
218 options.message_loop_type = type; | 218 options.message_loop_type = type; |
219 thread->StartWithOptions(options); | 219 thread->StartWithOptions(options); |
220 } | 220 } |
221 | 221 |
222 scoped_ptr<WaitableEvent> done_; | 222 scoped_ptr<WaitableEvent> done_; |
223 scoped_ptr<WaitableEvent> channel_created_; | 223 scoped_ptr<WaitableEvent> channel_created_; |
224 std::string channel_name_; | 224 std::string channel_name_; |
225 Channel::Mode mode_; | 225 Channel::Mode mode_; |
226 scoped_ptr<SyncChannel> channel_; | 226 scoped_ptr<SyncChannel> channel_; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 workers[i]->done_event()->Wait(); | 259 workers[i]->done_event()->Wait(); |
260 | 260 |
261 for (size_t i = 0; i < workers.size(); ++i) { | 261 for (size_t i = 0; i < workers.size(); ++i) { |
262 workers[i]->Shutdown(); | 262 workers[i]->Shutdown(); |
263 delete workers[i]; | 263 delete workers[i]; |
264 } | 264 } |
265 } | 265 } |
266 | 266 |
267 class IPCSyncChannelTest : public testing::Test { | 267 class IPCSyncChannelTest : public testing::Test { |
268 private: | 268 private: |
269 MessageLoop message_loop_; | 269 base::MessageLoop message_loop_; |
270 }; | 270 }; |
271 | 271 |
272 //------------------------------------------------------------------------------ | 272 //------------------------------------------------------------------------------ |
273 | 273 |
274 class SimpleServer : public Worker { | 274 class SimpleServer : public Worker { |
275 public: | 275 public: |
276 explicit SimpleServer(bool pump_during_send) | 276 explicit SimpleServer(bool pump_during_send) |
277 : Worker(Channel::MODE_SERVER, "simpler_server"), | 277 : Worker(Channel::MODE_SERVER, "simpler_server"), |
278 pump_during_send_(pump_during_send) { } | 278 pump_during_send_(pump_during_send) { } |
279 virtual void Run() OVERRIDE { | 279 virtual void Run() OVERRIDE { |
(...skipping 714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
994 void TimeoutCallback() { | 994 void TimeoutCallback() { |
995 timeout_occurred = true; | 995 timeout_occurred = true; |
996 } | 996 } |
997 | 997 |
998 class DoneEventRaceServer : public Worker { | 998 class DoneEventRaceServer : public Worker { |
999 public: | 999 public: |
1000 DoneEventRaceServer() | 1000 DoneEventRaceServer() |
1001 : Worker(Channel::MODE_SERVER, "done_event_race_server") { } | 1001 : Worker(Channel::MODE_SERVER, "done_event_race_server") { } |
1002 | 1002 |
1003 virtual void Run() OVERRIDE { | 1003 virtual void Run() OVERRIDE { |
1004 MessageLoop::current()->PostTask(FROM_HERE, | 1004 base::MessageLoop::current()->PostTask(FROM_HERE, |
1005 base::Bind(&NestedCallback, this)); | 1005 base::Bind(&NestedCallback, this)); |
1006 MessageLoop::current()->PostDelayedTask( | 1006 base::MessageLoop::current()->PostDelayedTask( |
1007 FROM_HERE, | 1007 FROM_HERE, |
1008 base::Bind(&TimeoutCallback), | 1008 base::Bind(&TimeoutCallback), |
1009 base::TimeDelta::FromSeconds(9)); | 1009 base::TimeDelta::FromSeconds(9)); |
1010 // Even though we have a timeout on the Send, it will succeed since for this | 1010 // Even though we have a timeout on the Send, it will succeed since for this |
1011 // bug, the reply message comes back and is deserialized, however the done | 1011 // bug, the reply message comes back and is deserialized, however the done |
1012 // event wasn't set. So we indirectly use the timeout task to notice if a | 1012 // event wasn't set. So we indirectly use the timeout task to notice if a |
1013 // timeout occurred. | 1013 // timeout occurred. |
1014 SendAnswerToLife(true, 10000, true); | 1014 SendAnswerToLife(true, 10000, true); |
1015 DCHECK(!timeout_occurred); | 1015 DCHECK(!timeout_occurred); |
1016 Done(); | 1016 Done(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1061 Worker* worker_; | 1061 Worker* worker_; |
1062 scoped_refptr<base::MessageLoopProxy> message_loop_; | 1062 scoped_refptr<base::MessageLoopProxy> message_loop_; |
1063 }; | 1063 }; |
1064 | 1064 |
1065 class SyncMessageFilterServer : public Worker { | 1065 class SyncMessageFilterServer : public Worker { |
1066 public: | 1066 public: |
1067 SyncMessageFilterServer() | 1067 SyncMessageFilterServer() |
1068 : Worker(Channel::MODE_SERVER, "sync_message_filter_server"), | 1068 : Worker(Channel::MODE_SERVER, "sync_message_filter_server"), |
1069 thread_("helper_thread") { | 1069 thread_("helper_thread") { |
1070 base::Thread::Options options; | 1070 base::Thread::Options options; |
1071 options.message_loop_type = MessageLoop::TYPE_DEFAULT; | 1071 options.message_loop_type = base::MessageLoop::TYPE_DEFAULT; |
1072 thread_.StartWithOptions(options); | 1072 thread_.StartWithOptions(options); |
1073 filter_ = new TestSyncMessageFilter(shutdown_event(), this, | 1073 filter_ = new TestSyncMessageFilter(shutdown_event(), this, |
1074 thread_.message_loop_proxy()); | 1074 thread_.message_loop_proxy()); |
1075 } | 1075 } |
1076 | 1076 |
1077 virtual void Run() OVERRIDE { | 1077 virtual void Run() OVERRIDE { |
1078 channel()->AddFilter(filter_.get()); | 1078 channel()->AddFilter(filter_.get()); |
1079 } | 1079 } |
1080 | 1080 |
1081 base::Thread thread_; | 1081 base::Thread thread_; |
(...skipping 798 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1880 } | 1880 } |
1881 | 1881 |
1882 // Windows needs to send an out-of-band secret to verify the client end of the | 1882 // Windows needs to send an out-of-band secret to verify the client end of the |
1883 // channel. Test that we still connect correctly in that case. | 1883 // channel. Test that we still connect correctly in that case. |
1884 TEST_F(IPCSyncChannelTest, Verified) { | 1884 TEST_F(IPCSyncChannelTest, Verified) { |
1885 Verified(); | 1885 Verified(); |
1886 } | 1886 } |
1887 | 1887 |
1888 } // namespace | 1888 } // namespace |
1889 } // namespace IPC | 1889 } // namespace IPC |
OLD | NEW |