Home | History | Annotate | Download | only in proxy
      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 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);
     65 
     66   // Write the graph to a message using the given HandleWriter.
     67   void Write(IPC::Message* m, const HandleWriter& handle_writer);
     68 
     69   // Create a RawVarDataGraph from the given message.
     70   static scoped_ptr<RawVarDataGraph> Read(const IPC::Message* m,
     71                                           PickleIterator* iter);
     72 
     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();
     76 
     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);
     82 
     83   // A list of the nodes in the graph.
     84   ScopedVector<RawVarData> data_;
     85 };
     86 
     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();
     94 
     95   // Returns the type of the PP_Var represented by the RawVarData.
     96   virtual PP_VarType Type() = 0;
     97 
     98   // Initializes a RawVarData from a PP_Var. Returns true on success.
     99   virtual bool Init(const PP_Var& var, PP_Instance instance) = 0;
    100 
    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;
    109 
    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;
    117 
    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();
    121 
    122   bool initialized() { return initialized_; }
    123 
    124  protected:
    125   bool initialized_;
    126 };
    127 
    128 // A RawVarData class for PP_Vars which are value types.
    129 class BasicRawVarData : public RawVarData {
    130  public:
    131   BasicRawVarData();
    132   virtual ~BasicRawVarData();
    133 
    134   // RawVarData implementation.
    135   virtual PP_VarType Type() OVERRIDE;
    136   virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE;
    137   virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE;
    138   virtual void PopulatePPVar(const PP_Var& var,
    139                              const std::vector<PP_Var>& graph) OVERRIDE;
    140   virtual void Write(IPC::Message* m,
    141                      const HandleWriter& handle_writer) OVERRIDE;
    142   virtual bool Read(PP_VarType type,
    143                     const IPC::Message* m,
    144                     PickleIterator* iter) OVERRIDE;
    145 
    146  private:
    147   PP_Var var_;
    148 };
    149 
    150 // A RawVarData class for string PP_Vars.
    151 class StringRawVarData : public RawVarData {
    152  public:
    153   StringRawVarData();
    154   virtual ~StringRawVarData();
    155 
    156   // RawVarData implementation.
    157   virtual PP_VarType Type() OVERRIDE;
    158   virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE;
    159   virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE;
    160   virtual void PopulatePPVar(const PP_Var& var,
    161                              const std::vector<PP_Var>& graph) OVERRIDE;
    162   virtual void Write(IPC::Message* m,
    163                      const HandleWriter& handle_writer) OVERRIDE;
    164   virtual bool Read(PP_VarType type,
    165                     const IPC::Message* m,
    166                     PickleIterator* iter) OVERRIDE;
    167 
    168  private:
    169   // The data in the string.
    170   std::string data_;
    171 };
    172 
    173 // A RawVarData class for array buffer PP_Vars.
    174 class ArrayBufferRawVarData : public RawVarData {
    175  public:
    176   // Enum for array buffer message types.
    177   enum ShmemType {
    178     ARRAY_BUFFER_NO_SHMEM,
    179     ARRAY_BUFFER_SHMEM_HOST,
    180     ARRAY_BUFFER_SHMEM_PLUGIN,
    181   };
    182 
    183   ArrayBufferRawVarData();
    184   virtual ~ArrayBufferRawVarData();
    185 
    186   // RawVarData implementation.
    187   virtual PP_VarType Type() OVERRIDE;
    188   virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE;
    189   virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE;
    190   virtual void PopulatePPVar(const PP_Var& var,
    191                              const std::vector<PP_Var>& graph) OVERRIDE;
    192   virtual void Write(IPC::Message* m,
    193                      const HandleWriter& handle_writer) OVERRIDE;
    194   virtual bool Read(PP_VarType type,
    195                     const IPC::Message* m,
    196                     PickleIterator* iter) OVERRIDE;
    197   virtual SerializedHandle* GetHandle() OVERRIDE;
    198 
    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_;
    208 };
    209 
    210 // A RawVarData class for array PP_Vars.
    211 class ArrayRawVarData : public RawVarData {
    212  public:
    213   ArrayRawVarData();
    214   virtual ~ArrayRawVarData();
    215 
    216   void AddChild(size_t element);
    217 
    218   // RawVarData implementation.
    219   virtual PP_VarType Type() OVERRIDE;
    220   virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE;
    221   virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE;
    222   virtual void PopulatePPVar(const PP_Var& var,
    223                              const std::vector<PP_Var>& graph) OVERRIDE;
    224   virtual void Write(IPC::Message* m,
    225                      const HandleWriter& handle_writer) OVERRIDE;
    226   virtual bool Read(PP_VarType type,
    227                     const IPC::Message* m,
    228                     PickleIterator* iter) OVERRIDE;
    229 
    230  private:
    231   std::vector<size_t> children_;
    232 };
    233 
    234 // A RawVarData class for dictionary PP_Vars.
    235 class DictionaryRawVarData : public RawVarData {
    236  public:
    237   DictionaryRawVarData();
    238   virtual ~DictionaryRawVarData();
    239 
    240   void AddChild(const std::string& key, size_t value);
    241 
    242   // RawVarData implementation.
    243   virtual PP_VarType Type() OVERRIDE;
    244   virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE;
    245   virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE;
    246   virtual void PopulatePPVar(const PP_Var& var,
    247                              const std::vector<PP_Var>& graph) OVERRIDE;
    248   virtual void Write(IPC::Message* m,
    249                      const HandleWriter& handle_writer) OVERRIDE;
    250   virtual bool Read(PP_VarType type,
    251                     const IPC::Message* m,
    252                     PickleIterator* iter) OVERRIDE;
    253 
    254  private:
    255   std::vector<std::pair<std::string, size_t> > children_;
    256 };
    257 
    258 // A RawVarData class for resource PP_Vars.
    259 // This class does not hold a reference on the PP_Resource that is being
    260 // serialized. If sending a resource from the plugin to the host, the plugin
    261 // should not release the ResourceVar before sending the serialized message to
    262 // the host, and the host should immediately consume the ResourceVar before
    263 // processing further messages.
    264 class ResourceRawVarData : public RawVarData {
    265  public:
    266   ResourceRawVarData();
    267   virtual ~ResourceRawVarData();
    268 
    269   // RawVarData implementation.
    270   virtual PP_VarType Type() OVERRIDE;
    271   virtual bool Init(const PP_Var& var, PP_Instance instance) OVERRIDE;
    272   virtual PP_Var CreatePPVar(PP_Instance instance) OVERRIDE;
    273   virtual void PopulatePPVar(const PP_Var& var,
    274                              const std::vector<PP_Var>& graph) OVERRIDE;
    275   virtual void Write(IPC::Message* m,
    276                      const HandleWriter& handle_writer) OVERRIDE;
    277   virtual bool Read(PP_VarType type,
    278                     const IPC::Message* m,
    279                     PickleIterator* iter) OVERRIDE;
    280 
    281  private:
    282   // Resource ID in the plugin. If one has not yet been created, this is 0.
    283   // This is a borrowed reference; the resource's refcount is not incremented.
    284   PP_Resource pp_resource_;
    285 
    286   // Pending resource host ID in the renderer.
    287   int pending_renderer_host_id_;
    288 
    289   // Pending resource host ID in the browser.
    290   int pending_browser_host_id_;
    291 
    292   // A message containing information about how to create a plugin-side
    293   // resource. The message type will vary based on the resource type, and will
    294   // usually contain a pending resource host ID, and other required information.
    295   // If the resource was created directly, this is NULL.
    296   scoped_ptr<IPC::Message> creation_message_;
    297 };
    298 
    299 }  // namespace proxy
    300 }  // namespace ppapi
    301 
    302 #endif  // PPAPI_PROXY_RAW_VAR_DATA_H_
    303