OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ipc/attachment_broker_unprivileged_mac.h" | |
6 | |
7 #include <mach/mach.h> | |
8 | |
9 #include "base/mac/mach_port_util.h" | |
10 #include "base/mac/scoped_mach_port.h" | |
11 #include "base/process/process.h" | |
12 #include "ipc/attachment_broker_messages.h" | |
13 #include "ipc/brokerable_attachment.h" | |
14 #include "ipc/ipc_sender.h" | |
15 #include "ipc/mach_port_attachment_mac.h" | |
16 | |
17 namespace IPC { | |
18 | |
19 AttachmentBrokerUnprivilegedMac::AttachmentBrokerUnprivilegedMac() {} | |
20 | |
21 AttachmentBrokerUnprivilegedMac::~AttachmentBrokerUnprivilegedMac() {} | |
22 | |
23 bool AttachmentBrokerUnprivilegedMac::SendAttachmentToProcess( | |
24 const scoped_refptr<IPC::BrokerableAttachment>& attachment, | |
25 base::ProcessId destination_process) { | |
26 switch (attachment->GetBrokerableType()) { | |
27 case BrokerableAttachment::MACH_PORT: { | |
28 internal::MachPortAttachmentMac* mach_port_attachment = | |
29 static_cast<internal::MachPortAttachmentMac*>(attachment.get()); | |
30 internal::MachPortAttachmentMac::WireFormat format = | |
31 mach_port_attachment->GetWireFormat(destination_process); | |
32 bool success = | |
33 get_sender()->Send(new AttachmentBrokerMsg_DuplicateMachPort(format)); | |
34 if (success) | |
35 mach_port_attachment->reset_mach_port_ownership(); | |
36 return success; | |
37 } | |
38 default: | |
39 NOTREACHED(); | |
40 return false; | |
41 } | |
42 return false; | |
43 } | |
44 | |
45 bool AttachmentBrokerUnprivilegedMac::OnMessageReceived(const Message& msg) { | |
46 bool handled = true; | |
47 IPC_BEGIN_MESSAGE_MAP(AttachmentBrokerUnprivilegedMac, msg) | |
48 IPC_MESSAGE_HANDLER(AttachmentBrokerMsg_MachPortHasBeenDuplicated, | |
49 OnMachPortHasBeenDuplicated) | |
50 IPC_MESSAGE_UNHANDLED(handled = false) | |
51 IPC_END_MESSAGE_MAP() | |
52 return handled; | |
53 } | |
54 | |
55 void AttachmentBrokerUnprivilegedMac::OnMachPortHasBeenDuplicated( | |
56 const IPC::internal::MachPortAttachmentMac::WireFormat& wire_format) { | |
57 // The IPC message was intended for a different process. Ignore it. | |
58 if (wire_format.destination_process != base::Process::Current().Pid()) { | |
59 // If everything is functioning correctly, this path should not be taken. | |
60 // However, it's still important to validate all fields of the IPC message. | |
61 LogError(WRONG_DESTINATION); | |
62 return; | |
63 } | |
64 | |
65 base::mac::ScopedMachReceiveRight message_port(wire_format.mach_port); | |
66 base::mac::ScopedMachSendRight memory_object( | |
67 base::ReceiveMachPort(message_port.get())); | |
68 | |
69 LogError(memory_object.get() == MACH_PORT_NULL ? ERR_RECEIVE_MACH_MESSAGE | |
70 : SUCCESS); | |
71 | |
72 IPC::internal::MachPortAttachmentMac::WireFormat translated_wire_format( | |
73 memory_object.release(), wire_format.destination_process, | |
74 wire_format.attachment_id); | |
75 | |
76 scoped_refptr<BrokerableAttachment> attachment( | |
77 new IPC::internal::MachPortAttachmentMac(translated_wire_format)); | |
78 HandleReceivedAttachment(attachment); | |
79 } | |
80 | |
81 } // namespace IPC | |
OLD | NEW |