Chromium Code Reviews| 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 <tuple> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/strings/string_piece.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Returns the serialization of |root|. It expects |children[x]| to be the | |
| 17 // vector of child nodes for all descendants |x| of |root|. The serialization | |
| 18 // consists of taking the |representation| value of each node, starting in | |
| 19 // leaves, and injecting children's representations into a ListValue under the | |
| 20 // key |child_key| in the parent's |representation|. This is desctructive to the | |
| 21 // representation stored with the nodes (which gets moved out of them). | |
| 22 base::DictionaryValue Serialize( | |
| 23 base::StringPiece child_key, | |
| 24 base::DictionaryValue* root, | |
| 25 const std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>>& | |
| 26 children) { | |
| 27 auto children_list = base::MakeUnique<base::ListValue>(); | |
| 28 auto child_it = children.find(root); | |
| 29 if (child_it != children.end()) { | |
| 30 for (base::DictionaryValue* child : child_it->second) { | |
| 31 children_list->base::Value::GetList().push_back( | |
| 32 Serialize(child_key, child, children)); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 if (!children_list->empty()) | |
| 37 root->Set(child_key, std::move(children_list)); | |
| 38 return std::move(*root); | |
| 39 } | |
| 40 | |
| 41 // Takes a vector of host description and converts it into a tuple of: | |
| 42 // (1) map from a host's representation to representations of its children. | |
| 43 // (2) a vector of representations of hosts with no parents, | |
| 44 // (3) a vector actually storing all those representations to which the rest | |
| 45 // just points. | |
| 46 std::tuple< | |
| 47 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>>, | |
| 48 std::vector<base::DictionaryValue*>, | |
| 49 std::vector<base::DictionaryValue>> | |
| 50 CreateDictionaryForest(std::vector<HostDescriptionNode> hosts) { | |
| 51 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>> | |
| 52 children; | |
| 53 std::vector<base::DictionaryValue*> roots; | |
| 54 std::vector<base::DictionaryValue> representations; | |
| 55 representations.reserve(hosts.size()); | |
| 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 return std::make_tuple(std::move(children), std::move(roots), | |
| 82 std::move(representations)); | |
| 83 } | |
| 84 | |
| 85 } // namespace | |
| 86 | |
| 87 base::ListValue SerializeHostDescriptions( | |
| 88 std::vector<HostDescriptionNode> hosts, | |
| 89 base::StringPiece child_key) { | |
| 90 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>> | |
| 91 children; | |
| 92 std::vector<base::DictionaryValue*> roots; | |
| 93 std::vector<base::DictionaryValue> representations; | |
| 94 std::tie(children, roots, representations) = | |
| 95 CreateDictionaryForest(std::move(hosts)); | |
|
dgozman
2017/04/19 22:05:14
It's usually less typing to use output params rath
vabr (Chromium)
2017/04/20 07:29:39
I thought about that, but even with output params
| |
| 96 | |
| 97 base::ListValue list_value; | |
| 98 for (auto* root : roots) { | |
| 99 list_value.base::Value::GetList().push_back( | |
|
dgozman
2017/04/19 22:05:14
I'm curious why do you have to specify the full na
vabr (Chromium)
2017/04/20 07:29:39
Currenty, both Value and ListValue define GetList.
| |
| 100 Serialize(child_key, root, children)); | |
| 101 } | |
| 102 return list_value; | |
| 103 } | |
| OLD | NEW |