array_writer.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 18namespace ppapi { 19 20class Resource; 21 22// Holds a PP_ArrayWriter and provides helper functions for writing arrays 23// to it. It also handles 0-initialization of the raw C struct and attempts 24// to prevent you from writing the array twice. 25class PPAPI_SHARED_EXPORT ArrayWriter { 26 public: 27 ArrayWriter(); // Creates an is_null() object 28 ArrayWriter(const PP_ArrayOutput& output); 29 ~ArrayWriter(); 30 31 bool is_valid() const { return !!pp_array_output_.GetDataBuffer; } 32 bool is_null() const { return !is_valid(); } 33 34 void set_pp_array_output(const PP_ArrayOutput& output) { 35 pp_array_output_ = output; 36 } 37 38 // Sets the array output back to its is_null() state. 39 void Reset(); 40 41 // Copies the given vector of data to the plugin output array. 42 // 43 // Returns true on success, false if the plugin reported allocation failure. 44 // In either case, the object will become is_null() immediately after the 45 // call since one output function should only be issued once. 46 // 47 // THIS IS DESIGNED FOR POD ONLY. For the case of resources, for example, we 48 // want to transfer a reference only on success. Likewise, if you have a 49 // structure of PP_Vars or a struct that contains a PP_Resource, we need to 50 // make sure that the right thing happens with the ref on success and failure. 51 template<typename T> 52 bool StoreVector(const std::vector<T>& input) { 53 // Always call the alloc function, even on 0 array size. 54 void* dest = pp_array_output_.GetDataBuffer( 55 pp_array_output_.user_data, 56 static_cast<uint32_t>(input.size()), 57 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 (input.empty()) 64 return true; // Allow plugin to return NULL on 0 elements. 65 if (!dest) 66 return false; 67 68 memcpy(dest, &input[0], sizeof(T) * input.size()); 69 return true; 70 } 71 72 // Stores the given vector of resources as PP_Resources to the output vector, 73 // adding one reference to each. 74 // 75 // On failure this returns false, nothing will be copied, and the resource 76 // refcounts will be unchanged. In either case, the object will become 77 // is_null() immediately after the call since one output function should only 78 // be issued once. 79 // 80 // Note: potentially this could be a template in case you have a vector of 81 // FileRef objects, for example. However, this saves code since there's only 82 // one instantiation and is sufficient for now. 83 bool StoreResourceVector( 84 const std::vector< scoped_refptr<Resource> >& input); 85 86 // Like the above version but takes an array of AddRed'ed PP_Resources. On 87 // storage failure, this will release each resource. 88 bool StoreResourceVector(const std::vector<PP_Resource>& input); 89 90 private: 91 PP_ArrayOutput pp_array_output_; 92 93 DISALLOW_COPY_AND_ASSIGN(ArrayWriter); 94}; 95 96} // namespace ppapi 97 98#endif // PPAPI_SHARED_IMPL_ARRAY_WRITER_H_ 99