1 // Copyright (c) 2013 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/var_value_conversions.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/stl_util.h"
15 #include "base/values.h"
16 #include "ppapi/c/pp_bool.h"
17 #include "ppapi/c/pp_stdint.h"
18 #include "ppapi/shared_impl/array_var.h"
19 #include "ppapi/shared_impl/dictionary_var.h"
20 #include "ppapi/shared_impl/ppapi_globals.h"
21 #include "ppapi/shared_impl/scoped_pp_var.h"
22 #include "ppapi/shared_impl/var.h"
23 #include "ppapi/shared_impl/var_tracker.h"
29 // In CreateValueFromVar(), a stack is used to keep track of conversion progress
30 // of array and dictionary vars. VarNode represents elements of that stack.
32 VarNode(const PP_Var
& in_var
, base::Value
* in_value
)
33 : var(in_var
), value(in_value
), sentinel(false) {}
35 // This object doesn't hold a reference to it.
37 // It is not owned by this object.
39 // When this is set to true for a node in the stack, it means that we have
40 // finished processing the node itself. However, we keep it in the stack as
41 // a sentinel. When it becomes the top element of the stack again, we know
42 // that we have processed all the descendants of this node.
46 // In CreateVarFromValue(), a stack is used to keep track of conversion progress
47 // of list and dictionary values. ValueNode represents elements of that stack.
49 ValueNode(const PP_Var
& in_var
, const base::Value
* in_value
)
50 : var(in_var
), value(in_value
) {}
52 // This object doesn't hold a reference to it.
54 // It is not owned by this object.
55 const base::Value
* value
;
58 // Helper function for CreateValueFromVar(). It only looks at |var| but not its
59 // descendants. The conversion result is stored in |value|. If |var| is array or
60 // dictionary, a new node is pushed onto |state|.
62 // Returns false on failure.
63 bool CreateValueFromVarHelper(const std::set
<int64_t>& parent_ids
,
65 scoped_ptr
<base::Value
>* value
,
66 std::stack
<VarNode
>* state
) {
68 case PP_VARTYPE_UNDEFINED
:
69 case PP_VARTYPE_NULL
: {
70 value
->reset(base::Value::CreateNullValue());
73 case PP_VARTYPE_BOOL
: {
74 value
->reset(new base::FundamentalValue(PP_ToBool(var
.value
.as_bool
)));
77 case PP_VARTYPE_INT32
: {
78 value
->reset(new base::FundamentalValue(var
.value
.as_int
));
81 case PP_VARTYPE_DOUBLE
: {
82 value
->reset(new base::FundamentalValue(var
.value
.as_double
));
85 case PP_VARTYPE_STRING
: {
86 StringVar
* string_var
= StringVar::FromPPVar(var
);
90 value
->reset(new base::StringValue(string_var
->value()));
93 case PP_VARTYPE_OBJECT
: { return false; }
94 case PP_VARTYPE_ARRAY
: {
95 if (ContainsKey(parent_ids
, var
.value
.as_id
)) {
96 // A circular reference is found.
100 value
->reset(new base::ListValue());
101 state
->push(VarNode(var
, value
->get()));
104 case PP_VARTYPE_DICTIONARY
: {
105 if (ContainsKey(parent_ids
, var
.value
.as_id
)) {
106 // A circular reference is found.
110 value
->reset(new base::DictionaryValue());
111 state
->push(VarNode(var
, value
->get()));
114 case PP_VARTYPE_ARRAY_BUFFER
: {
115 ArrayBufferVar
* array_buffer
= ArrayBufferVar::FromPPVar(var
);
119 base::BinaryValue
* binary_value
=
120 base::BinaryValue::CreateWithCopiedBuffer(
121 static_cast<const char*>(array_buffer
->Map()),
122 array_buffer
->ByteLength());
123 array_buffer
->Unmap();
124 value
->reset(binary_value
);
127 case PP_VARTYPE_RESOURCE
: { return false; }
133 // Helper function for CreateVarFromValue(). It only looks at |value| but not
134 // its descendants. The conversion result is stored in |var|. If |value| is list
135 // or dictionary, a new node is pushed onto |state|.
137 // Returns false on failure.
138 bool CreateVarFromValueHelper(const base::Value
& value
,
140 std::stack
<ValueNode
>* state
) {
141 switch (value
.GetType()) {
142 case base::Value::TYPE_NULL
: {
143 *var
= PP_MakeNull();
146 case base::Value::TYPE_BOOLEAN
: {
148 if (value
.GetAsBoolean(&result
)) {
149 *var
= PP_MakeBool(PP_FromBool(result
));
154 case base::Value::TYPE_INTEGER
: {
156 if (value
.GetAsInteger(&result
)) {
157 *var
= PP_MakeInt32(result
);
162 case base::Value::TYPE_DOUBLE
: {
164 if (value
.GetAsDouble(&result
)) {
165 *var
= PP_MakeDouble(result
);
170 case base::Value::TYPE_STRING
: {
172 if (value
.GetAsString(&result
)) {
173 *var
= ScopedPPVar(ScopedPPVar::PassRef(),
174 StringVar::StringToPPVar(result
));
179 case base::Value::TYPE_BINARY
: {
180 const base::BinaryValue
& binary_value
=
181 static_cast<const base::BinaryValue
&>(value
);
183 size_t size
= binary_value
.GetSize();
184 if (size
> std::numeric_limits
<uint32
>::max())
188 ScopedPPVar::PassRef(),
189 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(
190 static_cast<uint32
>(size
), binary_value
.GetBuffer()));
191 if (temp
.get().type
== PP_VARTYPE_ARRAY_BUFFER
) {
197 case base::Value::TYPE_DICTIONARY
: {
198 scoped_refptr
<DictionaryVar
> dict_var(new DictionaryVar());
199 *var
= ScopedPPVar(ScopedPPVar::PassRef(), dict_var
->GetPPVar());
200 state
->push(ValueNode(var
->get(), &value
));
203 case base::Value::TYPE_LIST
: {
204 scoped_refptr
<ArrayVar
> array_var(new ArrayVar());
205 *var
= ScopedPPVar(ScopedPPVar::PassRef(), array_var
->GetPPVar());
206 state
->push(ValueNode(var
->get(), &value
));
216 base::Value
* CreateValueFromVar(const PP_Var
& var
) {
217 // Used to detect circular references.
218 std::set
<int64_t> parent_ids
;
219 std::stack
<VarNode
> state
;
220 scoped_ptr
<base::Value
> root_value
;
222 if (!CreateValueFromVarHelper(parent_ids
, var
, &root_value
, &state
))
225 while (!state
.empty()) {
226 VarNode
& top
= state
.top();
228 parent_ids
.erase(top
.var
.value
.as_id
);
230 } else if (top
.var
.type
== PP_VARTYPE_DICTIONARY
) {
231 parent_ids
.insert(top
.var
.value
.as_id
);
234 DictionaryVar
* dict_var
= DictionaryVar::FromPPVar(top
.var
);
238 DCHECK(top
.value
->GetType() == base::Value::TYPE_DICTIONARY
);
239 base::DictionaryValue
* dict_value
=
240 static_cast<base::DictionaryValue
*>(top
.value
);
242 for (DictionaryVar::KeyValueMap::const_iterator iter
=
243 dict_var
->key_value_map().begin();
244 iter
!= dict_var
->key_value_map().end();
246 // Skip the key-value pair if the value is undefined or null.
247 if (iter
->second
.get().type
== PP_VARTYPE_UNDEFINED
||
248 iter
->second
.get().type
== PP_VARTYPE_NULL
) {
252 scoped_ptr
<base::Value
> child_value
;
253 if (!CreateValueFromVarHelper(
254 parent_ids
, iter
->second
.get(), &child_value
, &state
)) {
258 dict_value
->SetWithoutPathExpansion(iter
->first
, child_value
.release());
260 } else if (top
.var
.type
== PP_VARTYPE_ARRAY
) {
261 parent_ids
.insert(top
.var
.value
.as_id
);
264 ArrayVar
* array_var
= ArrayVar::FromPPVar(top
.var
);
268 DCHECK(top
.value
->GetType() == base::Value::TYPE_LIST
);
269 base::ListValue
* list_value
= static_cast<base::ListValue
*>(top
.value
);
271 for (ArrayVar::ElementVector::const_iterator iter
=
272 array_var
->elements().begin();
273 iter
!= array_var
->elements().end();
275 scoped_ptr
<base::Value
> child_value
;
276 if (!CreateValueFromVarHelper(
277 parent_ids
, iter
->get(), &child_value
, &state
)) {
281 list_value
->Append(child_value
.release());
288 DCHECK(parent_ids
.empty());
289 return root_value
.release();
292 PP_Var
CreateVarFromValue(const base::Value
& value
) {
293 std::stack
<ValueNode
> state
;
294 ScopedPPVar root_var
;
296 if (!CreateVarFromValueHelper(value
, &root_var
, &state
))
297 return PP_MakeUndefined();
299 while (!state
.empty()) {
300 ValueNode top
= state
.top();
303 if (top
.value
->GetType() == base::Value::TYPE_DICTIONARY
) {
304 const base::DictionaryValue
* dict_value
=
305 static_cast<const base::DictionaryValue
*>(top
.value
);
306 DictionaryVar
* dict_var
= DictionaryVar::FromPPVar(top
.var
);
308 for (base::DictionaryValue::Iterator
iter(*dict_value
); !iter
.IsAtEnd();
310 ScopedPPVar child_var
;
311 if (!CreateVarFromValueHelper(iter
.value(), &child_var
, &state
) ||
312 !dict_var
->SetWithStringKey(iter
.key(), child_var
.get())) {
313 return PP_MakeUndefined();
316 } else if (top
.value
->GetType() == base::Value::TYPE_LIST
) {
317 const base::ListValue
* list_value
=
318 static_cast<const base::ListValue
*>(top
.value
);
319 ArrayVar
* array_var
= ArrayVar::FromPPVar(top
.var
);
321 for (base::ListValue::const_iterator iter
= list_value
->begin();
322 iter
!= list_value
->end();
324 ScopedPPVar child_var
;
325 if (!CreateVarFromValueHelper(**iter
, &child_var
, &state
))
326 return PP_MakeUndefined();
328 array_var
->elements().push_back(child_var
);
332 return PP_MakeUndefined();
336 return root_var
.Release();
339 base::ListValue
* CreateListValueFromVarVector(const std::vector
<PP_Var
>& vars
) {
340 scoped_ptr
<base::ListValue
> list_value(new base::ListValue());
342 for (std::vector
<PP_Var
>::const_iterator iter
= vars
.begin();
345 base::Value
* value
= CreateValueFromVar(*iter
);
348 list_value
->Append(value
);
350 return list_value
.release();
353 bool CreateVarVectorFromListValue(const base::ListValue
& list_value
,
354 std::vector
<PP_Var
>* vars
) {
358 std::vector
<ScopedPPVar
> result
;
359 result
.reserve(list_value
.GetSize());
360 for (base::ListValue::const_iterator iter
= list_value
.begin();
361 iter
!= list_value
.end();
363 ScopedPPVar
child_var(ScopedPPVar::PassRef(), CreateVarFromValue(**iter
));
364 if (child_var
.get().type
== PP_VARTYPE_UNDEFINED
)
367 result
.push_back(child_var
);
371 vars
->reserve(result
.size());
372 for (std::vector
<ScopedPPVar
>::iterator iter
= result
.begin();
373 iter
!= result
.end();
375 vars
->push_back(iter
->Release());