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() {
21 ArrayWriter::ArrayWriter(const PP_ArrayOutput
& output
)
22 : pp_array_output_(output
) {
25 ArrayWriter::~ArrayWriter() {
28 void ArrayWriter::Reset() {
29 pp_array_output_
.GetDataBuffer
= NULL
;
30 pp_array_output_
.user_data
= NULL
;
33 bool ArrayWriter::StoreResourceVector(
34 const std::vector
< scoped_refptr
<Resource
> >& input
) {
35 // Always call the alloc function, even on 0 array size.
36 void* dest
= pp_array_output_
.GetDataBuffer(
37 pp_array_output_
.user_data
,
38 static_cast<uint32_t>(input
.size()),
41 // Regardless of success, we clear the output to prevent future calls on
42 // this same output object.
46 return true; // Allow plugin to return NULL on 0 elements.
50 // Convert to PP_Resources.
51 PP_Resource
* dest_resources
= static_cast<PP_Resource
*>(dest
);
52 for (size_t i
= 0; i
< input
.size(); i
++)
53 dest_resources
[i
] = input
[i
]->GetReference();
57 bool ArrayWriter::StoreResourceVector(const std::vector
<PP_Resource
>& input
) {
58 // Always call the alloc function, even on 0 array size.
59 void* dest
= pp_array_output_
.GetDataBuffer(
60 pp_array_output_
.user_data
,
61 static_cast<uint32_t>(input
.size()),
64 // Regardless of success, we clear the output to prevent future calls on
65 // this same output object.
69 return true; // Allow plugin to return NULL on 0 elements.
71 // Free the resources.
72 for (size_t i
= 0; i
< input
.size(); i
++)
73 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(input
[i
]);
77 std::copy(input
.begin(), input
.end(), static_cast<PP_Resource
*>(dest
));
81 bool ArrayWriter::StoreVarVector(
82 const std::vector
< scoped_refptr
<Var
> >& input
) {
83 // Always call the alloc function, even on 0 array size.
84 void* dest
= pp_array_output_
.GetDataBuffer(
85 pp_array_output_
.user_data
,
86 static_cast<uint32_t>(input
.size()),
89 // Regardless of success, we clear the output to prevent future calls on
90 // this same output object.
94 return true; // Allow plugin to return NULL on 0 elements.
98 // Convert to PP_Vars.
99 PP_Var
* dest_vars
= static_cast<PP_Var
*>(dest
);
100 for (size_t i
= 0; i
< input
.size(); i
++)
101 dest_vars
[i
] = input
[i
]->GetPPVar();
105 bool ArrayWriter::StoreVarVector(const std::vector
<PP_Var
>& input
) {
106 // Always call the alloc function, even on 0 array size.
107 void* dest
= pp_array_output_
.GetDataBuffer(
108 pp_array_output_
.user_data
,
109 static_cast<uint32_t>(input
.size()),
112 // Regardless of success, we clear the output to prevent future calls on
113 // this same output object.
117 return true; // Allow plugin to return NULL on 0 elements.
120 for (size_t i
= 0; i
< input
.size(); i
++)
121 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(input
[i
]);
125 std::copy(input
.begin(), input
.end(), static_cast<PP_Var
*>(dest
));