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
,
78 static_cast<uint32_t>(input
.size()));
81 // Stores the given vector of resources as PP_Resources to the output vector,
82 // adding one reference to each.
84 // On failure this returns false, nothing will be copied, and the resource
85 // refcounts will be unchanged. In either case, the object will become
86 // is_null() immediately after the call since one output function should only
89 // Note: potentially this could be a template in case you have a vector of
90 // FileRef objects, for example. However, this saves code since there's only
91 // one instantiation and is sufficient for now.
92 bool StoreResourceVector(const std::vector
<scoped_refptr
<Resource
> >& input
);
94 // Like the above version but takes an array of AddRef'ed PP_Resources. On
95 // storage failure, this will release each resource.
96 bool StoreResourceVector(const std::vector
<PP_Resource
>& input
);
98 // Stores the given vector of vars as PP_Vars to the output vector,
99 // adding one reference to each.
101 // On failure this returns false, nothing will be copied, and the var
102 // refcounts will be unchanged. In either case, the object will become
103 // is_null() immediately after the call since one output function should only
105 bool StoreVarVector(const std::vector
<scoped_refptr
<Var
> >& input
);
107 // Like the above version but takes an array of AddRef'ed PP_Vars. On
108 // storage failure, this will release each var.
109 bool StoreVarVector(const std::vector
<PP_Var
>& input
);
112 PP_ArrayOutput pp_array_output_
;
114 DISALLOW_COPY_AND_ASSIGN(ArrayWriter
);
119 #endif // PPAPI_SHARED_IMPL_ARRAY_WRITER_H_