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" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 class ListenerThatExpectsOK : public IPC::Listener { | 25 class ListenerThatExpectsOK : public IPC::Listener { |
26 public: | 26 public: |
27 ListenerThatExpectsOK() | 27 ListenerThatExpectsOK() |
28 : received_ok_(false) {} | 28 : received_ok_(false) {} |
29 | 29 |
30 virtual ~ListenerThatExpectsOK() {} | 30 virtual ~ListenerThatExpectsOK() {} |
31 | 31 |
32 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 32 virtual bool OnMessageReceived(const IPC::Message& message) override { |
33 PickleIterator iter(message); | 33 PickleIterator iter(message); |
34 std::string should_be_ok; | 34 std::string should_be_ok; |
35 EXPECT_TRUE(iter.ReadString(&should_be_ok)); | 35 EXPECT_TRUE(iter.ReadString(&should_be_ok)); |
36 EXPECT_EQ(should_be_ok, "OK"); | 36 EXPECT_EQ(should_be_ok, "OK"); |
37 received_ok_ = true; | 37 received_ok_ = true; |
38 base::MessageLoop::current()->Quit(); | 38 base::MessageLoop::current()->Quit(); |
39 return true; | 39 return true; |
40 } | 40 } |
41 | 41 |
42 virtual void OnChannelError() OVERRIDE { | 42 virtual void OnChannelError() override { |
43 // The connection should be healthy while the listener is waiting | 43 // The connection should be healthy while the listener is waiting |
44 // message. An error can occur after that because the peer | 44 // message. An error can occur after that because the peer |
45 // process dies. | 45 // process dies. |
46 DCHECK(received_ok_); | 46 DCHECK(received_ok_); |
47 } | 47 } |
48 | 48 |
49 static void SendOK(IPC::Sender* sender) { | 49 static void SendOK(IPC::Sender* sender) { |
50 IPC::Message* message = new IPC::Message( | 50 IPC::Message* message = new IPC::Message( |
51 0, 2, IPC::Message::PRIORITY_NORMAL); | 51 0, 2, IPC::Message::PRIORITY_NORMAL); |
52 message->WriteString(std::string("OK")); | 52 message->WriteString(std::string("OK")); |
(...skipping 21 matching lines...) Expand all Loading... |
74 | 74 |
75 private: | 75 private: |
76 base::MessageLoopForIO main_message_loop_; | 76 base::MessageLoopForIO main_message_loop_; |
77 scoped_ptr<IPC::ChannelMojo> channel_; | 77 scoped_ptr<IPC::ChannelMojo> channel_; |
78 }; | 78 }; |
79 | 79 |
80 class IPCChannelMojoTest : public IPCTestBase { | 80 class IPCChannelMojoTest : public IPCTestBase { |
81 protected: | 81 protected: |
82 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( | 82 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
83 const IPC::ChannelHandle& handle, | 83 const IPC::ChannelHandle& handle, |
84 base::TaskRunner* runner) OVERRIDE { | 84 base::TaskRunner* runner) override { |
85 host_.reset(new IPC::ChannelMojoHost(task_runner())); | 85 host_.reset(new IPC::ChannelMojoHost(task_runner())); |
86 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), | 86 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), |
87 handle); | 87 handle); |
88 } | 88 } |
89 | 89 |
90 virtual bool DidStartClient() OVERRIDE { | 90 virtual bool DidStartClient() override { |
91 bool ok = IPCTestBase::DidStartClient(); | 91 bool ok = IPCTestBase::DidStartClient(); |
92 DCHECK(ok); | 92 DCHECK(ok); |
93 host_->OnClientLaunched(client_process()); | 93 host_->OnClientLaunched(client_process()); |
94 return ok; | 94 return ok; |
95 } | 95 } |
96 | 96 |
97 private: | 97 private: |
98 scoped_ptr<IPC::ChannelMojoHost> host_; | 98 scoped_ptr<IPC::ChannelMojoHost> host_; |
99 }; | 99 }; |
100 | 100 |
101 | 101 |
102 class TestChannelListenerWithExtraExpectations | 102 class TestChannelListenerWithExtraExpectations |
103 : public IPC::TestChannelListener { | 103 : public IPC::TestChannelListener { |
104 public: | 104 public: |
105 TestChannelListenerWithExtraExpectations() | 105 TestChannelListenerWithExtraExpectations() |
106 : is_connected_called_(false) { | 106 : is_connected_called_(false) { |
107 } | 107 } |
108 | 108 |
109 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { | 109 virtual void OnChannelConnected(int32 peer_pid) override { |
110 IPC::TestChannelListener::OnChannelConnected(peer_pid); | 110 IPC::TestChannelListener::OnChannelConnected(peer_pid); |
111 EXPECT_TRUE(base::kNullProcessId != peer_pid); | 111 EXPECT_TRUE(base::kNullProcessId != peer_pid); |
112 is_connected_called_ = true; | 112 is_connected_called_ = true; |
113 } | 113 } |
114 | 114 |
115 bool is_connected_called() const { return is_connected_called_; } | 115 bool is_connected_called() const { return is_connected_called_; } |
116 | 116 |
117 private: | 117 private: |
118 bool is_connected_called_; | 118 bool is_connected_called_; |
119 }; | 119 }; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 158 |
159 return 0; | 159 return 0; |
160 } | 160 } |
161 | 161 |
162 class ListenerExpectingErrors : public IPC::Listener { | 162 class ListenerExpectingErrors : public IPC::Listener { |
163 public: | 163 public: |
164 ListenerExpectingErrors() | 164 ListenerExpectingErrors() |
165 : has_error_(false) { | 165 : has_error_(false) { |
166 } | 166 } |
167 | 167 |
168 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { | 168 virtual void OnChannelConnected(int32 peer_pid) override { |
169 base::MessageLoop::current()->Quit(); | 169 base::MessageLoop::current()->Quit(); |
170 } | 170 } |
171 | 171 |
172 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 172 virtual bool OnMessageReceived(const IPC::Message& message) override { |
173 return true; | 173 return true; |
174 } | 174 } |
175 | 175 |
176 virtual void OnChannelError() OVERRIDE { | 176 virtual void OnChannelError() override { |
177 has_error_ = true; | 177 has_error_ = true; |
178 base::MessageLoop::current()->Quit(); | 178 base::MessageLoop::current()->Quit(); |
179 } | 179 } |
180 | 180 |
181 bool has_error() const { return has_error_; } | 181 bool has_error() const { return has_error_; } |
182 | 182 |
183 private: | 183 private: |
184 bool has_error_; | 184 bool has_error_; |
185 }; | 185 }; |
186 | 186 |
187 | 187 |
188 class IPCChannelMojoErrorTest : public IPCTestBase { | 188 class IPCChannelMojoErrorTest : public IPCTestBase { |
189 protected: | 189 protected: |
190 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( | 190 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
191 const IPC::ChannelHandle& handle, | 191 const IPC::ChannelHandle& handle, |
192 base::TaskRunner* runner) OVERRIDE { | 192 base::TaskRunner* runner) override { |
193 host_.reset(new IPC::ChannelMojoHost(task_runner())); | 193 host_.reset(new IPC::ChannelMojoHost(task_runner())); |
194 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), | 194 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), |
195 handle); | 195 handle); |
196 } | 196 } |
197 | 197 |
198 virtual bool DidStartClient() OVERRIDE { | 198 virtual bool DidStartClient() override { |
199 bool ok = IPCTestBase::DidStartClient(); | 199 bool ok = IPCTestBase::DidStartClient(); |
200 DCHECK(ok); | 200 DCHECK(ok); |
201 host_->OnClientLaunched(client_process()); | 201 host_->OnClientLaunched(client_process()); |
202 return ok; | 202 return ok; |
203 } | 203 } |
204 | 204 |
205 private: | 205 private: |
206 scoped_ptr<IPC::ChannelMojoHost> host_; | 206 scoped_ptr<IPC::ChannelMojoHost> host_; |
207 }; | 207 }; |
208 | 208 |
209 class ListenerThatQuits : public IPC::Listener { | 209 class ListenerThatQuits : public IPC::Listener { |
210 public: | 210 public: |
211 ListenerThatQuits() { | 211 ListenerThatQuits() { |
212 } | 212 } |
213 | 213 |
214 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 214 virtual bool OnMessageReceived(const IPC::Message& message) override { |
215 return true; | 215 return true; |
216 } | 216 } |
217 | 217 |
218 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { | 218 virtual void OnChannelConnected(int32 peer_pid) override { |
219 base::MessageLoop::current()->Quit(); | 219 base::MessageLoop::current()->Quit(); |
220 } | 220 } |
221 }; | 221 }; |
222 | 222 |
223 // A long running process that connects to us. | 223 // A long running process that connects to us. |
224 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) { | 224 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) { |
225 ListenerThatQuits listener; | 225 ListenerThatQuits listener; |
226 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient"); | 226 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient"); |
227 client.Connect(); | 227 client.Connect(); |
228 | 228 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 | 261 |
262 | 262 |
263 #if defined(OS_POSIX) | 263 #if defined(OS_POSIX) |
264 class ListenerThatExpectsFile : public IPC::Listener { | 264 class ListenerThatExpectsFile : public IPC::Listener { |
265 public: | 265 public: |
266 ListenerThatExpectsFile() | 266 ListenerThatExpectsFile() |
267 : sender_(NULL) {} | 267 : sender_(NULL) {} |
268 | 268 |
269 virtual ~ListenerThatExpectsFile() {} | 269 virtual ~ListenerThatExpectsFile() {} |
270 | 270 |
271 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 271 virtual bool OnMessageReceived(const IPC::Message& message) override { |
272 PickleIterator iter(message); | 272 PickleIterator iter(message); |
273 | 273 |
274 base::ScopedFD fd; | 274 base::ScopedFD fd; |
275 EXPECT_TRUE(message.ReadFile(&iter, &fd)); | 275 EXPECT_TRUE(message.ReadFile(&iter, &fd)); |
276 base::File file(fd.release()); | 276 base::File file(fd.release()); |
277 std::string content(GetSendingFileContent().size(), ' '); | 277 std::string content(GetSendingFileContent().size(), ' '); |
278 file.Read(0, &content[0], content.size()); | 278 file.Read(0, &content[0], content.size()); |
279 EXPECT_EQ(content, GetSendingFileContent()); | 279 EXPECT_EQ(content, GetSendingFileContent()); |
280 base::MessageLoop::current()->Quit(); | 280 base::MessageLoop::current()->Quit(); |
281 ListenerThatExpectsOK::SendOK(sender_); | 281 ListenerThatExpectsOK::SendOK(sender_); |
282 return true; | 282 return true; |
283 } | 283 } |
284 | 284 |
285 virtual void OnChannelError() OVERRIDE { | 285 virtual void OnChannelError() override { |
286 NOTREACHED(); | 286 NOTREACHED(); |
287 } | 287 } |
288 | 288 |
289 static std::string GetSendingFileContent() { | 289 static std::string GetSendingFileContent() { |
290 return "Hello"; | 290 return "Hello"; |
291 } | 291 } |
292 | 292 |
293 static base::FilePath GetSendingFilePath() { | 293 static base::FilePath GetSendingFilePath() { |
294 base::FilePath path; | 294 base::FilePath path; |
295 bool ok = PathService::Get(base::DIR_CACHE, &path); | 295 bool ok = PathService::Get(base::DIR_CACHE, &path); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 client.Connect(); | 341 client.Connect(); |
342 listener.set_sender(client.channel()); | 342 listener.set_sender(client.channel()); |
343 | 343 |
344 base::MessageLoop::current()->Run(); | 344 base::MessageLoop::current()->Run(); |
345 | 345 |
346 return 0; | 346 return 0; |
347 } | 347 } |
348 #endif | 348 #endif |
349 | 349 |
350 } // namespace | 350 } // namespace |
OLD | NEW |