blob: 22c7ca739ddc80c806b7c98551f26e5044b67d6f [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
dchengced92242016-04-07 00:00:1211#include <memory>
[email protected]42a37482013-04-17 17:51:0612#include <vector>
13
14#include "base/callback.h"
[email protected]42a37482013-04-17 17:51:0615#include "ppapi/c/pp_instance.h"
16#include "ppapi/c/pp_var.h"
17#include "ppapi/proxy/ppapi_param_traits.h"
18#include "ppapi/proxy/ppapi_proxy_export.h"
19#include "ppapi/proxy/serialized_handle.h"
20
brettw05cfd8ddb2015-06-02 07:02:4721namespace base {
22class Pickle;
[email protected]42a37482013-04-17 17:51:0623class PickleIterator;
brettw05cfd8ddb2015-06-02 07:02:4724}
[email protected]42a37482013-04-17 17:51:0625
26namespace IPC {
27class Message;
28}
29
30namespace ppapi {
31namespace proxy {
32
33class RawVarData;
34
Anand K Mistry483007852021-03-18 22:54:4835typedef base::RepeatingCallback<void(base::Pickle*, const SerializedHandle&)>
[email protected]42a37482013-04-17 17:51:0636 HandleWriter;
37
38// Contains the data associated with a graph of connected PP_Vars. Useful for
39// serializing/deserializing a graph of PP_Vars. First we compute the transitive
40// closure of the given PP_Var to find all PP_Vars which are referenced by that
41// var. A RawVarData object is created for each of these vars. We then write
42// data contained in each RawVarData to the message. The format looks like this:
43// idx | size | (number of vars in the graph)
44// 0 | var type |
45// | var data |
46// 1 | var type |
47// | var data |
48// 2 | var type |
49// | var data |
50// | .... |
51//
52// Vars that reference other vars (such as Arrays or Dictionaries) use indices
53// into the message to denote which PP_Var is pointed to.
54class PPAPI_PROXY_EXPORT RawVarDataGraph {
55 public:
56 // Construct a RawVarDataGraph from a given root PP_Var. A null pointer
57 // is returned upon failure.
dchengced92242016-04-07 00:00:1258 static std::unique_ptr<RawVarDataGraph> Create(const PP_Var& var,
59 PP_Instance instance);
[email protected]42a37482013-04-17 17:51:0660
61 // Constructs an empty RawVarDataGraph.
62 RawVarDataGraph();
Peter Boström3d5b3cb2021-09-23 21:35:4563
64 RawVarDataGraph(const RawVarDataGraph&) = delete;
65 RawVarDataGraph& operator=(const RawVarDataGraph&) = delete;
66
[email protected]42a37482013-04-17 17:51:0667 ~RawVarDataGraph();
68
69 // Construct a new PP_Var from the graph. All of the PP_Vars referenced by
70 // the returned PP_Var are also constructed. Each PP_Var created has a
71 // ref-count equal to the number of references it has in the graph of vars.
72 // The returned var (the "root") has one additional reference.
73 PP_Var CreatePPVar(PP_Instance instance);
74
75 // Write the graph to a message using the given HandleWriter.
rockot502c94f2016-02-03 20:20:1676 void Write(base::Pickle* m, const HandleWriter& handle_writer);
[email protected]42a37482013-04-17 17:51:0677
78 // Create a RawVarDataGraph from the given message.
dchengced92242016-04-07 00:00:1279 static std::unique_ptr<RawVarDataGraph> Read(const base::Pickle* m,
80 base::PickleIterator* iter);
[email protected]42a37482013-04-17 17:51:0681
[email protected]38f428f12013-04-19 14:46:0582 // Returns a vector of SerializedHandles associated with this RawVarDataGraph.
83 // Ownership of the pointers remains with the elements of the RawVarDataGraph.
84 std::vector<SerializedHandle*> GetHandles();
85
86 // Sets the threshold size at which point we switch from transmitting
87 // array buffers in IPC messages to using shared memory. This is only used
88 // for testing purposes where we need to transmit small buffers using shmem
89 // (in order to have fast tests).
avie029c4132015-12-23 06:45:2290 static void SetMinimumArrayBufferSizeForShmemForTest(uint32_t threshold);
[email protected]38f428f12013-04-19 14:46:0591
mdempskyd01f5142016-02-01 01:52:1392 private:
[email protected]42a37482013-04-17 17:51:0693 // A list of the nodes in the graph.
dchengced92242016-04-07 00:00:1294 std::vector<std::unique_ptr<RawVarData>> data_;
[email protected]42a37482013-04-17 17:51:0695};
96
97// Abstract base class for the data contained in a PP_Var.
98class RawVarData {
99 public:
100 // Create a new, empty RawVarData for the given type.
101 static RawVarData* Create(PP_VarType type);
102 RawVarData();
103 virtual ~RawVarData();
104
105 // Returns the type of the PP_Var represented by the RawVarData.
106 virtual PP_VarType Type() = 0;
107
108 // Initializes a RawVarData from a PP_Var. Returns true on success.
109 virtual bool Init(const PP_Var& var, PP_Instance instance) = 0;
110
111 // Create a PP_Var from the raw data contained in this object.
112 virtual PP_Var CreatePPVar(PP_Instance instance) = 0;
113 // Some PP_Vars may require 2-step initialization. For example, they may
114 // reference other PP_Vars which had not yet been created when |CreatePPVar|
115 // was called. The original var created with |CreatePPVar| is passed back in,
116 // along with the graph it is a part of to be initialized.
117 virtual void PopulatePPVar(const PP_Var& var,
118 const std::vector<PP_Var>& graph) = 0;
119
120 // Writes the RawVarData to a message.
rockot502c94f2016-02-03 20:20:16121 virtual void Write(base::Pickle* m, const HandleWriter& handle_writer) = 0;
[email protected]42a37482013-04-17 17:51:06122 // Reads the RawVarData from a message. Returns true on success.
123 virtual bool Read(PP_VarType type,
rockot502c94f2016-02-03 20:20:16124 const base::Pickle* 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;
rockot502c94f2016-02-03 20:20:16149 void Write(base::Pickle* m, const HandleWriter& handle_writer) override;
nicke4784432015-04-23 14:01:48150 bool Read(PP_VarType type,
rockot502c94f2016-02-03 20:20:16151 const base::Pickle* 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;
rockot502c94f2016-02-03 20:20:16170 void Write(base::Pickle* m, const HandleWriter& handle_writer) override;
nicke4784432015-04-23 14:01:48171 bool Read(PP_VarType type,
rockot502c94f2016-02-03 20:20:16172 const base::Pickle* 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;
rockot502c94f2016-02-03 20:20:16199 void Write(base::Pickle* m, const HandleWriter& handle_writer) override;
nicke4784432015-04-23 14:01:48200 bool Read(PP_VarType type,
rockot502c94f2016-02-03 20:20:16201 const base::Pickle* 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;
rockot502c94f2016-02-03 20:20:16230 void Write(base::Pickle* m, const HandleWriter& handle_writer) override;
nicke4784432015-04-23 14:01:48231 bool Read(PP_VarType type,
rockot502c94f2016-02-03 20:20:16232 const base::Pickle* 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;
rockot502c94f2016-02-03 20:20:16253 void Write(base::Pickle* m, const HandleWriter& handle_writer) override;
nicke4784432015-04-23 14:01:48254 bool Read(PP_VarType type,
rockot502c94f2016-02-03 20:20:16255 const base::Pickle* 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;
rockot502c94f2016-02-03 20:20:16279 void Write(base::Pickle* m, const HandleWriter& handle_writer) override;
nicke4784432015-04-23 14:01:48280 bool Read(PP_VarType type,
rockot502c94f2016-02-03 20:20:16281 const base::Pickle* 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.
dchengced92242016-04-07 00:00:12299 std::unique_ptr<IPC::Message> creation_message_;
[email protected]09198682013-09-24 10:11:29300};
301
[email protected]42a37482013-04-17 17:51:06302} // namespace proxy
303} // namespace ppapi
304
305#endif // PPAPI_PROXY_RAW_VAR_DATA_H_