blob: 26dc9cf9a6824c450506e712b21dadc781465374 [file] [log] [blame]
Scott Graham3eebff02017-06-30 01:07:101// 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
16namespace IPC {
17
Wez8c49ebc92017-08-10 17:11:1318HandleFuchsia::HandleFuchsia() : handle_(MX_HANDLE_INVALID) {}
Scott Graham3eebff02017-06-30 01:07:1019
Wez8c49ebc92017-08-10 17:11:1320HandleFuchsia::HandleFuchsia(const mx_handle_t& handle) : handle_(handle) {}
Scott Graham3eebff02017-06-30 01:07:1021
22// static
23void ParamTraits<HandleFuchsia>::Write(base::Pickle* m, const param_type& p) {
24 scoped_refptr<IPC::internal::HandleAttachmentFuchsia> attachment(
Wez8c49ebc92017-08-10 17:11:1325 new IPC::internal::HandleAttachmentFuchsia(p.get_handle()));
Scott Graham3eebff02017-06-30 01:07:1026 if (!m->WriteAttachment(std::move(attachment)))
27 NOTREACHED();
28}
29
30// static
31bool 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);
Wez8c49ebc92017-08-10 17:11:1343 r->set_handle(handle_attachment->Take());
Scott Graham3eebff02017-06-30 01:07:1044 return true;
45}
46
47// static
48void ParamTraits<HandleFuchsia>::Log(const param_type& p, std::string* l) {
49 l->append(base::StringPrintf("0x%x", p.get_handle()));
Scott Graham3eebff02017-06-30 01:07:1050}
51
52} // namespace IPC