[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 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 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 11 | #include <memory> |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
| 14 | #include "base/callback.h" |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 15 | #include "ppapi/c/pp_instance.h" |
| 16 | #include "ppapi/c/pp_var.h" |
| 17 | #include "ppapi/proxy/ppapi_param_traits.h" |
| 18 | #include "ppapi/proxy/ppapi_proxy_export.h" |
| 19 | #include "ppapi/proxy/serialized_handle.h" |
| 20 | |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 21 | namespace base { |
| 22 | class Pickle; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 23 | class PickleIterator; |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 24 | } |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 25 | |
| 26 | namespace IPC { |
| 27 | class Message; |
| 28 | } |
| 29 | |
| 30 | namespace ppapi { |
| 31 | namespace proxy { |
| 32 | |
| 33 | class RawVarData; |
| 34 | |
Anand K Mistry | 48300785 | 2021-03-18 22:54:48 | [diff] [blame] | 35 | typedef base::RepeatingCallback<void(base::Pickle*, const SerializedHandle&)> |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 36 | HandleWriter; |
| 37 | |
| 38 | // Contains the data associated with a graph of connected PP_Vars. Useful for |
| 39 | // serializing/deserializing a graph of PP_Vars. First we compute the transitive |
| 40 | // closure of the given PP_Var to find all PP_Vars which are referenced by that |
| 41 | // var. A RawVarData object is created for each of these vars. We then write |
| 42 | // data contained in each RawVarData to the message. The format looks like this: |
| 43 | // idx | size | (number of vars in the graph) |
| 44 | // 0 | var type | |
| 45 | // | var data | |
| 46 | // 1 | var type | |
| 47 | // | var data | |
| 48 | // 2 | var type | |
| 49 | // | var data | |
| 50 | // | .... | |
| 51 | // |
| 52 | // Vars that reference other vars (such as Arrays or Dictionaries) use indices |
| 53 | // into the message to denote which PP_Var is pointed to. |
| 54 | class PPAPI_PROXY_EXPORT RawVarDataGraph { |
| 55 | public: |
| 56 | // Construct a RawVarDataGraph from a given root PP_Var. A null pointer |
| 57 | // is returned upon failure. |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 58 | static std::unique_ptr<RawVarDataGraph> Create(const PP_Var& var, |
| 59 | PP_Instance instance); |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 60 | |
| 61 | // Constructs an empty RawVarDataGraph. |
| 62 | RawVarDataGraph(); |
Peter Boström | 3d5b3cb | 2021-09-23 21:35:45 | [diff] [blame] | 63 | |
| 64 | RawVarDataGraph(const RawVarDataGraph&) = delete; |
| 65 | RawVarDataGraph& operator=(const RawVarDataGraph&) = delete; |
| 66 | |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 67 | ~RawVarDataGraph(); |
| 68 | |
| 69 | // Construct a new PP_Var from the graph. All of the PP_Vars referenced by |
| 70 | // the returned PP_Var are also constructed. Each PP_Var created has a |
| 71 | // ref-count equal to the number of references it has in the graph of vars. |
| 72 | // The returned var (the "root") has one additional reference. |
| 73 | PP_Var CreatePPVar(PP_Instance instance); |
| 74 | |
| 75 | // Write the graph to a message using the given HandleWriter. |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 76 | void Write(base::Pickle* m, const HandleWriter& handle_writer); |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 77 | |
| 78 | // Create a RawVarDataGraph from the given message. |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 79 | static std::unique_ptr<RawVarDataGraph> Read(const base::Pickle* m, |
| 80 | base::PickleIterator* iter); |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 81 | |
[email protected] | 38f428f1 | 2013-04-19 14:46:05 | [diff] [blame] | 82 | // Returns a vector of SerializedHandles associated with this RawVarDataGraph. |
| 83 | // Ownership of the pointers remains with the elements of the RawVarDataGraph. |
| 84 | std::vector<SerializedHandle*> GetHandles(); |
| 85 | |
| 86 | // Sets the threshold size at which point we switch from transmitting |
| 87 | // array buffers in IPC messages to using shared memory. This is only used |
| 88 | // for testing purposes where we need to transmit small buffers using shmem |
| 89 | // (in order to have fast tests). |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 90 | static void SetMinimumArrayBufferSizeForShmemForTest(uint32_t threshold); |
[email protected] | 38f428f1 | 2013-04-19 14:46:05 | [diff] [blame] | 91 | |
mdempsky | d01f514 | 2016-02-01 01:52:13 | [diff] [blame] | 92 | private: |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 93 | // A list of the nodes in the graph. |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 94 | std::vector<std::unique_ptr<RawVarData>> data_; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | // Abstract base class for the data contained in a PP_Var. |
| 98 | class RawVarData { |
| 99 | public: |
| 100 | // Create a new, empty RawVarData for the given type. |
| 101 | static RawVarData* Create(PP_VarType type); |
| 102 | RawVarData(); |
| 103 | virtual ~RawVarData(); |
| 104 | |
| 105 | // Returns the type of the PP_Var represented by the RawVarData. |
| 106 | virtual PP_VarType Type() = 0; |
| 107 | |
| 108 | // Initializes a RawVarData from a PP_Var. Returns true on success. |
| 109 | virtual bool Init(const PP_Var& var, PP_Instance instance) = 0; |
| 110 | |
| 111 | // Create a PP_Var from the raw data contained in this object. |
| 112 | virtual PP_Var CreatePPVar(PP_Instance instance) = 0; |
| 113 | // Some PP_Vars may require 2-step initialization. For example, they may |
| 114 | // reference other PP_Vars which had not yet been created when |CreatePPVar| |
| 115 | // was called. The original var created with |CreatePPVar| is passed back in, |
| 116 | // along with the graph it is a part of to be initialized. |
| 117 | virtual void PopulatePPVar(const PP_Var& var, |
| 118 | const std::vector<PP_Var>& graph) = 0; |
| 119 | |
| 120 | // Writes the RawVarData to a message. |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 121 | virtual void Write(base::Pickle* m, const HandleWriter& handle_writer) = 0; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 122 | // Reads the RawVarData from a message. Returns true on success. |
| 123 | virtual bool Read(PP_VarType type, |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 124 | const base::Pickle* m, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 125 | base::PickleIterator* iter) = 0; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 126 | |
[email protected] | 38f428f1 | 2013-04-19 14:46:05 | [diff] [blame] | 127 | // Returns a SerializedHandle associated with this RawVarData or NULL if none |
| 128 | // exists. Ownership of the pointer remains with the RawVarData. |
| 129 | virtual SerializedHandle* GetHandle(); |
| 130 | |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 131 | bool initialized() { return initialized_; } |
| 132 | |
| 133 | protected: |
| 134 | bool initialized_; |
| 135 | }; |
| 136 | |
| 137 | // A RawVarData class for PP_Vars which are value types. |
| 138 | class BasicRawVarData : public RawVarData { |
| 139 | public: |
| 140 | BasicRawVarData(); |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 141 | ~BasicRawVarData() override; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 142 | |
| 143 | // RawVarData implementation. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 144 | PP_VarType Type() override; |
| 145 | bool Init(const PP_Var& var, PP_Instance instance) override; |
| 146 | PP_Var CreatePPVar(PP_Instance instance) override; |
| 147 | void PopulatePPVar(const PP_Var& var, |
| 148 | const std::vector<PP_Var>& graph) override; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 149 | void Write(base::Pickle* m, const HandleWriter& handle_writer) override; |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 150 | bool Read(PP_VarType type, |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 151 | const base::Pickle* m, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 152 | base::PickleIterator* iter) override; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 153 | |
| 154 | private: |
| 155 | PP_Var var_; |
| 156 | }; |
| 157 | |
| 158 | // A RawVarData class for string PP_Vars. |
| 159 | class StringRawVarData : public RawVarData { |
| 160 | public: |
| 161 | StringRawVarData(); |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 162 | ~StringRawVarData() override; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 163 | |
| 164 | // RawVarData implementation. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 165 | PP_VarType Type() override; |
| 166 | bool Init(const PP_Var& var, PP_Instance instance) override; |
| 167 | PP_Var CreatePPVar(PP_Instance instance) override; |
| 168 | void PopulatePPVar(const PP_Var& var, |
| 169 | const std::vector<PP_Var>& graph) override; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 170 | void Write(base::Pickle* m, const HandleWriter& handle_writer) override; |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 171 | bool Read(PP_VarType type, |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 172 | const base::Pickle* m, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 173 | base::PickleIterator* iter) override; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 174 | |
| 175 | private: |
| 176 | // The data in the string. |
| 177 | std::string data_; |
| 178 | }; |
| 179 | |
| 180 | // A RawVarData class for array buffer PP_Vars. |
| 181 | class ArrayBufferRawVarData : public RawVarData { |
| 182 | public: |
| 183 | // Enum for array buffer message types. |
| 184 | enum ShmemType { |
| 185 | ARRAY_BUFFER_NO_SHMEM, |
| 186 | ARRAY_BUFFER_SHMEM_HOST, |
| 187 | ARRAY_BUFFER_SHMEM_PLUGIN, |
| 188 | }; |
| 189 | |
| 190 | ArrayBufferRawVarData(); |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 191 | ~ArrayBufferRawVarData() override; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 192 | |
| 193 | // RawVarData implementation. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 194 | PP_VarType Type() override; |
| 195 | bool Init(const PP_Var& var, PP_Instance instance) override; |
| 196 | PP_Var CreatePPVar(PP_Instance instance) override; |
| 197 | void PopulatePPVar(const PP_Var& var, |
| 198 | const std::vector<PP_Var>& graph) override; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 199 | void Write(base::Pickle* m, const HandleWriter& handle_writer) override; |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 200 | bool Read(PP_VarType type, |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 201 | const base::Pickle* m, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 202 | base::PickleIterator* iter) override; |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 203 | SerializedHandle* GetHandle() override; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 204 | |
| 205 | private: |
| 206 | // The type of the storage underlying the array buffer. |
| 207 | ShmemType type_; |
| 208 | // The data in the buffer. Valid for |type_| == ARRAY_BUFFER_NO_SHMEM. |
| 209 | std::string data_; |
| 210 | // Host shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_HOST. |
| 211 | int host_shm_handle_id_; |
| 212 | // Plugin shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_PLUGIN. |
| 213 | SerializedHandle plugin_shm_handle_; |
| 214 | }; |
| 215 | |
| 216 | // A RawVarData class for array PP_Vars. |
| 217 | class ArrayRawVarData : public RawVarData { |
| 218 | public: |
| 219 | ArrayRawVarData(); |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 220 | ~ArrayRawVarData() override; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 221 | |
| 222 | void AddChild(size_t element); |
| 223 | |
| 224 | // RawVarData implementation. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 225 | PP_VarType Type() override; |
| 226 | bool Init(const PP_Var& var, PP_Instance instance) override; |
| 227 | PP_Var CreatePPVar(PP_Instance instance) override; |
| 228 | void PopulatePPVar(const PP_Var& var, |
| 229 | const std::vector<PP_Var>& graph) override; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 230 | void Write(base::Pickle* m, const HandleWriter& handle_writer) override; |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 231 | bool Read(PP_VarType type, |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 232 | const base::Pickle* m, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 233 | base::PickleIterator* iter) override; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 234 | |
| 235 | private: |
| 236 | std::vector<size_t> children_; |
| 237 | }; |
| 238 | |
| 239 | // A RawVarData class for dictionary PP_Vars. |
| 240 | class DictionaryRawVarData : public RawVarData { |
| 241 | public: |
| 242 | DictionaryRawVarData(); |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 243 | ~DictionaryRawVarData() override; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 244 | |
| 245 | void AddChild(const std::string& key, size_t value); |
| 246 | |
| 247 | // RawVarData implementation. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 248 | PP_VarType Type() override; |
| 249 | bool Init(const PP_Var& var, PP_Instance instance) override; |
| 250 | PP_Var CreatePPVar(PP_Instance instance) override; |
| 251 | void PopulatePPVar(const PP_Var& var, |
| 252 | const std::vector<PP_Var>& graph) override; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 253 | void Write(base::Pickle* m, const HandleWriter& handle_writer) override; |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 254 | bool Read(PP_VarType type, |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 255 | const base::Pickle* m, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 256 | base::PickleIterator* iter) override; |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 257 | |
| 258 | private: |
| 259 | std::vector<std::pair<std::string, size_t> > children_; |
| 260 | }; |
| 261 | |
[email protected] | 0919868 | 2013-09-24 10:11:29 | [diff] [blame] | 262 | // A RawVarData class for resource PP_Vars. |
| 263 | // This class does not hold a reference on the PP_Resource that is being |
| 264 | // serialized. If sending a resource from the plugin to the host, the plugin |
| 265 | // should not release the ResourceVar before sending the serialized message to |
| 266 | // the host, and the host should immediately consume the ResourceVar before |
| 267 | // processing further messages. |
| 268 | class ResourceRawVarData : public RawVarData { |
| 269 | public: |
| 270 | ResourceRawVarData(); |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 271 | ~ResourceRawVarData() override; |
[email protected] | 0919868 | 2013-09-24 10:11:29 | [diff] [blame] | 272 | |
| 273 | // RawVarData implementation. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 274 | PP_VarType Type() override; |
| 275 | bool Init(const PP_Var& var, PP_Instance instance) override; |
| 276 | PP_Var CreatePPVar(PP_Instance instance) override; |
| 277 | void PopulatePPVar(const PP_Var& var, |
| 278 | const std::vector<PP_Var>& graph) override; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 279 | void Write(base::Pickle* m, const HandleWriter& handle_writer) override; |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 280 | bool Read(PP_VarType type, |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 281 | const base::Pickle* m, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 282 | base::PickleIterator* iter) override; |
[email protected] | 0919868 | 2013-09-24 10:11:29 | [diff] [blame] | 283 | |
| 284 | private: |
| 285 | // Resource ID in the plugin. If one has not yet been created, this is 0. |
| 286 | // This is a borrowed reference; the resource's refcount is not incremented. |
| 287 | PP_Resource pp_resource_; |
| 288 | |
[email protected] | 29b8f23 | 2013-09-26 07:38:33 | [diff] [blame] | 289 | // Pending resource host ID in the renderer. |
| 290 | int pending_renderer_host_id_; |
| 291 | |
| 292 | // Pending resource host ID in the browser. |
| 293 | int pending_browser_host_id_; |
| 294 | |
[email protected] | 0919868 | 2013-09-24 10:11:29 | [diff] [blame] | 295 | // A message containing information about how to create a plugin-side |
| 296 | // resource. The message type will vary based on the resource type, and will |
| 297 | // usually contain a pending resource host ID, and other required information. |
| 298 | // If the resource was created directly, this is NULL. |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 299 | std::unique_ptr<IPC::Message> creation_message_; |
[email protected] | 0919868 | 2013-09-24 10:11:29 | [diff] [blame] | 300 | }; |
| 301 | |
[email protected] | 42a3748 | 2013-04-17 17:51:06 | [diff] [blame] | 302 | } // namespace proxy |
| 303 | } // namespace ppapi |
| 304 | |
| 305 | #endif // PPAPI_PROXY_RAW_VAR_DATA_H_ |