Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / ppapi / proxy / raw_var_data.h
blob08b85fad02d0c4b0370ce4a7184a6a88635366d6
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 class PickleIterator;
21 namespace IPC {
22 class Message;
25 namespace ppapi {
26 namespace proxy {
28 class RawVarData;
30 typedef base::Callback<void(IPC::Message*, const SerializedHandle&)>
31 HandleWriter;
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)
39 // 0 | var type |
40 // | var data |
41 // 1 | var type |
42 // | var data |
43 // 2 | var type |
44 // | var data |
45 // | .... |
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 {
50 public:
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.
57 RawVarDataGraph();
58 ~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.
88 class RawVarData {
89 public:
90 // Create a new, empty RawVarData for the given type.
91 static RawVarData* Create(PP_VarType type);
92 RawVarData();
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_; }
124 protected:
125 bool initialized_;
128 // A RawVarData class for PP_Vars which are value types.
129 class BasicRawVarData : public RawVarData {
130 public:
131 BasicRawVarData();
132 ~BasicRawVarData() override;
134 // RawVarData implementation.
135 PP_VarType Type() override;
136 bool Init(const PP_Var& var, PP_Instance instance) override;
137 PP_Var CreatePPVar(PP_Instance instance) override;
138 void PopulatePPVar(const PP_Var& var,
139 const std::vector<PP_Var>& graph) override;
140 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
141 bool Read(PP_VarType type,
142 const IPC::Message* m,
143 PickleIterator* iter) override;
145 private:
146 PP_Var var_;
149 // A RawVarData class for string PP_Vars.
150 class StringRawVarData : public RawVarData {
151 public:
152 StringRawVarData();
153 ~StringRawVarData() override;
155 // RawVarData implementation.
156 PP_VarType Type() override;
157 bool Init(const PP_Var& var, PP_Instance instance) override;
158 PP_Var CreatePPVar(PP_Instance instance) override;
159 void PopulatePPVar(const PP_Var& var,
160 const std::vector<PP_Var>& graph) override;
161 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
162 bool Read(PP_VarType type,
163 const IPC::Message* m,
164 PickleIterator* iter) override;
166 private:
167 // The data in the string.
168 std::string data_;
171 // A RawVarData class for array buffer PP_Vars.
172 class ArrayBufferRawVarData : public RawVarData {
173 public:
174 // Enum for array buffer message types.
175 enum ShmemType {
176 ARRAY_BUFFER_NO_SHMEM,
177 ARRAY_BUFFER_SHMEM_HOST,
178 ARRAY_BUFFER_SHMEM_PLUGIN,
181 ArrayBufferRawVarData();
182 ~ArrayBufferRawVarData() override;
184 // RawVarData implementation.
185 PP_VarType Type() override;
186 bool Init(const PP_Var& var, PP_Instance instance) override;
187 PP_Var CreatePPVar(PP_Instance instance) override;
188 void PopulatePPVar(const PP_Var& var,
189 const std::vector<PP_Var>& graph) override;
190 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
191 bool Read(PP_VarType type,
192 const IPC::Message* m,
193 PickleIterator* iter) override;
194 SerializedHandle* GetHandle() override;
196 private:
197 // The type of the storage underlying the array buffer.
198 ShmemType type_;
199 // The data in the buffer. Valid for |type_| == ARRAY_BUFFER_NO_SHMEM.
200 std::string data_;
201 // Host shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_HOST.
202 int host_shm_handle_id_;
203 // Plugin shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_PLUGIN.
204 SerializedHandle plugin_shm_handle_;
207 // A RawVarData class for array PP_Vars.
208 class ArrayRawVarData : public RawVarData {
209 public:
210 ArrayRawVarData();
211 ~ArrayRawVarData() override;
213 void AddChild(size_t element);
215 // RawVarData implementation.
216 PP_VarType Type() override;
217 bool Init(const PP_Var& var, PP_Instance instance) override;
218 PP_Var CreatePPVar(PP_Instance instance) override;
219 void PopulatePPVar(const PP_Var& var,
220 const std::vector<PP_Var>& graph) override;
221 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
222 bool Read(PP_VarType type,
223 const IPC::Message* m,
224 PickleIterator* iter) override;
226 private:
227 std::vector<size_t> children_;
230 // A RawVarData class for dictionary PP_Vars.
231 class DictionaryRawVarData : public RawVarData {
232 public:
233 DictionaryRawVarData();
234 ~DictionaryRawVarData() override;
236 void AddChild(const std::string& key, size_t value);
238 // RawVarData implementation.
239 PP_VarType Type() override;
240 bool Init(const PP_Var& var, PP_Instance instance) override;
241 PP_Var CreatePPVar(PP_Instance instance) override;
242 void PopulatePPVar(const PP_Var& var,
243 const std::vector<PP_Var>& graph) override;
244 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
245 bool Read(PP_VarType type,
246 const IPC::Message* m,
247 PickleIterator* iter) override;
249 private:
250 std::vector<std::pair<std::string, size_t> > children_;
253 // A RawVarData class for resource PP_Vars.
254 // This class does not hold a reference on the PP_Resource that is being
255 // serialized. If sending a resource from the plugin to the host, the plugin
256 // should not release the ResourceVar before sending the serialized message to
257 // the host, and the host should immediately consume the ResourceVar before
258 // processing further messages.
259 class ResourceRawVarData : public RawVarData {
260 public:
261 ResourceRawVarData();
262 ~ResourceRawVarData() override;
264 // RawVarData implementation.
265 PP_VarType Type() override;
266 bool Init(const PP_Var& var, PP_Instance instance) override;
267 PP_Var CreatePPVar(PP_Instance instance) override;
268 void PopulatePPVar(const PP_Var& var,
269 const std::vector<PP_Var>& graph) override;
270 void Write(IPC::Message* m, const HandleWriter& handle_writer) override;
271 bool Read(PP_VarType type,
272 const IPC::Message* m,
273 PickleIterator* iter) override;
275 private:
276 // Resource ID in the plugin. If one has not yet been created, this is 0.
277 // This is a borrowed reference; the resource's refcount is not incremented.
278 PP_Resource pp_resource_;
280 // Pending resource host ID in the renderer.
281 int pending_renderer_host_id_;
283 // Pending resource host ID in the browser.
284 int pending_browser_host_id_;
286 // A message containing information about how to create a plugin-side
287 // resource. The message type will vary based on the resource type, and will
288 // usually contain a pending resource host ID, and other required information.
289 // If the resource was created directly, this is NULL.
290 scoped_ptr<IPC::Message> creation_message_;
293 } // namespace proxy
294 } // namespace ppapi
296 #endif // PPAPI_PROXY_RAW_VAR_DATA_H_