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 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/memory/ref_counted.h" |
| 9 | #include "base/strings/string_number_conversions.h" |
| 10 | #include "ipc/handle_attachment_win.h" |
| 11 | |
| 12 | namespace IPC { |
| 13 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 14 | HandleWin::HandleWin() : handle_(nullptr), permissions_(INVALID) {} |
| 15 | |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 16 | HandleWin::HandleWin(const HANDLE& handle, Permissions permissions) |
| 17 | : handle_(handle), permissions_(permissions) {} |
| 18 | |
| 19 | // static |
| 20 | void ParamTraits<HandleWin>::Write(Message* m, const param_type& p) { |
| 21 | scoped_refptr<IPC::internal::HandleAttachmentWin> attachment( |
| 22 | new IPC::internal::HandleAttachmentWin(p.get_handle(), |
| 23 | p.get_permissions())); |
| 24 | if (!m->WriteAttachment(attachment.Pass())) |
| 25 | NOTREACHED(); |
| 26 | } |
| 27 | |
| 28 | // static |
| 29 | bool ParamTraits<HandleWin>::Read(const Message* m, |
| 30 | base::PickleIterator* iter, |
| 31 | param_type* r) { |
| 32 | scoped_refptr<MessageAttachment> attachment; |
| 33 | if (!m->ReadAttachment(iter, &attachment)) |
| 34 | return false; |
| 35 | if (attachment->GetType() != MessageAttachment::TYPE_BROKERABLE_ATTACHMENT) |
| 36 | return false; |
| 37 | BrokerableAttachment* brokerable_attachment = |
| 38 | static_cast<BrokerableAttachment*>(attachment.get()); |
| 39 | if (brokerable_attachment->GetBrokerableType() != |
| 40 | BrokerableAttachment::WIN_HANDLE) { |
| 41 | return false; |
| 42 | } |
| 43 | IPC::internal::HandleAttachmentWin* handle_attachment = |
| 44 | static_cast<IPC::internal::HandleAttachmentWin*>(brokerable_attachment); |
| 45 | r->set_handle(handle_attachment->get_handle()); |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | // static |
| 50 | void ParamTraits<HandleWin>::Log(const param_type& p, std::string* l) { |
| 51 | l->append(base::StringPrintf("0x%X", p.get_handle())); |
| 52 | l->append(base::IntToString(p.get_permissions())); |
| 53 | } |
| 54 | |
| 55 | } // namespace IPC |