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