blob: 7e437e9c1582719d9502a22977385b4822de64c0 [file] [log] [blame]
[email protected]42a37482013-04-17 17:51:061// 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.
4
5#ifndef PPAPI_PROXY_RAW_VAR_DATA_H_
6#define PPAPI_PROXY_RAW_VAR_DATA_H_
7
avie029c4132015-12-23 06:45:228#include <stddef.h>
9#include <stdint.h>
10
[email protected]42a37482013-04-17 17:51:0611#include <vector>
12
13#include "base/callback.h"
mdempskyd01f5142016-02-01 01:52:1314#include "base/macros.h"
[email protected]42a37482013-04-17 17:51:0615#include "base/memory/scoped_ptr.h"
[email protected]42a37482013-04-17 17:51:0616#include "ppapi/c/pp_instance.h"
17#include "ppapi/c/pp_var.h"
18#include "ppapi/proxy/ppapi_param_traits.h"
19#include "ppapi/proxy/ppapi_proxy_export.h"
20#include "ppapi/proxy/serialized_handle.h"
21
brettw05cfd8ddb2015-06-02 07:02:4722namespace base {
23class Pickle;
[email protected]42a37482013-04-17 17:51:0624class PickleIterator;
brettw05cfd8ddb2015-06-02 07:02:4725}
[email protected]42a37482013-04-17 17:51:0626
27namespace IPC {
28class Message;
29}
30
31namespace ppapi {
32namespace proxy {
33
34class RawVarData;
35
36typedef base::Callback<void(IPC::Message*, const SerializedHandle&)>
37 HandleWriter;
38
39// Contains the data associated with a graph of connected PP_Vars. Useful for
40// serializing/deserializing a graph of PP_Vars. First we compute the transitive
41// closure of the given PP_Var to find all PP_Vars which are referenced by that
42// var. A RawVarData object is created for each of these vars. We then write
43// data contained in each RawVarData to the message. The format looks like this:
44// idx | size | (number of vars in the graph)
45// 0 | var type |
46// | var data |
47// 1 | var type |
48// | var data |
49// 2 | var type |
50// | var data |
51// | .... |
52//
53// Vars that reference other vars (such as Arrays or Dictionaries) use indices
54// into the message to denote which PP_Var is pointed to.
55class PPAPI_PROXY_EXPORT RawVarDataGraph {
56 public:
57 // Construct a RawVarDataGraph from a given root PP_Var. A null pointer
58 // is returned upon failure.
59 static scoped_ptr<RawVarDataGraph> Create(const PP_Var& var,
60 PP_Instance instance);
61
62 // Constructs an empty RawVarDataGraph.
63 RawVarDataGraph();
64 ~RawVarDataGraph();
65
66 // Construct a new PP_Var from the graph. All of the PP_Vars referenced by
67 // the returned PP_Var are also constructed. Each PP_Var created has a
68 // ref-count equal to the number of references it has in the graph of vars.
69 // The returned var (the "root") has one additional reference.
70 PP_Var CreatePPVar(PP_Instance instance);
71
72 // Write the graph to a message using the given HandleWriter.
73 void Write(IPC::Message* m, const HandleWriter& handle_writer);
[email protected]42a37482013-04-17 17:51:0674
75 // Create a RawVarDataGraph from the given message.
76 static scoped_ptr<RawVarDataGraph> Read(const IPC::Message* m,
brettw05cfd8ddb2015-06-02 07:02:4777 base::PickleIterator* iter);
[email protected]42a37482013-04-17 17:51:0678
[email protected]38f428f12013-04-19 14:46:0579 // Returns a vector of SerializedHandles associated with this RawVarDataGraph.
80 // Ownership of the pointers remains with the elements of the RawVarDataGraph.
81 std::vector<SerializedHandle*> GetHandles();
82
83 // Sets the threshold size at which point we switch from transmitting
84 // array buffers in IPC messages to using shared memory. This is only used
85 // for testing purposes where we need to transmit small buffers using shmem
86 // (in order to have fast tests).
avie029c4132015-12-23 06:45:2287 static void SetMinimumArrayBufferSizeForShmemForTest(uint32_t threshold);
[email protected]38f428f12013-04-19 14:46:0588
mdempskyd01f5142016-02-01 01:52:1389 private:
[email protected]42a37482013-04-17 17:51:0690 // A list of the nodes in the graph.
mdempskyd01f5142016-02-01 01:52:1391 std::vector<scoped_ptr<RawVarData>> data_;
92
93 DISALLOW_COPY_AND_ASSIGN(RawVarDataGraph);
[email protected]42a37482013-04-17 17:51:0694};
95
96// Abstract base class for the data contained in a PP_Var.
97class RawVarData {
98 public:
99 // Create a new, empty RawVarData for the given type.
100 static RawVarData* Create(PP_VarType type);
101 RawVarData();
102 virtual ~RawVarData();
103
104 // Returns the type of the PP_Var represented by the RawVarData.
105 virtual PP_VarType Type() = 0;
106
107 // Initializes a RawVarData from a PP_Var. Returns true on success.
108 virtual bool Init(const PP_Var& var, PP_Instance instance) = 0;
109
110 // Create a PP_Var from the raw data contained in this object.
111 virtual PP_Var CreatePPVar(PP_Instance instance) = 0;
112 // Some PP_Vars may require 2-step initialization. For example, they may
113 // reference other PP_Vars which had not yet been created when |CreatePPVar|
114 // was called. The original var created with |CreatePPVar| is passed back in,
115 // along with the graph it is a part of to be initialized.
116 virtual void PopulatePPVar(const PP_Var& var,
117 const std::vector<PP_Var>& graph) = 0;
118
119 // Writes the RawVarData to a message.
120 virtual void Write(IPC::Message* m,
121 const HandleWriter& handle_writer) = 0;
122 // Reads the RawVarData from a message. Returns true on success.
123 virtual bool Read(PP_VarType type,
124 const IPC::Message* m,
brettw05cfd8ddb2015-06-02 07:02:47125 base::PickleIterator* iter) = 0;
[email protected]42a37482013-04-17 17:51:06126
[email protected]38f428f12013-04-19 14:46:05127 // Returns a SerializedHandle associated with this RawVarData or NULL if none
128 // exists. Ownership of the pointer remains with the RawVarData.
129 virtual SerializedHandle* GetHandle();
130
[email protected]42a37482013-04-17 17:51:06131 bool initialized() { return initialized_; }
132
133 protected:
134 bool initialized_;
135};
136
137// A RawVarData class for PP_Vars which are value types.
138class BasicRawVarData : public RawVarData {
139 public:
140 BasicRawVarData();
nicke4784432015-04-23 14:01:48141 ~BasicRawVarData() override;
[email protected]42a37482013-04-17 17:51:06142
143 // RawVarData implementation.
nicke4784432015-04-23 14:01:48144 PP_VarType Type() override;
145 bool Init(const PP_Var& var, PP_Instance instance) override;
146 PP_Var CreatePPVar(PP_Instance instance) override;
147 void PopulatePPVar(const PP_Var& var,
148 const std::vector<PP_Var>& graph) override;
149 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
150 bool Read(PP_VarType type,
151 const IPC::Message* m,
brettw05cfd8ddb2015-06-02 07:02:47152 base::PickleIterator* iter) override;
[email protected]42a37482013-04-17 17:51:06153
154 private:
155 PP_Var var_;
156};
157
158// A RawVarData class for string PP_Vars.
159class StringRawVarData : public RawVarData {
160 public:
161 StringRawVarData();
nicke4784432015-04-23 14:01:48162 ~StringRawVarData() override;
[email protected]42a37482013-04-17 17:51:06163
164 // RawVarData implementation.
nicke4784432015-04-23 14:01:48165 PP_VarType Type() override;
166 bool Init(const PP_Var& var, PP_Instance instance) override;
167 PP_Var CreatePPVar(PP_Instance instance) override;
168 void PopulatePPVar(const PP_Var& var,
169 const std::vector<PP_Var>& graph) override;
170 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
171 bool Read(PP_VarType type,
172 const IPC::Message* m,
brettw05cfd8ddb2015-06-02 07:02:47173 base::PickleIterator* iter) override;
[email protected]42a37482013-04-17 17:51:06174
175 private:
176 // The data in the string.
177 std::string data_;
178};
179
180// A RawVarData class for array buffer PP_Vars.
181class ArrayBufferRawVarData : public RawVarData {
182 public:
183 // Enum for array buffer message types.
184 enum ShmemType {
185 ARRAY_BUFFER_NO_SHMEM,
186 ARRAY_BUFFER_SHMEM_HOST,
187 ARRAY_BUFFER_SHMEM_PLUGIN,
188 };
189
190 ArrayBufferRawVarData();
nicke4784432015-04-23 14:01:48191 ~ArrayBufferRawVarData() override;
[email protected]42a37482013-04-17 17:51:06192
193 // RawVarData implementation.
nicke4784432015-04-23 14:01:48194 PP_VarType Type() override;
195 bool Init(const PP_Var& var, PP_Instance instance) override;
196 PP_Var CreatePPVar(PP_Instance instance) override;
197 void PopulatePPVar(const PP_Var& var,
198 const std::vector<PP_Var>& graph) override;
199 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
200 bool Read(PP_VarType type,
201 const IPC::Message* m,
brettw05cfd8ddb2015-06-02 07:02:47202 base::PickleIterator* iter) override;
nicke4784432015-04-23 14:01:48203 SerializedHandle* GetHandle() override;
[email protected]42a37482013-04-17 17:51:06204
205 private:
206 // The type of the storage underlying the array buffer.
207 ShmemType type_;
208 // The data in the buffer. Valid for |type_| == ARRAY_BUFFER_NO_SHMEM.
209 std::string data_;
210 // Host shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_HOST.
211 int host_shm_handle_id_;
212 // Plugin shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_PLUGIN.
213 SerializedHandle plugin_shm_handle_;
214};
215
216// A RawVarData class for array PP_Vars.
217class ArrayRawVarData : public RawVarData {
218 public:
219 ArrayRawVarData();
nicke4784432015-04-23 14:01:48220 ~ArrayRawVarData() override;
[email protected]42a37482013-04-17 17:51:06221
222 void AddChild(size_t element);
223
224 // RawVarData implementation.
nicke4784432015-04-23 14:01:48225 PP_VarType Type() override;
226 bool Init(const PP_Var& var, PP_Instance instance) override;
227 PP_Var CreatePPVar(PP_Instance instance) override;
228 void PopulatePPVar(const PP_Var& var,
229 const std::vector<PP_Var>& graph) override;
230 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
231 bool Read(PP_VarType type,
232 const IPC::Message* m,
brettw05cfd8ddb2015-06-02 07:02:47233 base::PickleIterator* iter) override;
[email protected]42a37482013-04-17 17:51:06234
235 private:
236 std::vector<size_t> children_;
237};
238
239// A RawVarData class for dictionary PP_Vars.
240class DictionaryRawVarData : public RawVarData {
241 public:
242 DictionaryRawVarData();
nicke4784432015-04-23 14:01:48243 ~DictionaryRawVarData() override;
[email protected]42a37482013-04-17 17:51:06244
245 void AddChild(const std::string& key, size_t value);
246
247 // RawVarData implementation.
nicke4784432015-04-23 14:01:48248 PP_VarType Type() override;
249 bool Init(const PP_Var& var, PP_Instance instance) override;
250 PP_Var CreatePPVar(PP_Instance instance) override;
251 void PopulatePPVar(const PP_Var& var,
252 const std::vector<PP_Var>& graph) override;
253 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
254 bool Read(PP_VarType type,
255 const IPC::Message* m,
brettw05cfd8ddb2015-06-02 07:02:47256 base::PickleIterator* iter) override;
[email protected]42a37482013-04-17 17:51:06257
258 private:
259 std::vector<std::pair<std::string, size_t> > children_;
260};
261
[email protected]09198682013-09-24 10:11:29262// A RawVarData class for resource PP_Vars.
263// This class does not hold a reference on the PP_Resource that is being
264// serialized. If sending a resource from the plugin to the host, the plugin
265// should not release the ResourceVar before sending the serialized message to
266// the host, and the host should immediately consume the ResourceVar before
267// processing further messages.
268class ResourceRawVarData : public RawVarData {
269 public:
270 ResourceRawVarData();
nicke4784432015-04-23 14:01:48271 ~ResourceRawVarData() override;
[email protected]09198682013-09-24 10:11:29272
273 // RawVarData implementation.
nicke4784432015-04-23 14:01:48274 PP_VarType Type() override;
275 bool Init(const PP_Var& var, PP_Instance instance) override;
276 PP_Var CreatePPVar(PP_Instance instance) override;
277 void PopulatePPVar(const PP_Var& var,
278 const std::vector<PP_Var>& graph) override;
279 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
280 bool Read(PP_VarType type,
281 const IPC::Message* m,
brettw05cfd8ddb2015-06-02 07:02:47282 base::PickleIterator* iter) override;
[email protected]09198682013-09-24 10:11:29283
284 private:
285 // Resource ID in the plugin. If one has not yet been created, this is 0.
286 // This is a borrowed reference; the resource's refcount is not incremented.
287 PP_Resource pp_resource_;
288
[email protected]29b8f232013-09-26 07:38:33289 // Pending resource host ID in the renderer.
290 int pending_renderer_host_id_;
291
292 // Pending resource host ID in the browser.
293 int pending_browser_host_id_;
294
[email protected]09198682013-09-24 10:11:29295 // A message containing information about how to create a plugin-side
296 // resource. The message type will vary based on the resource type, and will
297 // usually contain a pending resource host ID, and other required information.
298 // If the resource was created directly, this is NULL.
299 scoped_ptr<IPC::Message> creation_message_;
300};
301
[email protected]42a37482013-04-17 17:51:06302} // namespace proxy
303} // namespace ppapi
304
305#endif // PPAPI_PROXY_RAW_VAR_DATA_H_