Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1234)

Side by Side Diff: headless/public/util/dom_tree_extractor.cc

Issue 2882193002: [devtools] Add DOMSnapshot domain for dom+layout+style snapshots. (Closed)
Patch Set: move to CSS domain, remove getLayoutTreeAndStyles Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "headless/public/util/dom_tree_extractor.h"
6
7 #include "base/bind.h"
8 #include "base/json/json_writer.h"
9 #include "headless/public/headless_devtools_client.h"
10
11 namespace headless {
12
13 DomTreeExtractor::DomTreeExtractor(HeadlessDevToolsClient* devtools_client)
14 : work_in_progress_(false),
15 devtools_client_(devtools_client),
16 weak_factory_(this) {}
17
18 DomTreeExtractor::~DomTreeExtractor() {}
19
20 void DomTreeExtractor::ExtractDomTree(
21 const std::vector<std::string>& css_style_whitelist,
22 DomResultCB callback) {
23 DCHECK(!work_in_progress_);
24 work_in_progress_ = true;
25
26 callback_ = std::move(callback);
27
28 devtools_client_->GetDOM()->GetDocument(
29 dom::GetDocumentParams::Builder().SetDepth(-1).SetPierce(true).Build(),
30 base::Bind(&DomTreeExtractor::OnDocumentFetched,
31 weak_factory_.GetWeakPtr()));
32
33 devtools_client_->GetCSS()->GetExperimental()->GetLayoutTreeAndStyles(
34 css::GetLayoutTreeAndStylesParams::Builder()
35 .SetComputedStyleWhitelist(css_style_whitelist)
36 .Build(),
37 base::Bind(&DomTreeExtractor::OnLayoutTreeAndStylesFetched,
38 weak_factory_.GetWeakPtr()));
39 }
40
41 void DomTreeExtractor::OnDocumentFetched(
42 std::unique_ptr<dom::GetDocumentResult> result) {
43 dom_tree_.document_result_ = std::move(result);
44 MaybeExtractDomTree();
45 }
46
47 void DomTreeExtractor::OnLayoutTreeAndStylesFetched(
48 std::unique_ptr<css::GetLayoutTreeAndStylesResult> result) {
49 dom_tree_.layout_tree_and_styles_result_ = std::move(result);
50 MaybeExtractDomTree();
51 }
52
53 void DomTreeExtractor::MaybeExtractDomTree() {
54 if (dom_tree_.document_result_ && dom_tree_.layout_tree_and_styles_result_) {
55 EnumerateNodes(dom_tree_.document_result_->GetRoot());
56 ExtractLayoutTreeNodes();
57 ExtractComputedStyles();
58
59 work_in_progress_ = false;
60
61 callback_.Run(std::move(dom_tree_));
62 }
63 }
64
65 void DomTreeExtractor::EnumerateNodes(const dom::Node* node) {
66 // Allocate an index and record the node pointer.
67 size_t index = dom_tree_.node_id_to_index_.size();
68 dom_tree_.node_id_to_index_[node->GetNodeId()] = index;
69 dom_tree_.dom_nodes_.push_back(node);
70
71 if (node->HasContentDocument())
72 EnumerateNodes(node->GetContentDocument());
73
74 if (node->HasChildren()) {
75 for (const std::unique_ptr<dom::Node>& child : *node->GetChildren()) {
76 EnumerateNodes(child.get());
77 }
78 }
79 }
80
81 void DomTreeExtractor::ExtractLayoutTreeNodes() {
82 dom_tree_.layout_tree_nodes_.reserve(
83 dom_tree_.layout_tree_and_styles_result_->GetLayoutTreeNodes()->size());
84
85 for (const std::unique_ptr<css::LayoutTreeNode>& layout_node :
86 *dom_tree_.layout_tree_and_styles_result_->GetLayoutTreeNodes()) {
87 std::unordered_map<NodeId, size_t>::const_iterator it =
88 dom_tree_.node_id_to_index_.find(layout_node->GetNodeId());
89 DCHECK(it != dom_tree_.node_id_to_index_.end());
90 dom_tree_.layout_tree_nodes_.push_back(layout_node.get());
91 }
92 }
93
94 void DomTreeExtractor::ExtractComputedStyles() {
95 dom_tree_.computed_styles_.reserve(
96 dom_tree_.layout_tree_and_styles_result_->GetComputedStyles()->size());
97
98 for (const std::unique_ptr<css::ComputedStyle>& computed_style :
99 *dom_tree_.layout_tree_and_styles_result_->GetComputedStyles()) {
100 dom_tree_.computed_styles_.push_back(computed_style.get());
101 }
102 }
103
104 DomTreeExtractor::DomTree::DomTree() {}
105 DomTreeExtractor::DomTree::~DomTree() {}
106
107 DomTreeExtractor::DomTree::DomTree(DomTree&& other) = default;
108
109 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698