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_
;
261 #endif // PPAPI_PROXY_RAW_VAR_DATA_H_