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