This source file includes following definitions.
- CreateValueFromVarHelper
- CreateVarFromValueHelper
- CreateValueFromVar
- CreateVarFromValue
- CreateListValueFromVarVector
- CreateVarVectorFromListValue
#include "ppapi/shared_impl/var_value_conversions.h"
#include <limits>
#include <set>
#include <stack>
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "base/values.h"
#include "ppapi/c/pp_bool.h"
#include "ppapi/c/pp_stdint.h"
#include "ppapi/shared_impl/array_var.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 {
struct VarNode {
VarNode(const PP_Var& in_var, base::Value* in_value)
: var(in_var), value(in_value), sentinel(false) {}
PP_Var var;
base::Value* value;
bool sentinel;
};
struct ValueNode {
ValueNode(const PP_Var& in_var, const base::Value* in_value)
: var(in_var), value(in_value) {}
PP_Var var;
const base::Value* value;
};
bool CreateValueFromVarHelper(const std::set<int64_t>& parent_ids,
const PP_Var& var,
scoped_ptr<base::Value>* value,
std::stack<VarNode>* state) {
switch (var.type) {
case PP_VARTYPE_UNDEFINED:
case PP_VARTYPE_NULL: {
value->reset(base::Value::CreateNullValue());
return true;
}
case PP_VARTYPE_BOOL: {
value->reset(new base::FundamentalValue(PP_ToBool(var.value.as_bool)));
return true;
}
case PP_VARTYPE_INT32: {
value->reset(new base::FundamentalValue(var.value.as_int));
return true;
}
case PP_VARTYPE_DOUBLE: {
value->reset(new base::FundamentalValue(var.value.as_double));
return true;
}
case PP_VARTYPE_STRING: {
StringVar* string_var = StringVar::FromPPVar(var);
if (!string_var)
return false;
value->reset(new base::StringValue(string_var->value()));
return true;
}
case PP_VARTYPE_OBJECT: { return false; }
case PP_VARTYPE_ARRAY: {
if (ContainsKey(parent_ids, var.value.as_id)) {
return false;
}
value->reset(new base::ListValue());
state->push(VarNode(var, value->get()));
return true;
}
case PP_VARTYPE_DICTIONARY: {
if (ContainsKey(parent_ids, var.value.as_id)) {
return false;
}
value->reset(new base::DictionaryValue());
state->push(VarNode(var, value->get()));
return true;
}
case PP_VARTYPE_ARRAY_BUFFER: {
ArrayBufferVar* array_buffer = ArrayBufferVar::FromPPVar(var);
if (!array_buffer)
return false;
base::BinaryValue* binary_value =
base::BinaryValue::CreateWithCopiedBuffer(
static_cast<const char*>(array_buffer->Map()),
array_buffer->ByteLength());
array_buffer->Unmap();
value->reset(binary_value);
return true;
}
case PP_VARTYPE_RESOURCE: { return false; }
}
NOTREACHED();
return false;
}
bool CreateVarFromValueHelper(const base::Value& value,
ScopedPPVar* var,
std::stack<ValueNode>* state) {
switch (value.GetType()) {
case base::Value::TYPE_NULL: {
*var = PP_MakeNull();
return true;
}
case base::Value::TYPE_BOOLEAN: {
bool result = false;
if (value.GetAsBoolean(&result)) {
*var = PP_MakeBool(PP_FromBool(result));
return true;
}
return false;
}
case base::Value::TYPE_INTEGER: {
int result = 0;
if (value.GetAsInteger(&result)) {
*var = PP_MakeInt32(result);
return true;
}
return false;
}
case base::Value::TYPE_DOUBLE: {
double result = 0;
if (value.GetAsDouble(&result)) {
*var = PP_MakeDouble(result);
return true;
}
return false;
}
case base::Value::TYPE_STRING: {
std::string result;
if (value.GetAsString(&result)) {
*var = ScopedPPVar(ScopedPPVar::PassRef(),
StringVar::StringToPPVar(result));
return true;
}
return false;
}
case base::Value::TYPE_BINARY: {
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 false;
ScopedPPVar temp(
ScopedPPVar::PassRef(),
PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(
static_cast<uint32>(size), binary_value.GetBuffer()));
if (temp.get().type == PP_VARTYPE_ARRAY_BUFFER) {
*var = temp;
return true;
}
return false;
}
case base::Value::TYPE_DICTIONARY: {
scoped_refptr<DictionaryVar> dict_var(new DictionaryVar());
*var = ScopedPPVar(ScopedPPVar::PassRef(), dict_var->GetPPVar());
state->push(ValueNode(var->get(), &value));
return true;
}
case base::Value::TYPE_LIST: {
scoped_refptr<ArrayVar> array_var(new ArrayVar());
*var = ScopedPPVar(ScopedPPVar::PassRef(), array_var->GetPPVar());
state->push(ValueNode(var->get(), &value));
return true;
}
}
NOTREACHED();
return false;
}
}
base::Value* CreateValueFromVar(const PP_Var& var) {
std::set<int64_t> parent_ids;
std::stack<VarNode> state;
scoped_ptr<base::Value> root_value;
if (!CreateValueFromVarHelper(parent_ids, var, &root_value, &state))
return NULL;
while (!state.empty()) {
VarNode& top = state.top();
if (top.sentinel) {
parent_ids.erase(top.var.value.as_id);
state.pop();
} else if (top.var.type == PP_VARTYPE_DICTIONARY) {
parent_ids.insert(top.var.value.as_id);
top.sentinel = true;
DictionaryVar* dict_var = DictionaryVar::FromPPVar(top.var);
if (!dict_var)
return NULL;
DCHECK(top.value->GetType() == base::Value::TYPE_DICTIONARY);
base::DictionaryValue* dict_value =
static_cast<base::DictionaryValue*>(top.value);
for (DictionaryVar::KeyValueMap::const_iterator iter =
dict_var->key_value_map().begin();
iter != dict_var->key_value_map().end();
++iter) {
if (iter->second.get().type == PP_VARTYPE_UNDEFINED ||
iter->second.get().type == PP_VARTYPE_NULL) {
continue;
}
scoped_ptr<base::Value> child_value;
if (!CreateValueFromVarHelper(
parent_ids, iter->second.get(), &child_value, &state)) {
return NULL;
}
dict_value->SetWithoutPathExpansion(iter->first, child_value.release());
}
} else if (top.var.type == PP_VARTYPE_ARRAY) {
parent_ids.insert(top.var.value.as_id);
top.sentinel = true;
ArrayVar* array_var = ArrayVar::FromPPVar(top.var);
if (!array_var)
return NULL;
DCHECK(top.value->GetType() == base::Value::TYPE_LIST);
base::ListValue* list_value = static_cast<base::ListValue*>(top.value);
for (ArrayVar::ElementVector::const_iterator iter =
array_var->elements().begin();
iter != array_var->elements().end();
++iter) {
scoped_ptr<base::Value> child_value;
if (!CreateValueFromVarHelper(
parent_ids, iter->get(), &child_value, &state)) {
return NULL;
}
list_value->Append(child_value.release());
}
} else {
NOTREACHED();
return NULL;
}
}
DCHECK(parent_ids.empty());
return root_value.release();
}
PP_Var CreateVarFromValue(const base::Value& value) {
std::stack<ValueNode> state;
ScopedPPVar root_var;
if (!CreateVarFromValueHelper(value, &root_var, &state))
return PP_MakeUndefined();
while (!state.empty()) {
ValueNode top = state.top();
state.pop();
if (top.value->GetType() == base::Value::TYPE_DICTIONARY) {
const base::DictionaryValue* dict_value =
static_cast<const base::DictionaryValue*>(top.value);
DictionaryVar* dict_var = DictionaryVar::FromPPVar(top.var);
DCHECK(dict_var);
for (base::DictionaryValue::Iterator iter(*dict_value); !iter.IsAtEnd();
iter.Advance()) {
ScopedPPVar child_var;
if (!CreateVarFromValueHelper(iter.value(), &child_var, &state) ||
!dict_var->SetWithStringKey(iter.key(), child_var.get())) {
return PP_MakeUndefined();
}
}
} else if (top.value->GetType() == base::Value::TYPE_LIST) {
const base::ListValue* list_value =
static_cast<const base::ListValue*>(top.value);
ArrayVar* array_var = ArrayVar::FromPPVar(top.var);
DCHECK(array_var);
for (base::ListValue::const_iterator iter = list_value->begin();
iter != list_value->end();
++iter) {
ScopedPPVar child_var;
if (!CreateVarFromValueHelper(**iter, &child_var, &state))
return PP_MakeUndefined();
array_var->elements().push_back(child_var);
}
} else {
NOTREACHED();
return PP_MakeUndefined();
}
}
return root_var.Release();
}
base::ListValue* CreateListValueFromVarVector(const std::vector<PP_Var>& vars) {
scoped_ptr<base::ListValue> list_value(new base::ListValue());
for (std::vector<PP_Var>::const_iterator iter = vars.begin();
iter != vars.end();
++iter) {
base::Value* value = CreateValueFromVar(*iter);
if (!value)
return NULL;
list_value->Append(value);
}
return list_value.release();
}
bool CreateVarVectorFromListValue(const base::ListValue& list_value,
std::vector<PP_Var>* vars) {
if (!vars)
return false;
std::vector<ScopedPPVar> result;
result.reserve(list_value.GetSize());
for (base::ListValue::const_iterator iter = list_value.begin();
iter != list_value.end();
++iter) {
ScopedPPVar child_var(ScopedPPVar::PassRef(), CreateVarFromValue(**iter));
if (child_var.get().type == PP_VARTYPE_UNDEFINED)
return false;
result.push_back(child_var);
}
vars->clear();
vars->reserve(result.size());
for (std::vector<ScopedPPVar>::iterator iter = result.begin();
iter != result.end();
++iter) {
vars->push_back(iter->Release());
}
return true;
}
}