Home | History | Annotate | Download | only in shared_impl
      1 // Copyright (c) 2012 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_SHARED_IMPL_ARRAY_WRITER_H_
      6 #define PPAPI_SHARED_IMPL_ARRAY_WRITER_H_
      7 
      8 #include <string.h>
      9 
     10 #include <vector>
     11 
     12 #include "base/memory/ref_counted.h"
     13 #include "ppapi/c/pp_array_output.h"
     14 #include "ppapi/c/pp_resource.h"
     15 #include "ppapi/c/pp_var.h"
     16 #include "ppapi/shared_impl/ppapi_shared_export.h"
     17 
     18 namespace ppapi {
     19 
     20 class Resource;
     21 class Var;
     22 
     23 // Holds a PP_ArrayWriter and provides helper functions for writing arrays
     24 // to it. It also handles 0-initialization of the raw C struct and attempts
     25 // to prevent you from writing the array twice.
     26 class PPAPI_SHARED_EXPORT ArrayWriter {
     27  public:
     28   ArrayWriter();  // Creates an is_null() object
     29   ArrayWriter(const PP_ArrayOutput& output);
     30   ~ArrayWriter();
     31 
     32   bool is_valid() const { return !!pp_array_output_.GetDataBuffer; }
     33   bool is_null() const { return !is_valid(); }
     34 
     35   void set_pp_array_output(const PP_ArrayOutput& output) {
     36     pp_array_output_ = output;
     37   }
     38 
     39   // Sets the array output back to its is_null() state.
     40   void Reset();
     41 
     42   // StoreArray() and StoreVector() copy the given array/vector of data to the
     43   // plugin output array.
     44   //
     45   // Returns true on success, false if the plugin reported allocation failure.
     46   // In either case, the object will become is_null() immediately after the
     47   // call since one output function should only be issued once.
     48   //
     49   // THIS IS DESIGNED FOR POD ONLY. For the case of resources, for example, we
     50   // want to transfer a reference only on success. Likewise, if you have a
     51   // structure of PP_Vars or a struct that contains a PP_Resource, we need to
     52   // make sure that the right thing happens with the ref on success and failure.
     53   template <typename T>
     54   bool StoreArray(const T* input, uint32_t count) {
     55     // Always call the alloc function, even on 0 array size.
     56     void* dest = pp_array_output_.GetDataBuffer(
     57         pp_array_output_.user_data, count, sizeof(T));
     58 
     59     // Regardless of success, we clear the output to prevent future calls on
     60     // this same output object.
     61     Reset();
     62 
     63     if (count == 0)
     64       return true;  // Allow plugin to return NULL on 0 elements.
     65     if (!dest)
     66       return false;
     67 
     68     if (input)
     69       memcpy(dest, input, sizeof(T) * count);
     70     return true;
     71   }
     72 
     73   // Copies the given array/vector of data to the plugin output array.  See
     74   // comment of StoreArray() for detail.
     75   template <typename T>
     76   bool StoreVector(const std::vector<T>& input) {
     77     return StoreArray(input.size() ? &input[0] : NULL, input.size());
     78   }
     79 
     80   // Stores the given vector of resources as PP_Resources to the output vector,
     81   // adding one reference to each.
     82   //
     83   // On failure this returns false, nothing will be copied, and the resource
     84   // refcounts will be unchanged. In either case, the object will become
     85   // is_null() immediately after the call since one output function should only
     86   // be issued once.
     87   //
     88   // Note: potentially this could be a template in case you have a vector of
     89   // FileRef objects, for example. However, this saves code since there's only
     90   // one instantiation and is sufficient for now.
     91   bool StoreResourceVector(const std::vector<scoped_refptr<Resource> >& input);
     92 
     93   // Like the above version but takes an array of AddRef'ed PP_Resources. On
     94   // storage failure, this will release each resource.
     95   bool StoreResourceVector(const std::vector<PP_Resource>& input);
     96 
     97   // Stores the given vector of vars as PP_Vars to the output vector,
     98   // adding one reference to each.
     99   //
    100   // On failure this returns false, nothing will be copied, and the var
    101   // refcounts will be unchanged. In either case, the object will become
    102   // is_null() immediately after the call since one output function should only
    103   // be issued once.
    104   bool StoreVarVector(const std::vector<scoped_refptr<Var> >& input);
    105 
    106   // Like the above version but takes an array of AddRef'ed PP_Vars. On
    107   // storage failure, this will release each var.
    108   bool StoreVarVector(const std::vector<PP_Var>& input);
    109 
    110  private:
    111   PP_ArrayOutput pp_array_output_;
    112 
    113   DISALLOW_COPY_AND_ASSIGN(ArrayWriter);
    114 };
    115 
    116 }  // namespace ppapi
    117 
    118 #endif  // PPAPI_SHARED_IMPL_ARRAY_WRITER_H_
    119