| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef ASH_DEVTOOLS_ASH_DEVTOOLS_DOM_AGENT_H_ | 5 #ifndef ASH_DEVTOOLS_ASH_DEVTOOLS_DOM_AGENT_H_ |
| 6 #define ASH_DEVTOOLS_ASH_DEVTOOLS_DOM_AGENT_H_ | 6 #define ASH_DEVTOOLS_ASH_DEVTOOLS_DOM_AGENT_H_ |
| 7 | 7 |
| 8 #include "ash/ash_export.h" | 8 #include "ash/ash_export.h" |
| 9 #include "ash/devtools/ui_element_delegate.h" |
| 9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 10 #include "base/observer_list.h" | |
| 11 #include "components/ui_devtools/DOM.h" | 11 #include "components/ui_devtools/DOM.h" |
| 12 #include "components/ui_devtools/devtools_base_agent.h" | 12 #include "components/ui_devtools/devtools_base_agent.h" |
| 13 #include "ui/aura/window_observer.h" | |
| 14 #include "ui/views/view.h" | 13 #include "ui/views/view.h" |
| 15 #include "ui/views/view_observer.h" | |
| 16 #include "ui/views/widget/widget.h" | 14 #include "ui/views/widget/widget.h" |
| 17 #include "ui/views/widget/widget_observer.h" | |
| 18 #include "ui/views/widget/widget_removals_observer.h" | |
| 19 | 15 |
| 20 namespace aura { | 16 namespace aura { |
| 21 class Window; | 17 class Window; |
| 22 } | 18 } |
| 23 | 19 |
| 24 namespace ash { | 20 namespace ash { |
| 25 | 21 |
| 26 namespace devtools { | 22 namespace devtools { |
| 27 | 23 |
| 24 class UIElement; |
| 25 |
| 28 class ASH_EXPORT AshDevToolsDOMAgentObserver { | 26 class ASH_EXPORT AshDevToolsDOMAgentObserver { |
| 29 public: | 27 public: |
| 30 virtual void OnWindowBoundsChanged(aura::Window* window) {} | 28 virtual void OnNodeBoundsChanged(int node_id); |
| 31 virtual void OnWidgetBoundsChanged(views::Widget* widget) {} | |
| 32 virtual void OnViewBoundsChanged(views::View* view) {} | |
| 33 }; | 29 }; |
| 34 | 30 |
| 35 class ASH_EXPORT AshDevToolsDOMAgent | 31 class ASH_EXPORT AshDevToolsDOMAgent |
| 36 : public NON_EXPORTED_BASE(ui::devtools::UiDevToolsBaseAgent< | 32 : public NON_EXPORTED_BASE(ui::devtools::UiDevToolsBaseAgent< |
| 37 ui::devtools::protocol::DOM::Metainfo>), | 33 ui::devtools::protocol::DOM::Metainfo>), |
| 38 public aura::WindowObserver, | 34 public UIElementDelegate, |
| 39 public views::WidgetObserver, | 35 public AshDevToolsDOMAgentObserver { |
| 40 public views::WidgetRemovalsObserver, | |
| 41 public views::ViewObserver { | |
| 42 public: | 36 public: |
| 43 AshDevToolsDOMAgent(); | 37 AshDevToolsDOMAgent(); |
| 44 ~AshDevToolsDOMAgent() override; | 38 ~AshDevToolsDOMAgent() override; |
| 45 | 39 |
| 46 // DOM::Backend | 40 // DOM::Backend: |
| 47 ui::devtools::protocol::Response disable() override; | 41 ui::devtools::protocol::Response disable() override; |
| 48 ui::devtools::protocol::Response getDocument( | 42 ui::devtools::protocol::Response getDocument( |
| 49 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) override; | 43 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) override; |
| 50 ui::devtools::protocol::Response highlightNode( | 44 ui::devtools::protocol::Response highlightNode( |
| 51 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> | 45 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> |
| 52 highlight_config, | 46 highlight_config, |
| 53 ui::devtools::protocol::Maybe<int> node_id) override; | 47 ui::devtools::protocol::Maybe<int> node_id) override; |
| 54 ui::devtools::protocol::Response hideHighlight() override; | 48 ui::devtools::protocol::Response hideHighlight() override; |
| 55 | 49 |
| 56 // WindowObserver | 50 // UIElementDelegate: |
| 57 void OnWindowHierarchyChanging(const HierarchyChangeParams& params) override; | 51 void OnUIElementAdded( |
| 58 void OnWindowHierarchyChanged(const HierarchyChangeParams& params) override; | 52 int node_id, |
| 59 void OnWindowStackingChanged(aura::Window* window) override; | 53 UIElement* child_ui_element, |
| 60 void OnWindowBoundsChanged(aura::Window* window, | 54 std::vector<UIElement*>::iterator prev_sibling_position) override; |
| 61 const gfx::Rect& old_bounds, | 55 void RemoveNodeIdMap(int node_id) override; |
| 62 const gfx::Rect& new_bounds) override; | 56 bool OnUIElementRemoved(int node_id) override; |
| 63 | 57 void OnUIElementBoundsChanged(int node_id) override; |
| 64 // views::WidgetRemovalsObserver | |
| 65 void OnWillRemoveView(views::Widget* widget, views::View* view) override; | |
| 66 | |
| 67 // views::WidgetObserver | |
| 68 void OnWidgetBoundsChanged(views::Widget* widget, | |
| 69 const gfx::Rect& new_bounds) override; | |
| 70 | |
| 71 // views::ViewObserver | |
| 72 void OnChildViewRemoved(views::View* parent, views::View* view) override; | |
| 73 void OnChildViewAdded(views::View* parent, views::View* view) override; | |
| 74 void OnChildViewReordered(views::View* parent, views::View*) override; | |
| 75 void OnViewBoundsChanged(views::View* view) override; | |
| 76 | |
| 77 aura::Window* GetWindowFromNodeId(int nodeId); | |
| 78 views::Widget* GetWidgetFromNodeId(int nodeId); | |
| 79 views::View* GetViewFromNodeId(int nodeId); | |
| 80 | |
| 81 int GetNodeIdFromWindow(aura::Window* window); | |
| 82 int GetNodeIdFromWidget(views::Widget* widget); | |
| 83 int GetNodeIdFromView(views::View* view); | |
| 84 | 58 |
| 85 void AddObserver(AshDevToolsDOMAgentObserver* observer); | 59 void AddObserver(AshDevToolsDOMAgentObserver* observer); |
| 86 void RemoveObserver(AshDevToolsDOMAgentObserver* observer); | 60 void RemoveObserver(AshDevToolsDOMAgentObserver* observer); |
| 61 UIElement* GetElementFromNodeId(int node_id); |
| 62 UIElement* GetWindowElementRoot(); |
| 87 | 63 |
| 88 private: | 64 private: |
| 65 // AshDevToolsDOMAgentObserver: |
| 66 void OnNodeBoundsChanged(int node_id) override; |
| 67 |
| 89 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildInitialTree(); | 68 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildInitialTree(); |
| 69 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForUIElement( |
| 70 UIElement* ui_element); |
| 90 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForWindow( | 71 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForWindow( |
| 72 UIElement* window_element_root, |
| 91 aura::Window* window); | 73 aura::Window* window); |
| 92 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForRootWidget( | 74 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForRootWidget( |
| 75 UIElement* widget_element, |
| 93 views::Widget* widget); | 76 views::Widget* widget); |
| 94 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForView( | 77 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForView( |
| 78 UIElement* view_element, |
| 95 views::View* view); | 79 views::View* view); |
| 96 | 80 void RemoveDomNode(UIElement* ui_element); |
| 97 void AddWindowTree(aura::Window* window); | |
| 98 // |remove_observer| ensures that we don't add a duplicate observer in any | |
| 99 // observer callback. For example, |remove_observer| is false when rebuilding | |
| 100 // the tree in OnWindowStackingChanged so that the observer is not removed and | |
| 101 // re-added, thus causing endless calls on the observer. | |
| 102 void RemoveWindowTree(aura::Window* window, bool remove_observer); | |
| 103 void RemoveWindowNode(aura::Window* window, bool remove_observer); | |
| 104 | |
| 105 // Don't need AddWidgetTree because |widget| will always be inside a window, | |
| 106 // so when windows are created, their widget nodes are created as well. | |
| 107 void RemoveWidgetTree(views::Widget* widget, bool remove_observer); | |
| 108 void RemoveWidgetNode(views::Widget* widget, bool remove_observer); | |
| 109 | |
| 110 void AddViewTree(views::View* view); | |
| 111 void RemoveViewTree(views::View* view, | |
| 112 views::View* parent, | |
| 113 bool remove_observer); | |
| 114 void RemoveViewNode(views::View* view, | |
| 115 views::View* parent, | |
| 116 bool remove_observer); | |
| 117 | |
| 118 void DestroyHighlightingWidget(); | |
| 119 void RemoveObservers(); | |
| 120 void Reset(); | 81 void Reset(); |
| 121 | |
| 122 using WindowAndBoundsPair = std::pair<aura::Window*, gfx::Rect>; | |
| 123 WindowAndBoundsPair GetNodeWindowAndBounds(int node_id); | |
| 124 void InitializeHighlightingWidget(); | 82 void InitializeHighlightingWidget(); |
| 125 void UpdateHighlight(const WindowAndBoundsPair& window_and_bounds, | 83 void UpdateHighlight( |
| 126 SkColor background, | 84 const std::pair<aura::Window*, gfx::Rect>& window_and_bounds, |
| 127 SkColor border); | 85 SkColor background, |
| 86 SkColor border); |
| 128 ui::devtools::protocol::Response HighlightNode( | 87 ui::devtools::protocol::Response HighlightNode( |
| 129 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> | 88 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> |
| 130 highlight_config, | 89 highlight_config, |
| 131 int node_id); | 90 int node_id); |
| 132 bool IsHighlightingWindow(aura::Window* window); | 91 bool IsHighlightingWindow(aura::Window* window); |
| 133 | 92 |
| 93 UIElement* window_element_root; |
| 94 std::unordered_map<int, UIElement*> node_id_to_ui_element_; |
| 134 std::unique_ptr<views::Widget> widget_for_highlighting_; | 95 std::unique_ptr<views::Widget> widget_for_highlighting_; |
| 135 | |
| 136 using WindowToNodeIdMap = std::unordered_map<aura::Window*, int>; | |
| 137 WindowToNodeIdMap window_to_node_id_map_; | |
| 138 using NodeIdToWindowMap = std::unordered_map<int, aura::Window*>; | |
| 139 NodeIdToWindowMap node_id_to_window_map_; | |
| 140 | |
| 141 using WidgetToNodeIdMap = std::unordered_map<views::Widget*, int>; | |
| 142 WidgetToNodeIdMap widget_to_node_id_map_; | |
| 143 using NodeIdToWidgetMap = std::unordered_map<int, views::Widget*>; | |
| 144 NodeIdToWidgetMap node_id_to_widget_map_; | |
| 145 | |
| 146 using ViewToNodeIdMap = std::unordered_map<views::View*, int>; | |
| 147 ViewToNodeIdMap view_to_node_id_map_; | |
| 148 using NodeIdToViewMap = std::unordered_map<int, views::View*>; | |
| 149 NodeIdToViewMap node_id_to_view_map_; | |
| 150 | |
| 151 base::ObserverList<AshDevToolsDOMAgentObserver> observers_; | 96 base::ObserverList<AshDevToolsDOMAgentObserver> observers_; |
| 152 | 97 |
| 153 DISALLOW_COPY_AND_ASSIGN(AshDevToolsDOMAgent); | 98 DISALLOW_COPY_AND_ASSIGN(AshDevToolsDOMAgent); |
| 154 }; | 99 }; |
| 155 | 100 |
| 156 } // namespace devtools | 101 } // namespace devtools |
| 157 } // namespace ash | 102 } // namespace ash |
| 158 | 103 |
| 159 #endif // ASH_DEVTOOLS_ASH_DEVTOOLS_DOM_AGENT_H_ | 104 #endif // ASH_DEVTOOLS_ASH_DEVTOOLS_DOM_AGENT_H_ |
| OLD | NEW |