| 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; |
| 39 void RemoveUIElementTree(UIElement* root); |
| 45 | 40 |
| 46 // DOM::Backend | 41 // DOM::Backend: |
| 47 ui::devtools::protocol::Response disable() override; | 42 ui::devtools::protocol::Response disable() override; |
| 48 ui::devtools::protocol::Response getDocument( | 43 ui::devtools::protocol::Response getDocument( |
| 49 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) override; | 44 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) override; |
| 50 ui::devtools::protocol::Response highlightNode( | 45 ui::devtools::protocol::Response highlightNode( |
| 51 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> | 46 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> |
| 52 highlight_config, | 47 highlight_config, |
| 53 ui::devtools::protocol::Maybe<int> node_id) override; | 48 ui::devtools::protocol::Maybe<int> node_id) override; |
| 54 ui::devtools::protocol::Response hideHighlight() override; | 49 ui::devtools::protocol::Response hideHighlight() override; |
| 55 | 50 |
| 56 // WindowObserver | 51 // UIElementDelegate: |
| 57 void OnWindowHierarchyChanging(const HierarchyChangeParams& params) override; | 52 void OnUIElementAdded( |
| 58 void OnWindowHierarchyChanged(const HierarchyChangeParams& params) override; | 53 int node_id, |
| 59 void OnWindowStackingChanged(aura::Window* window) override; | 54 UIElement* child_ui_element, |
| 60 void OnWindowBoundsChanged(aura::Window* window, | 55 std::vector<UIElement*>::iterator prev_sibling_position) override; |
| 61 const gfx::Rect& old_bounds, | 56 // Return iterator of the next siblings of node_id. |
| 62 const gfx::Rect& new_bounds) override; | 57 std::vector<UIElement*>::iterator OnUIElementRemoved(int node_id) override; |
| 63 | 58 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 | 59 |
| 85 void AddObserver(AshDevToolsDOMAgentObserver* observer); | 60 void AddObserver(AshDevToolsDOMAgentObserver* observer); |
| 86 void RemoveObserver(AshDevToolsDOMAgentObserver* observer); | 61 void RemoveObserver(AshDevToolsDOMAgentObserver* observer); |
| 62 UIElement* GetElementFromNodeId(int node_id); |
| 63 UIElement* GetWindowElementRoot(); |
| 87 | 64 |
| 88 private: | 65 private: |
| 66 // AshDevToolsDOMAgentObserver: |
| 67 void OnNodeBoundsChanged(int node_id) override; |
| 68 |
| 89 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildInitialTree(); | 69 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildInitialTree(); |
| 70 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForUIElement( |
| 71 UIElement* ui_element); |
| 90 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForWindow( | 72 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForWindow( |
| 73 UIElement* window_element_root, |
| 91 aura::Window* window); | 74 aura::Window* window); |
| 92 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForRootWidget( | 75 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForRootWidget( |
| 76 UIElement* widget_element, |
| 93 views::Widget* widget); | 77 views::Widget* widget); |
| 94 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForView( | 78 std::unique_ptr<ui::devtools::protocol::DOM::Node> BuildTreeForView( |
| 79 UIElement* view_element, |
| 95 views::View* view); | 80 views::View* view); |
| 96 | 81 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(); | 82 void Reset(); |
| 121 | |
| 122 using WindowAndBoundsPair = std::pair<aura::Window*, gfx::Rect>; | |
| 123 WindowAndBoundsPair GetNodeWindowAndBounds(int node_id); | |
| 124 void InitializeHighlightingWidget(); | 83 void InitializeHighlightingWidget(); |
| 125 void UpdateHighlight(const WindowAndBoundsPair& window_and_bounds, | 84 void UpdateHighlight( |
| 126 SkColor background, | 85 const std::pair<aura::Window*, gfx::Rect>& window_and_bounds, |
| 127 SkColor border); | 86 SkColor background, |
| 87 SkColor border); |
| 128 ui::devtools::protocol::Response HighlightNode( | 88 ui::devtools::protocol::Response HighlightNode( |
| 129 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> | 89 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> |
| 130 highlight_config, | 90 highlight_config, |
| 131 int node_id); | 91 int node_id); |
| 132 bool IsHighlightingWindow(aura::Window* window); | 92 bool IsHighlightingWindow(aura::Window* window); |
| 133 | 93 |
| 94 UIElement* window_element_root; |
| 95 std::unordered_map<int, UIElement*> node_id_to_ui_element_; |
| 134 std::unique_ptr<views::Widget> widget_for_highlighting_; | 96 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_; | 97 base::ObserverList<AshDevToolsDOMAgentObserver> observers_; |
| 152 | 98 |
| 153 DISALLOW_COPY_AND_ASSIGN(AshDevToolsDOMAgent); | 99 DISALLOW_COPY_AND_ASSIGN(AshDevToolsDOMAgent); |
| 154 }; | 100 }; |
| 155 | 101 |
| 156 } // namespace devtools | 102 } // namespace devtools |
| 157 } // namespace ash | 103 } // namespace ash |
| 158 | 104 |
| 159 #endif // ASH_DEVTOOLS_ASH_DEVTOOLS_DOM_AGENT_H_ | 105 #endif // ASH_DEVTOOLS_ASH_DEVTOOLS_DOM_AGENT_H_ |
| OLD | NEW |