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/files/file.h"
8 #include "base/memory/shared_memory.h"
9 #include "base/pickle.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::File file_closer
= IPC::PlatformFileForTransitToFile(descriptor_
);
86 // No default so the compiler will warn us if a new type is added.
89 *this = SerializedHandle();
93 bool SerializedHandle::WriteHeader(const Header
& hdr
, Pickle
* pickle
) {
94 if (!pickle
->WriteInt(hdr
.type
))
96 if (hdr
.type
== SHARED_MEMORY
) {
97 if (!pickle
->WriteUInt32(hdr
.size
))
100 if (hdr
.type
== FILE) {
101 if (!pickle
->WriteInt(hdr
.open_flags
) || !pickle
->WriteInt(hdr
.file_io
))
108 bool SerializedHandle::ReadHeader(PickleIterator
* iter
, Header
* hdr
) {
109 *hdr
= Header(INVALID
, 0, 0, 0);
111 if (!iter
->ReadInt(&type
))
113 bool valid_type
= false;
115 case SHARED_MEMORY
: {
117 if (!iter
->ReadUInt32(&size
))
125 PP_Resource file_io
= 0;
126 if (!iter
->ReadInt(&open_flags
) || !iter
->ReadInt(&file_io
))
128 hdr
->open_flags
= open_flags
;
129 hdr
->file_io
= file_io
;
136 // No default so the compiler will warn us if a new type is added.
139 hdr
->type
= Type(type
);