Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / ppapi / shared_impl / array_writer.cc
blob26ee1bd669b00d38f36ee4cb3c5cbed61edf88e0
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"
7 #include <algorithm>
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"
15 namespace ppapi {
17 ArrayWriter::ArrayWriter() {
18 Reset();
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()),
39 sizeof(PP_Resource));
41 // Regardless of success, we clear the output to prevent future calls on
42 // this same output object.
43 Reset();
45 if (input.empty())
46 return true; // Allow plugin to return NULL on 0 elements.
47 if (!dest)
48 return false;
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();
54 return true;
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()),
62 sizeof(PP_Resource));
64 // Regardless of success, we clear the output to prevent future calls on
65 // this same output object.
66 Reset();
68 if (input.empty())
69 return true; // Allow plugin to return NULL on 0 elements.
70 if (!dest) {
71 // Free the resources.
72 for (size_t i = 0; i < input.size(); i++)
73 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(input[i]);
74 return false;
77 std::copy(input.begin(), input.end(), static_cast<PP_Resource*>(dest));
78 return true;
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()),
87 sizeof(PP_Var));
89 // Regardless of success, we clear the output to prevent future calls on
90 // this same output object.
91 Reset();
93 if (input.empty())
94 return true; // Allow plugin to return NULL on 0 elements.
95 if (!dest)
96 return false;
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();
102 return true;
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()),
110 sizeof(PP_Var));
112 // Regardless of success, we clear the output to prevent future calls on
113 // this same output object.
114 Reset();
116 if (input.empty())
117 return true; // Allow plugin to return NULL on 0 elements.
118 if (!dest) {
119 // Free the vars.
120 for (size_t i = 0; i < input.size(); i++)
121 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(input[i]);
122 return false;
125 std::copy(input.begin(), input.end(), static_cast<PP_Var*>(dest));
126 return true;
129 } // namespace ppapi