OLD | NEW |
1 // Copyright 2016 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 #ifndef HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_ | 5 #ifndef HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_ |
6 #define HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_ | 6 #define HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_ |
7 | 7 |
8 #include <unordered_map> | 8 #include <unordered_map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "headless/public/devtools/domains/css.h" | 12 #include "headless/public/devtools/domains/css.h" |
13 #include "headless/public/devtools/domains/dom.h" | 13 #include "headless/public/devtools/domains/dom.h" |
14 | 14 |
15 namespace headless { | 15 namespace headless { |
16 class HeadlessDevToolsClient; | 16 class HeadlessDevToolsClient; |
17 | 17 |
18 // A utility class for extracting information from the DOM via DevTools. In | 18 // A utility class for extracting information from the DOM via DevTools. In |
19 // addition, it also extracts details of bounding boxes and layout text (NB the | 19 // addition, it also extracts details of bounding boxes and layout text (NB the |
20 // exact layout should not be regarded as stable, it's subject to change without | 20 // exact layout should not be regarded as stable, it's subject to change without |
21 // notice). | 21 // notice). |
22 class DomTreeExtractor { | 22 class FlatDomTreeExtractor { |
23 public: | 23 public: |
24 explicit DomTreeExtractor(HeadlessDevToolsClient* devtools_client); | 24 explicit FlatDomTreeExtractor(HeadlessDevToolsClient* devtools_client); |
25 ~DomTreeExtractor(); | 25 ~FlatDomTreeExtractor(); |
26 | 26 |
27 using NodeId = int; | 27 using NodeId = int; |
28 using Index = size_t; | 28 using Index = size_t; |
29 | 29 |
30 class DomTree { | 30 class DomTree { |
31 public: | 31 public: |
32 DomTree(); | 32 DomTree(); |
33 DomTree(DomTree&& other); | 33 DomTree(DomTree&& other); |
34 ~DomTree(); | 34 ~DomTree(); |
35 | 35 |
36 // Flattened dom tree. The root node is always the first entry. | 36 // Flattened dom tree. The root node is always the first entry. |
37 std::vector<const dom::Node*> dom_nodes_; | 37 std::vector<const dom::Node*> dom_nodes_; |
38 | 38 |
39 // Map of node IDs to indexes into |dom_nodes_|. | 39 // Map of node IDs to indexes into |dom_nodes_|. |
40 std::unordered_map<NodeId, Index> node_id_to_index_; | 40 std::unordered_map<NodeId, Index> node_id_to_index_; |
41 | 41 |
42 std::vector<const css::LayoutTreeNode*> layout_tree_nodes_; | 42 std::vector<const css::LayoutTreeNode*> layout_tree_nodes_; |
43 | 43 |
44 std::vector<const css::ComputedStyle*> computed_styles_; | 44 std::vector<const css::ComputedStyle*> computed_styles_; |
45 | 45 |
46 private: | 46 private: |
47 friend class DomTreeExtractor; | 47 friend class FlatDomTreeExtractor; |
48 | 48 |
49 // Owns the raw pointers in |dom_nodes_|. | 49 // Owns the raw pointers in |dom_nodes_|. |
50 std::unique_ptr<dom::GetDocumentResult> document_result_; | 50 std::unique_ptr<dom::GetFlattenedDocumentResult> document_result_; |
51 | 51 |
52 // Owns the raw pointers in |layout_tree_nodes_|. | 52 // Owns the raw pointers in |layout_tree_nodes_|. |
53 std::unique_ptr<css::GetLayoutTreeAndStylesResult> | 53 std::unique_ptr<css::GetLayoutTreeAndStylesResult> |
54 layout_tree_and_styles_result_; | 54 layout_tree_and_styles_result_; |
55 | 55 |
56 DISALLOW_COPY_AND_ASSIGN(DomTree); | 56 DISALLOW_COPY_AND_ASSIGN(DomTree); |
57 }; | 57 }; |
58 | 58 |
59 using DomResultCB = base::Callback<void(DomTree)>; | 59 using DomResultCB = base::Callback<void(DomTree)>; |
60 | 60 |
61 // Extracts all nodes from the DOM. This is an asynchronous operation and | 61 // Extracts all nodes from the DOM. This is an asynchronous operation and |
62 // it's an error to call ExtractDom while a previous operation is in flight. | 62 // it's an error to call ExtractDom while a previous operation is in flight. |
63 void ExtractDomTree(const std::vector<std::string>& css_style_whitelist, | 63 void ExtractDomTree(const std::vector<std::string>& css_style_whitelist, |
64 DomResultCB callback); | 64 DomResultCB callback); |
65 | 65 |
66 private: | 66 private: |
67 void OnDocumentFetched(std::unique_ptr<dom::GetDocumentResult> result); | 67 void OnDocumentFetched( |
| 68 std::unique_ptr<dom::GetFlattenedDocumentResult> result); |
68 | 69 |
69 void OnLayoutTreeAndStylesFetched( | 70 void OnLayoutTreeAndStylesFetched( |
70 std::unique_ptr<css::GetLayoutTreeAndStylesResult> result); | 71 std::unique_ptr<css::GetLayoutTreeAndStylesResult> result); |
71 | 72 |
72 void MaybeExtractDomTree(); | 73 void MaybeExtractDomTree(); |
73 void EnumerateNodes(const dom::Node* node); | 74 void EnumerateNodes(const dom::Node* node); |
74 void ExtractLayoutTreeNodes(); | 75 void ExtractLayoutTreeNodes(); |
75 void ExtractComputedStyles(); | 76 void ExtractComputedStyles(); |
76 | 77 |
77 DomResultCB callback_; | 78 DomResultCB callback_; |
78 DomTree dom_tree_; | 79 DomTree dom_tree_; |
79 bool work_in_progress_; | 80 bool work_in_progress_; |
80 HeadlessDevToolsClient* devtools_client_; // NOT OWNED | 81 HeadlessDevToolsClient* devtools_client_; // NOT OWNED |
81 base::WeakPtrFactory<DomTreeExtractor> weak_factory_; | 82 base::WeakPtrFactory<FlatDomTreeExtractor> weak_factory_; |
82 | 83 |
83 DISALLOW_COPY_AND_ASSIGN(DomTreeExtractor); | 84 DISALLOW_COPY_AND_ASSIGN(FlatDomTreeExtractor); |
84 }; | 85 }; |
85 | 86 |
86 } // namespace headless | 87 } // namespace headless |
87 | 88 |
88 #endif // HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_ | 89 #endif // HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_ |
OLD | NEW |