Index: ppapi/shared_impl/var_graph.cc |
diff --git a/ppapi/shared_impl/var_graph.cc b/ppapi/shared_impl/var_graph.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f1ad875423a030620b9f65c544584c971f36480f |
--- /dev/null |
+++ b/ppapi/shared_impl/var_graph.cc |
@@ -0,0 +1,66 @@ |
+// 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_graph.h" |
+ |
+#include <stack> |
+ |
+#include "base/stl_util.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/var.h" |
+#include "ppapi/shared_impl/var_tracker.h" |
+ |
+namespace ppapi { |
+ |
+// static |
+bool VarGraph::Create(const PP_Var& start_node, VarGraph* var_graph) { |
+ std::stack<PP_Var> stack; |
+ stack.push(start_node); |
+ |
+ // Traverse the PP_Var graph with DFS and add each reference node to |nodes| |
+ // exactly once. |
+ while (!stack.empty()) { |
+ PP_Var current = stack.top(); |
+ stack.pop(); |
+ if (!PpapiGlobals::Get()->GetVarTracker()->GetVar(current.value.as_id) || |
+ ContainsKey(var_graph->id_map, current.value.as_id)) { |
+ continue; |
+ } |
+ var_graph->nodes.push_back(current); |
dmichael (off chromium)
2013/04/12 22:40:38
Why have the vector at all? You could just stick P
raymes
2013/04/14 16:32:57
This has changed.
|
+ var_graph->id_map[current.value.as_id] = var_graph->nodes.size() - 1; |
+ |
+ // Add child nodes to the stack. |
+ if (current.type == PP_VARTYPE_ARRAY) { |
+ ArrayVar* array_var = ArrayVar::FromPPVar(current); |
+ if (!array_var) { |
+ NOTREACHED(); |
+ return false; |
+ } |
+ for (ArrayVar::ElementVector::const_iterator iter = |
+ array_var->elements().begin(); |
+ iter != array_var->elements().end(); |
+ ++iter) { |
+ stack.push(iter->get()); |
+ } |
+ } else if (current.type == PP_VARTYPE_DICTIONARY) { |
+ DictionaryVar* dict_var = DictionaryVar::FromPPVar(current); |
+ if (!dict_var) { |
+ NOTREACHED(); |
+ return false; |
+ } |
+ for (DictionaryVar::KeyValueMap::const_iterator iter = |
+ dict_var->key_value_map().begin(); |
+ iter != dict_var->key_value_map().end(); |
+ ++iter) { |
+ stack.push(iter->second.get()); |
+ } |
+ } |
+ } |
+ DCHECK(var_graph->nodes.size() == var_graph->id_map.size()); |
+ return true; |
+} |
+ |
+} // namespace ppapi |