[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 1 | // 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 Wennborg | 708fa82 | 2020-04-27 17:23:15 | [diff] [blame] | 7 | #include "base/check_op.h" |
[email protected] | 35b05b1 | 2014-06-03 00:01:03 | [diff] [blame] | 8 | #include "base/files/file.h" |
Hans Wennborg | 708fa82 | 2020-04-27 17:23:15 | [diff] [blame] | 9 | #include "base/notreached.h" |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 10 | #include "base/pickle.h" |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 11 | #include "build/build_config.h" |
| 12 | #include "ipc/ipc_platform_file.h" |
| 13 | |
Xiaohan Wang | 0fd6e56a | 2022-01-13 20:26:11 | [diff] [blame] | 14 | #if BUILDFLAG(IS_NACL) |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | #endif |
| 17 | |
| 18 | namespace ppapi { |
| 19 | namespace proxy { |
| 20 | |
| 21 | SerializedHandle::SerializedHandle() |
| 22 | : type_(INVALID), |
[email protected] | 80f8f84 | 2013-12-18 22:47:57 | [diff] [blame] | 23 | descriptor_(IPC::InvalidPlatformFileForTransit()), |
| 24 | open_flags_(0), |
| 25 | file_io_(0) { |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 26 | } |
| 27 | |
Alexandr Ilin | 1d00410 | 2018-05-30 10:55:32 | [diff] [blame] | 28 | SerializedHandle::SerializedHandle(SerializedHandle&& other) |
| 29 | : type_(other.type_), |
Alexandr Ilin | ebab9da | 2018-06-01 02:37:47 | [diff] [blame] | 30 | shm_region_(std::move(other.shm_region_)), |
Alexandr Ilin | 1d00410 | 2018-05-30 10:55:32 | [diff] [blame] | 31 | descriptor_(other.descriptor_), |
| 32 | open_flags_(other.open_flags_), |
| 33 | file_io_(other.file_io_) { |
| 34 | other.set_null(); |
| 35 | } |
| 36 | |
| 37 | SerializedHandle& SerializedHandle::operator=(SerializedHandle&& other) { |
| 38 | Close(); |
| 39 | type_ = other.type_; |
Alexandr Ilin | ebab9da | 2018-06-01 02:37:47 | [diff] [blame] | 40 | shm_region_ = std::move(other.shm_region_); |
Alexandr Ilin | 1d00410 | 2018-05-30 10:55:32 | [diff] [blame] | 41 | 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] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 48 | SerializedHandle::SerializedHandle(Type type_param) |
| 49 | : type_(type_param), |
[email protected] | 80f8f84 | 2013-12-18 22:47:57 | [diff] [blame] | 50 | descriptor_(IPC::InvalidPlatformFileForTransit()), |
| 51 | open_flags_(0), |
| 52 | file_io_(0) { |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 53 | } |
| 54 | |
Matthew Cary | 66e4f70 | 2019-06-05 08:47:52 | [diff] [blame] | 55 | SerializedHandle::SerializedHandle(base::ReadOnlySharedMemoryRegion region) |
| 56 | : SerializedHandle( |
| 57 | base::ReadOnlySharedMemoryRegion::TakeHandleForSerialization( |
| 58 | std::move(region))) {} |
| 59 | |
| 60 | SerializedHandle::SerializedHandle(base::UnsafeSharedMemoryRegion region) |
| 61 | : SerializedHandle( |
| 62 | base::UnsafeSharedMemoryRegion::TakeHandleForSerialization( |
| 63 | std::move(region))) {} |
| 64 | |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 65 | SerializedHandle::SerializedHandle( |
Alexandr Ilin | ebab9da | 2018-06-01 02:37:47 | [diff] [blame] | 66 | base::subtle::PlatformSharedMemoryRegion region) |
| 67 | : type_(SHARED_MEMORY_REGION), |
Alexandr Ilin | ebab9da | 2018-06-01 02:37:47 | [diff] [blame] | 68 | 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 | |
| 77 | SerializedHandle::SerializedHandle( |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 78 | Type type, |
| 79 | const IPC::PlatformFileForTransit& socket_descriptor) |
| 80 | : type_(type), |
[email protected] | 80f8f84 | 2013-12-18 22:47:57 | [diff] [blame] | 81 | descriptor_(socket_descriptor), |
| 82 | open_flags_(0), |
| 83 | file_io_(0) { |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool SerializedHandle::IsHandleValid() const { |
| 87 | switch (type_) { |
Alexandr Ilin | ebab9da | 2018-06-01 02:37:47 | [diff] [blame] | 88 | case SHARED_MEMORY_REGION: |
| 89 | return shm_region_.IsValid(); |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 90 | case SOCKET: |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 91 | 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 | |
| 100 | void SerializedHandle::Close() { |
| 101 | if (IsHandleValid()) { |
| 102 | switch (type_) { |
| 103 | case INVALID: |
| 104 | NOTREACHED(); |
| 105 | break; |
Alexandr Ilin | ebab9da | 2018-06-01 02:37:47 | [diff] [blame] | 106 | case SHARED_MEMORY_REGION: |
| 107 | shm_region_ = base::subtle::PlatformSharedMemoryRegion(); |
| 108 | break; |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 109 | case SOCKET: |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 110 | case FILE: |
[email protected] | 35b05b1 | 2014-06-03 00:01:03 | [diff] [blame] | 111 | base::File file_closer = IPC::PlatformFileForTransitToFile(descriptor_); |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 112 | break; |
| 113 | // No default so the compiler will warn us if a new type is added. |
| 114 | } |
| 115 | } |
Alexandr Ilin | 1d00410 | 2018-05-30 10:55:32 | [diff] [blame] | 116 | set_null(); |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // static |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 120 | void SerializedHandle::WriteHeader(const Header& hdr, base::Pickle* pickle) { |
| 121 | pickle->WriteInt(hdr.type); |
Matthew Cary | 36e2ec86 | 2019-08-07 23:58:56 | [diff] [blame] | 122 | if (hdr.type == FILE) { |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 123 | pickle->WriteInt(hdr.open_flags); |
| 124 | pickle->WriteInt(hdr.file_io); |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 125 | } |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // static |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 129 | bool SerializedHandle::ReadHeader(base::PickleIterator* iter, Header* hdr) { |
Matthew Cary | 36e2ec86 | 2019-08-07 23:58:56 | [diff] [blame] | 130 | *hdr = Header(INVALID, 0, 0); |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 131 | int type = 0; |
| 132 | if (!iter->ReadInt(&type)) |
| 133 | return false; |
| 134 | bool valid_type = false; |
| 135 | switch (type) { |
[email protected] | 9285b13 | 2013-05-14 22:32:03 | [diff] [blame] | 136 | case FILE: { |
[email protected] | 80f8f84 | 2013-12-18 22:47:57 | [diff] [blame] | 137 | int open_flags = 0; |
| 138 | PP_Resource file_io = 0; |
| 139 | if (!iter->ReadInt(&open_flags) || !iter->ReadInt(&file_io)) |
[email protected] | 9285b13 | 2013-05-14 22:32:03 | [diff] [blame] | 140 | return false; |
[email protected] | 80f8f84 | 2013-12-18 22:47:57 | [diff] [blame] | 141 | hdr->open_flags = open_flags; |
| 142 | hdr->file_io = file_io; |
[email protected] | 9285b13 | 2013-05-14 22:32:03 | [diff] [blame] | 143 | valid_type = true; |
tzik | c873786 | 2015-12-28 22:11:26 | [diff] [blame] | 144 | break; |
[email protected] | 9285b13 | 2013-05-14 22:32:03 | [diff] [blame] | 145 | } |
Alexandr Ilin | ebab9da | 2018-06-01 02:37:47 | [diff] [blame] | 146 | case SHARED_MEMORY_REGION: |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 147 | case SOCKET: |
[email protected] | eb5960da | 2013-01-16 23:23:53 | [diff] [blame] | 148 | 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 |