OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef PPAPI_PROXY_RAW_VAR_DATA_H_ | |
6 #define PPAPI_PROXY_RAW_VAR_DATA_H_ | |
7 | |
8 #include <vector> | |
9 | |
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/serialized_handle.h" | |
17 #include "ppapi/shared_impl/var_graph.h" | |
18 | |
19 class PickleIterator; | |
20 | |
21 namespace IPC { | |
22 class Message; | |
23 } | |
24 | |
25 namespace ppapi { | |
26 namespace proxy { | |
27 | |
28 class RawVarData; | |
29 | |
30 typedef base::Callback<void(IPC::Message*, const SerializedHandle&)> | |
31 HandleWriter; | |
32 | |
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. Only PP_Vars which are valid vars in the VarTracker are included (other | |
37 // vars are leaves in the graph, see below for how they are handled). A | |
38 // RawVarData object is created for each of these vars. We then write data | |
39 // contained in each RawVarData to the message. The format looks like this: | |
40 // idx | size | (number of vars in the graph) | |
41 // 0 | var type | | |
42 // | var data | | |
43 // 1 | var type | | |
44 // | var data | | |
45 // 2 | var type | | |
46 // | var data | | |
47 // | .... | | |
48 // | |
49 // Vars that reference other vars (such as arrays and dictionaries) are written | |
50 // specially. We know that a PP_Var contained in another PP_Var must either | |
51 // be a reference to a var in the transitive closure (or a null reference) or be | |
52 // a primitive value. If it is a primitive value, we write it out in the same | |
53 // format as above (type followed by data). If it is a reference, we write the | |
54 // index in the array written above to denote the reference: | |
55 // | var type | (the type of the var pointed to) | |
56 // | valid ref | (boolean: whether it is a null reference) | |
57 // | index | (index into the array of vars written to the msg) | |
58 class RawVarDataGraph { | |
59 public: | |
60 // Constructs an empty RawVarDataGraph. | |
61 RawVarDataGraph(); | |
62 // Construct a RawVarDataGraph from a given root PP_Var. | |
63 RawVarDataGraph(const PP_Var& var, PP_Instance instance); | |
64 virtual ~RawVarDataGraph(); | |
dmichael (off chromium)
2013/04/12 22:40:38
I don't see you using this as a base class, so it
raymes
2013/04/14 16:32:57
Done.
| |
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 of at least one, plus the number of references to it in the | |
69 // graph. | |
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); | |
74 // Write the graph to a message using the default handle writer. | |
75 void Write(IPC::Message* m); | |
76 | |
77 // Create a RawVarDataGraph from the given message. | |
78 static scoped_ptr<RawVarDataGraph> Read(const IPC::Message* m, | |
79 PickleIterator* iter); | |
80 | |
81 // A list of the nodes in the graph. | |
82 ScopedVector<RawVarData> data_; | |
83 }; | |
84 | |
85 // Abstract base class for the data contained in a PP_Var. | |
86 class RawVarData { | |
87 public: | |
88 virtual ~RawVarData(); | |
89 | |
90 // Create a PP_Var from the raw data contained in this object. | |
91 virtual PP_Var CreatePPVar(PP_Instance instance) = 0; | |
92 // Some PP_Vars may require 2-step initialization. For example, they may | |
93 // reference other PP_Vars which had not yet been created when |CreatePPVar| | |
94 // was called. The original var created with |CreatePPVar| is passed back in, | |
95 // along with the graph it is apart of to be initialized. | |
dmichael (off chromium)
2013/04/12 22:40:38
apart->a part
raymes
2013/04/14 16:32:57
Done.
| |
96 virtual void InitPPVar(const PP_Var& var, | |
dmichael (off chromium)
2013/04/12 22:40:38
If CreatePPVar returns |var|, it seems a little od
raymes
2013/04/14 16:32:57
I debated about doing something like this be decid
| |
97 const std::vector<PP_Var>& graph) = 0; | |
98 | |
99 // Writes the RawVarData to a message. | |
100 virtual void Write(IPC::Message* m, | |
101 const HandleWriter& handle_writer) = 0; | |
102 | |
103 // Represents a reference to another PP_Var. For example references can be | |
104 // held by array and dictionary PP_Vars. | |
105 struct VarReference { | |
106 PP_Var var; | |
107 // Whether or not the reference is valid (i.e. whether or not the |as_id| | |
108 // property points to a live var). | |
109 bool valid_var_ref; | |
dmichael (off chromium)
2013/04/12 22:40:38
Not sure we need this... if any are invalid, mayb
raymes
2013/04/14 16:32:57
Done - if there are any invalid vars in the graph,
| |
110 // An index into the |data_| field of |RawVarData| denoting which node this | |
111 // var references. | |
112 uint32_t ref_id; | |
113 }; | |
114 }; | |
115 | |
116 // A RawVarData class for PP_Vars which are value types. | |
117 class BasicRawVarData : public RawVarData { | |
118 public: | |
119 BasicRawVarData(const PP_Var& var); | |
120 virtual ~BasicRawVarData(); | |
121 | |
122 // RawVarData implementation. | |
123 virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE; | |
124 virtual void InitPPVar(const PP_Var& var, | |
125 const std::vector<PP_Var>& graph) OVERRIDE; | |
126 virtual void Write(IPC::Message* m, | |
127 const HandleWriter& handle_writer) OVERRIDE; | |
128 | |
129 static BasicRawVarData* Read(PP_VarType type, | |
130 const IPC::Message* m, | |
131 PickleIterator* iter); | |
132 | |
133 private: | |
134 PP_Var var_; | |
135 }; | |
136 | |
137 // A RawVarData class for string PP_Vars. | |
138 class StringRawVarData : public RawVarData { | |
139 public: | |
140 StringRawVarData(const PP_Var& var); | |
141 StringRawVarData(bool valid_var_ref, const std::string& data); | |
142 virtual ~StringRawVarData(); | |
143 | |
144 // RawVarData implementation. | |
145 virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE; | |
146 virtual void InitPPVar(const PP_Var& var, | |
147 const std::vector<PP_Var>& graph) OVERRIDE; | |
148 virtual void Write(IPC::Message* m, | |
149 const HandleWriter& handle_writer) OVERRIDE; | |
150 | |
151 static StringRawVarData* Read(PP_VarType type, | |
152 const IPC::Message* m, | |
153 PickleIterator* iter); | |
154 | |
155 private: | |
156 // Denotes whether the underlying PP_Var was a valid reference. | |
157 bool valid_var_ref_; | |
158 // The data in the string. | |
159 std::string data_; | |
160 }; | |
161 | |
162 // A RawVarData class for array buffer PP_Vars. | |
163 class ArrayBufferRawVarData : public RawVarData { | |
164 public: | |
165 // Enum for array buffer message types. | |
166 enum ShmemType { | |
167 ARRAY_BUFFER_NO_SHMEM, | |
168 ARRAY_BUFFER_SHMEM_HOST, | |
169 ARRAY_BUFFER_SHMEM_PLUGIN, | |
170 }; | |
171 | |
172 ArrayBufferRawVarData(const PP_Var& var, PP_Instance instance); | |
173 ArrayBufferRawVarData(bool valid_var_ref, ShmemType type); | |
174 virtual ~ArrayBufferRawVarData(); | |
175 | |
176 // RawVarData implementation. | |
177 virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE; | |
178 virtual void InitPPVar(const PP_Var& var, | |
179 const std::vector<PP_Var>& graph) OVERRIDE; | |
180 virtual void Write(IPC::Message* m, | |
181 const HandleWriter& handle_writer) OVERRIDE; | |
182 | |
183 static ArrayBufferRawVarData* Read(PP_VarType type, | |
184 const IPC::Message* m, | |
185 PickleIterator* iter); | |
186 | |
187 private: | |
188 bool valid_var_ref_; | |
189 // The type of the storage underlying the array buffer. | |
190 ShmemType type_; | |
191 // The data in the buffer. Valid for |type_| == ARRAY_BUFFER_NO_SHMEM. | |
192 std::string data_; | |
dmichael (off chromium)
2013/04/12 22:40:38
vector<uint8_t> seems more natural to me.
| |
193 // Host shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_HOST. | |
194 int host_shm_handle_id_; | |
195 // Plugin shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_PLUGIN. | |
196 SerializedHandle plugin_shm_handle_; | |
197 }; | |
198 | |
199 // A RawVarData class for array PP_Vars. | |
200 class ArrayRawVarData : public RawVarData { | |
201 public: | |
202 ArrayRawVarData(const PP_Var& var, const VarGraph& var_graph); | |
203 ArrayRawVarData(bool valid_var_ref, | |
204 const std::vector<VarReference>& data); | |
205 virtual ~ArrayRawVarData(); | |
206 | |
207 // RawVarData implementation. | |
208 virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE; | |
209 virtual void InitPPVar(const PP_Var& var, | |
210 const std::vector<PP_Var>& graph) OVERRIDE; | |
211 virtual void Write(IPC::Message* m, | |
212 const HandleWriter& handle_writer) OVERRIDE; | |
213 | |
214 static ArrayRawVarData* Read(PP_VarType type, | |
215 const IPC::Message* m, | |
216 PickleIterator* iter); | |
217 | |
218 private: | |
219 bool valid_var_ref_; | |
220 // A vector of the references representing the elements of the array. | |
221 std::vector<VarReference> data_; | |
222 }; | |
223 | |
224 // A RawVarData class for dictionary PP_Vars. | |
225 class DictionaryRawVarData : public RawVarData { | |
226 public: | |
227 DictionaryRawVarData(const PP_Var& var, const VarGraph& graph); | |
228 DictionaryRawVarData( | |
229 bool valid_var_ref, | |
230 const std::vector<std::pair<std::string, VarReference> >& data); | |
231 virtual ~DictionaryRawVarData(); | |
232 | |
233 // RawVarData implementation. | |
234 virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE; | |
235 virtual void InitPPVar(const PP_Var& var, | |
236 const std::vector<PP_Var>& graph) OVERRIDE; | |
237 virtual void Write(IPC::Message* m, | |
238 const HandleWriter& handle_writer) OVERRIDE; | |
239 | |
240 static DictionaryRawVarData* Read(PP_VarType type, | |
241 const IPC::Message* m, | |
242 PickleIterator* iter); | |
243 | |
244 private: | |
245 bool valid_var_ref_; | |
246 // A vector of the key/value reference pairs representing elements of the | |
247 // dictionary. | |
248 std::vector<std::pair<std::string, VarReference> > data_; | |
249 }; | |
250 | |
251 } // namespace proxy | |
252 } // namespace ppapi | |
253 | |
254 #endif // PPAPI_PROXY_RAW_VAR_DATA_H_ | |
OLD | NEW |