serialized_structs.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 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.
4
5#ifndef PPAPI_PROXY_SERIALIZED_STRUCTS_H_
6#define PPAPI_PROXY_SERIALIZED_STRUCTS_H_
7
8#include <string>
9#include <vector>
10
11#include "base/logging.h"
12#include "base/shared_memory.h"
13#include "build/build_config.h"
14#include "ipc/ipc_platform_file.h"
15#include "ppapi/c/pp_bool.h"
16#include "ppapi/c/pp_instance.h"
17#include "ppapi/c/pp_point.h"
18#include "ppapi/c/pp_rect.h"
19#include "ppapi/proxy/ppapi_proxy_export.h"
20#include "ppapi/shared_impl/host_resource.h"
21
22class Pickle;
23struct PP_FontDescription_Dev;
24
25namespace ppapi {
26namespace proxy {
27
28// PP_FontDescript_Dev has to be redefined with a string in place of the PP_Var
29// used for the face name.
30struct PPAPI_PROXY_EXPORT SerializedFontDescription {
31  SerializedFontDescription();
32  ~SerializedFontDescription();
33
34  // Converts a PP_FontDescription_Dev to a SerializedFontDescription.
35  //
36  // The reference of |face| owned by the PP_FontDescription_Dev will be
37  // unchanged and the caller is responsible for freeing it.
38  void SetFromPPFontDescription(const PP_FontDescription_Dev& desc);
39
40  // Converts to a PP_FontDescription_Dev. The face name will have one ref
41  // assigned to it. The caller is responsible for freeing it.
42  void SetToPPFontDescription(PP_FontDescription_Dev* desc) const;
43
44  std::string face;
45  int32_t family;
46  uint32_t size;
47  int32_t weight;
48  PP_Bool italic;
49  PP_Bool small_caps;
50  int32_t letter_spacing;
51  int32_t word_spacing;
52};
53
54struct SerializedDirEntry {
55  std::string name;
56  bool is_dir;
57};
58
59struct PPBFlash_DrawGlyphs_Params {
60  PPBFlash_DrawGlyphs_Params();
61  ~PPBFlash_DrawGlyphs_Params();
62
63  PP_Instance instance;
64  ppapi::HostResource image_data;
65  SerializedFontDescription font_desc;
66  uint32_t color;
67  PP_Point position;
68  PP_Rect clip;
69  float transformation[3][3];
70  PP_Bool allow_subpixel_aa;
71  std::vector<uint16_t> glyph_indices;
72  std::vector<PP_Point> glyph_advances;
73};
74
75struct PPBURLLoader_UpdateProgress_Params {
76  PP_Instance instance;
77  ppapi::HostResource resource;
78  int64_t bytes_sent;
79  int64_t total_bytes_to_be_sent;
80  int64_t bytes_received;
81  int64_t total_bytes_to_be_received;
82};
83
84struct PPPVideoCapture_Buffer {
85  ppapi::HostResource resource;
86  uint32_t size;
87  base::SharedMemoryHandle handle;
88};
89
90// We put all our handles in a unified structure to make it easy to translate
91// them in NaClIPCAdapter for use in NaCl.
92class PPAPI_PROXY_EXPORT SerializedHandle {
93 public:
94  enum Type { INVALID, SHARED_MEMORY, SOCKET, CHANNEL_HANDLE };
95  struct Header {
96    Header() : type(INVALID), size(0) {}
97    Header(Type type_arg, uint32_t size_arg)
98        : type(type_arg), size(size_arg) {
99    }
100    Type type;
101    uint32_t size;
102  };
103
104  SerializedHandle();
105  // Create an invalid handle of the given type.
106  explicit SerializedHandle(Type type);
107
108  // Create a shared memory handle.
109  SerializedHandle(const base::SharedMemoryHandle& handle, uint32_t size);
110
111  // Create a socket or channel handle.
112  SerializedHandle(const Type type,
113                   const IPC::PlatformFileForTransit& descriptor);
114
115  Type type() const { return type_; }
116  bool is_shmem() const { return type_ == SHARED_MEMORY; }
117  bool is_socket() const { return type_ == SOCKET; }
118  bool is_channel_handle() const { return type_ == CHANNEL_HANDLE; }
119  const base::SharedMemoryHandle& shmem() const {
120    DCHECK(is_shmem());
121    return shm_handle_;
122  }
123  uint32_t size() const {
124    DCHECK(is_shmem());
125    return size_;
126  }
127  const IPC::PlatformFileForTransit& descriptor() const {
128    DCHECK(is_socket() || is_channel_handle());
129    return descriptor_;
130  }
131  void set_shmem(const base::SharedMemoryHandle& handle, uint32_t size) {
132    type_ = SHARED_MEMORY;
133    shm_handle_ = handle;
134    size_ = size;
135
136    descriptor_ = IPC::InvalidPlatformFileForTransit();
137  }
138  void set_socket(const IPC::PlatformFileForTransit& socket) {
139    type_ = SOCKET;
140    descriptor_ = socket;
141
142    shm_handle_ = base::SharedMemory::NULLHandle();
143    size_ = 0;
144  }
145  void set_channel_handle(const IPC::PlatformFileForTransit& descriptor) {
146    type_ = CHANNEL_HANDLE;
147
148    descriptor_ = descriptor;
149    shm_handle_ = base::SharedMemory::NULLHandle();
150    size_ = 0;
151  }
152  void set_null_shmem() {
153    set_shmem(base::SharedMemory::NULLHandle(), 0);
154  }
155  void set_null_socket() {
156    set_socket(IPC::InvalidPlatformFileForTransit());
157  }
158  void set_null_channel_handle() {
159    set_channel_handle(IPC::InvalidPlatformFileForTransit());
160  }
161  bool IsHandleValid() const;
162
163  Header header() const {
164    return Header(type_, size_);
165  }
166
167  // Closes the handle and sets it to invalid.
168  void Close();
169
170  // Write/Read a Header, which contains all the data except the handle. This
171  // allows us to write the handle in a platform-specific way, as is necessary
172  // in NaClIPCAdapter to share handles with NaCl from Windows.
173  static bool WriteHeader(const Header& hdr, Pickle* pickle);
174  static bool ReadHeader(PickleIterator* iter, Header* hdr);
175
176 private:
177  // The kind of handle we're holding.
178  Type type_;
179
180  // We hold more members than we really need; we can't easily use a union,
181  // because we hold non-POD types. But these types are pretty light-weight. If
182  // we add more complex things later, we should come up with a more memory-
183  // efficient strategy.
184  // These are valid if type == SHARED_MEMORY.
185  base::SharedMemoryHandle shm_handle_;
186  uint32_t size_;
187
188  // This is valid if type == SOCKET || type == CHANNEL_HANDLE.
189  IPC::PlatformFileForTransit descriptor_;
190};
191
192// TODO(tomfinegan): This is identical to PPPVideoCapture_Buffer, maybe replace
193// both with a single type?
194struct PPPDecryptor_Buffer {
195  ppapi::HostResource resource;
196  uint32_t size;
197  base::SharedMemoryHandle handle;
198};
199
200#if defined(OS_WIN)
201typedef HANDLE ImageHandle;
202#elif defined(OS_MACOSX) || defined(OS_ANDROID)
203typedef base::SharedMemoryHandle ImageHandle;
204#else
205// On X Windows this is a SysV shared memory key.
206typedef int ImageHandle;
207#endif
208
209}  // namespace proxy
210}  // namespace ppapi
211
212#endif  // PPAPI_PROXY_SERIALIZED_STRUCTS_H_
213