OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/shared_impl/array_writer.h" | 5 #include "ppapi/shared_impl/array_writer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "ppapi/shared_impl/ppapi_globals.h" | 9 #include "ppapi/shared_impl/ppapi_globals.h" |
10 #include "ppapi/shared_impl/resource.h" | 10 #include "ppapi/shared_impl/resource.h" |
11 #include "ppapi/shared_impl/resource_tracker.h" | 11 #include "ppapi/shared_impl/resource_tracker.h" |
12 #include "ppapi/shared_impl/var.h" | 12 #include "ppapi/shared_impl/var.h" |
13 #include "ppapi/shared_impl/var_tracker.h" | 13 #include "ppapi/shared_impl/var_tracker.h" |
14 | 14 |
15 namespace ppapi { | 15 namespace ppapi { |
16 | 16 |
17 ArrayWriter::ArrayWriter() { | 17 ArrayWriter::ArrayWriter() { Reset(); } |
18 Reset(); | |
19 } | |
20 | 18 |
21 ArrayWriter::ArrayWriter(const PP_ArrayOutput& output) | 19 ArrayWriter::ArrayWriter(const PP_ArrayOutput& output) |
22 : pp_array_output_(output) { | 20 : pp_array_output_(output) {} |
23 } | |
24 | 21 |
25 ArrayWriter::~ArrayWriter() { | 22 ArrayWriter::~ArrayWriter() {} |
26 } | |
27 | 23 |
28 void ArrayWriter::Reset() { | 24 void ArrayWriter::Reset() { |
29 pp_array_output_.GetDataBuffer = NULL; | 25 pp_array_output_.GetDataBuffer = NULL; |
30 pp_array_output_.user_data = NULL; | 26 pp_array_output_.user_data = NULL; |
31 } | 27 } |
32 | 28 |
33 bool ArrayWriter::StoreResourceVector( | 29 bool ArrayWriter::StoreResourceVector( |
34 const std::vector< scoped_refptr<Resource> >& input) { | 30 const std::vector<scoped_refptr<Resource> >& input) { |
35 // Always call the alloc function, even on 0 array size. | 31 // Always call the alloc function, even on 0 array size. |
36 void* dest = pp_array_output_.GetDataBuffer( | 32 void* dest = |
37 pp_array_output_.user_data, | 33 pp_array_output_.GetDataBuffer(pp_array_output_.user_data, |
38 static_cast<uint32_t>(input.size()), | 34 static_cast<uint32_t>(input.size()), |
39 sizeof(PP_Resource)); | 35 sizeof(PP_Resource)); |
40 | 36 |
41 // Regardless of success, we clear the output to prevent future calls on | 37 // Regardless of success, we clear the output to prevent future calls on |
42 // this same output object. | 38 // this same output object. |
43 Reset(); | 39 Reset(); |
44 | 40 |
45 if (input.empty()) | 41 if (input.empty()) |
46 return true; // Allow plugin to return NULL on 0 elements. | 42 return true; // Allow plugin to return NULL on 0 elements. |
47 if (!dest) | 43 if (!dest) |
48 return false; | 44 return false; |
49 | 45 |
50 // Convert to PP_Resources. | 46 // Convert to PP_Resources. |
51 PP_Resource* dest_resources = static_cast<PP_Resource*>(dest); | 47 PP_Resource* dest_resources = static_cast<PP_Resource*>(dest); |
52 for (size_t i = 0; i < input.size(); i++) | 48 for (size_t i = 0; i < input.size(); i++) |
53 dest_resources[i] = input[i]->GetReference(); | 49 dest_resources[i] = input[i]->GetReference(); |
54 return true; | 50 return true; |
55 } | 51 } |
56 | 52 |
57 bool ArrayWriter::StoreResourceVector(const std::vector<PP_Resource>& input) { | 53 bool ArrayWriter::StoreResourceVector(const std::vector<PP_Resource>& input) { |
58 // Always call the alloc function, even on 0 array size. | 54 // Always call the alloc function, even on 0 array size. |
59 void* dest = pp_array_output_.GetDataBuffer( | 55 void* dest = |
60 pp_array_output_.user_data, | 56 pp_array_output_.GetDataBuffer(pp_array_output_.user_data, |
61 static_cast<uint32_t>(input.size()), | 57 static_cast<uint32_t>(input.size()), |
62 sizeof(PP_Resource)); | 58 sizeof(PP_Resource)); |
63 | 59 |
64 // Regardless of success, we clear the output to prevent future calls on | 60 // Regardless of success, we clear the output to prevent future calls on |
65 // this same output object. | 61 // this same output object. |
66 Reset(); | 62 Reset(); |
67 | 63 |
68 if (input.empty()) | 64 if (input.empty()) |
69 return true; // Allow plugin to return NULL on 0 elements. | 65 return true; // Allow plugin to return NULL on 0 elements. |
70 if (!dest) { | 66 if (!dest) { |
71 // Free the resources. | 67 // Free the resources. |
72 for (size_t i = 0; i < input.size(); i++) | 68 for (size_t i = 0; i < input.size(); i++) |
73 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(input[i]); | 69 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(input[i]); |
74 return false; | 70 return false; |
75 } | 71 } |
76 | 72 |
77 std::copy(input.begin(), input.end(), static_cast<PP_Resource*>(dest)); | 73 std::copy(input.begin(), input.end(), static_cast<PP_Resource*>(dest)); |
78 return true; | 74 return true; |
79 } | 75 } |
80 | 76 |
81 bool ArrayWriter::StoreVarVector( | 77 bool ArrayWriter::StoreVarVector( |
82 const std::vector< scoped_refptr<Var> >& input) { | 78 const std::vector<scoped_refptr<Var> >& input) { |
83 // Always call the alloc function, even on 0 array size. | 79 // Always call the alloc function, even on 0 array size. |
84 void* dest = pp_array_output_.GetDataBuffer( | 80 void* dest = |
85 pp_array_output_.user_data, | 81 pp_array_output_.GetDataBuffer(pp_array_output_.user_data, |
86 static_cast<uint32_t>(input.size()), | 82 static_cast<uint32_t>(input.size()), |
87 sizeof(PP_Var)); | 83 sizeof(PP_Var)); |
88 | 84 |
89 // Regardless of success, we clear the output to prevent future calls on | 85 // Regardless of success, we clear the output to prevent future calls on |
90 // this same output object. | 86 // this same output object. |
91 Reset(); | 87 Reset(); |
92 | 88 |
93 if (input.empty()) | 89 if (input.empty()) |
94 return true; // Allow plugin to return NULL on 0 elements. | 90 return true; // Allow plugin to return NULL on 0 elements. |
95 if (!dest) | 91 if (!dest) |
96 return false; | 92 return false; |
97 | 93 |
98 // Convert to PP_Vars. | 94 // Convert to PP_Vars. |
99 PP_Var* dest_vars = static_cast<PP_Var*>(dest); | 95 PP_Var* dest_vars = static_cast<PP_Var*>(dest); |
100 for (size_t i = 0; i < input.size(); i++) | 96 for (size_t i = 0; i < input.size(); i++) |
101 dest_vars[i] = input[i]->GetPPVar(); | 97 dest_vars[i] = input[i]->GetPPVar(); |
102 return true; | 98 return true; |
103 } | 99 } |
104 | 100 |
105 bool ArrayWriter::StoreVarVector(const std::vector<PP_Var>& input) { | 101 bool ArrayWriter::StoreVarVector(const std::vector<PP_Var>& input) { |
106 // Always call the alloc function, even on 0 array size. | 102 // Always call the alloc function, even on 0 array size. |
107 void* dest = pp_array_output_.GetDataBuffer( | 103 void* dest = |
108 pp_array_output_.user_data, | 104 pp_array_output_.GetDataBuffer(pp_array_output_.user_data, |
109 static_cast<uint32_t>(input.size()), | 105 static_cast<uint32_t>(input.size()), |
110 sizeof(PP_Var)); | 106 sizeof(PP_Var)); |
111 | 107 |
112 // Regardless of success, we clear the output to prevent future calls on | 108 // Regardless of success, we clear the output to prevent future calls on |
113 // this same output object. | 109 // this same output object. |
114 Reset(); | 110 Reset(); |
115 | 111 |
116 if (input.empty()) | 112 if (input.empty()) |
117 return true; // Allow plugin to return NULL on 0 elements. | 113 return true; // Allow plugin to return NULL on 0 elements. |
118 if (!dest) { | 114 if (!dest) { |
119 // Free the vars. | 115 // Free the vars. |
120 for (size_t i = 0; i < input.size(); i++) | 116 for (size_t i = 0; i < input.size(); i++) |
121 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(input[i]); | 117 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(input[i]); |
122 return false; | 118 return false; |
123 } | 119 } |
124 | 120 |
125 std::copy(input.begin(), input.end(), static_cast<PP_Var*>(dest)); | 121 std::copy(input.begin(), input.end(), static_cast<PP_Var*>(dest)); |
126 return true; | 122 return true; |
127 } | 123 } |
128 | 124 |
129 } // namespace ppapi | 125 } // namespace ppapi |
OLD | NEW |