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.
5 #ifndef PPAPI_SHARED_IMPL_ARRAY_WRITER_H_
6 #define PPAPI_SHARED_IMPL_ARRAY_WRITER_H_
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"
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
{
28 ArrayWriter(); // Creates an is_null() object
29 ArrayWriter(const PP_ArrayOutput
& output
);
32 bool is_valid() const { return !!pp_array_output_
.GetDataBuffer
; }
33 bool is_null() const { return !is_valid(); }
35 void set_pp_array_output(const PP_ArrayOutput
& output
) {
36 pp_array_output_
= output
;
39 // Sets the array output back to its is_null() state.
42 // StoreArray() and StoreVector() copy the given array/vector of data to the
43 // plugin output array.
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.
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.
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
));
59 // Regardless of success, we clear the output to prevent future calls on
60 // this same output object.
64 return true; // Allow plugin to return NULL on 0 elements.
69 memcpy(dest
, input
, sizeof(T
) * count
);
73 // Copies the given array/vector of data to the plugin output array. See
74 // comment of StoreArray() for detail.
76 bool StoreVector(const std::vector
<T
>& input
) {
77 return StoreArray(input
.size() ? &input
[0] : NULL
, input
.size());
80 // Stores the given vector of resources as PP_Resources to the output vector,
81 // adding one reference to each.
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
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
);
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
);
97 // Stores the given vector of vars as PP_Vars to the output vector,
98 // adding one reference to each.
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
104 bool StoreVarVector(const std::vector
<scoped_refptr
<Var
> >& input
);
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
);
111 PP_ArrayOutput pp_array_output_
;
113 DISALLOW_COPY_AND_ASSIGN(ArrayWriter
);
118 #endif // PPAPI_SHARED_IMPL_ARRAY_WRITER_H_