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 | |
Wez | 51eaaad | 2017-08-09 05:51:38 | [diff] [blame] | 18 | HandleWin::HandleWin() : handle_(INVALID_HANDLE_VALUE) {} |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 19 | |
Wez | 51eaaad | 2017-08-09 05:51:38 | [diff] [blame] | 20 | HandleWin::HandleWin(const HANDLE& handle) : handle_(handle) {} |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 21 | |
| 22 | // static |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 23 | void ParamTraits<HandleWin>::Write(base::Pickle* m, const param_type& p) { |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 24 | scoped_refptr<IPC::internal::HandleAttachmentWin> attachment( |
Wez | 51eaaad | 2017-08-09 05:51:38 | [diff] [blame] | 25 | new IPC::internal::HandleAttachmentWin(p.get_handle())); |
danakj | 4b041ab | 2015-12-04 20:12:27 | [diff] [blame] | 26 | if (!m->WriteAttachment(std::move(attachment))) |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 27 | NOTREACHED(); |
| 28 | } |
| 29 | |
| 30 | // static |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 31 | bool ParamTraits<HandleWin>::Read(const base::Pickle* m, |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 32 | base::PickleIterator* iter, |
| 33 | param_type* r) { |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 34 | scoped_refptr<base::Pickle::Attachment> base_attachment; |
| 35 | if (!m->ReadAttachment(iter, &base_attachment)) |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 36 | return false; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 37 | MessageAttachment* attachment = |
| 38 | static_cast<MessageAttachment*>(base_attachment.get()); |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 39 | if (attachment->GetType() != MessageAttachment::Type::WIN_HANDLE) |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 40 | return false; |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 41 | IPC::internal::HandleAttachmentWin* handle_attachment = |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 42 | static_cast<IPC::internal::HandleAttachmentWin*>(attachment); |
Wez | 51eaaad | 2017-08-09 05:51:38 | [diff] [blame] | 43 | r->set_handle(handle_attachment->Take()); |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 44 | return true; |
| 45 | } |
| 46 | |
| 47 | // static |
| 48 | void ParamTraits<HandleWin>::Log(const param_type& p, std::string* l) { |
brucedawson | 5604a11d | 2015-10-06 19:22:00 | [diff] [blame] | 49 | l->append(base::StringPrintf("0x%p", p.get_handle())); |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | } // namespace IPC |