| 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/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 // TODO(alexclarke): Remove in favor of one using DOM.getFlattenedDocument. | |
| 24 class HEADLESS_EXPORT DomTreeExtractor { | |
| 25 public: | |
| 26 explicit DomTreeExtractor(HeadlessDevToolsClient* devtools_client); | |
| 27 ~DomTreeExtractor(); | |
| 28 | |
| 29 using NodeId = int; | |
| 30 using Index = size_t; | |
| 31 | |
| 32 class HEADLESS_EXPORT DomTree { | |
| 33 public: | |
| 34 DomTree(); | |
| 35 DomTree(DomTree&& other); | |
| 36 ~DomTree(); | |
| 37 | |
| 38 // Flattened dom tree. The root node is always the first entry. | |
| 39 std::vector<const dom::Node*> dom_nodes_; | |
| 40 | |
| 41 // Map of node IDs to indexes into |dom_nodes_|. | |
| 42 std::unordered_map<NodeId, Index> node_id_to_index_; | |
| 43 | |
| 44 std::vector<const css::LayoutTreeNode*> layout_tree_nodes_; | |
| 45 | |
| 46 std::vector<const css::ComputedStyle*> computed_styles_; | |
| 47 | |
| 48 private: | |
| 49 friend class DomTreeExtractor; | |
| 50 | |
| 51 // Owns the raw pointers in |dom_nodes_|. | |
| 52 std::unique_ptr<dom::GetDocumentResult> document_result_; | |
| 53 | |
| 54 // Owns the raw pointers in |layout_tree_nodes_|. | |
| 55 std::unique_ptr<css::GetLayoutTreeAndStylesResult> | |
| 56 layout_tree_and_styles_result_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(DomTree); | |
| 59 }; | |
| 60 | |
| 61 using DomResultCB = base::Callback<void(DomTree)>; | |
| 62 | |
| 63 // Extracts all nodes from the DOM. This is an asynchronous operation and | |
| 64 // it's an error to call ExtractDom while a previous operation is in flight. | |
| 65 void ExtractDomTree(const std::vector<std::string>& css_style_whitelist, | |
| 66 DomResultCB callback); | |
| 67 | |
| 68 private: | |
| 69 void OnDocumentFetched(std::unique_ptr<dom::GetDocumentResult> 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<DomTreeExtractor> weak_factory_; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(DomTreeExtractor); | |
| 86 }; | |
| 87 | |
| 88 } // namespace headless | |
| 89 | |
| 90 #endif // HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_ | |
| OLD | NEW |