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 #include "ppapi/shared_impl/array_writer.h"
9 #include "ppapi/shared_impl/ppapi_globals.h"
10 #include "ppapi/shared_impl/resource.h"
11 #include "ppapi/shared_impl/resource_tracker.h"
12 #include "ppapi/shared_impl/var.h"
13 #include "ppapi/shared_impl/var_tracker.h"
17 ArrayWriter::ArrayWriter() { Reset(); }
19 ArrayWriter::ArrayWriter(const PP_ArrayOutput
& output
)
20 : pp_array_output_(output
) {}
22 ArrayWriter::~ArrayWriter() {}
24 void ArrayWriter::Reset() {
25 pp_array_output_
.GetDataBuffer
= NULL
;
26 pp_array_output_
.user_data
= NULL
;
29 bool ArrayWriter::StoreResourceVector(
30 const std::vector
<scoped_refptr
<Resource
> >& input
) {
31 // Always call the alloc function, even on 0 array size.
33 pp_array_output_
.GetDataBuffer(pp_array_output_
.user_data
,
34 static_cast<uint32_t>(input
.size()),
37 // Regardless of success, we clear the output to prevent future calls on
38 // this same output object.
42 return true; // Allow plugin to return NULL on 0 elements.
46 // Convert to PP_Resources.
47 PP_Resource
* dest_resources
= static_cast<PP_Resource
*>(dest
);
48 for (size_t i
= 0; i
< input
.size(); i
++)
49 dest_resources
[i
] = input
[i
]->GetReference();
53 bool ArrayWriter::StoreResourceVector(const std::vector
<PP_Resource
>& input
) {
54 // Always call the alloc function, even on 0 array size.
56 pp_array_output_
.GetDataBuffer(pp_array_output_
.user_data
,
57 static_cast<uint32_t>(input
.size()),
60 // Regardless of success, we clear the output to prevent future calls on
61 // this same output object.
65 return true; // Allow plugin to return NULL on 0 elements.
67 // Free the resources.
68 for (size_t i
= 0; i
< input
.size(); i
++)
69 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(input
[i
]);
73 std::copy(input
.begin(), input
.end(), static_cast<PP_Resource
*>(dest
));
77 bool ArrayWriter::StoreVarVector(
78 const std::vector
<scoped_refptr
<Var
> >& input
) {
79 // Always call the alloc function, even on 0 array size.
81 pp_array_output_
.GetDataBuffer(pp_array_output_
.user_data
,
82 static_cast<uint32_t>(input
.size()),
85 // Regardless of success, we clear the output to prevent future calls on
86 // this same output object.
90 return true; // Allow plugin to return NULL on 0 elements.
94 // Convert to PP_Vars.
95 PP_Var
* dest_vars
= static_cast<PP_Var
*>(dest
);
96 for (size_t i
= 0; i
< input
.size(); i
++)
97 dest_vars
[i
] = input
[i
]->GetPPVar();
101 bool ArrayWriter::StoreVarVector(const std::vector
<PP_Var
>& input
) {
102 // Always call the alloc function, even on 0 array size.
104 pp_array_output_
.GetDataBuffer(pp_array_output_
.user_data
,
105 static_cast<uint32_t>(input
.size()),
108 // Regardless of success, we clear the output to prevent future calls on
109 // this same output object.
113 return true; // Allow plugin to return NULL on 0 elements.
116 for (size_t i
= 0; i
< input
.size(); i
++)
117 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(input
[i
]);
121 std::copy(input
.begin(), input
.end(), static_cast<PP_Var
*>(dest
));