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

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

Issue 2385653003: Add a utility class for extracting details of the DOM (Closed)
Patch Set: 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..8162a61e9707737957ab72e931d98a0f93592c0c
--- /dev/null
+++ b/headless/public/util/dom_tree_extractor.h
@@ -0,0 +1,66 @@
+// 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).
Sami 2016/09/30 10:56:03 Could you briefly document what the received struc
alex clarke (OOO till 29th) 2016/09/30 13:16:33 Is that still needed? It should be obvious now.
+class DomTreeExtractor : public dom::Observer {
+ public:
+ explicit DomTreeExtractor(HeadlessDevToolsClient* devtools_client);
+ ~DomTreeExtractor() override;
+
+ using DomResultCB =
+ base::Callback<void(std::vector<std::unique_ptr<base::DictionaryValue>>)>;
+
+ // Extracts all nodes from the DOM. This is an asynchronous operation and its
+ // an error to call ExtractDom while a previous operation is in flight.
+ void ExtractDom(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 ExtractDomTree();
+
+ DomResultCB callback_;
+
+ using NodeId = int;
+ using Index = size_t;
+
+ std::unique_ptr<dom::GetDocumentResult> document_result_;
+ std::unique_ptr<dom::GetLayoutTreeNodesResult> layout_tree_result_;
+ std::vector<const dom::Node*> nodes_;
+ std::unordered_map<NodeId, size_t> node_id_to_index_;
+ 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