| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/devtools/serialize_host_descriptions.h" | 5 #include "chrome/browser/devtools/serialize_host_descriptions.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <unordered_set> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 // Returns the serialization of |root|. It expects |children[x]| to be the | 16 // 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 // 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 // consists of taking the |representation| value of each node, starting in |
| (...skipping 15 matching lines...) Expand all Loading... |
| 33 } | 34 } |
| 34 | 35 |
| 35 if (!children_list->empty()) | 36 if (!children_list->empty()) |
| 36 root->Set(child_key, std::move(children_list)); | 37 root->Set(child_key, std::move(children_list)); |
| 37 return std::move(*root); | 38 return std::move(*root); |
| 38 } | 39 } |
| 39 | 40 |
| 40 // Takes a vector of host description and converts it into: | 41 // 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|: a map from a host's representation to representations of its |
| 42 // children, | 43 // children, |
| 43 // |roots|: a vector of representations of hosts with no parents, and | 44 // |roots|: a set of representations of hosts with no parents, and |
| 44 // |representations|: a vector actually storing all those representations to | 45 // |representations|: a vector actually storing all those representations to |
| 45 // which the rest just points. | 46 // which the rest just points. |
| 46 void CreateDictionaryForest( | 47 void CreateDictionaryForest( |
| 47 std::vector<HostDescriptionNode> hosts, | 48 std::vector<HostDescriptionNode> hosts, |
| 48 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>>* | 49 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>>* |
| 49 children, | 50 children, |
| 50 std::vector<base::DictionaryValue*>* roots, | 51 std::unordered_set<base::DictionaryValue*>* roots, |
| 51 std::vector<base::DictionaryValue>* representations) { | 52 std::vector<base::DictionaryValue>* representations) { |
| 52 representations->reserve(hosts.size()); | 53 representations->reserve(hosts.size()); |
| 53 children->clear(); | 54 children->clear(); |
| 54 roots->clear(); | 55 roots->clear(); |
| 55 representations->clear(); | 56 representations->clear(); |
| 56 | 57 |
| 57 std::map<base::StringPiece, base::DictionaryValue*> name_to_representation; | 58 std::map<base::StringPiece, base::DictionaryValue*> name_to_representation; |
| 58 | 59 |
| 59 // First move the representations and map the names to them. | 60 // First move the representations and map the names to them. |
| 60 for (HostDescriptionNode& node : hosts) { | 61 for (HostDescriptionNode& node : hosts) { |
| 61 representations->push_back(std::move(node.representation)); | 62 representations->push_back(std::move(node.representation)); |
| 62 name_to_representation[node.name] = &representations->back(); | 63 // If there are multiple nodes with the same name, subsequent insertions |
| 64 // will be ignored, so only the first node with a given name will be |
| 65 // referenced by |roots| and |children|. |
| 66 name_to_representation.emplace(node.name, &representations->back()); |
| 63 } | 67 } |
| 64 | 68 |
| 65 // Now compute children. | 69 // Now compute children. |
| 66 for (HostDescriptionNode& node : hosts) { | 70 for (HostDescriptionNode& node : hosts) { |
| 67 base::DictionaryValue* node_rep = name_to_representation[node.name]; | 71 base::DictionaryValue* node_rep = name_to_representation[node.name]; |
| 68 base::StringPiece parent_name = node.parent_name; | 72 base::StringPiece parent_name = node.parent_name; |
| 69 if (parent_name.empty()) { | 73 if (parent_name.empty()) { |
| 70 roots->push_back(node_rep); | 74 roots->insert(node_rep); |
| 71 continue; | 75 continue; |
| 72 } | 76 } |
| 73 auto node_it = name_to_representation.find(parent_name); | 77 auto node_it = name_to_representation.find(parent_name); |
| 74 if (node_it == name_to_representation.end()) { | 78 if (node_it == name_to_representation.end()) { |
| 75 roots->push_back(node_rep); | 79 roots->insert(node_rep); |
| 76 continue; | 80 continue; |
| 77 } | 81 } |
| 78 (*children)[name_to_representation[parent_name]].push_back(node_rep); | 82 (*children)[name_to_representation[parent_name]].push_back(node_rep); |
| 79 } | 83 } |
| 80 } | 84 } |
| 81 | 85 |
| 82 } // namespace | 86 } // namespace |
| 83 | 87 |
| 84 base::ListValue SerializeHostDescriptions( | 88 base::ListValue SerializeHostDescriptions( |
| 85 std::vector<HostDescriptionNode> hosts, | 89 std::vector<HostDescriptionNode> hosts, |
| 86 base::StringPiece child_key) { | 90 base::StringPiece child_key) { |
| 87 // |representations| must outlive |children| and |roots|, which contain | 91 // |representations| must outlive |children| and |roots|, which contain |
| 88 // pointers to objects in |representations|. | 92 // pointers to objects in |representations|. |
| 89 std::vector<base::DictionaryValue> representations; | 93 std::vector<base::DictionaryValue> representations; |
| 90 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>> | 94 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>> |
| 91 children; | 95 children; |
| 92 std::vector<base::DictionaryValue*> roots; | 96 std::unordered_set<base::DictionaryValue*> roots; |
| 93 | 97 |
| 94 CreateDictionaryForest(std::move(hosts), &children, &roots, &representations); | 98 CreateDictionaryForest(std::move(hosts), &children, &roots, &representations); |
| 95 | 99 |
| 96 base::ListValue list_value; | 100 base::ListValue list_value; |
| 97 for (auto* root : roots) { | 101 for (auto* root : roots) { |
| 98 list_value.base::Value::GetList().push_back( | 102 list_value.base::Value::GetList().push_back( |
| 99 Serialize(child_key, root, children)); | 103 Serialize(child_key, root, children)); |
| 100 } | 104 } |
| 101 return list_value; | 105 return list_value; |
| 102 } | 106 } |
| OLD | NEW |