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/pickle.h"
8 #include "base/platform_file.h"
9 #include "base/shared_memory.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()) {
27 SerializedHandle::SerializedHandle(Type type_param
)
29 shm_handle_(base::SharedMemory::NULLHandle()),
31 descriptor_(IPC::InvalidPlatformFileForTransit()) {
34 SerializedHandle::SerializedHandle(const base::SharedMemoryHandle
& handle
,
36 : type_(SHARED_MEMORY
),
39 descriptor_(IPC::InvalidPlatformFileForTransit()) {
42 SerializedHandle::SerializedHandle(
44 const IPC::PlatformFileForTransit
& socket_descriptor
)
46 shm_handle_(base::SharedMemory::NULLHandle()),
48 descriptor_(socket_descriptor
) {
51 bool SerializedHandle::IsHandleValid() const {
54 return base::SharedMemory::IsHandleValid(shm_handle_
);
58 return !(IPC::InvalidPlatformFileForTransit() == descriptor_
);
61 // No default so the compiler will warn us if a new type is added.
66 void SerializedHandle::Close() {
67 if (IsHandleValid()) {
73 base::SharedMemory::CloseHandle(shm_handle_
);
78 base::PlatformFile file
=
79 IPC::PlatformFileForTransitToPlatformFile(descriptor_
);
81 base::ClosePlatformFile(file
);
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_flag
))
108 bool SerializedHandle::ReadHeader(PickleIterator
* iter
, Header
* hdr
) {
109 *hdr
= Header(INVALID
, 0, 0);
111 if (!iter
->ReadInt(&type
))
113 bool valid_type
= false;
115 case SHARED_MEMORY
: {
117 if (!iter
->ReadUInt32(&size
))
125 if (!iter
->ReadInt(&open_flag
))
127 hdr
->open_flag
= open_flag
;
135 // No default so the compiler will warn us if a new type is added.
138 hdr
->type
= Type(type
);