erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 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_win.h" |
| 6 | |
erikchen | 7252aa36 | 2015-07-15 01:35:39 | [diff] [blame] | 7 | #include "base/process/process.h" |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 8 | #include "ipc/attachment_broker_messages.h" |
| 9 | #include "ipc/brokerable_attachment.h" |
| 10 | #include "ipc/handle_attachment_win.h" |
| 11 | #include "ipc/ipc_sender.h" |
| 12 | |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 13 | namespace IPC { |
| 14 | |
| 15 | AttachmentBrokerWin::AttachmentBrokerWin() { |
| 16 | } |
| 17 | |
| 18 | AttachmentBrokerWin::~AttachmentBrokerWin() { |
| 19 | } |
| 20 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 21 | bool AttachmentBrokerWin::SendAttachmentToProcess( |
| 22 | const BrokerableAttachment* attachment, |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 23 | base::ProcessId destination_process) { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 24 | switch (attachment->GetBrokerableType()) { |
| 25 | case BrokerableAttachment::WIN_HANDLE: |
| 26 | const internal::HandleAttachmentWin* handle_attachment = |
| 27 | static_cast<const internal::HandleAttachmentWin*>(attachment); |
| 28 | internal::HandleAttachmentWin::WireFormat format = |
| 29 | handle_attachment->GetWireFormat(destination_process); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame^] | 30 | // TODO(erikchen): Replace the call to base::Process::Current().Pid() with |
| 31 | // a non-forgeable mechanism. http://crbug.com/513431. |
| 32 | return sender_->Send(new AttachmentBrokerMsg_DuplicateWinHandle( |
| 33 | format, base::Process::Current().Pid())); |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 34 | } |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 35 | return false; |
| 36 | } |
| 37 | |
erikchen | 7252aa36 | 2015-07-15 01:35:39 | [diff] [blame] | 38 | bool AttachmentBrokerWin::OnMessageReceived(const Message& msg) { |
| 39 | bool handled = true; |
| 40 | IPC_BEGIN_MESSAGE_MAP(AttachmentBrokerWin, msg) |
| 41 | IPC_MESSAGE_HANDLER(AttachmentBrokerMsg_WinHandleHasBeenDuplicated, |
| 42 | OnWinHandleHasBeenDuplicated) |
| 43 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 44 | IPC_END_MESSAGE_MAP() |
| 45 | return handled; |
| 46 | } |
| 47 | |
| 48 | void AttachmentBrokerWin::OnWinHandleHasBeenDuplicated( |
| 49 | const IPC::internal::HandleAttachmentWin::WireFormat& wire_format) { |
| 50 | // The IPC message was intended for a different process. Ignore it. |
| 51 | if (wire_format.destination_process != base::Process::Current().Pid()) |
| 52 | return; |
| 53 | |
| 54 | scoped_refptr<BrokerableAttachment> attachment( |
| 55 | new IPC::internal::HandleAttachmentWin(wire_format)); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame^] | 56 | HandleReceivedAttachment(attachment); |
erikchen | 7252aa36 | 2015-07-15 01:35:39 | [diff] [blame] | 57 | } |
| 58 | |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 59 | } // namespace IPC |