OLD | NEW |
(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_ |
OLD | NEW |