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.
5 #include "ppapi/proxy/serialized_handle.h"
7 #include "base/memory/shared_memory.h"
8 #include "base/pickle.h"
9 #include "base/platform_file.h"
10 #include "build/build_config.h"
11 #include "ipc/ipc_platform_file.h"
20 SerializedHandle::SerializedHandle()
22 shm_handle_(base::SharedMemory::NULLHandle()),
24 descriptor_(IPC::InvalidPlatformFileForTransit()),
29 SerializedHandle::SerializedHandle(Type type_param
)
31 shm_handle_(base::SharedMemory::NULLHandle()),
33 descriptor_(IPC::InvalidPlatformFileForTransit()),
38 SerializedHandle::SerializedHandle(const base::SharedMemoryHandle
& handle
,
40 : type_(SHARED_MEMORY
),
43 descriptor_(IPC::InvalidPlatformFileForTransit()),
48 SerializedHandle::SerializedHandle(
50 const IPC::PlatformFileForTransit
& socket_descriptor
)
52 shm_handle_(base::SharedMemory::NULLHandle()),
54 descriptor_(socket_descriptor
),
59 bool SerializedHandle::IsHandleValid() const {
62 return base::SharedMemory::IsHandleValid(shm_handle_
);
65 return !(IPC::InvalidPlatformFileForTransit() == descriptor_
);
68 // No default so the compiler will warn us if a new type is added.
73 void SerializedHandle::Close() {
74 if (IsHandleValid()) {
80 base::SharedMemory::CloseHandle(shm_handle_
);
84 base::PlatformFile file
=
85 IPC::PlatformFileForTransitToPlatformFile(descriptor_
);
87 base::ClosePlatformFile(file
);
92 // No default so the compiler will warn us if a new type is added.
95 *this = SerializedHandle();
99 bool SerializedHandle::WriteHeader(const Header
& hdr
, Pickle
* pickle
) {
100 if (!pickle
->WriteInt(hdr
.type
))
102 if (hdr
.type
== SHARED_MEMORY
) {
103 if (!pickle
->WriteUInt32(hdr
.size
))
106 if (hdr
.type
== FILE) {
107 if (!pickle
->WriteInt(hdr
.open_flags
) || !pickle
->WriteInt(hdr
.file_io
))
114 bool SerializedHandle::ReadHeader(PickleIterator
* iter
, Header
* hdr
) {
115 *hdr
= Header(INVALID
, 0, 0, 0);
117 if (!iter
->ReadInt(&type
))
119 bool valid_type
= false;
121 case SHARED_MEMORY
: {
123 if (!iter
->ReadUInt32(&size
))
131 PP_Resource file_io
= 0;
132 if (!iter
->ReadInt(&open_flags
) || !iter
->ReadInt(&file_io
))
134 hdr
->open_flags
= open_flags
;
135 hdr
->file_io
= file_io
;
142 // No default so the compiler will warn us if a new type is added.
145 hdr
->type
= Type(type
);