blob: 15b7f44db9ac0f21547e29777366fc1b4459fe57 [file] [log] [blame]
[email protected]eb5960da2013-01-16 23:23:531// Copyright (c) 2013 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 "ppapi/proxy/serialized_handle.h"
6
Hans Wennborg708fa822020-04-27 17:23:157#include "base/check_op.h"
[email protected]35b05b12014-06-03 00:01:038#include "base/files/file.h"
Hans Wennborg708fa822020-04-27 17:23:159#include "base/notreached.h"
[email protected]eb5960da2013-01-16 23:23:5310#include "base/pickle.h"
[email protected]eb5960da2013-01-16 23:23:5311#include "build/build_config.h"
12#include "ipc/ipc_platform_file.h"
13
Xiaohan Wang0fd6e56a2022-01-13 20:26:1114#if BUILDFLAG(IS_NACL)
[email protected]eb5960da2013-01-16 23:23:5315#include <unistd.h>
16#endif
17
18namespace ppapi {
19namespace proxy {
20
21SerializedHandle::SerializedHandle()
22 : type_(INVALID),
[email protected]80f8f842013-12-18 22:47:5723 descriptor_(IPC::InvalidPlatformFileForTransit()),
24 open_flags_(0),
25 file_io_(0) {
[email protected]eb5960da2013-01-16 23:23:5326}
27
Alexandr Ilin1d004102018-05-30 10:55:3228SerializedHandle::SerializedHandle(SerializedHandle&& other)
29 : type_(other.type_),
Alexandr Ilinebab9da2018-06-01 02:37:4730 shm_region_(std::move(other.shm_region_)),
Alexandr Ilin1d004102018-05-30 10:55:3231 descriptor_(other.descriptor_),
32 open_flags_(other.open_flags_),
33 file_io_(other.file_io_) {
34 other.set_null();
35}
36
37SerializedHandle& SerializedHandle::operator=(SerializedHandle&& other) {
38 Close();
39 type_ = other.type_;
Alexandr Ilinebab9da2018-06-01 02:37:4740 shm_region_ = std::move(other.shm_region_);
Alexandr Ilin1d004102018-05-30 10:55:3241 descriptor_ = other.descriptor_;
42 open_flags_ = other.open_flags_;
43 file_io_ = other.file_io_;
44 other.set_null();
45 return *this;
46}
47
[email protected]eb5960da2013-01-16 23:23:5348SerializedHandle::SerializedHandle(Type type_param)
49 : type_(type_param),
[email protected]80f8f842013-12-18 22:47:5750 descriptor_(IPC::InvalidPlatformFileForTransit()),
51 open_flags_(0),
52 file_io_(0) {
[email protected]eb5960da2013-01-16 23:23:5353}
54
Matthew Cary66e4f702019-06-05 08:47:5255SerializedHandle::SerializedHandle(base::ReadOnlySharedMemoryRegion region)
56 : SerializedHandle(
57 base::ReadOnlySharedMemoryRegion::TakeHandleForSerialization(
58 std::move(region))) {}
59
60SerializedHandle::SerializedHandle(base::UnsafeSharedMemoryRegion region)
61 : SerializedHandle(
62 base::UnsafeSharedMemoryRegion::TakeHandleForSerialization(
63 std::move(region))) {}
64
[email protected]eb5960da2013-01-16 23:23:5365SerializedHandle::SerializedHandle(
Alexandr Ilinebab9da2018-06-01 02:37:4766 base::subtle::PlatformSharedMemoryRegion region)
67 : type_(SHARED_MEMORY_REGION),
Alexandr Ilinebab9da2018-06-01 02:37:4768 shm_region_(std::move(region)),
69 descriptor_(IPC::InvalidPlatformFileForTransit()),
70 open_flags_(0),
71 file_io_(0) {
72 // Writable regions are not supported.
73 DCHECK_NE(shm_region_.GetMode(),
74 base::subtle::PlatformSharedMemoryRegion::Mode::kWritable);
75}
76
77SerializedHandle::SerializedHandle(
[email protected]eb5960da2013-01-16 23:23:5378 Type type,
79 const IPC::PlatformFileForTransit& socket_descriptor)
80 : type_(type),
[email protected]80f8f842013-12-18 22:47:5781 descriptor_(socket_descriptor),
82 open_flags_(0),
83 file_io_(0) {
[email protected]eb5960da2013-01-16 23:23:5384}
85
86bool SerializedHandle::IsHandleValid() const {
87 switch (type_) {
Alexandr Ilinebab9da2018-06-01 02:37:4788 case SHARED_MEMORY_REGION:
89 return shm_region_.IsValid();
[email protected]eb5960da2013-01-16 23:23:5390 case SOCKET:
[email protected]eb5960da2013-01-16 23:23:5391 case FILE:
92 return !(IPC::InvalidPlatformFileForTransit() == descriptor_);
93 case INVALID:
94 return false;
95 // No default so the compiler will warn us if a new type is added.
96 }
97 return false;
98}
99
100void SerializedHandle::Close() {
101 if (IsHandleValid()) {
102 switch (type_) {
103 case INVALID:
104 NOTREACHED();
105 break;
Alexandr Ilinebab9da2018-06-01 02:37:47106 case SHARED_MEMORY_REGION:
107 shm_region_ = base::subtle::PlatformSharedMemoryRegion();
108 break;
[email protected]eb5960da2013-01-16 23:23:53109 case SOCKET:
[email protected]eb5960da2013-01-16 23:23:53110 case FILE:
[email protected]35b05b12014-06-03 00:01:03111 base::File file_closer = IPC::PlatformFileForTransitToFile(descriptor_);
[email protected]eb5960da2013-01-16 23:23:53112 break;
113 // No default so the compiler will warn us if a new type is added.
114 }
115 }
Alexandr Ilin1d004102018-05-30 10:55:32116 set_null();
[email protected]eb5960da2013-01-16 23:23:53117}
118
119// static
Daniel Cheng0d89f9222017-09-22 05:05:07120void SerializedHandle::WriteHeader(const Header& hdr, base::Pickle* pickle) {
121 pickle->WriteInt(hdr.type);
Matthew Cary36e2ec862019-08-07 23:58:56122 if (hdr.type == FILE) {
Daniel Cheng0d89f9222017-09-22 05:05:07123 pickle->WriteInt(hdr.open_flags);
124 pickle->WriteInt(hdr.file_io);
[email protected]eb5960da2013-01-16 23:23:53125 }
[email protected]eb5960da2013-01-16 23:23:53126}
127
128// static
brettwbd4d7112015-06-03 04:29:25129bool SerializedHandle::ReadHeader(base::PickleIterator* iter, Header* hdr) {
Matthew Cary36e2ec862019-08-07 23:58:56130 *hdr = Header(INVALID, 0, 0);
[email protected]eb5960da2013-01-16 23:23:53131 int type = 0;
132 if (!iter->ReadInt(&type))
133 return false;
134 bool valid_type = false;
135 switch (type) {
[email protected]9285b132013-05-14 22:32:03136 case FILE: {
[email protected]80f8f842013-12-18 22:47:57137 int open_flags = 0;
138 PP_Resource file_io = 0;
139 if (!iter->ReadInt(&open_flags) || !iter->ReadInt(&file_io))
[email protected]9285b132013-05-14 22:32:03140 return false;
[email protected]80f8f842013-12-18 22:47:57141 hdr->open_flags = open_flags;
142 hdr->file_io = file_io;
[email protected]9285b132013-05-14 22:32:03143 valid_type = true;
tzikc8737862015-12-28 22:11:26144 break;
[email protected]9285b132013-05-14 22:32:03145 }
Alexandr Ilinebab9da2018-06-01 02:37:47146 case SHARED_MEMORY_REGION:
[email protected]eb5960da2013-01-16 23:23:53147 case SOCKET:
[email protected]eb5960da2013-01-16 23:23:53148 case INVALID:
149 valid_type = true;
150 break;
151 // No default so the compiler will warn us if a new type is added.
152 }
153 if (valid_type)
154 hdr->type = Type(type);
155 return valid_type;
156}
157
158} // namespace proxy
159} // namespace ppapi