blob: a4cb067afa69c170afea18bdd1034ac17f551d63 [file] [log] [blame]
erikchen151b2f92015-06-16 20:20:511// 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
erikchen7252aa362015-07-15 01:35:397#include "base/process/process.h"
erikcheneece6c32015-07-07 22:13:118#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
erikchen151b2f92015-06-16 20:20:5113namespace IPC {
14
15AttachmentBrokerWin::AttachmentBrokerWin() {
16}
17
18AttachmentBrokerWin::~AttachmentBrokerWin() {
19}
20
erikcheneece6c32015-07-07 22:13:1121bool AttachmentBrokerWin::SendAttachmentToProcess(
22 const BrokerableAttachment* attachment,
erikchen151b2f92015-06-16 20:20:5123 base::ProcessId destination_process) {
erikcheneece6c32015-07-07 22:13:1124 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);
erikchenc04ab34c2015-07-27 20:28:2030 // 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()));
erikchende9412b82015-07-27 18:26:1434 }
erikchen151b2f92015-06-16 20:20:5135 return false;
36}
37
erikchen7252aa362015-07-15 01:35:3938bool 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
48void 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));
erikchenc04ab34c2015-07-27 20:28:2056 HandleReceivedAttachment(attachment);
erikchen7252aa362015-07-15 01:35:3957}
58
erikchen151b2f92015-06-16 20:20:5159} // namespace IPC