- add sources.
[platform/framework/web/crosswalk.git] / src / ppapi / shared_impl / array_writer.cc
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.
4
5 #include "ppapi/shared_impl/array_writer.h"
6
7 #include <algorithm>
8
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"
14
15 namespace ppapi {
16
17 ArrayWriter::ArrayWriter() {
18   Reset();
19 }
20
21 ArrayWriter::ArrayWriter(const PP_ArrayOutput& output)
22     : pp_array_output_(output) {
23 }
24
25 ArrayWriter::~ArrayWriter() {
26 }
27
28 void ArrayWriter::Reset() {
29   pp_array_output_.GetDataBuffer = NULL;
30   pp_array_output_.user_data = NULL;
31 }
32
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));
40
41   // Regardless of success, we clear the output to prevent future calls on
42   // this same output object.
43   Reset();
44
45   if (input.empty())
46     return true;  // Allow plugin to return NULL on 0 elements.
47   if (!dest)
48     return false;
49
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;
55 }
56
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));
63
64   // Regardless of success, we clear the output to prevent future calls on
65   // this same output object.
66   Reset();
67
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;
75   }
76
77   std::copy(input.begin(), input.end(), static_cast<PP_Resource*>(dest));
78   return true;
79 }
80
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));
88
89   // Regardless of success, we clear the output to prevent future calls on
90   // this same output object.
91   Reset();
92
93   if (input.empty())
94     return true;  // Allow plugin to return NULL on 0 elements.
95   if (!dest)
96     return false;
97
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;
103 }
104
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));
111
112   // Regardless of success, we clear the output to prevent future calls on
113   // this same output object.
114   Reset();
115
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;
123   }
124
125   std::copy(input.begin(), input.end(), static_cast<PP_Var*>(dest));
126   return true;
127 }
128
129 }  // namespace ppapi