Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ppapi / proxy / raw_var_data.h
blob9db31007abbf95b33c6c4ca74ae7ef4e5af1387f
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_
8 #include <vector>
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"
19 namespace base {
20 class Pickle;
21 class PickleIterator;
24 namespace IPC {
25 class Message;
28 namespace ppapi {
29 namespace proxy {
31 class RawVarData;
33 typedef base::Callback<void(IPC::Message*, const SerializedHandle&)>
34 HandleWriter;
36 // Contains the data associated with a graph of connected PP_Vars. Useful for
37 // serializing/deserializing a graph of PP_Vars. First we compute the transitive
38 // closure of the given PP_Var to find all PP_Vars which are referenced by that
39 // var. A RawVarData object is created for each of these vars. We then write
40 // data contained in each RawVarData to the message. The format looks like this:
41 // idx | size | (number of vars in the graph)
42 // 0 | var type |
43 // | var data |
44 // 1 | var type |
45 // | var data |
46 // 2 | var type |
47 // | var data |
48 // | .... |
50 // Vars that reference other vars (such as Arrays or Dictionaries) use indices
51 // into the message to denote which PP_Var is pointed to.
52 class PPAPI_PROXY_EXPORT RawVarDataGraph {
53 public:
54 // Construct a RawVarDataGraph from a given root PP_Var. A null pointer
55 // is returned upon failure.
56 static scoped_ptr<RawVarDataGraph> Create(const PP_Var& var,
57 PP_Instance instance);
59 // Constructs an empty RawVarDataGraph.
60 RawVarDataGraph();
61 ~RawVarDataGraph();
63 // Construct a new PP_Var from the graph. All of the PP_Vars referenced by
64 // the returned PP_Var are also constructed. Each PP_Var created has a
65 // ref-count equal to the number of references it has in the graph of vars.
66 // The returned var (the "root") has one additional reference.
67 PP_Var CreatePPVar(PP_Instance instance);
69 // Write the graph to a message using the given HandleWriter.
70 void Write(IPC::Message* m, const HandleWriter& handle_writer);
72 // Create a RawVarDataGraph from the given message.
73 static scoped_ptr<RawVarDataGraph> Read(const IPC::Message* m,
74 base::PickleIterator* iter);
76 // Returns a vector of SerializedHandles associated with this RawVarDataGraph.
77 // Ownership of the pointers remains with the elements of the RawVarDataGraph.
78 std::vector<SerializedHandle*> GetHandles();
80 // Sets the threshold size at which point we switch from transmitting
81 // array buffers in IPC messages to using shared memory. This is only used
82 // for testing purposes where we need to transmit small buffers using shmem
83 // (in order to have fast tests).
84 static void SetMinimumArrayBufferSizeForShmemForTest(uint32 threshold);
86 // A list of the nodes in the graph.
87 ScopedVector<RawVarData> data_;
90 // Abstract base class for the data contained in a PP_Var.
91 class RawVarData {
92 public:
93 // Create a new, empty RawVarData for the given type.
94 static RawVarData* Create(PP_VarType type);
95 RawVarData();
96 virtual ~RawVarData();
98 // Returns the type of the PP_Var represented by the RawVarData.
99 virtual PP_VarType Type() = 0;
101 // Initializes a RawVarData from a PP_Var. Returns true on success.
102 virtual bool Init(const PP_Var& var, PP_Instance instance) = 0;
104 // Create a PP_Var from the raw data contained in this object.
105 virtual PP_Var CreatePPVar(PP_Instance instance) = 0;
106 // Some PP_Vars may require 2-step initialization. For example, they may
107 // reference other PP_Vars which had not yet been created when |CreatePPVar|
108 // was called. The original var created with |CreatePPVar| is passed back in,
109 // along with the graph it is a part of to be initialized.
110 virtual void PopulatePPVar(const PP_Var& var,
111 const std::vector<PP_Var>& graph) = 0;
113 // Writes the RawVarData to a message.
114 virtual void Write(IPC::Message* m,
115 const HandleWriter& handle_writer) = 0;
116 // Reads the RawVarData from a message. Returns true on success.
117 virtual bool Read(PP_VarType type,
118 const IPC::Message* m,
119 base::PickleIterator* iter) = 0;
121 // Returns a SerializedHandle associated with this RawVarData or NULL if none
122 // exists. Ownership of the pointer remains with the RawVarData.
123 virtual SerializedHandle* GetHandle();
125 bool initialized() { return initialized_; }
127 protected:
128 bool initialized_;
131 // A RawVarData class for PP_Vars which are value types.
132 class BasicRawVarData : public RawVarData {
133 public:
134 BasicRawVarData();
135 ~BasicRawVarData() override;
137 // RawVarData implementation.
138 PP_VarType Type() override;
139 bool Init(const PP_Var& var, PP_Instance instance) override;
140 PP_Var CreatePPVar(PP_Instance instance) override;
141 void PopulatePPVar(const PP_Var& var,
142 const std::vector<PP_Var>& graph) override;
143 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
144 bool Read(PP_VarType type,
145 const IPC::Message* m,
146 base::PickleIterator* iter) override;
148 private:
149 PP_Var var_;
152 // A RawVarData class for string PP_Vars.
153 class StringRawVarData : public RawVarData {
154 public:
155 StringRawVarData();
156 ~StringRawVarData() override;
158 // RawVarData implementation.
159 PP_VarType Type() override;
160 bool Init(const PP_Var& var, PP_Instance instance) override;
161 PP_Var CreatePPVar(PP_Instance instance) override;
162 void PopulatePPVar(const PP_Var& var,
163 const std::vector<PP_Var>& graph) override;
164 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
165 bool Read(PP_VarType type,
166 const IPC::Message* m,
167 base::PickleIterator* iter) override;
169 private:
170 // The data in the string.
171 std::string data_;
174 // A RawVarData class for array buffer PP_Vars.
175 class ArrayBufferRawVarData : public RawVarData {
176 public:
177 // Enum for array buffer message types.
178 enum ShmemType {
179 ARRAY_BUFFER_NO_SHMEM,
180 ARRAY_BUFFER_SHMEM_HOST,
181 ARRAY_BUFFER_SHMEM_PLUGIN,
184 ArrayBufferRawVarData();
185 ~ArrayBufferRawVarData() override;
187 // RawVarData implementation.
188 PP_VarType Type() override;
189 bool Init(const PP_Var& var, PP_Instance instance) override;
190 PP_Var CreatePPVar(PP_Instance instance) override;
191 void PopulatePPVar(const PP_Var& var,
192 const std::vector<PP_Var>& graph) override;
193 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
194 bool Read(PP_VarType type,
195 const IPC::Message* m,
196 base::PickleIterator* iter) override;
197 SerializedHandle* GetHandle() override;
199 private:
200 // The type of the storage underlying the array buffer.
201 ShmemType type_;
202 // The data in the buffer. Valid for |type_| == ARRAY_BUFFER_NO_SHMEM.
203 std::string data_;
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 {
212 public:
213 ArrayRawVarData();
214 ~ArrayRawVarData() override;
216 void AddChild(size_t element);
218 // RawVarData implementation.
219 PP_VarType Type() override;
220 bool Init(const PP_Var& var, PP_Instance instance) override;
221 PP_Var CreatePPVar(PP_Instance instance) override;
222 void PopulatePPVar(const PP_Var& var,
223 const std::vector<PP_Var>& graph) override;
224 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
225 bool Read(PP_VarType type,
226 const IPC::Message* m,
227 base::PickleIterator* iter) override;
229 private:
230 std::vector<size_t> children_;
233 // A RawVarData class for dictionary PP_Vars.
234 class DictionaryRawVarData : public RawVarData {
235 public:
236 DictionaryRawVarData();
237 ~DictionaryRawVarData() override;
239 void AddChild(const std::string& key, size_t value);
241 // RawVarData implementation.
242 PP_VarType Type() override;
243 bool Init(const PP_Var& var, PP_Instance instance) override;
244 PP_Var CreatePPVar(PP_Instance instance) override;
245 void PopulatePPVar(const PP_Var& var,
246 const std::vector<PP_Var>& graph) override;
247 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
248 bool Read(PP_VarType type,
249 const IPC::Message* m,
250 base::PickleIterator* iter) override;
252 private:
253 std::vector<std::pair<std::string, size_t> > children_;
256 // A RawVarData class for resource PP_Vars.
257 // This class does not hold a reference on the PP_Resource that is being
258 // serialized. If sending a resource from the plugin to the host, the plugin
259 // should not release the ResourceVar before sending the serialized message to
260 // the host, and the host should immediately consume the ResourceVar before
261 // processing further messages.
262 class ResourceRawVarData : public RawVarData {
263 public:
264 ResourceRawVarData();
265 ~ResourceRawVarData() override;
267 // RawVarData implementation.
268 PP_VarType Type() override;
269 bool Init(const PP_Var& var, PP_Instance instance) override;
270 PP_Var CreatePPVar(PP_Instance instance) override;
271 void PopulatePPVar(const PP_Var& var,
272 const std::vector<PP_Var>& graph) override;
273 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
274 bool Read(PP_VarType type,
275 const IPC::Message* m,
276 base::PickleIterator* iter) override;
278 private:
279 // Resource ID in the plugin. If one has not yet been created, this is 0.
280 // This is a borrowed reference; the resource's refcount is not incremented.
281 PP_Resource pp_resource_;
283 // Pending resource host ID in the renderer.
284 int pending_renderer_host_id_;
286 // Pending resource host ID in the browser.
287 int pending_browser_host_id_;
289 // A message containing information about how to create a plugin-side
290 // resource. The message type will vary based on the resource type, and will
291 // usually contain a pending resource host ID, and other required information.
292 // If the resource was created directly, this is NULL.
293 scoped_ptr<IPC::Message> creation_message_;
296 } // namespace proxy
297 } // namespace ppapi
299 #endif // PPAPI_PROXY_RAW_VAR_DATA_H_