erikchen | 959039d | 2015-08-11 21:17:47 | [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/handle_win.h" |
| 6 | |
danakj | 4b041ab | 2015-12-04 20:12:27 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 9 | #include "base/logging.h" |
| 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "base/strings/string_number_conversions.h" |
erikchen | b82097cc | 2015-10-12 23:27:55 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 13 | #include "ipc/handle_attachment_win.h" |
erikchen | b82097cc | 2015-10-12 23:27:55 | [diff] [blame] | 14 | #include "ipc/ipc_message.h" |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 15 | |
| 16 | namespace IPC { |
| 17 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 18 | HandleWin::HandleWin() : handle_(nullptr), permissions_(INVALID) {} |
| 19 | |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 20 | HandleWin::HandleWin(const HANDLE& handle, Permissions permissions) |
| 21 | : handle_(handle), permissions_(permissions) {} |
| 22 | |
| 23 | // static |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame^] | 24 | void ParamTraits<HandleWin>::Write(base::Pickle* m, const param_type& p) { |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 25 | scoped_refptr<IPC::internal::HandleAttachmentWin> attachment( |
| 26 | new IPC::internal::HandleAttachmentWin(p.get_handle(), |
| 27 | p.get_permissions())); |
danakj | 4b041ab | 2015-12-04 20:12:27 | [diff] [blame] | 28 | if (!m->WriteAttachment(std::move(attachment))) |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 29 | NOTREACHED(); |
| 30 | } |
| 31 | |
| 32 | // static |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame^] | 33 | bool ParamTraits<HandleWin>::Read(const base::Pickle* m, |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 34 | base::PickleIterator* iter, |
| 35 | param_type* r) { |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame^] | 36 | scoped_refptr<base::Pickle::Attachment> base_attachment; |
| 37 | if (!m->ReadAttachment(iter, &base_attachment)) |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 38 | return false; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame^] | 39 | MessageAttachment* attachment = |
| 40 | static_cast<MessageAttachment*>(base_attachment.get()); |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 41 | if (attachment->GetType() != MessageAttachment::TYPE_BROKERABLE_ATTACHMENT) |
| 42 | return false; |
| 43 | BrokerableAttachment* brokerable_attachment = |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame^] | 44 | static_cast<BrokerableAttachment*>(attachment); |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 45 | if (brokerable_attachment->GetBrokerableType() != |
| 46 | BrokerableAttachment::WIN_HANDLE) { |
| 47 | return false; |
| 48 | } |
| 49 | IPC::internal::HandleAttachmentWin* handle_attachment = |
| 50 | static_cast<IPC::internal::HandleAttachmentWin*>(brokerable_attachment); |
| 51 | r->set_handle(handle_attachment->get_handle()); |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 52 | handle_attachment->reset_handle_ownership(); |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | |
| 56 | // static |
| 57 | void ParamTraits<HandleWin>::Log(const param_type& p, std::string* l) { |
brucedawson | 5604a11d | 2015-10-06 19:22:00 | [diff] [blame] | 58 | l->append(base::StringPrintf("0x%p", p.get_handle())); |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 59 | l->append(base::IntToString(p.get_permissions())); |
| 60 | } |
| 61 | |
| 62 | } // namespace IPC |