From 53d399fe6415a96ea6986ec0d402a9c07da72453 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Tue, 12 Sep 2017 10:27:29 +0200 Subject: BASELINE: Update Chromium to 60.0.3112.116 and Ninja to 1.8.2 Also adds a few devtools and webui files needed for new features. Change-Id: I431976cc9f4c209d062a925ab6a5d63ec61abcfe Reviewed-by: Alexandru Croitor --- .../devtools/serialize_host_descriptions.cc | 106 +++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 chromium/chrome/browser/devtools/serialize_host_descriptions.cc (limited to 'chromium/chrome/browser/devtools/serialize_host_descriptions.cc') diff --git a/chromium/chrome/browser/devtools/serialize_host_descriptions.cc b/chromium/chrome/browser/devtools/serialize_host_descriptions.cc new file mode 100644 index 00000000000..26aa0206369 --- /dev/null +++ b/chromium/chrome/browser/devtools/serialize_host_descriptions.cc @@ -0,0 +1,106 @@ +// Copyright 2017 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 "chrome/browser/devtools/serialize_host_descriptions.h" + +#include +#include +#include + +#include "base/memory/ptr_util.h" +#include "base/strings/string_piece.h" + +namespace { + +// Returns the serialization of |root|. It expects |children[x]| to be the +// vector of child nodes for all descendants |x| of |root|. The serialization +// consists of taking the |representation| value of each node, starting in +// leaves, and injecting children's representations into a ListValue under the +// key |child_key| in the parent's |representation|. This is desctructive to the +// representation stored with the nodes (which gets moved out of them). +base::DictionaryValue Serialize( + base::StringPiece child_key, + base::DictionaryValue* root, + const std::map>& + children) { + auto children_list = base::MakeUnique(); + auto child_it = children.find(root); + if (child_it != children.end()) { + for (base::DictionaryValue* child : child_it->second) { + children_list->base::Value::GetList().push_back( + Serialize(child_key, child, children)); + } + } + + if (!children_list->empty()) + root->Set(child_key, std::move(children_list)); + return std::move(*root); +} + +// Takes a vector of host description and converts it into: +// |children|: a map from a host's representation to representations of its +// children, +// |roots|: a set of representations of hosts with no parents, and +// |representations|: a vector actually storing all those representations to +// which the rest just points. +void CreateDictionaryForest( + std::vector hosts, + std::map>* + children, + std::unordered_set* roots, + std::vector* representations) { + representations->reserve(hosts.size()); + children->clear(); + roots->clear(); + representations->clear(); + + std::map name_to_representation; + + // First move the representations and map the names to them. + for (HostDescriptionNode& node : hosts) { + representations->push_back(std::move(node.representation)); + // If there are multiple nodes with the same name, subsequent insertions + // will be ignored, so only the first node with a given name will be + // referenced by |roots| and |children|. + name_to_representation.emplace(node.name, &representations->back()); + } + + // Now compute children. + for (HostDescriptionNode& node : hosts) { + base::DictionaryValue* node_rep = name_to_representation[node.name]; + base::StringPiece parent_name = node.parent_name; + if (parent_name.empty()) { + roots->insert(node_rep); + continue; + } + auto node_it = name_to_representation.find(parent_name); + if (node_it == name_to_representation.end()) { + roots->insert(node_rep); + continue; + } + (*children)[name_to_representation[parent_name]].push_back(node_rep); + } +} + +} // namespace + +base::ListValue SerializeHostDescriptions( + std::vector hosts, + base::StringPiece child_key) { + // |representations| must outlive |children| and |roots|, which contain + // pointers to objects in |representations|. + std::vector representations; + std::map> + children; + std::unordered_set roots; + + CreateDictionaryForest(std::move(hosts), &children, &roots, &representations); + + base::ListValue list_value; + for (auto* root : roots) { + list_value.base::Value::GetList().push_back( + Serialize(child_key, root, children)); + } + return list_value; +} -- cgit v1.2.3