Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / ppapi / proxy / serialized_handle.h
blob6e765fd35fa746f89fce593c92a2762b10b494f9
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 namespace base {
22 class Pickle;
25 namespace ppapi {
26 namespace proxy {
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 {
33 public:
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.
37 struct Header {
38 Header() : type(INVALID), size(0), open_flags(0) {}
39 Header(Type type_arg,
40 uint32 size_arg,
41 int32 open_flags_arg,
42 PP_Resource file_io_arg)
43 : type(type_arg),
44 size(size_arg),
45 open_flags(open_flags_arg),
46 file_io(file_io_arg) {
49 Type type;
50 uint32 size;
51 int32 open_flags;
52 PP_Resource file_io;
55 SerializedHandle();
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 {
71 DCHECK(is_shmem());
72 return shm_handle_;
74 uint32 size() const {
75 DCHECK(is_shmem());
76 return size_;
78 const IPC::PlatformFileForTransit& descriptor() const {
79 DCHECK(is_socket() || is_file());
80 return descriptor_;
82 int32 open_flags() const {
83 return open_flags_;
85 PP_Resource file_io() const {
86 return file_io_;
88 void set_shmem(const base::SharedMemoryHandle& handle, uint32 size) {
89 type_ = SHARED_MEMORY;
90 shm_handle_ = handle;
91 size_ = size;
93 descriptor_ = IPC::InvalidPlatformFileForTransit();
95 void set_socket(const IPC::PlatformFileForTransit& socket) {
96 type_ = SOCKET;
97 descriptor_ = socket;
99 shm_handle_ = base::SharedMemory::NULLHandle();
100 size_ = 0;
102 void set_file_handle(const IPC::PlatformFileForTransit& descriptor,
103 int32 open_flags,
104 PP_Resource file_io) {
105 type_ = FILE;
107 descriptor_ = descriptor;
108 shm_handle_ = base::SharedMemory::NULLHandle();
109 size_ = 0;
110 open_flags_ = open_flags;
111 file_io_ = file_io;
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.
129 void Close();
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);
137 private:
138 // The kind of handle we're holding.
139 Type type_;
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_;
147 uint32 size_;
149 // This is valid if type == SOCKET || type == FILE.
150 IPC::PlatformFileForTransit descriptor_;
152 // The following fields are valid if type == FILE.
153 int32 open_flags_;
154 // This is non-zero if file writes require quota checking.
155 PP_Resource file_io_;
158 } // namespace proxy
159 } // namespace ppapi
161 #endif // PPAPI_PROXY_SERIALIZED_HANDLES_H_