Chromium Code Reviews| 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/dom.h" | |
| 13 | |
| 14 namespace headless { | |
| 15 class HeadlessDevToolsClient; | |
| 16 | |
| 17 // A utility class for extracting information from the DOM via DevTools. In | |
| 18 // addition it also extracts details of bounding boxes and layout text (NB the | |
| 19 // exact layout should not be regarded as stable, it's subject to change without | |
| 20 // notice). | |
| 21 class DomTreeExtractor : public dom::Observer { | |
| 22 public: | |
| 23 explicit DomTreeExtractor(HeadlessDevToolsClient* devtools_client); | |
| 24 ~DomTreeExtractor() override; | |
| 25 | |
| 26 using NodeId = int; | |
| 27 using Index = size_t; | |
| 28 | |
| 29 class DomTree { | |
| 30 public: | |
| 31 DomTree(); | |
| 32 DomTree(DomTree&& other); | |
| 33 ~DomTree(); | |
| 34 | |
| 35 // Flattened dom tree. | |
| 36 std::vector<const dom::Node*> dom_nodes_; | |
|
Sami
2016/10/03 10:42:18
The root (document) is always guaranteed to be fir
alex clarke (OOO till 29th)
2016/10/19 16:23:26
Yes. I've added a comment.
| |
| 37 | |
| 38 // Map of node IDs to indexes into |dom_nodes_|. | |
| 39 std::unordered_map<NodeId, Index> node_id_to_index_; | |
| 40 | |
| 41 std::vector<const dom::LayoutTreeNode*> layout_tree_nodes_; | |
| 42 | |
| 43 private: | |
| 44 friend class DomTreeExtractor; | |
| 45 | |
| 46 // Owns the raw pointers in |dom_nodes_|. | |
| 47 std::unique_ptr<dom::GetDocumentResult> document_result_; | |
| 48 | |
| 49 // Owns the raw pointers in |layout_tree_nodes_|. | |
| 50 std::unique_ptr<dom::GetLayoutTreeNodesResult> layout_tree_result_; | |
| 51 }; | |
|
Sami
2016/10/03 10:42:18
DISALLOW_COPY_AND_ASSIGN?
alex clarke (OOO till 29th)
2016/10/19 16:23:26
Done.
| |
| 52 | |
| 53 using DomResultCB = base::Callback<void(DomTree)>; | |
| 54 | |
| 55 // Extracts all nodes from the DOM. This is an asynchronous operation and its | |
|
Sami
2016/10/03 10:42:18
s/its/it's/
alex clarke (OOO till 29th)
2016/10/19 16:23:26
Done.
| |
| 56 // an error to call ExtractDom while a previous operation is in flight. | |
| 57 void ExtractDomTree(DomResultCB callback); | |
| 58 | |
| 59 // headless::dom::Observer implementation: | |
| 60 void OnSetChildNodes(const dom::SetChildNodesParams& params) override; | |
| 61 | |
| 62 private: | |
| 63 void OnRootDocumentFetched(std::unique_ptr<dom::GetDocumentResult> result); | |
| 64 | |
| 65 void OnLayoutTreeNodesFetched( | |
| 66 std::unique_ptr<dom::GetLayoutTreeNodesResult> result); | |
| 67 | |
| 68 void MaybeExtractDomTree(); | |
| 69 void EnumerateNodes(const dom::Node* node); | |
| 70 void ExtractLayoutTreeNodes(); | |
| 71 | |
| 72 DomResultCB callback_; | |
| 73 DomTree dom_tree_; | |
| 74 bool child_nodes_fetched_; | |
| 75 bool dom_observer_registered_; | |
| 76 bool work_in_progress_; | |
| 77 HeadlessDevToolsClient* devtools_client_; // NOT OWNED | |
| 78 base::WeakPtrFactory<DomTreeExtractor> weak_factory_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(DomTreeExtractor); | |
| 81 }; | |
| 82 | |
| 83 } // namespace headless | |
| 84 | |
| 85 #endif // HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_ | |
| OLD | NEW |