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_host.h" |
17 | 17 |
18 #if defined(OS_POSIX) | 18 #if defined(OS_POSIX) |
19 #include "base/file_descriptor_posix.h" | 19 #include "base/file_descriptor_posix.h" |
| 20 #include "ipc/ipc_platform_file_attachment_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: |
26 ListenerThatExpectsOK() | 27 ListenerThatExpectsOK() |
27 : received_ok_(false) {} | 28 : received_ok_(false) {} |
28 | 29 |
29 ~ListenerThatExpectsOK() override {} | 30 ~ListenerThatExpectsOK() override {} |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 public: | 322 public: |
322 ListenerThatExpectsFile() | 323 ListenerThatExpectsFile() |
323 : sender_(NULL) {} | 324 : sender_(NULL) {} |
324 | 325 |
325 ~ListenerThatExpectsFile() override {} | 326 ~ListenerThatExpectsFile() override {} |
326 | 327 |
327 bool OnMessageReceived(const IPC::Message& message) override { | 328 bool OnMessageReceived(const IPC::Message& message) override { |
328 PickleIterator iter(message); | 329 PickleIterator iter(message); |
329 | 330 |
330 base::ScopedFD fd; | 331 base::ScopedFD fd; |
331 EXPECT_TRUE(message.ReadFile(&iter, &fd)); | 332 scoped_refptr<IPC::MessageAttachment> attachment; |
332 base::File file(fd.release()); | 333 EXPECT_TRUE(message.ReadAttachment(&iter, &attachment)); |
| 334 base::File file(attachment->TakePlatformFile()); |
333 std::string content(GetSendingFileContent().size(), ' '); | 335 std::string content(GetSendingFileContent().size(), ' '); |
334 file.Read(0, &content[0], content.size()); | 336 file.Read(0, &content[0], content.size()); |
335 EXPECT_EQ(content, GetSendingFileContent()); | 337 EXPECT_EQ(content, GetSendingFileContent()); |
336 base::MessageLoop::current()->Quit(); | 338 base::MessageLoop::current()->Quit(); |
337 ListenerThatExpectsOK::SendOK(sender_); | 339 ListenerThatExpectsOK::SendOK(sender_); |
338 return true; | 340 return true; |
339 } | 341 } |
340 | 342 |
341 void OnChannelError() override { | 343 void OnChannelError() override { |
342 NOTREACHED(); | 344 NOTREACHED(); |
343 } | 345 } |
344 | 346 |
345 static std::string GetSendingFileContent() { | 347 static std::string GetSendingFileContent() { |
346 return "Hello"; | 348 return "Hello"; |
347 } | 349 } |
348 | 350 |
349 static base::FilePath GetSendingFilePath() { | 351 static base::FilePath GetSendingFilePath() { |
350 base::FilePath path; | 352 base::FilePath path; |
351 bool ok = PathService::Get(base::DIR_CACHE, &path); | 353 bool ok = PathService::Get(base::DIR_CACHE, &path); |
352 EXPECT_TRUE(ok); | 354 EXPECT_TRUE(ok); |
353 return path.Append("ListenerThatExpectsFile.txt"); | 355 return path.Append("ListenerThatExpectsFile.txt"); |
354 } | 356 } |
355 | 357 |
356 static void WriteAndSendFile(IPC::Sender* sender, base::File& file) { | 358 static void WriteAndSendFile(IPC::Sender* sender, base::File& file) { |
357 std::string content = GetSendingFileContent(); | 359 std::string content = GetSendingFileContent(); |
358 file.WriteAtCurrentPos(content.data(), content.size()); | 360 file.WriteAtCurrentPos(content.data(), content.size()); |
359 file.Flush(); | 361 file.Flush(); |
360 IPC::Message* message = new IPC::Message( | 362 IPC::Message* message = new IPC::Message( |
361 0, 2, IPC::Message::PRIORITY_NORMAL); | 363 0, 2, IPC::Message::PRIORITY_NORMAL); |
362 message->WriteFile(base::ScopedFD(file.TakePlatformFile())); | 364 message->WriteAttachment(new IPC::internal::PlatformFileAttachment( |
| 365 base::ScopedFD(file.TakePlatformFile()))); |
363 ASSERT_TRUE(sender->Send(message)); | 366 ASSERT_TRUE(sender->Send(message)); |
364 } | 367 } |
365 | 368 |
366 void set_sender(IPC::Sender* sender) { sender_ = sender; } | 369 void set_sender(IPC::Sender* sender) { sender_ = sender; } |
367 | 370 |
368 private: | 371 private: |
369 IPC::Sender* sender_; | 372 IPC::Sender* sender_; |
370 }; | 373 }; |
371 | 374 |
372 | 375 |
(...skipping 24 matching lines...) Expand all Loading... |
397 client.Connect(); | 400 client.Connect(); |
398 listener.set_sender(client.channel()); | 401 listener.set_sender(client.channel()); |
399 | 402 |
400 base::MessageLoop::current()->Run(); | 403 base::MessageLoop::current()->Run(); |
401 | 404 |
402 return 0; | 405 return 0; |
403 } | 406 } |
404 #endif | 407 #endif |
405 | 408 |
406 } // namespace | 409 } // namespace |
OLD | NEW |