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 #ifndef PPAPI_PROXY_SERIALIZED_HANDLES_H_
6 #define PPAPI_PROXY_SERIALIZED_HANDLES_H_
11 #include "base/atomicops.h"
12 #include "base/basictypes.h"
13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/shared_memory.h"
16 #include "build/build_config.h"
17 #include "ipc/ipc_platform_file.h"
18 #include "ppapi/c/pp_resource.h"
19 #include "ppapi/proxy/ppapi_proxy_export.h"
26 // SerializedHandle is a unified structure for holding a handle (e.g., a shared
27 // memory handle, socket descriptor, etc). This is useful for passing handles in
28 // resource messages and also makes it easier to translate handles in
29 // NaClIPCAdapter for use in NaCl.
30 class PPAPI_PROXY_EXPORT SerializedHandle
{
32 enum Type
{ INVALID
, SHARED_MEMORY
, SOCKET
, CHANNEL_HANDLE
, FILE };
33 // Header contains the fields that we send in IPC messages, apart from the
34 // actual handle. See comments on the SerializedHandle fields below.
36 Header() : type(INVALID
), size(0), open_flags(0) {}
40 PP_Resource file_io_arg
)
43 open_flags(open_flags_arg
),
44 file_io(file_io_arg
) {
54 // Create an invalid handle of the given type.
55 explicit SerializedHandle(Type type
);
57 // Create a shared memory handle.
58 SerializedHandle(const base::SharedMemoryHandle
& handle
, uint32 size
);
60 // Create a socket, channel or file handle.
61 SerializedHandle(const Type type
,
62 const IPC::PlatformFileForTransit
& descriptor
);
64 Type
type() const { return type_
; }
65 bool is_shmem() const { return type_
== SHARED_MEMORY
; }
66 bool is_socket() const { return type_
== SOCKET
; }
67 bool is_channel_handle() const { return type_
== CHANNEL_HANDLE
; }
68 bool is_file() const { return type_
== FILE; }
69 const base::SharedMemoryHandle
& shmem() const {
77 const IPC::PlatformFileForTransit
& descriptor() const {
78 DCHECK(is_socket() || is_channel_handle() || is_file());
81 int32
open_flags() const {
84 PP_Resource
file_io() const {
87 void set_shmem(const base::SharedMemoryHandle
& handle
, uint32 size
) {
88 type_
= SHARED_MEMORY
;
92 descriptor_
= IPC::InvalidPlatformFileForTransit();
94 void set_socket(const IPC::PlatformFileForTransit
& socket
) {
98 shm_handle_
= base::SharedMemory::NULLHandle();
101 void set_channel_handle(const IPC::PlatformFileForTransit
& descriptor
) {
102 type_
= CHANNEL_HANDLE
;
104 descriptor_
= descriptor
;
105 shm_handle_
= base::SharedMemory::NULLHandle();
108 void set_file_handle(const IPC::PlatformFileForTransit
& descriptor
,
110 PP_Resource file_io
) {
113 descriptor_
= descriptor
;
114 shm_handle_
= base::SharedMemory::NULLHandle();
116 open_flags_
= open_flags
;
119 void set_null_shmem() {
120 set_shmem(base::SharedMemory::NULLHandle(), 0);
122 void set_null_socket() {
123 set_socket(IPC::InvalidPlatformFileForTransit());
125 void set_null_channel_handle() {
126 set_channel_handle(IPC::InvalidPlatformFileForTransit());
128 void set_null_file_handle() {
129 set_file_handle(IPC::InvalidPlatformFileForTransit(), 0, 0);
131 bool IsHandleValid() const;
133 Header
header() const {
134 return Header(type_
, size_
, open_flags_
, file_io_
);
137 // Closes the handle and sets it to invalid.
140 // Write/Read a Header, which contains all the data except the handle. This
141 // allows us to write the handle in a platform-specific way, as is necessary
142 // in NaClIPCAdapter to share handles with NaCl from Windows.
143 static bool WriteHeader(const Header
& hdr
, Pickle
* pickle
);
144 static bool ReadHeader(PickleIterator
* iter
, Header
* hdr
);
147 // The kind of handle we're holding.
150 // We hold more members than we really need; we can't easily use a union,
151 // because we hold non-POD types. But these types are pretty light-weight. If
152 // we add more complex things later, we should come up with a more memory-
153 // efficient strategy.
154 // These are valid if type == SHARED_MEMORY.
155 base::SharedMemoryHandle shm_handle_
;
158 // This is valid if type == SOCKET || type == CHANNEL_HANDLE || type == FILE.
159 IPC::PlatformFileForTransit descriptor_
;
161 // The following fields are valid if type == FILE.
163 // This is non-zero if file writes require quota checking.
164 PP_Resource file_io_
;
170 #endif // PPAPI_PROXY_SERIALIZED_HANDLES_H_