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, 58 count, 59 sizeof(T)); 60 61 // Regardless of success, we clear the output to prevent future calls on 62 // this same output object. 63 Reset(); 64 65 if (count == 0) 66 return true; // Allow plugin to return NULL on 0 elements. 67 if (!dest) 68 return false; 69 70 if (input) 71 memcpy(dest, input, sizeof(T) * count); 72 return true; 73 } 74 75 // Copies the given array/vector of data to the plugin output array. See 76 // comment of StoreArray() for detail. 77 template<typename T> 78 bool StoreVector(const std::vector<T>& input) { 79 return StoreArray(input.size() ? &input[0] : NULL, input.size()); 80 } 81 82 // Stores the given vector of resources as PP_Resources to the output vector, 83 // adding one reference to each. 84 // 85 // On failure this returns false, nothing will be copied, and the resource 86 // refcounts will be unchanged. In either case, the object will become 87 // is_null() immediately after the call since one output function should only 88 // be issued once. 89 // 90 // Note: potentially this could be a template in case you have a vector of 91 // FileRef objects, for example. However, this saves code since there's only 92 // one instantiation and is sufficient for now. 93 bool StoreResourceVector(const std::vector< scoped_refptr<Resource> >& input); 94 95 // Like the above version but takes an array of AddRef'ed PP_Resources. On 96 // storage failure, this will release each resource. 97 bool StoreResourceVector(const std::vector<PP_Resource>& input); 98 99 // Stores the given vector of vars as PP_Vars to the output vector, 100 // adding one reference to each. 101 // 102 // On failure this returns false, nothing will be copied, and the var 103 // refcounts will be unchanged. In either case, the object will become 104 // is_null() immediately after the call since one output function should only 105 // be issued once. 106 bool StoreVarVector(const std::vector< scoped_refptr<Var> >& input); 107 108 // Like the above version but takes an array of AddRef'ed PP_Vars. On 109 // storage failure, this will release each var. 110 bool StoreVarVector(const std::vector<PP_Var>& input); 111 112 private: 113 PP_ArrayOutput pp_array_output_; 114 115 DISALLOW_COPY_AND_ASSIGN(ArrayWriter); 116 }; 117 118 } // namespace ppapi 119 120 #endif // PPAPI_SHARED_IMPL_ARRAY_WRITER_H_ 121