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

Unified Diff: headless/public/util/dom_tree_extractor.h

Issue 2385653003: Add a utility class for extracting details of the DOM (Closed)
Patch Set: Refactor Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: headless/public/util/dom_tree_extractor.h
diff --git a/headless/public/util/dom_tree_extractor.h b/headless/public/util/dom_tree_extractor.h
new file mode 100644
index 0000000000000000000000000000000000000000..8645b40c98f53a3b7264a8d4bd86648784659f22
--- /dev/null
+++ b/headless/public/util/dom_tree_extractor.h
@@ -0,0 +1,85 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_
+#define HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_
+
+#include <unordered_map>
+#include <vector>
+
+#include "base/macros.h"
+#include "headless/public/domains/dom.h"
+
+namespace headless {
+class HeadlessDevToolsClient;
+
+// A utility class for extracting information from the DOM via DevTools. In
+// addition it also extracts details of bounding boxes and layout text (NB the
+// exact layout should not be regarded as stable, it's subject to change without
+// notice).
+class DomTreeExtractor : public dom::Observer {
+ public:
+ explicit DomTreeExtractor(HeadlessDevToolsClient* devtools_client);
+ ~DomTreeExtractor() override;
+
+ using NodeId = int;
+ using Index = size_t;
+
+ class DomTree {
+ public:
+ DomTree();
+ DomTree(DomTree&& other);
+ ~DomTree();
+
+ // Flattened dom tree.
+ 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.
+
+ // Map of node IDs to indexes into |dom_nodes_|.
+ std::unordered_map<NodeId, Index> node_id_to_index_;
+
+ std::vector<const dom::LayoutTreeNode*> layout_tree_nodes_;
+
+ private:
+ friend class DomTreeExtractor;
+
+ // Owns the raw pointers in |dom_nodes_|.
+ std::unique_ptr<dom::GetDocumentResult> document_result_;
+
+ // Owns the raw pointers in |layout_tree_nodes_|.
+ std::unique_ptr<dom::GetLayoutTreeNodesResult> layout_tree_result_;
+ };
Sami 2016/10/03 10:42:18 DISALLOW_COPY_AND_ASSIGN?
alex clarke (OOO till 29th) 2016/10/19 16:23:26 Done.
+
+ using DomResultCB = base::Callback<void(DomTree)>;
+
+ // 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.
+ // an error to call ExtractDom while a previous operation is in flight.
+ void ExtractDomTree(DomResultCB callback);
+
+ // headless::dom::Observer implementation:
+ void OnSetChildNodes(const dom::SetChildNodesParams& params) override;
+
+ private:
+ void OnRootDocumentFetched(std::unique_ptr<dom::GetDocumentResult> result);
+
+ void OnLayoutTreeNodesFetched(
+ std::unique_ptr<dom::GetLayoutTreeNodesResult> result);
+
+ void MaybeExtractDomTree();
+ void EnumerateNodes(const dom::Node* node);
+ void ExtractLayoutTreeNodes();
+
+ DomResultCB callback_;
+ DomTree dom_tree_;
+ bool child_nodes_fetched_;
+ bool dom_observer_registered_;
+ bool work_in_progress_;
+ HeadlessDevToolsClient* devtools_client_; // NOT OWNED
+ base::WeakPtrFactory<DomTreeExtractor> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(DomTreeExtractor);
+};
+
+} // namespace headless
+
+#endif // HEADLESS_PUBLIC_UTIL_DOM_TREE_EXTRACTOR_H_

Powered by Google App Engine
This is Rietveld 408576698