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/ppapi_proxy_export.h" |
| 17 #include "ppapi/proxy/serialized_handle.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. 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 // | .... | |
| 46 // |
| 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); |
| 55 |
| 56 // Constructs an empty RawVarDataGraph. |
| 57 RawVarDataGraph(); |
| 58 ~RawVarDataGraph(); |
| 59 |
| 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 of at least one, plus the number of references to it in the |
| 63 // graph. |
| 64 PP_Var CreatePPVar(PP_Instance instance); |
| 65 |
| 66 // Write the graph to a message using the given HandleWriter. |
| 67 void Write(IPC::Message* m, const HandleWriter& handle_writer); |
| 68 // Write the graph to a message using the default handle writer. |
| 69 void Write(IPC::Message* m); |
| 70 |
| 71 // Create a RawVarDataGraph from the given message. |
| 72 static scoped_ptr<RawVarDataGraph> Read(const IPC::Message* m, |
| 73 PickleIterator* iter); |
| 74 |
| 75 // A list of the nodes in the graph. |
| 76 ScopedVector<RawVarData> data_; |
| 77 }; |
| 78 |
| 79 // Abstract base class for the data contained in a PP_Var. |
| 80 class RawVarData { |
| 81 public: |
| 82 // Create a new, empty RawVarData for the given type. |
| 83 static RawVarData* Create(PP_VarType type); |
| 84 RawVarData(); |
| 85 virtual ~RawVarData(); |
| 86 |
| 87 // Returns the type of the PP_Var represented by the RawVarData. |
| 88 virtual PP_VarType Type() = 0; |
| 89 |
| 90 // Initializes a RawVarData from a PP_Var. Returns true on success. |
| 91 virtual bool Init(const PP_Var& var, PP_Instance instance) = 0; |
| 92 |
| 93 // Create a PP_Var from the raw data contained in this object. |
| 94 virtual PP_Var CreatePPVar(PP_Instance instance) = 0; |
| 95 // Some PP_Vars may require 2-step initialization. For example, they may |
| 96 // reference other PP_Vars which had not yet been created when |CreatePPVar| |
| 97 // was called. The original var created with |CreatePPVar| is passed back in, |
| 98 // along with the graph it is a part of to be initialized. |
| 99 virtual void PopulatePPVar(const PP_Var& var, |
| 100 const std::vector<PP_Var>& graph) = 0; |
| 101 |
| 102 // Writes the RawVarData to a message. |
| 103 virtual void Write(IPC::Message* m, |
| 104 const HandleWriter& handle_writer) = 0; |
| 105 // Reads the RawVarData from a message. Returns true on success. |
| 106 virtual bool Read(PP_VarType type, |
| 107 const IPC::Message* m, |
| 108 PickleIterator* iter) = 0; |
| 109 |
| 110 bool initialized() { return initialized_; } |
| 111 |
| 112 protected: |
| 113 bool initialized_; |
| 114 }; |
| 115 |
| 116 // A RawVarData class for PP_Vars which are value types. |
| 117 class BasicRawVarData : public RawVarData { |
| 118 public: |
| 119 BasicRawVarData(); |
| 120 virtual ~BasicRawVarData(); |
| 121 |
| 122 // RawVarData implementation. |
| 123 virtual PP_VarType Type() OVERRIDE; |
| 124 virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE; |
| 125 virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE; |
| 126 virtual void PopulatePPVar(const PP_Var& var, |
| 127 const std::vector<PP_Var>& graph) OVERRIDE; |
| 128 virtual void Write(IPC::Message* m, |
| 129 const HandleWriter& handle_writer) OVERRIDE; |
| 130 virtual bool Read(PP_VarType type, |
| 131 const IPC::Message* m, |
| 132 PickleIterator* iter) OVERRIDE; |
| 133 |
| 134 private: |
| 135 PP_Var var_; |
| 136 }; |
| 137 |
| 138 // A RawVarData class for string PP_Vars. |
| 139 class StringRawVarData : public RawVarData { |
| 140 public: |
| 141 StringRawVarData(); |
| 142 virtual ~StringRawVarData(); |
| 143 |
| 144 // RawVarData implementation. |
| 145 virtual PP_VarType Type() OVERRIDE; |
| 146 virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE; |
| 147 virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE; |
| 148 virtual void PopulatePPVar(const PP_Var& var, |
| 149 const std::vector<PP_Var>& graph) OVERRIDE; |
| 150 virtual void Write(IPC::Message* m, |
| 151 const HandleWriter& handle_writer) OVERRIDE; |
| 152 virtual bool Read(PP_VarType type, |
| 153 const IPC::Message* m, |
| 154 PickleIterator* iter) OVERRIDE; |
| 155 |
| 156 private: |
| 157 // The data in the string. |
| 158 std::string data_; |
| 159 }; |
| 160 |
| 161 // A RawVarData class for array buffer PP_Vars. |
| 162 class ArrayBufferRawVarData : public RawVarData { |
| 163 public: |
| 164 // Enum for array buffer message types. |
| 165 enum ShmemType { |
| 166 ARRAY_BUFFER_NO_SHMEM, |
| 167 ARRAY_BUFFER_SHMEM_HOST, |
| 168 ARRAY_BUFFER_SHMEM_PLUGIN, |
| 169 }; |
| 170 |
| 171 ArrayBufferRawVarData(); |
| 172 virtual ~ArrayBufferRawVarData(); |
| 173 |
| 174 // RawVarData implementation. |
| 175 virtual PP_VarType Type() OVERRIDE; |
| 176 virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE; |
| 177 virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE; |
| 178 virtual void PopulatePPVar(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 virtual bool Read(PP_VarType type, |
| 183 const IPC::Message* m, |
| 184 PickleIterator* iter) OVERRIDE; |
| 185 |
| 186 private: |
| 187 // The type of the storage underlying the array buffer. |
| 188 ShmemType type_; |
| 189 // The data in the buffer. Valid for |type_| == ARRAY_BUFFER_NO_SHMEM. |
| 190 std::string data_; |
| 191 // Host shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_HOST. |
| 192 int host_shm_handle_id_; |
| 193 // Plugin shmem handle. Valid for |type_| == ARRAY_BUFFER_SHMEM_PLUGIN. |
| 194 SerializedHandle plugin_shm_handle_; |
| 195 }; |
| 196 |
| 197 // A RawVarData class for array PP_Vars. |
| 198 class ArrayRawVarData : public RawVarData { |
| 199 public: |
| 200 ArrayRawVarData(); |
| 201 virtual ~ArrayRawVarData(); |
| 202 |
| 203 void AddChild(size_t element); |
| 204 |
| 205 // RawVarData implementation. |
| 206 virtual PP_VarType Type() OVERRIDE; |
| 207 virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE; |
| 208 virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE; |
| 209 virtual void PopulatePPVar(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 virtual bool Read(PP_VarType type, |
| 214 const IPC::Message* m, |
| 215 PickleIterator* iter) OVERRIDE; |
| 216 |
| 217 private: |
| 218 std::vector<size_t> children_; |
| 219 }; |
| 220 |
| 221 // A RawVarData class for dictionary PP_Vars. |
| 222 class DictionaryRawVarData : public RawVarData { |
| 223 public: |
| 224 DictionaryRawVarData(); |
| 225 virtual ~DictionaryRawVarData(); |
| 226 |
| 227 void AddChild(const std::string& key, size_t value); |
| 228 |
| 229 // RawVarData implementation. |
| 230 virtual PP_VarType Type() OVERRIDE; |
| 231 virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE; |
| 232 virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE; |
| 233 virtual void PopulatePPVar(const PP_Var& var, |
| 234 const std::vector<PP_Var>& graph) OVERRIDE; |
| 235 virtual void Write(IPC::Message* m, |
| 236 const HandleWriter& handle_writer) OVERRIDE; |
| 237 virtual bool Read(PP_VarType type, |
| 238 const IPC::Message* m, |
| 239 PickleIterator* iter) OVERRIDE; |
| 240 |
| 241 private: |
| 242 std::vector<std::pair<std::string, size_t> > children_; |
| 243 }; |
| 244 |
| 245 } // namespace proxy |
| 246 } // namespace ppapi |
| 247 |
| 248 #endif // PPAPI_PROXY_RAW_VAR_DATA_H_ |
OLD | NEW |