OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/mojo/ipc_channel_mojo.h" | 5 #include "ipc/mojo/ipc_channel_mojo.h" |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
13 #include "ipc/ipc_message.h" | 13 #include "ipc/ipc_message.h" |
14 #include "ipc/ipc_test_base.h" | 14 #include "ipc/ipc_test_base.h" |
15 #include "ipc/ipc_test_channel_listener.h" | 15 #include "ipc/ipc_test_channel_listener.h" |
| 16 #include "ipc/mojo/ipc_channel_mojo_host.h" |
16 #include "ipc/mojo/ipc_channel_mojo_readers.h" | 17 #include "ipc/mojo/ipc_channel_mojo_readers.h" |
17 | 18 |
18 #if defined(OS_POSIX) | 19 #if defined(OS_POSIX) |
19 #include "base/file_descriptor_posix.h" | 20 #include "base/file_descriptor_posix.h" |
20 #endif | 21 #endif |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 class ListenerThatExpectsOK : public IPC::Listener { | 25 class ListenerThatExpectsOK : public IPC::Listener { |
25 public: | 26 public: |
(...skipping 26 matching lines...) Expand all Loading... |
52 ASSERT_TRUE(sender->Send(message)); | 53 ASSERT_TRUE(sender->Send(message)); |
53 } | 54 } |
54 | 55 |
55 private: | 56 private: |
56 bool received_ok_; | 57 bool received_ok_; |
57 }; | 58 }; |
58 | 59 |
59 class ChannelClient { | 60 class ChannelClient { |
60 public: | 61 public: |
61 explicit ChannelClient(IPC::Listener* listener, const char* name) { | 62 explicit ChannelClient(IPC::Listener* listener, const char* name) { |
62 channel_ = IPC::ChannelMojo::Create( | 63 channel_ = IPC::ChannelMojo::Create(NULL, |
63 IPCTestBase::GetChannelName(name), IPC::Channel::MODE_CLIENT, listener, | 64 IPCTestBase::GetChannelName(name), |
64 main_message_loop_.message_loop_proxy()); | 65 IPC::Channel::MODE_CLIENT, |
| 66 listener); |
65 } | 67 } |
66 | 68 |
67 void Connect() { | 69 void Connect() { |
68 CHECK(channel_->Connect()); | 70 CHECK(channel_->Connect()); |
69 } | 71 } |
70 | 72 |
71 IPC::ChannelMojo* channel() const { return channel_.get(); } | 73 IPC::ChannelMojo* channel() const { return channel_.get(); } |
72 | 74 |
73 private: | 75 private: |
| 76 base::MessageLoopForIO main_message_loop_; |
74 scoped_ptr<IPC::ChannelMojo> channel_; | 77 scoped_ptr<IPC::ChannelMojo> channel_; |
75 base::MessageLoopForIO main_message_loop_; | |
76 }; | 78 }; |
77 | 79 |
78 class IPCChannelMojoTest : public IPCTestBase { | 80 class IPCChannelMojoTest : public IPCTestBase { |
79 protected: | 81 protected: |
80 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( | 82 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
81 const IPC::ChannelHandle& handle, | 83 const IPC::ChannelHandle& handle, |
82 base::TaskRunner* runner) OVERRIDE { | 84 base::TaskRunner* runner) OVERRIDE { |
83 return IPC::ChannelMojo::CreateFactory( | 85 host_.reset(new IPC::ChannelMojoHost(task_runner())); |
84 handle, IPC::Channel::MODE_SERVER, runner); | 86 return IPC::ChannelMojo::CreateServerFactory(host_.get(), handle); |
85 } | 87 } |
| 88 |
| 89 virtual bool DidStartClient() OVERRIDE { |
| 90 bool ok = IPCTestBase::DidStartClient(); |
| 91 DCHECK(ok); |
| 92 host_->OnClientLaunched(client_process()); |
| 93 return ok; |
| 94 } |
| 95 |
| 96 private: |
| 97 scoped_ptr<IPC::ChannelMojoHost> host_; |
86 }; | 98 }; |
87 | 99 |
88 | 100 |
89 class TestChannelListenerWithExtraExpectations | 101 class TestChannelListenerWithExtraExpectations |
90 : public IPC::TestChannelListener { | 102 : public IPC::TestChannelListener { |
91 public: | 103 public: |
92 TestChannelListenerWithExtraExpectations() | 104 TestChannelListenerWithExtraExpectations() |
93 : is_connected_called_(false) { | 105 : is_connected_called_(false) { |
94 } | 106 } |
95 | 107 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 base::MessageLoop::current()->Run(); | 154 base::MessageLoop::current()->Run(); |
143 EXPECT_TRUE(listener.is_connected_called()); | 155 EXPECT_TRUE(listener.is_connected_called()); |
144 EXPECT_TRUE(listener.HasSentAll()); | 156 EXPECT_TRUE(listener.HasSentAll()); |
145 | 157 |
146 return 0; | 158 return 0; |
147 } | 159 } |
148 | 160 |
149 // Close given handle before use to simulate an error. | 161 // Close given handle before use to simulate an error. |
150 class ErraticChannelMojo : public IPC::ChannelMojo { | 162 class ErraticChannelMojo : public IPC::ChannelMojo { |
151 public: | 163 public: |
152 ErraticChannelMojo( | 164 ErraticChannelMojo(IPC::ChannelMojoHost* host, |
153 const IPC::ChannelHandle& channel_handle, | 165 const IPC::ChannelHandle& channel_handle, |
154 IPC::Channel::Mode mode, | 166 IPC::Channel::Mode mode, |
155 IPC::Listener* listener, | 167 IPC::Listener* listener, |
156 scoped_refptr<base::TaskRunner> runner) | 168 scoped_refptr<base::TaskRunner> runner) |
157 : ChannelMojo(channel_handle, mode, listener, runner) { | 169 : ChannelMojo(host, channel_handle, mode, listener) {} |
158 } | |
159 | 170 |
160 virtual void OnConnected(mojo::ScopedMessagePipeHandle pipe) { | 171 virtual void OnConnected(mojo::ScopedMessagePipeHandle pipe) { |
161 MojoClose(pipe.get().value()); | 172 MojoClose(pipe.get().value()); |
162 OnConnected(pipe.Pass()); | 173 OnConnected(pipe.Pass()); |
163 } | 174 } |
164 }; | 175 }; |
165 | 176 |
166 // Exists to create ErraticChannelMojo. | 177 // Exists to create ErraticChannelMojo. |
167 class ErraticChannelFactory : public IPC::ChannelFactory { | 178 class ErraticChannelFactory : public IPC::ChannelFactory { |
168 public: | 179 public: |
169 explicit ErraticChannelFactory( | 180 explicit ErraticChannelFactory(IPC::ChannelMojoHost* host, |
170 const IPC::ChannelHandle& handle, | 181 const IPC::ChannelHandle& handle, |
171 base::TaskRunner* runner) | 182 base::TaskRunner* runner) |
172 : handle_(handle), runner_(runner) { | 183 : host_(host), handle_(handle), runner_(runner) {} |
173 } | |
174 | 184 |
175 virtual std::string GetName() const OVERRIDE { | 185 virtual std::string GetName() const OVERRIDE { |
176 return ""; | 186 return ""; |
177 } | 187 } |
178 | 188 |
179 virtual scoped_ptr<IPC::Channel> BuildChannel( | 189 virtual scoped_ptr<IPC::Channel> BuildChannel( |
180 IPC::Listener* listener) OVERRIDE { | 190 IPC::Listener* listener) OVERRIDE { |
181 return scoped_ptr<IPC::Channel>( | 191 return scoped_ptr<IPC::Channel>(new ErraticChannelMojo( |
182 new ErraticChannelMojo( | 192 host_, handle_, IPC::Channel::MODE_SERVER, listener, runner_)); |
183 handle_, IPC::Channel::MODE_SERVER, listener, runner_)); | |
184 } | 193 } |
185 | 194 |
186 private: | 195 private: |
| 196 IPC::ChannelMojoHost* host_; |
187 IPC::ChannelHandle handle_; | 197 IPC::ChannelHandle handle_; |
188 scoped_refptr<base::TaskRunner> runner_; | 198 scoped_refptr<base::TaskRunner> runner_; |
189 }; | 199 }; |
190 | 200 |
191 class ListenerExpectingErrors : public IPC::Listener { | 201 class ListenerExpectingErrors : public IPC::Listener { |
192 public: | 202 public: |
193 ListenerExpectingErrors() | 203 ListenerExpectingErrors() |
194 : has_error_(false) { | 204 : has_error_(false) { |
195 } | 205 } |
196 | 206 |
(...skipping 11 matching lines...) Expand all Loading... |
208 private: | 218 private: |
209 bool has_error_; | 219 bool has_error_; |
210 }; | 220 }; |
211 | 221 |
212 | 222 |
213 class IPCChannelMojoErrorTest : public IPCTestBase { | 223 class IPCChannelMojoErrorTest : public IPCTestBase { |
214 protected: | 224 protected: |
215 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( | 225 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
216 const IPC::ChannelHandle& handle, | 226 const IPC::ChannelHandle& handle, |
217 base::TaskRunner* runner) OVERRIDE { | 227 base::TaskRunner* runner) OVERRIDE { |
| 228 host_.reset(new IPC::ChannelMojoHost(task_runner())); |
218 return scoped_ptr<IPC::ChannelFactory>( | 229 return scoped_ptr<IPC::ChannelFactory>( |
219 new ErraticChannelFactory(handle, runner)); | 230 new ErraticChannelFactory(host_.get(), handle, runner)); |
220 } | 231 } |
| 232 |
| 233 virtual bool DidStartClient() OVERRIDE { |
| 234 bool ok = IPCTestBase::DidStartClient(); |
| 235 DCHECK(ok); |
| 236 host_->OnClientLaunched(client_process()); |
| 237 return ok; |
| 238 } |
| 239 |
| 240 private: |
| 241 scoped_ptr<IPC::ChannelMojoHost> host_; |
221 }; | 242 }; |
222 | 243 |
223 class ListenerThatQuits : public IPC::Listener { | 244 class ListenerThatQuits : public IPC::Listener { |
224 public: | 245 public: |
225 ListenerThatQuits() { | 246 ListenerThatQuits() { |
226 } | 247 } |
227 | 248 |
228 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 249 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
229 return true; | 250 return true; |
230 } | 251 } |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 client.Connect(); | 373 client.Connect(); |
353 listener.set_sender(client.channel()); | 374 listener.set_sender(client.channel()); |
354 | 375 |
355 base::MessageLoop::current()->Run(); | 376 base::MessageLoop::current()->Run(); |
356 | 377 |
357 return 0; | 378 return 0; |
358 } | 379 } |
359 #endif | 380 #endif |
360 | 381 |
361 } // namespace | 382 } // namespace |
OLD | NEW |