Chromium Code Reviews| Index: ppapi/shared_impl/var_value_conversions.cc |
| diff --git a/ppapi/shared_impl/var_value_conversions.cc b/ppapi/shared_impl/var_value_conversions.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6457c7ec42526d42b09c268ba4d5bdc596b995ac |
| --- /dev/null |
| +++ b/ppapi/shared_impl/var_value_conversions.cc |
| @@ -0,0 +1,197 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ppapi/shared_impl/var_value_conversions.h" |
| + |
| +#include <algorithm> |
| +#include <limits> |
| +#include <vector> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/values.h" |
| +#include "ppapi/c/pp_bool.h" |
| +#include "ppapi/c/pp_stdint.h" |
| +#include "ppapi/shared_impl/dictionary_var.h" |
| +#include "ppapi/shared_impl/ppapi_globals.h" |
| +#include "ppapi/shared_impl/scoped_pp_var.h" |
| +#include "ppapi/shared_impl/var.h" |
| +#include "ppapi/shared_impl/var_tracker.h" |
| + |
| +namespace ppapi { |
| + |
| +namespace { |
| + |
| +base::Value* CreateValueFromVarImpl(const PP_Var& var, |
| + std::vector<int64_t>* parent_ids) { |
| + switch (var.type) { |
| + case PP_VARTYPE_UNDEFINED: { |
| + return NULL; |
| + } |
| + case PP_VARTYPE_NULL: { |
| + return base::Value::CreateNullValue(); |
| + } |
| + case PP_VARTYPE_BOOL: { |
| + return new base::FundamentalValue(PP_ToBool(var.value.as_bool)); |
| + } |
| + case PP_VARTYPE_INT32: { |
| + return new base::FundamentalValue(var.value.as_int); |
| + } |
| + case PP_VARTYPE_DOUBLE: { |
| + return new base::FundamentalValue(var.value.as_double); |
| + } |
| + case PP_VARTYPE_STRING: { |
| + StringVar* string_var = StringVar::FromPPVar(var); |
| + if (!string_var) |
| + return NULL; |
| + return new base::StringValue(string_var->value()); |
| + } |
| + case PP_VARTYPE_OBJECT: { |
| + return NULL; |
| + } |
| + case PP_VARTYPE_ARRAY: { |
| + if (std::find(parent_ids->begin(), parent_ids->end(), var.value.as_id) != |
| + parent_ids->end()) { |
|
dmichael (off chromium)
2013/03/05 22:01:03
So the conversion is O(n^2). You could drop it to
yzshen1
2013/03/14 05:38:21
Done. I changed it to a set.
(I think it should b
dmichael (off chromium)
2013/03/15 17:35:48
O(n^2) worst-case for a line of Object->Child->Chi
yzshen1
2013/03/15 22:56:49
Right. That is true.
|
| + // A circular reference is found. |
| + return NULL; |
| + } |
| + |
| + scoped_ptr<base::ListValue> list_value(new base::ListValue()); |
| + parent_ids->push_back(var.value.as_id); |
| + |
| + // TODO(yzshen): Implement it once array var is supported. |
| + |
| + parent_ids->pop_back(); |
| + return list_value.release(); |
| + } |
| + case PP_VARTYPE_DICTIONARY: { |
| + if (std::find(parent_ids->begin(), parent_ids->end(), var.value.as_id) != |
| + parent_ids->end()) { |
| + // A circular reference is found. |
| + return NULL; |
| + } |
| + |
| + DictionaryVar* dict_var = DictionaryVar::FromPPVar(var); |
| + if (!dict_var) |
| + return NULL; |
| + |
| + scoped_ptr<base::DictionaryValue> dict_value(new base::DictionaryValue()); |
| + parent_ids->push_back(var.value.as_id); |
| + |
| + for (DictionaryVar::KeyValueMap::const_iterator iter = |
| + dict_var->key_value_map().begin(); |
| + iter != dict_var->key_value_map().end(); ++iter) { |
| + base::Value* value = CreateValueFromVarImpl(iter->second.get(), |
| + parent_ids); |
|
dmichael (off chromium)
2013/03/05 22:01:03
If a plugin creates a really deep object, they can
yzshen1
2013/03/14 05:38:21
Yeah, this is a good idea.
Thanks!
On 2013/03/05 2
|
| + if (!value) { |
| + parent_ids->pop_back(); |
| + return NULL; |
| + } |
| + |
| + dict_value->SetWithoutPathExpansion(iter->first, value); |
| + } |
| + |
| + parent_ids->pop_back(); |
| + return dict_value.release(); |
| + } |
| + case PP_VARTYPE_ARRAY_BUFFER: { |
| + ArrayBufferVar* array_buffer = ArrayBufferVar::FromPPVar(var); |
| + if (!array_buffer) |
| + return NULL; |
| + |
| + base::BinaryValue* binary_value = |
| + base::BinaryValue::CreateWithCopiedBuffer( |
| + static_cast<const char*>(array_buffer->Map()), |
| + array_buffer->ByteLength()); |
| + array_buffer->Unmap(); |
| + return binary_value; |
| + } |
| + } |
| + NOTREACHED(); |
| + return NULL; |
| +} |
| + |
| +} // namespace |
| + |
| +base::Value* CreateValueFromVar(const PP_Var& var) { |
| + std::vector<int64_t> parent_ids; |
| + base::Value* result = CreateValueFromVarImpl(var, &parent_ids); |
| + DCHECK(parent_ids.empty()); |
| + return result; |
| +} |
| + |
| +PP_Var CreateVarFromValue(const base::Value& value) { |
| + switch (value.GetType()) { |
| + case base::Value::TYPE_NULL: { |
| + return PP_MakeNull(); |
| + } |
| + case base::Value::TYPE_BOOLEAN: { |
| + bool result = false; |
| + if (value.GetAsBoolean(&result)) |
| + return PP_MakeBool(PP_FromBool(result)); |
| + else |
| + return PP_MakeUndefined(); |
| + } |
| + case base::Value::TYPE_INTEGER: { |
| + int result = 0; |
| + if (value.GetAsInteger(&result)) |
| + return PP_MakeInt32(result); |
| + else |
| + return PP_MakeUndefined(); |
| + } |
| + case base::Value::TYPE_DOUBLE: { |
| + double result = 0; |
| + if (value.GetAsDouble(&result)) |
| + return PP_MakeDouble(result); |
| + else |
| + return PP_MakeUndefined(); |
| + } |
| + case base::Value::TYPE_STRING: { |
| + std::string result; |
| + if (value.GetAsString(&result)) |
| + return StringVar::StringToPPVar(result); |
| + else |
| + return PP_MakeUndefined(); |
| + } |
| + case base::Value::TYPE_BINARY: { |
|
dmichael (off chromium)
2013/03/05 22:01:03
nit: It might be nice to order these cases the sam
yzshen1
2013/03/14 05:38:21
I ordered them according to the enum type definiti
|
| + const base::BinaryValue& binary_value = |
| + static_cast<const base::BinaryValue&>(value); |
| + |
| + size_t size = binary_value.GetSize(); |
| + if (size > std::numeric_limits<uint32>::max()) |
| + return PP_MakeUndefined(); |
| + |
| + return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( |
| + static_cast<uint32>(size), binary_value.GetBuffer()); |
| + } |
| + case base::Value::TYPE_DICTIONARY: { |
| + const base::DictionaryValue& dict_value = |
| + static_cast<const base::DictionaryValue&>(value); |
| + |
| + scoped_refptr<DictionaryVar> dict_var(new DictionaryVar()); |
| + |
| + for (base::DictionaryValue::Iterator iter(dict_value); !iter.IsAtEnd(); |
|
dmichael (off chromium)
2013/03/05 22:01:03
nit: I prefer each statement in the for to be on i
yzshen1
2013/03/14 05:38:21
Done.
|
| + iter.Advance()) { |
| + ScopedPPVar child_var(ScopedPPVar::PassRef(), |
| + CreateVarFromValue(iter.value())); |
| + if (child_var.get().type == PP_VARTYPE_UNDEFINED || |
| + !dict_var->SetWithStringKey(iter.key(), child_var.get())) { |
| + return PP_MakeUndefined(); |
| + } |
| + } |
| + |
| + return dict_var->GetPPVar(); |
| + } |
| + case base::Value::TYPE_LIST: { |
| + // TODO(yzshen): Add support once array var is supported. |
| + return PP_MakeUndefined(); |
| + } |
| + } |
| + NOTREACHED(); |
| + return PP_MakeUndefined(); |
| +} |
| + |
| +} // namespace ppapi |
| + |