Update .DEPS.git
[chromium-blink-merge.git] / ppapi / proxy / serialized_handle.h
bloba662b757fc3b5c9da5fc3af04074429d4dedc6ac
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_
8 #include <string>
9 #include <vector>
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"
21 class Pickle;
23 namespace ppapi {
24 namespace proxy {
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 {
31 public:
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.
35 struct Header {
36 Header() : type(INVALID), size(0), open_flags(0) {}
37 Header(Type type_arg,
38 uint32 size_arg,
39 int32 open_flags_arg,
40 PP_Resource file_io_arg)
41 : type(type_arg),
42 size(size_arg),
43 open_flags(open_flags_arg),
44 file_io(file_io_arg) {
47 Type type;
48 uint32 size;
49 int32 open_flags;
50 PP_Resource file_io;
53 SerializedHandle();
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 {
70 DCHECK(is_shmem());
71 return shm_handle_;
73 uint32 size() const {
74 DCHECK(is_shmem());
75 return size_;
77 const IPC::PlatformFileForTransit& descriptor() const {
78 DCHECK(is_socket() || is_channel_handle() || is_file());
79 return descriptor_;
81 int32 open_flags() const {
82 return open_flags_;
84 PP_Resource file_io() const {
85 return file_io_;
87 void set_shmem(const base::SharedMemoryHandle& handle, uint32 size) {
88 type_ = SHARED_MEMORY;
89 shm_handle_ = handle;
90 size_ = size;
92 descriptor_ = IPC::InvalidPlatformFileForTransit();
94 void set_socket(const IPC::PlatformFileForTransit& socket) {
95 type_ = SOCKET;
96 descriptor_ = socket;
98 shm_handle_ = base::SharedMemory::NULLHandle();
99 size_ = 0;
101 void set_channel_handle(const IPC::PlatformFileForTransit& descriptor) {
102 type_ = CHANNEL_HANDLE;
104 descriptor_ = descriptor;
105 shm_handle_ = base::SharedMemory::NULLHandle();
106 size_ = 0;
108 void set_file_handle(const IPC::PlatformFileForTransit& descriptor,
109 int32 open_flags,
110 PP_Resource file_io) {
111 type_ = FILE;
113 descriptor_ = descriptor;
114 shm_handle_ = base::SharedMemory::NULLHandle();
115 size_ = 0;
116 open_flags_ = open_flags;
117 file_io_ = file_io;
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.
138 void Close();
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);
146 private:
147 // The kind of handle we're holding.
148 Type type_;
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_;
156 uint32 size_;
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.
162 int32 open_flags_;
163 // This is non-zero if file writes require quota checking.
164 PP_Resource file_io_;
167 } // namespace proxy
168 } // namespace ppapi
170 #endif // PPAPI_PROXY_SERIALIZED_HANDLES_H_