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"
28 // SerializedHandle is a unified structure for holding a handle (e.g., a shared
29 // memory handle, socket descriptor, etc). This is useful for passing handles in
30 // resource messages and also makes it easier to translate handles in
31 // NaClIPCAdapter for use in NaCl.
32 class PPAPI_PROXY_EXPORT SerializedHandle
{
34 enum Type
{ INVALID
, SHARED_MEMORY
, SOCKET
, FILE };
35 // Header contains the fields that we send in IPC messages, apart from the
36 // actual handle. See comments on the SerializedHandle fields below.
38 Header() : type(INVALID
), size(0), open_flags(0) {}
42 PP_Resource file_io_arg
)
45 open_flags(open_flags_arg
),
46 file_io(file_io_arg
) {
56 // Create an invalid handle of the given type.
57 explicit SerializedHandle(Type type
);
59 // Create a shared memory handle.
60 SerializedHandle(const base::SharedMemoryHandle
& handle
, uint32 size
);
62 // Create a socket or file handle.
63 SerializedHandle(const Type type
,
64 const IPC::PlatformFileForTransit
& descriptor
);
66 Type
type() const { return type_
; }
67 bool is_shmem() const { return type_
== SHARED_MEMORY
; }
68 bool is_socket() const { return type_
== SOCKET
; }
69 bool is_file() const { return type_
== FILE; }
70 const base::SharedMemoryHandle
& shmem() const {
78 const IPC::PlatformFileForTransit
& descriptor() const {
79 DCHECK(is_socket() || is_file());
82 int32
open_flags() const {
85 PP_Resource
file_io() const {
88 void set_shmem(const base::SharedMemoryHandle
& handle
, uint32 size
) {
89 type_
= SHARED_MEMORY
;
93 descriptor_
= IPC::InvalidPlatformFileForTransit();
95 void set_socket(const IPC::PlatformFileForTransit
& socket
) {
99 shm_handle_
= base::SharedMemory::NULLHandle();
102 void set_file_handle(const IPC::PlatformFileForTransit
& descriptor
,
104 PP_Resource file_io
) {
107 descriptor_
= descriptor
;
108 shm_handle_
= base::SharedMemory::NULLHandle();
110 open_flags_
= open_flags
;
113 void set_null_shmem() {
114 set_shmem(base::SharedMemory::NULLHandle(), 0);
116 void set_null_socket() {
117 set_socket(IPC::InvalidPlatformFileForTransit());
119 void set_null_file_handle() {
120 set_file_handle(IPC::InvalidPlatformFileForTransit(), 0, 0);
122 bool IsHandleValid() const;
124 Header
header() const {
125 return Header(type_
, size_
, open_flags_
, file_io_
);
128 // Closes the handle and sets it to invalid.
131 // Write/Read a Header, which contains all the data except the handle. This
132 // allows us to write the handle in a platform-specific way, as is necessary
133 // in NaClIPCAdapter to share handles with NaCl from Windows.
134 static bool WriteHeader(const Header
& hdr
, base::Pickle
* pickle
);
135 static bool ReadHeader(base::PickleIterator
* iter
, Header
* hdr
);
138 // The kind of handle we're holding.
141 // We hold more members than we really need; we can't easily use a union,
142 // because we hold non-POD types. But these types are pretty light-weight. If
143 // we add more complex things later, we should come up with a more memory-
144 // efficient strategy.
145 // These are valid if type == SHARED_MEMORY.
146 base::SharedMemoryHandle shm_handle_
;
149 // This is valid if type == SOCKET || type == FILE.
150 IPC::PlatformFileForTransit descriptor_
;
152 // The following fields are valid if type == FILE.
154 // This is non-zero if file writes require quota checking.
155 PP_Resource file_io_
;
161 #endif // PPAPI_PROXY_SERIALIZED_HANDLES_H_