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

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

Issue 2385653003: Add a utility class for extracting details of the DOM (Closed)
Patch Set: Nits Created 4 years, 2 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
« no previous file with comments | « headless/BUILD.gn ('k') | headless/public/util/dom_tree_extractor.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #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/domains/css.h"
13 #include "headless/public/domains/dom.h"
14
15 namespace headless {
16 class HeadlessDevToolsClient;
17
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
20 // exact layout should not be regarded as stable, it's subject to change without
21 // notice).
22 class DomTreeExtractor {
23 public:
24 explicit DomTreeExtractor(HeadlessDevToolsClient* devtools_client);
25 ~DomTreeExtractor();
26
27 using NodeId = int;
28 using Index = size_t;
29
30 class DomTree {
31 public:
32 DomTree();
33 DomTree(DomTree&& other);
34 ~DomTree();
35
36 // Flattened dom tree. The root node is always the first entry.
37 std::vector<const dom::Node*> dom_nodes_;
38
39 // Map of node IDs to indexes into |dom_nodes_|.
40 std::unordered_map<NodeId, Index> node_id_to_index_;
41
42 std::vector<const css::LayoutTreeNode*> layout_tree_nodes_;
43
44 std::vector<const css::ComputedStyle*> computed_styles_;
45
46 private:
47 friend class DomTreeExtractor;
48
49 // Owns the raw pointers in |dom_nodes_|.
50 std::unique_ptr<dom::GetDocumentResult> document_result_;
51
52 // Owns the raw pointers in |layout_tree_nodes_|.
53 std::unique_ptr<css::GetLayoutTreeAndStylesResult>
54 layout_tree_and_styles_result_;
55
56 DISALLOW_COPY_AND_ASSIGN(DomTree);
57 };
58
59 using DomResultCB = base::Callback<void(DomTree)>;
60
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.
63 void ExtractDomTree(const std::vector<std::string>& css_style_whitelist,
64 DomResultCB callback);
65
66 private:
67 void OnDocumentFetched(std::unique_ptr<dom::GetDocumentResult> result);
68
69 void OnLayoutTreeAndStylesFetched(
70 std::unique_ptr<css::GetLayoutTreeAndStylesResult> result);
71
72 void MaybeExtractDomTree();
73 void EnumerateNodes(const dom::Node* node);
74 void ExtractLayoutTreeNodes();
75 void ExtractComputedStyles();
76
77 DomResultCB callback_;
78 DomTree dom_tree_;
79 bool work_in_progress_;
80 HeadlessDevToolsClient* devtools_client_; // NOT OWNED
81 base::WeakPtrFactory<DomTreeExtractor> weak_factory_;
82
83 DISALLOW_COPY_AND_ASSIGN(DomTreeExtractor);
84 };
85
86 } // namespace headless
87
88 #endif // HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_
OLDNEW
« no previous file with comments | « headless/BUILD.gn ('k') | headless/public/util/dom_tree_extractor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698