Scott Graham | 3eebff0 | 2017-06-30 01:07:10 | [diff] [blame] | 1 | // Copyright 2017 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_fuchsia.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "base/logging.h" |
| 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "base/strings/string_number_conversions.h" |
| 12 | #include "base/strings/stringprintf.h" |
| 13 | #include "ipc/handle_attachment_fuchsia.h" |
| 14 | #include "ipc/ipc_message.h" |
| 15 | |
| 16 | namespace IPC { |
| 17 | |
Wez | 8c49ebc9 | 2017-08-10 17:11:13 | [diff] [blame] | 18 | HandleFuchsia::HandleFuchsia() : handle_(MX_HANDLE_INVALID) {} |
Scott Graham | 3eebff0 | 2017-06-30 01:07:10 | [diff] [blame] | 19 | |
Wez | 8c49ebc9 | 2017-08-10 17:11:13 | [diff] [blame] | 20 | HandleFuchsia::HandleFuchsia(const mx_handle_t& handle) : handle_(handle) {} |
Scott Graham | 3eebff0 | 2017-06-30 01:07:10 | [diff] [blame] | 21 | |
| 22 | // static |
| 23 | void ParamTraits<HandleFuchsia>::Write(base::Pickle* m, const param_type& p) { |
| 24 | scoped_refptr<IPC::internal::HandleAttachmentFuchsia> attachment( |
Wez | 8c49ebc9 | 2017-08-10 17:11:13 | [diff] [blame] | 25 | new IPC::internal::HandleAttachmentFuchsia(p.get_handle())); |
Scott Graham | 3eebff0 | 2017-06-30 01:07:10 | [diff] [blame] | 26 | if (!m->WriteAttachment(std::move(attachment))) |
| 27 | NOTREACHED(); |
| 28 | } |
| 29 | |
| 30 | // static |
| 31 | bool ParamTraits<HandleFuchsia>::Read(const base::Pickle* m, |
| 32 | base::PickleIterator* iter, |
| 33 | param_type* r) { |
| 34 | scoped_refptr<base::Pickle::Attachment> base_attachment; |
| 35 | if (!m->ReadAttachment(iter, &base_attachment)) |
| 36 | return false; |
| 37 | MessageAttachment* attachment = |
| 38 | static_cast<MessageAttachment*>(base_attachment.get()); |
| 39 | if (attachment->GetType() != MessageAttachment::Type::FUCHSIA_HANDLE) |
| 40 | return false; |
| 41 | IPC::internal::HandleAttachmentFuchsia* handle_attachment = |
| 42 | static_cast<IPC::internal::HandleAttachmentFuchsia*>(attachment); |
Wez | 8c49ebc9 | 2017-08-10 17:11:13 | [diff] [blame] | 43 | r->set_handle(handle_attachment->Take()); |
Scott Graham | 3eebff0 | 2017-06-30 01:07:10 | [diff] [blame] | 44 | return true; |
| 45 | } |
| 46 | |
| 47 | // static |
| 48 | void ParamTraits<HandleFuchsia>::Log(const param_type& p, std::string* l) { |
| 49 | l->append(base::StringPrintf("0x%x", p.get_handle())); |
Scott Graham | 3eebff0 | 2017-06-30 01:07:10 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | } // namespace IPC |