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..a54c7263f00582c02fe6404ada01f0bcd4f391a5 |
--- /dev/null |
+++ b/ppapi/shared_impl/var_value_conversions.cc |
@@ -0,0 +1,298 @@ |
+// 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 <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/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 { |
+ |
+// In CreateValueFromVar(), a stack is used to keep track of conversion progress |
+// of array and dictionary vars. VarNode represents elements of that stack. |
+struct VarNode { |
+ VarNode(const PP_Var& in_var, base::Value* in_value) |
+ : var(in_var), |
+ value(in_value), |
+ sentinel(false) { |
+ } |
+ |
+ // This object doesn't hold a reference to it. |
+ PP_Var var; |
+ // It is not owned by this object. |
+ base::Value* value; |
+ // When this is set to true for a node in the stack, it means that we have |
+ // finished processing the node itself. However, we keep it in the stack as |
+ // a sentinel. When it becomes the top element of the stack again, we know |
+ // that we have processed all the descendants of this node. |
+ bool sentinel; |
+}; |
+ |
+// In CreateVarFromValue(), a stack is used to keep track of conversion progress |
+// of list and dictionary values. ValueNode represents elements of that stack. |
+struct ValueNode { |
+ ValueNode(const PP_Var& in_var, const base::Value* in_value) |
+ : var(in_var), |
+ value(in_value) { |
+ } |
+ |
+ // This object doesn't hold a reference to it. |
+ PP_Var var; |
+ // It is not owned by this object. |
+ const base::Value* value; |
+}; |
+ |
+// Helper function for CreateValueFromVar(). It only looks at |var| but not its |
+// descendants. The conversion result is stored in |value|. If |var| is array or |
+// dictionary, a new node is pushed onto |state|. |
+// |
+// Returns false on failure. |
+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: { |
+ // TODO(yzshen): Implement it once array var is supported. |
+ return false; |
+ } |
+ case PP_VARTYPE_DICTIONARY: { |
+ if (ContainsKey(parent_ids, var.value.as_id)) { |
+ // A circular reference is found. |
+ return false; |
+ } |
+ |
+ value->reset(new base::DictionaryValue()); |
dmichael (off chromium)
2013/03/18 16:21:05
I think it's worth noting somewhere (probably the
yzshen1
2013/03/18 17:24:09
Done.
|
+ 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; |
+ } |
+ } |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
+// Helper function for CreateVarFromValue(). It only looks at |value| but not |
+// its descendants. The conversion result is stored in |var|. If |value| is list |
+// or dictionary, a new node is pushed onto |state|. |
+// |
+// Returns false on failure. |
+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: { |
+ // TODO(yzshen): Add support once array var is supported. |
+ return false; |
+ } |
+ } |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
+} // namespace |
+ |
+base::Value* CreateValueFromVar(const PP_Var& var) { |
+ // Used to detect circular references. |
+ 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) { |
+ // Skip the key-value pair if the value is undefined. |
+ if (iter->second.get().type == PP_VARTYPE_UNDEFINED) |
+ 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 { |
+ 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 { |
+ NOTREACHED(); |
+ return PP_MakeUndefined(); |
+ } |
+ } |
+ |
+ return root_var.Release(); |
+} |
+} // namespace ppapi |
+ |