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 "headless/public/util/flat_dom_tree_extractor.h" | 5 #include "headless/public/util/flat_dom_tree_extractor.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "headless/public/headless_devtools_client.h" | 9 #include "headless/public/headless_devtools_client.h" |
10 | 10 |
11 namespace headless { | 11 namespace headless { |
12 | 12 |
13 FlatDomTreeExtractor::FlatDomTreeExtractor( | 13 FlatDomTreeExtractor::FlatDomTreeExtractor( |
14 HeadlessDevToolsClient* devtools_client) | 14 HeadlessDevToolsClient* devtools_client) |
15 : work_in_progress_(false), | 15 : work_in_progress_(false), |
16 devtools_client_(devtools_client), | 16 devtools_client_(devtools_client), |
17 weak_factory_(this) {} | 17 weak_factory_(this) {} |
18 | 18 |
19 FlatDomTreeExtractor::~FlatDomTreeExtractor() {} | 19 FlatDomTreeExtractor::~FlatDomTreeExtractor() {} |
20 | 20 |
21 void FlatDomTreeExtractor::ExtractDomTree( | 21 void FlatDomTreeExtractor::ExtractDomTree( |
22 const std::vector<std::string>& css_style_whitelist, | 22 const std::vector<std::string>& css_style_whitelist, |
23 DomResultCB callback) { | 23 DomResultCB callback) { |
24 DCHECK(!work_in_progress_); | 24 DCHECK(!work_in_progress_); |
25 work_in_progress_ = true; | 25 work_in_progress_ = true; |
26 | 26 |
27 callback_ = std::move(callback); | 27 callback_ = std::move(callback); |
28 | 28 |
| 29 // GetDocumentSnapshot requires DOM domain to be enabled. |
29 devtools_client_->GetDOM()->Enable(); | 30 devtools_client_->GetDOM()->Enable(); |
30 devtools_client_->GetDOM()->GetFlattenedDocument( | 31 |
31 dom::GetFlattenedDocumentParams::Builder() | 32 devtools_client_->GetCSS()->GetExperimental()->GetDocumentSnapshot( |
32 .SetDepth(-1) | 33 css::GetDocumentSnapshotParams::Builder() |
| 34 .SetComputedStyleWhitelist(css_style_whitelist) |
| 35 .SetDomDepth(-1) |
33 .SetPierce(true) | 36 .SetPierce(true) |
34 .Build(), | 37 .Build(), |
35 base::Bind(&FlatDomTreeExtractor::OnDocumentFetched, | 38 base::Bind(&FlatDomTreeExtractor::OnDocumentSnapshotResult, |
36 weak_factory_.GetWeakPtr())); | |
37 | |
38 devtools_client_->GetCSS()->GetExperimental()->GetLayoutTreeAndStyles( | |
39 css::GetLayoutTreeAndStylesParams::Builder() | |
40 .SetComputedStyleWhitelist(css_style_whitelist) | |
41 .Build(), | |
42 base::Bind(&FlatDomTreeExtractor::OnLayoutTreeAndStylesFetched, | |
43 weak_factory_.GetWeakPtr())); | 39 weak_factory_.GetWeakPtr())); |
44 } | 40 } |
45 | 41 |
46 void FlatDomTreeExtractor::OnDocumentFetched( | 42 void FlatDomTreeExtractor::OnDocumentSnapshotResult( |
47 std::unique_ptr<dom::GetFlattenedDocumentResult> result) { | 43 std::unique_ptr<css::GetDocumentSnapshotResult> result) { |
48 dom_tree_.document_result_ = std::move(result); | 44 dom_tree_.document_snapshot_result_ = std::move(result); |
49 MaybeExtractDomTree(); | |
50 } | |
51 | 45 |
52 void FlatDomTreeExtractor::OnLayoutTreeAndStylesFetched( | 46 for (const std::unique_ptr<headless::dom::Node>& node : |
53 std::unique_ptr<css::GetLayoutTreeAndStylesResult> result) { | 47 *dom_tree_.document_snapshot_result_->GetDomNodes()) { |
54 dom_tree_.layout_tree_and_styles_result_ = std::move(result); | 48 EnumerateNodes(node.get()); |
55 MaybeExtractDomTree(); | 49 } |
56 } | 50 ExtractLayoutTreeNodes(); |
| 51 ExtractComputedStyles(); |
| 52 devtools_client_->GetDOM()->Disable(); |
57 | 53 |
58 void FlatDomTreeExtractor::MaybeExtractDomTree() { | 54 work_in_progress_ = false; |
59 if (dom_tree_.document_result_ && dom_tree_.layout_tree_and_styles_result_) { | |
60 for (const std::unique_ptr<headless::dom::Node>& node : | |
61 *dom_tree_.document_result_->GetNodes()) { | |
62 EnumerateNodes(node.get()); | |
63 } | |
64 ExtractLayoutTreeNodes(); | |
65 ExtractComputedStyles(); | |
66 devtools_client_->GetDOM()->Disable(); | |
67 | 55 |
68 work_in_progress_ = false; | 56 callback_.Run(std::move(dom_tree_)); |
69 | |
70 callback_.Run(std::move(dom_tree_)); | |
71 } | |
72 } | 57 } |
73 | 58 |
74 void FlatDomTreeExtractor::EnumerateNodes(const dom::Node* node) { | 59 void FlatDomTreeExtractor::EnumerateNodes(const dom::Node* node) { |
75 // Allocate an index and record the node pointer. | 60 // Allocate an index and record the node pointer. |
76 size_t index = dom_tree_.node_id_to_index_.size(); | 61 size_t index = dom_tree_.node_id_to_index_.size(); |
77 dom_tree_.node_id_to_index_[node->GetNodeId()] = index; | 62 dom_tree_.node_id_to_index_[node->GetNodeId()] = index; |
78 dom_tree_.dom_nodes_.push_back(node); | 63 dom_tree_.dom_nodes_.push_back(node); |
79 | 64 |
80 if (node->HasContentDocument()) | 65 if (node->HasContentDocument()) |
81 EnumerateNodes(node->GetContentDocument()); | 66 EnumerateNodes(node->GetContentDocument()); |
82 | 67 |
83 DCHECK(!node->HasChildren() || node->GetChildren()->empty()); | 68 DCHECK(!node->HasChildren() || node->GetChildren()->empty()); |
84 } | 69 } |
85 | 70 |
86 void FlatDomTreeExtractor::ExtractLayoutTreeNodes() { | 71 void FlatDomTreeExtractor::ExtractLayoutTreeNodes() { |
87 dom_tree_.layout_tree_nodes_.reserve( | 72 dom_tree_.layout_tree_nodes_.reserve( |
88 dom_tree_.layout_tree_and_styles_result_->GetLayoutTreeNodes()->size()); | 73 dom_tree_.document_snapshot_result_->GetLayoutTreeNodes()->size()); |
89 | 74 |
90 for (const std::unique_ptr<css::LayoutTreeNode>& layout_node : | 75 for (const std::unique_ptr<css::LayoutTreeNode>& layout_node : |
91 *dom_tree_.layout_tree_and_styles_result_->GetLayoutTreeNodes()) { | 76 *dom_tree_.document_snapshot_result_->GetLayoutTreeNodes()) { |
92 std::unordered_map<NodeId, size_t>::const_iterator it = | 77 std::unordered_map<NodeId, size_t>::const_iterator it = |
93 dom_tree_.node_id_to_index_.find(layout_node->GetNodeId()); | 78 dom_tree_.node_id_to_index_.find(layout_node->GetNodeId()); |
94 DCHECK(it != dom_tree_.node_id_to_index_.end()); | 79 DCHECK(it != dom_tree_.node_id_to_index_.end()); |
95 dom_tree_.layout_tree_nodes_.push_back(layout_node.get()); | 80 dom_tree_.layout_tree_nodes_.push_back(layout_node.get()); |
96 } | 81 } |
97 } | 82 } |
98 | 83 |
99 void FlatDomTreeExtractor::ExtractComputedStyles() { | 84 void FlatDomTreeExtractor::ExtractComputedStyles() { |
100 dom_tree_.computed_styles_.reserve( | 85 dom_tree_.computed_styles_.reserve( |
101 dom_tree_.layout_tree_and_styles_result_->GetComputedStyles()->size()); | 86 dom_tree_.document_snapshot_result_->GetComputedStyles()->size()); |
102 | 87 |
103 for (const std::unique_ptr<css::ComputedStyle>& computed_style : | 88 for (const std::unique_ptr<css::ComputedStyle>& computed_style : |
104 *dom_tree_.layout_tree_and_styles_result_->GetComputedStyles()) { | 89 *dom_tree_.document_snapshot_result_->GetComputedStyles()) { |
105 dom_tree_.computed_styles_.push_back(computed_style.get()); | 90 dom_tree_.computed_styles_.push_back(computed_style.get()); |
106 } | 91 } |
107 } | 92 } |
108 | 93 |
109 FlatDomTreeExtractor::DomTree::DomTree() {} | 94 FlatDomTreeExtractor::DomTree::DomTree() {} |
110 FlatDomTreeExtractor::DomTree::~DomTree() {} | 95 FlatDomTreeExtractor::DomTree::~DomTree() {} |
111 | 96 |
112 FlatDomTreeExtractor::DomTree::DomTree(DomTree&& other) = default; | 97 FlatDomTreeExtractor::DomTree::DomTree(DomTree&& other) = default; |
113 | 98 |
114 } // namespace headless | 99 } // namespace headless |
OLD | NEW |