OLD | NEW |
(Empty) | |
| 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_CPP_ARRAY_OUTPUT_H_ |
| 6 #define PPAPI_CPP_ARRAY_OUTPUT_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "ppapi/c/pp_array_output.h" |
| 11 #include "ppapi/c/pp_resource.h" |
| 12 #include "ppapi/cpp/logging.h" |
| 13 #include "ppapi/cpp/pass_ref.h" |
| 14 #include "ppapi/cpp/var.h" |
| 15 |
| 16 namespace pp { |
| 17 |
| 18 // Converts the given array of PP_Resources into an array of the requested |
| 19 // C++ resource types, passing ownership of a reference in the process. |
| 20 // |
| 21 // This is used to convert output arrays of resources that the browser has |
| 22 // generated into the more convenient C++ wrappers for those resources. The |
| 23 // initial "PassRef" parameter is there to emphasize what happens to the |
| 24 // reference count of the input resource and to match the resource constructors |
| 25 // that look the same. |
| 26 template<typename ResourceObjectType> |
| 27 inline void ConvertPPResourceArrayToObjects( |
| 28 PassRef, |
| 29 const std::vector<PP_Resource>& input, |
| 30 std::vector<ResourceObjectType>* output) { |
| 31 output->resize(0); |
| 32 output->reserve(input.size()); |
| 33 for (size_t i = 0; i < input.size(); i++) |
| 34 output->push_back(ResourceObjectType(PASS_REF, input[i])); |
| 35 } |
| 36 |
| 37 // Non-templatized base class for the array output conversion. It provides the |
| 38 // C implementation of a PP_ArrayOutput whose callback function is implemented |
| 39 // as a virtual call on a derived class. Do not use directly, use one of the |
| 40 // derived classes below. |
| 41 class ArrayOutputAdapterBase { |
| 42 public: |
| 43 ArrayOutputAdapterBase() { |
| 44 pp_array_output_.GetDataBuffer = |
| 45 &ArrayOutputAdapterBase::GetDataBufferThunk; |
| 46 pp_array_output_.user_data = this; |
| 47 } |
| 48 virtual ~ArrayOutputAdapterBase() {} |
| 49 |
| 50 PP_ArrayOutput* pp_array_output() { return &pp_array_output_; } |
| 51 |
| 52 protected: |
| 53 virtual void* GetDataBuffer(uint32_t element_count, |
| 54 uint32_t element_size) = 0; |
| 55 |
| 56 private: |
| 57 static void* GetDataBufferThunk(void* user_data, |
| 58 uint32_t element_count, |
| 59 uint32_t element_size); |
| 60 |
| 61 PP_ArrayOutput pp_array_output_; |
| 62 |
| 63 // Disallow copying and assignment. This will do the wrong thing for most |
| 64 // subclasses. |
| 65 ArrayOutputAdapterBase(const ArrayOutputAdapterBase&); |
| 66 ArrayOutputAdapterBase& operator=(const ArrayOutputAdapterBase&); |
| 67 }; |
| 68 |
| 69 // This adapter provides functionality for implementing a PP_ArrayOutput |
| 70 // structure as writing to a given vector object. |
| 71 // |
| 72 // This is generally used internally in the C++ wrapper objects to |
| 73 // write into an output parameter supplied by the plugin. If the element size |
| 74 // that the browser is writing does not match the size of the type we're using |
| 75 // this will assert and return NULL (which will cause the browser to fail the |
| 76 // call). |
| 77 // |
| 78 // Example that allows the browser to write into a given vector: |
| 79 // void DoFoo(std::vector<int>* results) { |
| 80 // ArrayOutputAdapter<int> adapter(results); |
| 81 // ppb_foo->DoFoo(adapter.pp_array_output()); |
| 82 // } |
| 83 template<typename T> |
| 84 class ArrayOutputAdapter : public ArrayOutputAdapterBase { |
| 85 public: |
| 86 ArrayOutputAdapter(std::vector<T>* output) : output_(output) {} |
| 87 |
| 88 protected: |
| 89 // Two-step init for the "with storage" version below. |
| 90 ArrayOutputAdapter() : output_(NULL) {} |
| 91 void set_output(std::vector<T>* output) { output_ = output; } |
| 92 |
| 93 // ArrayOutputAdapterBase implementation. |
| 94 virtual void* GetDataBuffer(uint32_t element_count, uint32_t element_size) { |
| 95 PP_DCHECK(element_size == sizeof(T)); |
| 96 if (element_size != sizeof(T)) |
| 97 return NULL; |
| 98 output_->resize(element_count); |
| 99 return &(*output_)[0]; |
| 100 } |
| 101 |
| 102 private: |
| 103 std::vector<T>* output_; |
| 104 }; |
| 105 |
| 106 // This adapter provides functionality for implementing a PP_ArrayOutput |
| 107 // structure as writing resources to a given vector object. |
| 108 // |
| 109 // When returning an array of resources, the browser will write PP_Resources |
| 110 // via a PP_ArrayOutput. This code will automatically convert the PP_Resources |
| 111 // to the given wrapper type, (as long as that wrapper type supports the |
| 112 // correct constructor). The ownership of the resources that the browser passed |
| 113 // to us will be transferred to the C++ wrapper object. |
| 114 // |
| 115 // Conversion of the PP_Resources to the C++ wrapper object occurs in the |
| 116 // destructor. This object is intended to be used on the stack in a C++ wrapper |
| 117 // object for a call. |
| 118 // |
| 119 // Example: |
| 120 // void GetFiles(std::vector<pp::FileRef>* results) { |
| 121 // ResourceArrayOutputAdapter<pp::FileRef> adapter(results); |
| 122 // ppb_foo->DoFoo(adapter.pp_array_output()); |
| 123 // } |
| 124 template<typename T> |
| 125 class ResourceArrayOutputAdapter : public ArrayOutputAdapterBase { |
| 126 public: |
| 127 explicit ResourceArrayOutputAdapter(std::vector<T>* output) |
| 128 : output_(output) { |
| 129 output_->resize(0); |
| 130 } |
| 131 virtual ~ResourceArrayOutputAdapter() { |
| 132 ConvertPPResourceArrayToObjects(PASS_REF, intermediate_output_, output_); |
| 133 } |
| 134 |
| 135 protected: |
| 136 // Two-step init for the "with storage" version below. |
| 137 ResourceArrayOutputAdapter() : output_(NULL) {} |
| 138 void set_output(T* output) { output_ = output; } |
| 139 |
| 140 // ArrayOutputAdapterBase implementation. |
| 141 virtual void* GetDataBuffer(uint32_t element_count, |
| 142 uint32_t element_size) { |
| 143 PP_DCHECK(element_size == sizeof(PP_Resource)); |
| 144 if (element_size != sizeof(PP_Resource)) |
| 145 return NULL; |
| 146 intermediate_output_.resize(element_count); |
| 147 return &intermediate_output_[0]; |
| 148 } |
| 149 |
| 150 private: |
| 151 std::vector<PP_Resource> intermediate_output_; |
| 152 std::vector<T>* output_; |
| 153 }; |
| 154 |
| 155 // This adapter is like the ArrayOutputAdapter except that it also contains |
| 156 // the underlying std::vector that will be populated (rather than writing it to |
| 157 // an object passed into the constructor). |
| 158 // |
| 159 // This is used by the CompletionCallbackFactory system to collect the output |
| 160 // parameters from an async function call. The collected data is then passed to |
| 161 // the plugins callback function. |
| 162 // |
| 163 // You can also use it directly if you want to have an array output and aren't |
| 164 // using the CompletionCallbackFactory. For example, if you're calling a |
| 165 // PPAPI function DoFoo that takes a PP_OutputArray that is supposed to be |
| 166 // writing integers, do this: |
| 167 // |
| 168 // ArrayOutputAdapterWithStorage<int> adapter; |
| 169 // ppb_foo->DoFoo(adapter.pp_output_array()); |
| 170 // const std::vector<int>& result = adapter.output(); |
| 171 template<typename T> |
| 172 class ArrayOutputAdapterWithStorage : public ArrayOutputAdapter<T> { |
| 173 public: |
| 174 ArrayOutputAdapterWithStorage() { |
| 175 set_output(&output_storage_); |
| 176 } |
| 177 |
| 178 std::vector<T>& output() { return output_storage_; } |
| 179 |
| 180 private: |
| 181 std::vector<T> output_storage_; |
| 182 }; |
| 183 |
| 184 // This adapter is like the ArrayOutputAdapterWithStorage except this |
| 185 // additionally converts PP_Var structs to pp::Var objects. |
| 186 // |
| 187 // You can also use it directly if you want to have an array output and aren't |
| 188 // using the CompletionCallbackFactory. For example, if you're calling a |
| 189 // PPAPI function GetVars that takes a PP_OutputArray that is supposed to be |
| 190 // writing PP_Vars, do this: |
| 191 // |
| 192 // VarArrayOutputAdapterWithStorage adapter; |
| 193 // ppb_foo->GetVars(adapter.pp_output_array()); |
| 194 // const std::vector<pp::Var>& result = adapter.output(). |
| 195 // |
| 196 // This one is non-inline since it's not templatized. |
| 197 class VarArrayOutputAdapterWithStorage : public ArrayOutputAdapter<PP_Var> { |
| 198 public: |
| 199 VarArrayOutputAdapterWithStorage(); |
| 200 |
| 201 // Returns the final array of resource objects, converting the PP_Vars |
| 202 // written by the browser to pp::Var objects. |
| 203 // |
| 204 // This function should only be called once or we would end up converting |
| 205 // the array more than once, which would mess up the refcounting. |
| 206 std::vector<Var>& output(); |
| 207 |
| 208 private: |
| 209 // The browser will write the PP_Vars into this array. |
| 210 std::vector<PP_Var> temp_storage_; |
| 211 |
| 212 // When asked for the output, the resources above will be converted to the |
| 213 // C++ resource objects in this array for passing to the calling code. |
| 214 std::vector<Var> output_storage_; |
| 215 }; |
| 216 |
| 217 // This adapter is like the ArrayOutputAdapterWithStorage except this |
| 218 // additionally converts PP_Resources to C++ wrapper objects of the given type. |
| 219 // |
| 220 // You can also use it directly if you want to have an array output and aren't |
| 221 // using the CompletionCallbackFactory. For example, if you're calling a |
| 222 // PPAPI function GetFiles that takes a PP_OutputArray that is supposed to be |
| 223 // writing PP_Resources cooresponding to FileRefs, do this: |
| 224 // |
| 225 // ResourceArrayOutputAdapterWithStorage<FileRef> adapter; |
| 226 // ppb_foo->GetFiles(adapter.pp_output_array()); |
| 227 // std::vector<FileRef> result = adapter.output(). |
| 228 template<typename T> |
| 229 class ResourceArrayOutputAdapterWithStorage |
| 230 : public ArrayOutputAdapter<PP_Resource> { |
| 231 public: |
| 232 ResourceArrayOutputAdapterWithStorage() { |
| 233 set_output(&temp_storage_); |
| 234 } |
| 235 |
| 236 // Returns the final array of resource objects, converting the PP_Resources |
| 237 // written by the browser to resource objects. |
| 238 // |
| 239 // This function should only be called once or we would end up converting |
| 240 // the array more than once, which would mess up the refcounting. |
| 241 std::vector<T>& output() { |
| 242 PP_DCHECK(output_storage_.empty()); |
| 243 |
| 244 ConvertPPResourceArrayToObjects(PASS_REF, temp_storage_, &output_storage_); |
| 245 temp_storage_.clear(); |
| 246 return output_storage_; |
| 247 } |
| 248 |
| 249 private: |
| 250 // The browser will write the PP_Resources into this array. |
| 251 std::vector<PP_Resource> temp_storage_; |
| 252 |
| 253 // When asked for the output, the resources above will be converted to the |
| 254 // C++ resource objects in this array for passing to the calling code. |
| 255 std::vector<T> output_storage_; |
| 256 }; |
| 257 |
| 258 } // namespace pp |
| 259 |
| 260 #endif // PPAPI_CPP_ARRAY_OUTPUT_H_ |
OLD | NEW |