OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #include "chrome/browser/devtools/serialize_host_descriptions.h" |
| 6 |
| 7 #include <map> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_piece.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Returns the serialization of |root|. It expects |children[x]| to be the |
| 16 // vector of child nodes for all descendants |x| of |root|. The serialization |
| 17 // consists of taking the |representation| value of each node, starting in |
| 18 // leaves, and injecting children's representations into a ListValue under the |
| 19 // key |child_key| in the parent's |representation|. This is desctructive to the |
| 20 // representation stored with the nodes (which gets moved out of them). |
| 21 base::DictionaryValue Serialize( |
| 22 base::StringPiece child_key, |
| 23 base::DictionaryValue* root, |
| 24 const std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>>& |
| 25 children) { |
| 26 auto children_list = base::MakeUnique<base::ListValue>(); |
| 27 auto child_it = children.find(root); |
| 28 if (child_it != children.end()) { |
| 29 for (base::DictionaryValue* child : child_it->second) { |
| 30 children_list->base::Value::GetList().push_back( |
| 31 Serialize(child_key, child, children)); |
| 32 } |
| 33 } |
| 34 |
| 35 if (!children_list->empty()) |
| 36 root->Set(child_key, std::move(children_list)); |
| 37 return std::move(*root); |
| 38 } |
| 39 |
| 40 // Takes a vector of host description and converts it into: |
| 41 // |children|: a map from a host's representation to representations of its |
| 42 // children, |
| 43 // |roots|: a vector of representations of hosts with no parents, and |
| 44 // |representations|: a vector actually storing all those representations to |
| 45 // which the rest just points. |
| 46 void CreateDictionaryForest( |
| 47 std::vector<HostDescriptionNode> hosts, |
| 48 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>>* |
| 49 children, |
| 50 std::vector<base::DictionaryValue*>* roots, |
| 51 std::vector<base::DictionaryValue>* representations) { |
| 52 representations->reserve(hosts.size()); |
| 53 children->clear(); |
| 54 roots->clear(); |
| 55 representations->clear(); |
| 56 |
| 57 std::map<base::StringPiece, base::DictionaryValue*> name_to_representation; |
| 58 |
| 59 // First move the representations and map the names to them. |
| 60 for (HostDescriptionNode& node : hosts) { |
| 61 representations->push_back(std::move(node.representation)); |
| 62 name_to_representation[node.name] = &representations->back(); |
| 63 } |
| 64 |
| 65 // Now compute children. |
| 66 for (HostDescriptionNode& node : hosts) { |
| 67 base::DictionaryValue* node_rep = name_to_representation[node.name]; |
| 68 base::StringPiece parent_name = node.parent_name; |
| 69 if (parent_name.empty()) { |
| 70 roots->push_back(node_rep); |
| 71 continue; |
| 72 } |
| 73 auto node_it = name_to_representation.find(parent_name); |
| 74 if (node_it == name_to_representation.end()) { |
| 75 roots->push_back(node_rep); |
| 76 continue; |
| 77 } |
| 78 (*children)[name_to_representation[parent_name]].push_back(node_rep); |
| 79 } |
| 80 } |
| 81 |
| 82 } // namespace |
| 83 |
| 84 base::ListValue SerializeHostDescriptions( |
| 85 std::vector<HostDescriptionNode> hosts, |
| 86 base::StringPiece child_key) { |
| 87 // |representations| must outlive |children| and |roots|, which contain |
| 88 // pointers to objects in |representations|. |
| 89 std::vector<base::DictionaryValue> representations; |
| 90 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>> |
| 91 children; |
| 92 std::vector<base::DictionaryValue*> roots; |
| 93 |
| 94 CreateDictionaryForest(std::move(hosts), &children, &roots, &representations); |
| 95 |
| 96 base::ListValue list_value; |
| 97 for (auto* root : roots) { |
| 98 list_value.base::Value::GetList().push_back( |
| 99 Serialize(child_key, root, children)); |
| 100 } |
| 101 return list_value; |
| 102 } |
OLD | NEW |