OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 | 448 |
449 ResultListener result_listener_; | 449 ResultListener result_listener_; |
450 }; | 450 }; |
451 | 451 |
452 using OnMessageReceivedCallback = void (*)(IPC::Sender* sender, | 452 using OnMessageReceivedCallback = void (*)(IPC::Sender* sender, |
453 const IPC::Message& message); | 453 const IPC::Message& message); |
454 | 454 |
455 // These objects are globally accessible, and are expected to outlive all IPC | 455 // These objects are globally accessible, and are expected to outlive all IPC |
456 // Channels. | 456 // Channels. |
457 struct ChildProcessGlobals { | 457 struct ChildProcessGlobals { |
458 IPC::AttachmentBrokerPrivilegedMac broker; | 458 scoped_ptr<IPC::AttachmentBrokerPrivilegedMac> broker; |
459 MockPortProvider port_provider; | 459 MockPortProvider port_provider; |
460 base::mac::ScopedMachSendRight server_task_port; | 460 base::mac::ScopedMachSendRight server_task_port; |
461 }; | 461 }; |
462 | 462 |
463 // Sets up the Mach communication ports with the server. Returns a set of | 463 // Sets up the Mach communication ports with the server. Returns a set of |
464 // globals that must live at least as long as the test. | 464 // globals that must live at least as long as the test. |
465 scoped_ptr<ChildProcessGlobals> CommonChildProcessSetUp() { | 465 scoped_ptr<ChildProcessGlobals> CommonChildProcessSetUp() { |
466 base::CommandLine cmd_line = *base::CommandLine::ForCurrentProcess(); | 466 base::CommandLine cmd_line = *base::CommandLine::ForCurrentProcess(); |
467 std::string service_name = | 467 std::string service_name = |
468 cmd_line.GetSwitchValueASCII(g_service_switch_name); | 468 cmd_line.GetSwitchValueASCII(g_service_switch_name); |
469 base::mac::ScopedMachSendRight server_port( | 469 base::mac::ScopedMachSendRight server_port( |
470 IPC::LookupServer(service_name.c_str())); | 470 IPC::LookupServer(service_name.c_str())); |
471 base::mac::ScopedMachReceiveRight client_port(IPC::MakeReceivingPort()); | 471 base::mac::ScopedMachReceiveRight client_port(IPC::MakeReceivingPort()); |
472 | 472 |
473 // Send the port that this process is listening on to the server. | 473 // Send the port that this process is listening on to the server. |
474 IPC::SendMachPort( | 474 IPC::SendMachPort( |
475 server_port.get(), client_port.get(), MACH_MSG_TYPE_MAKE_SEND); | 475 server_port.get(), client_port.get(), MACH_MSG_TYPE_MAKE_SEND); |
476 | 476 |
477 // Receive the task port of the server process. | 477 // Receive the task port of the server process. |
478 base::mac::ScopedMachSendRight server_task_port( | 478 base::mac::ScopedMachSendRight server_task_port( |
479 IPC::ReceiveMachPort(client_port.get())); | 479 IPC::ReceiveMachPort(client_port.get())); |
480 | 480 |
481 scoped_ptr<ChildProcessGlobals> globals(new ChildProcessGlobals); | 481 scoped_ptr<ChildProcessGlobals> globals(new ChildProcessGlobals); |
| 482 globals->broker.reset( |
| 483 new IPC::AttachmentBrokerPrivilegedMac(&globals->port_provider)); |
482 globals->port_provider.InsertEntry(getppid(), server_task_port.get()); | 484 globals->port_provider.InsertEntry(getppid(), server_task_port.get()); |
483 globals->broker.set_port_provider(&globals->port_provider); | |
484 globals->server_task_port.reset(server_task_port.release()); | 485 globals->server_task_port.reset(server_task_port.release()); |
485 return globals; | 486 return globals; |
486 } | 487 } |
487 | 488 |
488 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, | 489 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, |
489 const char* channel_name) { | 490 const char* channel_name) { |
490 LOG(INFO) << "Privileged process start."; | 491 LOG(INFO) << "Privileged process start."; |
491 scoped_ptr<ChildProcessGlobals> globals(CommonChildProcessSetUp()); | 492 scoped_ptr<ChildProcessGlobals> globals(CommonChildProcessSetUp()); |
492 | 493 |
493 mach_msg_type_number_t active_names_at_start = IPC::GetActiveNameCount(); | 494 mach_msg_type_number_t active_names_at_start = IPC::GetActiveNameCount(); |
494 | 495 |
495 base::MessageLoopForIO main_message_loop; | 496 base::MessageLoopForIO main_message_loop; |
496 ProxyListener listener; | 497 ProxyListener listener; |
497 | 498 |
498 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( | 499 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( |
499 IPCTestBase::GetChannelName(channel_name), &listener)); | 500 IPCTestBase::GetChannelName(channel_name), &listener)); |
500 globals->broker.RegisterCommunicationChannel(channel.get()); | 501 globals->broker->RegisterCommunicationChannel(channel.get()); |
501 CHECK(channel->Connect()); | 502 CHECK(channel->Connect()); |
502 | 503 |
503 while (true) { | 504 while (true) { |
504 LOG(INFO) << "Privileged process spinning run loop."; | 505 LOG(INFO) << "Privileged process spinning run loop."; |
505 base::MessageLoop::current()->Run(); | 506 base::MessageLoop::current()->Run(); |
506 ProxyListener::Reason reason = listener.get_reason(); | 507 ProxyListener::Reason reason = listener.get_reason(); |
507 if (reason == ProxyListener::CHANNEL_ERROR) | 508 if (reason == ProxyListener::CHANNEL_ERROR) |
508 break; | 509 break; |
509 | 510 |
510 while (listener.has_message()) { | 511 while (listener.has_message()) { |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 // Do nothing special. The default behavior already runs the | 832 // Do nothing special. The default behavior already runs the |
832 // AttachmentBrokerPrivilegedMac. | 833 // AttachmentBrokerPrivilegedMac. |
833 } | 834 } |
834 | 835 |
835 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleToSelf) { | 836 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleToSelf) { |
836 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleToSelfCallback, | 837 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleToSelfCallback, |
837 "SendSharedMemoryHandleToSelf"); | 838 "SendSharedMemoryHandleToSelf"); |
838 } | 839 } |
839 | 840 |
840 } // namespace | 841 } // namespace |
OLD | NEW |