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" | |
10 #include "base/macros.h" | 9 #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" |
13 #include "ui/views/view.h" | 14 #include "ui/views/view.h" |
| 15 #include "ui/views/view_observer.h" |
14 #include "ui/views/widget/widget.h" | 16 #include "ui/views/widget/widget.h" |
| 17 #include "ui/views/widget/widget_observer.h" |
| 18 #include "ui/views/widget/widget_removals_observer.h" |
15 | 19 |
16 namespace aura { | 20 namespace aura { |
17 class Window; | 21 class Window; |
18 } | 22 } |
19 | 23 |
20 namespace ash { | 24 namespace ash { |
| 25 |
21 namespace devtools { | 26 namespace devtools { |
22 | 27 |
23 class UIElement; | |
24 | |
25 class ASH_EXPORT AshDevToolsDOMAgentObserver { | 28 class ASH_EXPORT AshDevToolsDOMAgentObserver { |
26 public: | 29 public: |
27 // TODO(thanhph): Use UIElement* as input argument instead. | 30 virtual void OnWindowBoundsChanged(aura::Window* window) {} |
28 virtual void OnNodeBoundsChanged(int node_id) = 0; | 31 virtual void OnWidgetBoundsChanged(views::Widget* widget) {} |
| 32 virtual void OnViewBoundsChanged(views::View* view) {} |
29 }; | 33 }; |
30 | 34 |
31 class ASH_EXPORT AshDevToolsDOMAgent | 35 class ASH_EXPORT AshDevToolsDOMAgent |
32 : public NON_EXPORTED_BASE(ui::devtools::UiDevToolsBaseAgent< | 36 : public NON_EXPORTED_BASE(ui::devtools::UiDevToolsBaseAgent< |
33 ui::devtools::protocol::DOM::Metainfo>), | 37 ui::devtools::protocol::DOM::Metainfo>), |
34 public UIElementDelegate { | 38 public aura::WindowObserver, |
| 39 public views::WidgetObserver, |
| 40 public views::WidgetRemovalsObserver, |
| 41 public views::ViewObserver { |
35 public: | 42 public: |
36 AshDevToolsDOMAgent(); | 43 AshDevToolsDOMAgent(); |
37 ~AshDevToolsDOMAgent() override; | 44 ~AshDevToolsDOMAgent() override; |
38 | 45 |
39 // DOM::Backend: | 46 // DOM::Backend |
40 ui::devtools::protocol::Response disable() override; | 47 ui::devtools::protocol::Response disable() override; |
41 ui::devtools::protocol::Response getDocument( | 48 ui::devtools::protocol::Response getDocument( |
42 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) override; | 49 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) override; |
43 ui::devtools::protocol::Response highlightNode( | 50 ui::devtools::protocol::Response highlightNode( |
44 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> | 51 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> |
45 highlight_config, | 52 highlight_config, |
46 ui::devtools::protocol::Maybe<int> node_id) override; | 53 ui::devtools::protocol::Maybe<int> node_id) override; |
47 ui::devtools::protocol::Response hideHighlight() override; | 54 ui::devtools::protocol::Response hideHighlight() override; |
48 | 55 |
49 // UIElementDelegate: | 56 // WindowObserver |
50 void OnUIElementAdded(UIElement* parent, UIElement* child) override; | 57 void OnWindowHierarchyChanging(const HierarchyChangeParams& params) override; |
51 void OnUIElementReordered(UIElement* parent, UIElement* child) override; | 58 void OnWindowHierarchyChanged(const HierarchyChangeParams& params) override; |
52 void OnUIElementRemoved(UIElement* ui_element) override; | 59 void OnWindowStackingChanged(aura::Window* window) override; |
53 void OnUIElementBoundsChanged(UIElement* ui_element) override; | 60 void OnWindowBoundsChanged(aura::Window* window, |
54 bool IsHighlightingWindow(aura::Window* window) override; | 61 const gfx::Rect& old_bounds, |
| 62 const gfx::Rect& new_bounds) override; |
| 63 |
| 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); |
55 | 84 |
56 void AddObserver(AshDevToolsDOMAgentObserver* observer); | 85 void AddObserver(AshDevToolsDOMAgentObserver* observer); |
57 void RemoveObserver(AshDevToolsDOMAgentObserver* observer); | 86 void RemoveObserver(AshDevToolsDOMAgentObserver* observer); |
58 UIElement* GetElementFromNodeId(int node_id); | |
59 UIElement* window_element_root() const { return window_element_root_; }; | |
60 | 87 |
61 private: | 88 private: |
62 void OnNodeBoundsChanged(int node_id); | |
63 | |
64 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildInitialTree(); | 89 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildInitialTree(); |
65 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForUIElement( | |
66 UIElement* ui_element); | |
67 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForWindow( | 90 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForWindow( |
68 UIElement* window_element_root, | |
69 aura::Window* window); | 91 aura::Window* window); |
70 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForRootWidget( | 92 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForRootWidget( |
71 UIElement* widget_element, | |
72 views::Widget* widget); | 93 views::Widget* widget); |
73 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForView( | 94 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForView( |
74 UIElement* view_element, | |
75 views::View* view); | 95 views::View* view); |
76 void RemoveDomNode(UIElement* ui_element); | 96 |
| 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(); |
77 void Reset(); | 120 void Reset(); |
| 121 |
| 122 using WindowAndBoundsPair = std::pair<aura::Window*, gfx::Rect>; |
| 123 WindowAndBoundsPair GetNodeWindowAndBounds(int node_id); |
78 void InitializeHighlightingWidget(); | 124 void InitializeHighlightingWidget(); |
79 void UpdateHighlight( | 125 void UpdateHighlight(const WindowAndBoundsPair& window_and_bounds, |
80 const std::pair<aura::Window*, gfx::Rect>& window_and_bounds, | 126 SkColor background, |
81 SkColor background, | 127 SkColor border); |
82 SkColor border); | |
83 ui::devtools::protocol::Response HighlightNode( | 128 ui::devtools::protocol::Response HighlightNode( |
84 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> | 129 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> |
85 highlight_config, | 130 highlight_config, |
86 int node_id); | 131 int node_id); |
| 132 bool IsHighlightingWindow(aura::Window* window); |
87 | 133 |
88 bool is_building_tree_; | |
89 UIElement* window_element_root_; | |
90 std::unordered_map<int, UIElement*> node_id_to_ui_element_; | |
91 std::unique_ptr<views::Widget> widget_for_highlighting_; | 134 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 |
92 base::ObserverList<AshDevToolsDOMAgentObserver> observers_; | 151 base::ObserverList<AshDevToolsDOMAgentObserver> observers_; |
93 | 152 |
94 DISALLOW_COPY_AND_ASSIGN(AshDevToolsDOMAgent); | 153 DISALLOW_COPY_AND_ASSIGN(AshDevToolsDOMAgent); |
95 }; | 154 }; |
96 | 155 |
97 } // namespace devtools | 156 } // namespace devtools |
98 } // namespace ash | 157 } // namespace ash |
99 | 158 |
100 #endif // ASH_DEVTOOLS_ASH_DEVTOOLS_DOM_AGENT_H_ | 159 #endif // ASH_DEVTOOLS_ASH_DEVTOOLS_DOM_AGENT_H_ |
OLD | NEW |