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_RAW_VAR_DATA_H_
6 #define PPAPI_PROXY_RAW_VAR_DATA_H_
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "ppapi/c/pp_instance.h"
14 #include "ppapi/c/pp_var.h"
15 #include "ppapi/proxy/ppapi_param_traits.h"
16 #include "ppapi/proxy/ppapi_proxy_export.h"
17 #include "ppapi/proxy/serialized_handle.h"
30 typedef base::Callback
<void(IPC::Message
*, const SerializedHandle
&)>
33 // Contains the data associated with a graph of connected PP_Vars. Useful for
34 // serializing/deserializing a graph of PP_Vars. First we compute the transitive
35 // closure of the given PP_Var to find all PP_Vars which are referenced by that
36 // var. A RawVarData object is created for each of these vars. We then write
37 // data contained in each RawVarData to the message. The format looks like this:
38 // idx | size | (number of vars in the graph)
47 // Vars that reference other vars (such as Arrays or Dictionaries) use indices
48 // into the message to denote which PP_Var is pointed to.
49 class PPAPI_PROXY_EXPORT RawVarDataGraph
{
51 // Construct a RawVarDataGraph from a given root PP_Var. A null pointer
52 // is returned upon failure.
53 static scoped_ptr
<RawVarDataGraph
> Create(const PP_Var
& var
,
54 PP_Instance instance
);
56 // Constructs an empty RawVarDataGraph.
60 // Construct a new PP_Var from the graph. All of the PP_Vars referenced by
61 // the returned PP_Var are also constructed. Each PP_Var created has a
62 // ref-count equal to the number of references it has in the graph of vars.
63 // The returned var (the "root") has one additional reference.
64 PP_Var
CreatePPVar(PP_Instance instance
);
66 // Write the graph to a message using the given HandleWriter.
67 void Write(IPC::Message
* m
, const HandleWriter
& handle_writer
);
69 // Create a RawVarDataGraph from the given message.
70 static scoped_ptr
<RawVarDataGraph
> Read(const IPC::Message
* m
,
71 PickleIterator
* iter
);
73 // Returns a vector of SerializedHandles associated with this RawVarDataGraph.
74 // Ownership of the pointers remains with the elements of the RawVarDataGraph.
75 std::vector
<SerializedHandle
*> GetHandles();
77 // Sets the threshold size at which point we switch from transmitting
78 // array buffers in IPC messages to using shared memory. This is only used
79 // for testing purposes where we need to transmit small buffers using shmem
80 // (in order to have fast tests).
81 static void SetMinimumArrayBufferSizeForShmemForTest(uint32 threshold
);
83 // A list of the nodes in the graph.
84 ScopedVector
<RawVarData
> data_
;
87 // Abstract base class for the data contained in a PP_Var.
90 // Create a new, empty RawVarData for the given type.
91 static RawVarData
* Create(PP_VarType type
);
93 virtual ~RawVarData();
95 // Returns the type of the PP_Var represented by the RawVarData.
96 virtual PP_VarType
Type() = 0;
98 // Initializes a RawVarData from a PP_Var. Returns true on success.
99 virtual bool Init(const PP_Var
& var
, PP_Instance instance
) = 0;
101 // Create a PP_Var from the raw data contained in this object.
102 virtual PP_Var
CreatePPVar(PP_Instance instance
) = 0;
103 // Some PP_Vars may require 2-step initialization. For example, they may
104 // reference other PP_Vars which had not yet been created when |CreatePPVar|
105 // was called. The original var created with |CreatePPVar| is passed back in,
106 // along with the graph it is a part of to be initialized.
107 virtual void PopulatePPVar(const PP_Var
& var
,
108 const std::vector
<PP_Var
>& graph
) = 0;
110 // Writes the RawVarData to a message.
111 virtual void Write(IPC::Message
* m
,
112 const HandleWriter
& handle_writer
) = 0;
113 // Reads the RawVarData from a message. Returns true on success.
114 virtual bool Read(PP_VarType type
,
115 const IPC::Message
* m
,
116 PickleIterator
* iter
) = 0;
118 // Returns a SerializedHandle associated with this RawVarData or NULL if none
119 // exists. Ownership of the pointer remains with the RawVarData.
120 virtual SerializedHandle
* GetHandle();
122 bool initialized() { return initialized_
; }
128 // A RawVarData class for PP_Vars which are value types.
129 class BasicRawVarData
: public RawVarData
{
132 virtual ~BasicRawVarData();
134 // RawVarData implementation.
135 virtual PP_VarType
Type() override
;
136 virtual bool Init(const PP_Var
& var
, PP_Instance instance
) override
;
137 virtual PP_Var
CreatePPVar(PP_Instance instance
) override
;
138 virtual void PopulatePPVar(const PP_Var
& var
,
139 const std::vector
<PP_Var
>& graph
) override
;
140 virtual void Write(IPC::Message
* m
,
141 const HandleWriter
& handle_writer
) override
;
142 virtual bool Read(PP_VarType type
,
143 const IPC::Message
* m
,
144 PickleIterator
* iter
) override
;
150 // A RawVarData class for string PP_Vars.
151 class StringRawVarData
: public RawVarData
{
154 virtual ~StringRawVarData();
156 // RawVarData implementation.
157 virtual PP_VarType
Type() override
;
158 virtual bool Init(const PP_Var
& var
, PP_Instance instance
) override
;
159 virtual PP_Var
CreatePPVar(PP_Instance instance
) override
;
160 virtual void PopulatePPVar(const PP_Var
& var
,
161 const std::vector
<PP_Var
>& graph
) override
;
162 virtual void Write(IPC::Message
* m
,
163 const HandleWriter
& handle_writer
) override
;
164 virtual bool Read(PP_VarType type
,
165 const IPC::Message
* m
,
166 PickleIterator
* iter
) override
;
169 // The data in the string.
173 // A RawVarData class for array buffer PP_Vars.
174 class ArrayBufferRawVarData
: public RawVarData
{
176 // Enum for array buffer message types.
178 ARRAY_BUFFER_NO_SHMEM
,
179 ARRAY_BUFFER_SHMEM_HOST
,
180 ARRAY_BUFFER_SHMEM_PLUGIN
,
183 ArrayBufferRawVarData();
184 virtual ~ArrayBufferRawVarData();
186 // RawVarData implementation.
187 virtual PP_VarType
Type() override
;
188 virtual bool Init(const PP_Var
& var
, PP_Instance instance
) override
;
189 virtual PP_Var
CreatePPVar(PP_Instance instance
) override
;
190 virtual void PopulatePPVar(const PP_Var
& var
,
191 const std::vector
<PP_Var
>& graph
) override
;
192 virtual void Write(IPC::Message
* m
,
193 const HandleWriter
& handle_writer
) override
;
194 virtual bool Read(PP_VarType type
,
195 const IPC::Message
* m
,
196 PickleIterator
* iter
) override
;
197 virtual SerializedHandle
* GetHandle() override
;
200 // The type of the storage underlying the array buffer.
202 // The data in the buffer. Valid for |type_| == ARRAY_BUFFER_NO_SHMEM.
204 // Host shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_HOST.
205 int host_shm_handle_id_
;
206 // Plugin shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_PLUGIN.
207 SerializedHandle plugin_shm_handle_
;
210 // A RawVarData class for array PP_Vars.
211 class ArrayRawVarData
: public RawVarData
{
214 virtual ~ArrayRawVarData();
216 void AddChild(size_t element
);
218 // RawVarData implementation.
219 virtual PP_VarType
Type() override
;
220 virtual bool Init(const PP_Var
& var
, PP_Instance instance
) override
;
221 virtual PP_Var
CreatePPVar(PP_Instance instance
) override
;
222 virtual void PopulatePPVar(const PP_Var
& var
,
223 const std::vector
<PP_Var
>& graph
) override
;
224 virtual void Write(IPC::Message
* m
,
225 const HandleWriter
& handle_writer
) override
;
226 virtual bool Read(PP_VarType type
,
227 const IPC::Message
* m
,
228 PickleIterator
* iter
) override
;
231 std::vector
<size_t> children_
;
234 // A RawVarData class for dictionary PP_Vars.
235 class DictionaryRawVarData
: public RawVarData
{
237 DictionaryRawVarData();
238 virtual ~DictionaryRawVarData();
240 void AddChild(const std::string
& key
, size_t value
);
242 // RawVarData implementation.
243 virtual PP_VarType
Type() override
;
244 virtual bool Init(const PP_Var
& var
, PP_Instance instance
) override
;
245 virtual PP_Var
CreatePPVar(PP_Instance instance
) override
;
246 virtual void PopulatePPVar(const PP_Var
& var
,
247 const std::vector
<PP_Var
>& graph
) override
;
248 virtual void Write(IPC::Message
* m
,
249 const HandleWriter
& handle_writer
) override
;
250 virtual bool Read(PP_VarType type
,
251 const IPC::Message
* m
,
252 PickleIterator
* iter
) override
;
255 std::vector
<std::pair
<std::string
, size_t> > children_
;
258 // A RawVarData class for resource PP_Vars.
259 // This class does not hold a reference on the PP_Resource that is being
260 // serialized. If sending a resource from the plugin to the host, the plugin
261 // should not release the ResourceVar before sending the serialized message to
262 // the host, and the host should immediately consume the ResourceVar before
263 // processing further messages.
264 class ResourceRawVarData
: public RawVarData
{
266 ResourceRawVarData();
267 virtual ~ResourceRawVarData();
269 // RawVarData implementation.
270 virtual PP_VarType
Type() override
;
271 virtual bool Init(const PP_Var
& var
, PP_Instance instance
) override
;
272 virtual PP_Var
CreatePPVar(PP_Instance instance
) override
;
273 virtual void PopulatePPVar(const PP_Var
& var
,
274 const std::vector
<PP_Var
>& graph
) override
;
275 virtual void Write(IPC::Message
* m
,
276 const HandleWriter
& handle_writer
) override
;
277 virtual bool Read(PP_VarType type
,
278 const IPC::Message
* m
,
279 PickleIterator
* iter
) override
;
282 // Resource ID in the plugin. If one has not yet been created, this is 0.
283 // This is a borrowed reference; the resource's refcount is not incremented.
284 PP_Resource pp_resource_
;
286 // Pending resource host ID in the renderer.
287 int pending_renderer_host_id_
;
289 // Pending resource host ID in the browser.
290 int pending_browser_host_id_
;
292 // A message containing information about how to create a plugin-side
293 // resource. The message type will vary based on the resource type, and will
294 // usually contain a pending resource host ID, and other required information.
295 // If the resource was created directly, this is NULL.
296 scoped_ptr
<IPC::Message
> creation_message_
;
302 #endif // PPAPI_PROXY_RAW_VAR_DATA_H_