dom_agent.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2018 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. #ifndef COMPONENTS_UI_DEVTOOLS_DOM_AGENT_H_
  5. #define COMPONENTS_UI_DEVTOOLS_DOM_AGENT_H_
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <vector>
  10. #include "base/observer_list.h"
  11. #include "components/ui_devtools/devtools_base_agent.h"
  12. #include "components/ui_devtools/devtools_export.h"
  13. #include "components/ui_devtools/dom.h"
  14. #include "components/ui_devtools/ui_element_delegate.h"
  15. namespace ui_devtools {
  16. class UIElement;
  17. class DOMAgentObserver {
  18. public:
  19. virtual void OnElementBoundsChanged(UIElement* ui_element) {}
  20. virtual void OnElementAdded(UIElement* ui_element) {}
  21. };
  22. class UI_DEVTOOLS_EXPORT DOMAgent
  23. : public UiDevToolsBaseAgent<protocol::DOM::Metainfo>,
  24. public UIElementDelegate {
  25. public:
  26. DOMAgent();
  27. DOMAgent(const DOMAgent&) = delete;
  28. DOMAgent& operator=(const DOMAgent&) = delete;
  29. ~DOMAgent() override;
  30. // DOM::Backend:
  31. protocol::Response disable() override;
  32. protocol::Response getDocument(
  33. std::unique_ptr<protocol::DOM::Node>* out_root) override;
  34. protocol::Response pushNodesByBackendIdsToFrontend(
  35. std::unique_ptr<protocol::Array<int>> backend_node_ids,
  36. std::unique_ptr<protocol::Array<int>>* result) override;
  37. protocol::Response performSearch(
  38. const protocol::String& query,
  39. protocol::Maybe<bool> include_user_agent_shadow_dom,
  40. protocol::String* search_id,
  41. int* result_count) override;
  42. protocol::Response getSearchResults(
  43. const protocol::String& search_id,
  44. int from_index,
  45. int to_index,
  46. std::unique_ptr<protocol::Array<int>>* node_ids) override;
  47. protocol::Response discardSearchResults(
  48. const protocol::String& search_id) override;
  49. protocol::Response dispatchMouseEvent(
  50. int node_id,
  51. std::unique_ptr<protocol::DOM::MouseEvent> event) override;
  52. protocol::Response dispatchKeyEvent(
  53. int node_id,
  54. std::unique_ptr<protocol::DOM::KeyEvent> event) override;
  55. // UIElementDelegate:
  56. void OnUIElementAdded(UIElement* parent, UIElement* child) override;
  57. void OnUIElementReordered(UIElement* parent, UIElement* child) override;
  58. void OnUIElementRemoved(UIElement* ui_element) override;
  59. void OnUIElementBoundsChanged(UIElement* ui_element) override;
  60. void AddObserver(DOMAgentObserver* observer);
  61. void RemoveObserver(DOMAgentObserver* observer);
  62. UIElement* GetElementFromNodeId(int node_id) const;
  63. UIElement* element_root() const { return element_root_.get(); }
  64. // Returns parent id of the element with id |node_id|. Returns 0 if parent
  65. // does not exist.
  66. int GetParentIdOfNodeId(int node_id) const;
  67. protected:
  68. std::unique_ptr<protocol::DOM::Node> BuildNode(
  69. const std::string& name,
  70. std::unique_ptr<std::vector<std::string>> attributes,
  71. std::unique_ptr<protocol::Array<protocol::DOM::Node>> children,
  72. int node_ids);
  73. std::unique_ptr<protocol::DOM::Node> BuildDomNodeFromUIElement(
  74. UIElement* root);
  75. private:
  76. struct Query;
  77. // These are called on creating a DOM document.
  78. std::unique_ptr<protocol::DOM::Node> BuildInitialTree();
  79. // The caller takes ownership of the returned pointers.
  80. virtual std::vector<UIElement*> CreateChildrenForRoot() = 0;
  81. virtual std::unique_ptr<protocol::DOM::Node> BuildTreeForUIElement(
  82. UIElement* ui_element) = 0;
  83. void OnElementBoundsChanged(UIElement* ui_element);
  84. // Recursively removes |ui_element| and its children from the frontend
  85. // elements tree. If |update_node_id_map|=true, also remove |ui_element|'s
  86. // |node_id| from |node_id_to_ui_element_|.
  87. void RemoveDomNode(UIElement* ui_element, bool update_node_id_map);
  88. void Reset();
  89. Query PreprocessQuery(protocol::String query);
  90. void SearchDomTree(const Query& query, std::vector<int>* result_collector);
  91. std::unique_ptr<UIElement> element_root_;
  92. std::unordered_map<int, UIElement*> node_id_to_ui_element_;
  93. base::ObserverList<DOMAgentObserver>::Unchecked observers_;
  94. using SearchResults = std::unordered_map<std::string, std::vector<int>>;
  95. SearchResults search_results_;
  96. bool is_document_created_ = false;
  97. };
  98. } // namespace ui_devtools
  99. #endif // COMPONENTS_UI_DEVTOOLS_DOM_AGENT_H_