Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(754)

Side by Side Diff: ash/devtools/ash_devtools_dom_agent.cc

Issue 2899503002: Revert of Create a unified UIElement interface for Widget, View and Window. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/devtools/ash_devtools_dom_agent.h ('k') | ash/devtools/ash_devtools_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "ash/devtools/ash_devtools_dom_agent.h" 5 #include "ash/devtools/ash_devtools_dom_agent.h"
6 6
7 #include "ash/devtools/ui_element.h"
8 #include "ash/devtools/view_element.h"
9 #include "ash/devtools/widget_element.h"
10 #include "ash/devtools/window_element.h"
11 #include "ash/public/cpp/shell_window_ids.h" 7 #include "ash/public/cpp/shell_window_ids.h"
12 #include "ash/root_window_controller.h" 8 #include "ash/root_window_controller.h"
13 #include "ash/shell.h" 9 #include "ash/shell.h"
14 #include "components/ui_devtools/devtools_server.h" 10 #include "components/ui_devtools/devtools_server.h"
15 #include "third_party/skia/include/core/SkColor.h" 11 #include "third_party/skia/include/core/SkColor.h"
16 #include "ui/aura/window.h" 12 #include "ui/aura/window.h"
17 #include "ui/display/display.h" 13 #include "ui/display/display.h"
18 #include "ui/views/background.h" 14 #include "ui/views/background.h"
19 #include "ui/views/border.h" 15 #include "ui/views/border.h"
20 #include "ui/views/view.h"
21 #include "ui/views/widget/widget.h"
22 #include "ui/wm/core/window_util.h" 16 #include "ui/wm/core/window_util.h"
23 17
24 namespace ash { 18 namespace ash {
25 namespace devtools { 19 namespace devtools {
20
26 namespace { 21 namespace {
27
28 using namespace ui::devtools::protocol; 22 using namespace ui::devtools::protocol;
29 // TODO(mhashmi): Make ids reusable 23 // TODO(mhashmi): Make ids reusable
24 DOM::NodeId node_ids = 1;
30 25
31 std::unique_ptr<DOM::Node> BuildNode( 26 std::unique_ptr<DOM::Node> BuildNode(
32 const std::string& name, 27 const std::string& name,
33 std::unique_ptr<Array<std::string>> attributes, 28 std::unique_ptr<Array<std::string>> attributes,
34 std::unique_ptr<Array<DOM::Node>> children, 29 std::unique_ptr<Array<DOM::Node>> children) {
35 int node_ids) {
36 constexpr int kDomElementNodeType = 1; 30 constexpr int kDomElementNodeType = 1;
37 std::unique_ptr<DOM::Node> node = DOM::Node::create() 31 std::unique_ptr<DOM::Node> node = DOM::Node::create()
38 .setNodeId(node_ids) 32 .setNodeId(node_ids++)
39 .setNodeName(name) 33 .setNodeName(name)
40 .setNodeType(kDomElementNodeType) 34 .setNodeType(kDomElementNodeType)
41 .setAttributes(std::move(attributes)) 35 .setAttributes(std::move(attributes))
42 .build(); 36 .build();
43 node->setChildNodeCount(children->length()); 37 node->setChildNodeCount(children->length());
44 node->setChildren(std::move(children)); 38 node->setChildren(std::move(children));
45 return node; 39 return node;
46 } 40 }
47 41
48 // TODO(thanhph): Move this function to UIElement::GetAttributes(). 42 std::unique_ptr<Array<std::string>> GetAttributes(const aura::Window* window) {
49 std::unique_ptr<Array<std::string>> GetAttributes(UIElement* ui_element) {
50 std::unique_ptr<Array<std::string>> attributes = Array<std::string>::create(); 43 std::unique_ptr<Array<std::string>> attributes = Array<std::string>::create();
51 attributes->addItem("name"); 44 attributes->addItem("name");
52 switch (ui_element->type()) { 45 attributes->addItem(window->GetName());
53 case UIElementType::WINDOW: { 46 attributes->addItem("active");
54 aura::Window* window = 47 attributes->addItem(::wm::IsActiveWindow(window) ? "true" : "false");
55 UIElement::GetBackingElement<aura::Window, WindowElement>(ui_element);
56 attributes->addItem(window->GetName());
57 attributes->addItem("active");
58 attributes->addItem(::wm::IsActiveWindow(window) ? "true" : "false");
59 break;
60 }
61 case UIElementType::WIDGET: {
62 views::Widget* widget =
63 UIElement::GetBackingElement<views::Widget, WidgetElement>(
64 ui_element);
65 attributes->addItem(widget->GetName());
66 attributes->addItem("active");
67 attributes->addItem(widget->IsActive() ? "true" : "false");
68 break;
69 }
70 case UIElementType::VIEW: {
71 attributes->addItem(
72 UIElement::GetBackingElement<views::View, ViewElement>(ui_element)
73 ->GetClassName());
74 break;
75 }
76 default:
77 DCHECK(false);
78 }
79 return attributes; 48 return attributes;
80 } 49 }
81 50
51 std::unique_ptr<Array<std::string>> GetAttributes(const views::Widget* widget) {
52 std::unique_ptr<Array<std::string>> attributes = Array<std::string>::create();
53 attributes->addItem("name");
54 attributes->addItem(widget->GetName());
55 attributes->addItem("active");
56 attributes->addItem(widget->IsActive() ? "true" : "false");
57 return attributes;
58 }
59
60 std::unique_ptr<Array<std::string>> GetAttributes(const views::View* view) {
61 std::unique_ptr<Array<std::string>> attributes = Array<std::string>::create();
62 attributes->addItem("name");
63 attributes->addItem(view->GetClassName());
64 return attributes;
65 }
66
67 aura::Window* FindPreviousSibling(aura::Window* window) {
68 const aura::Window::Windows& siblings = window->parent()->children();
69 auto it = std::find(siblings.begin(), siblings.end(), window);
70 DCHECK(it != siblings.end());
71 // If this is the first child of its parent, the previous sibling is null
72 return it == siblings.begin() ? nullptr : *std::prev(it);
73 }
74
75 views::View* FindPreviousSibling(views::View* view) {
76 views::View* parent = view->parent();
77 int view_index = -1;
78 for (int i = 0, count = parent->child_count(); i < count; i++) {
79 if (view == parent->child_at(i)) {
80 view_index = i;
81 break;
82 }
83 }
84 DCHECK_GE(view_index, 0);
85 return view_index == 0 ? nullptr : parent->child_at(view_index - 1);
86 }
87
82 int MaskColor(int value) { 88 int MaskColor(int value) {
83 return value & 0xff; 89 return value & 0xff;
84 } 90 }
85 91
86 SkColor RGBAToSkColor(DOM::RGBA* rgba) { 92 SkColor RGBAToSkColor(DOM::RGBA* rgba) {
87 if (!rgba) 93 if (!rgba)
88 return SkColorSetARGB(0, 0, 0, 0); 94 return SkColorSetARGB(0, 0, 0, 0);
89 // Default alpha value is 0 (not visible) and need to convert alpha decimal 95 // Default alpha value is 0 (not visible) and need to convert alpha decimal
90 // percentage value to hex 96 // percentage value to hex
91 return SkColorSetARGB(MaskColor(static_cast<int>(rgba->getA(0) * 255)), 97 return SkColorSetARGB(MaskColor(static_cast<int>(rgba->getA(0) * 255)),
92 MaskColor(rgba->getR()), MaskColor(rgba->getG()), 98 MaskColor(rgba->getR()), MaskColor(rgba->getG()),
93 MaskColor(rgba->getB())); 99 MaskColor(rgba->getB()));
94 } 100 }
95 101
96 views::Widget* GetWidgetFromWindow(aura::Window* window) { 102 views::Widget* GetWidgetFromWindow(aura::Window* window) {
97 return views::Widget::GetWidgetForNativeView(window); 103 return views::Widget::GetWidgetForNativeView(window);
98 } 104 }
99 105
100 std::unique_ptr<DOM::Node> BuildDomNodeFromUIElement(UIElement* root) {
101 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
102 for (auto* it : root->children())
103 children->addItem(BuildDomNodeFromUIElement(it));
104
105 constexpr int kDomElementNodeType = 1;
106 std::unique_ptr<DOM::Node> node = DOM::Node::create()
107 .setNodeId(root->node_id())
108 .setNodeName(root->GetTypeName())
109 .setNodeType(kDomElementNodeType)
110 .setAttributes(GetAttributes(root))
111 .build();
112 node->setChildNodeCount(children->length());
113 node->setChildren(std::move(children));
114 return node;
115 }
116
117 } // namespace 106 } // namespace
118 107
119 AshDevToolsDOMAgent::AshDevToolsDOMAgent() : is_building_tree_(false) {} 108 AshDevToolsDOMAgent::AshDevToolsDOMAgent() {}
120 109
121 AshDevToolsDOMAgent::~AshDevToolsDOMAgent() { 110 AshDevToolsDOMAgent::~AshDevToolsDOMAgent() {
122 Reset(); 111 RemoveObservers();
123 } 112 }
124 113
125 ui::devtools::protocol::Response AshDevToolsDOMAgent::disable() { 114 ui::devtools::protocol::Response AshDevToolsDOMAgent::disable() {
126 Reset(); 115 Reset();
127 return ui::devtools::protocol::Response::OK(); 116 return ui::devtools::protocol::Response::OK();
128 } 117 }
129 118
130 ui::devtools::protocol::Response AshDevToolsDOMAgent::getDocument( 119 ui::devtools::protocol::Response AshDevToolsDOMAgent::getDocument(
131 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) { 120 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) {
132 *out_root = BuildInitialTree(); 121 *out_root = BuildInitialTree();
133 return ui::devtools::protocol::Response::OK(); 122 return ui::devtools::protocol::Response::OK();
134 } 123 }
135 124
136 ui::devtools::protocol::Response AshDevToolsDOMAgent::highlightNode( 125 ui::devtools::protocol::Response AshDevToolsDOMAgent::highlightNode(
137 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> 126 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig>
138 highlight_config, 127 highlight_config,
139 ui::devtools::protocol::Maybe<int> node_id) { 128 ui::devtools::protocol::Maybe<int> node_id) {
140 return HighlightNode(std::move(highlight_config), node_id.fromJust()); 129 return HighlightNode(std::move(highlight_config), node_id.fromJust());
141 } 130 }
142 131
143 ui::devtools::protocol::Response AshDevToolsDOMAgent::hideHighlight() { 132 ui::devtools::protocol::Response AshDevToolsDOMAgent::hideHighlight() {
144 if (widget_for_highlighting_ && widget_for_highlighting_->IsVisible()) 133 if (widget_for_highlighting_ && widget_for_highlighting_->IsVisible())
145 widget_for_highlighting_->Hide(); 134 widget_for_highlighting_->Hide();
146 return ui::devtools::protocol::Response::OK(); 135 return ui::devtools::protocol::Response::OK();
147 } 136 }
148 137
149 void AshDevToolsDOMAgent::OnUIElementAdded(UIElement* parent, 138 // Handles removing windows.
150 UIElement* child) { 139 void AshDevToolsDOMAgent::OnWindowHierarchyChanging(
151 // When parent is null, only need to update |node_id_to_ui_element_|. 140 const HierarchyChangeParams& params) {
152 if (!parent) { 141 // Only trigger this when params.receiver == params.old_parent.
153 node_id_to_ui_element_[child->node_id()] = child; 142 // Only removals are handled here. Removing a node can occur as a result of
154 return; 143 // reorganizing a window or just destroying it. OnWindowHierarchyChanged
155 } 144 // is only called if there is a new_parent. The only case this method isn't
156 // If tree is being built, don't add child to dom tree again. 145 // called is when adding a node because old_parent is then null.
157 if (is_building_tree_) 146 // Finally, We only trigger this 0 or 1 times as an old_parent will
158 return; 147 // either exist and only call this callback once, or not at all.
159 DCHECK(node_id_to_ui_element_.count(parent->node_id())); 148 if (params.receiver == params.old_parent)
160 149 RemoveWindowTree(params.target, true);
161 const auto& children = parent->children();
162 auto iter = std::find(children.begin(), children.end(), child);
163 int prev_node_id =
164 (iter == children.end() - 1) ? 0 : (*std::next(iter))->node_id();
165 frontend()->childNodeInserted(parent->node_id(), prev_node_id,
166 BuildTreeForUIElement(child));
167 } 150 }
168 151
169 void AshDevToolsDOMAgent::OnUIElementReordered(UIElement* parent, 152 // Handles adding windows.
170 UIElement* child) { 153 void AshDevToolsDOMAgent::OnWindowHierarchyChanged(
171 DCHECK(node_id_to_ui_element_.count(parent->node_id())); 154 const HierarchyChangeParams& params) {
172 155 // Only trigger this when params.receiver == params.new_parent.
173 const auto& children = parent->children(); 156 // If there is an old_parent + new_parent, then this window's node was
174 auto iter = std::find(children.begin(), children.end(), child); 157 // removed in OnWindowHierarchyChanging and will now be added to the
175 int prev_node_id = 158 // new_parent. If there is only a new_parent, OnWindowHierarchyChanging is
176 (iter == children.begin()) ? 0 : (*std::prev(iter))->node_id(); 159 // never called and the window is only added here.
177 RemoveDomNode(child); 160 if (params.receiver == params.new_parent)
178 frontend()->childNodeInserted(parent->node_id(), prev_node_id, 161 AddWindowTree(params.target);
179 BuildDomNodeFromUIElement(child));
180 } 162 }
181 163
182 void AshDevToolsDOMAgent::OnUIElementRemoved(UIElement* ui_element) { 164 void AshDevToolsDOMAgent::OnWindowStackingChanged(aura::Window* window) {
183 DCHECK(node_id_to_ui_element_.count(ui_element->node_id())); 165 RemoveWindowTree(window, false);
184 166 AddWindowTree(window);
185 RemoveDomNode(ui_element);
186 node_id_to_ui_element_.erase(ui_element->node_id());
187 } 167 }
188 168
189 void AshDevToolsDOMAgent::OnUIElementBoundsChanged(UIElement* ui_element) { 169 void AshDevToolsDOMAgent::OnWindowBoundsChanged(aura::Window* window,
170 const gfx::Rect& old_bounds,
171 const gfx::Rect& new_bounds) {
190 for (auto& observer : observers_) 172 for (auto& observer : observers_)
191 observer.OnNodeBoundsChanged(ui_element->node_id()); 173 observer.OnWindowBoundsChanged(window);
192 } 174 }
193 175
194 bool AshDevToolsDOMAgent::IsHighlightingWindow(aura::Window* window) { 176 void AshDevToolsDOMAgent::OnWillRemoveView(views::Widget* widget,
195 return widget_for_highlighting_ && 177 views::View* view) {
196 GetWidgetFromWindow(window) == widget_for_highlighting_.get(); 178 if (view == widget->GetRootView())
179 RemoveViewTree(view, nullptr, true);
180 }
181
182 void AshDevToolsDOMAgent::OnWidgetBoundsChanged(views::Widget* widget,
183 const gfx::Rect& new_bounds) {
184 for (auto& observer : observers_)
185 observer.OnWidgetBoundsChanged(widget);
186 }
187
188 void AshDevToolsDOMAgent::OnChildViewRemoved(views::View* parent,
189 views::View* view) {
190 RemoveViewTree(view, parent, true);
191 }
192
193 void AshDevToolsDOMAgent::OnChildViewAdded(views::View* parent,
194 views::View* view) {
195 AddViewTree(view);
196 }
197
198 void AshDevToolsDOMAgent::OnChildViewReordered(views::View* parent,
199 views::View* view) {
200 RemoveViewTree(view, parent, false);
201 AddViewTree(view);
202 }
203
204 void AshDevToolsDOMAgent::OnViewBoundsChanged(views::View* view) {
205 for (auto& observer : observers_)
206 observer.OnViewBoundsChanged(view);
207 }
208
209 aura::Window* AshDevToolsDOMAgent::GetWindowFromNodeId(int nodeId) {
210 return node_id_to_window_map_.count(nodeId) ? node_id_to_window_map_[nodeId]
211 : nullptr;
212 }
213
214 views::Widget* AshDevToolsDOMAgent::GetWidgetFromNodeId(int nodeId) {
215 return node_id_to_widget_map_.count(nodeId) ? node_id_to_widget_map_[nodeId]
216 : nullptr;
217 }
218
219 views::View* AshDevToolsDOMAgent::GetViewFromNodeId(int nodeId) {
220 return node_id_to_view_map_.count(nodeId) ? node_id_to_view_map_[nodeId]
221 : nullptr;
222 }
223
224 int AshDevToolsDOMAgent::GetNodeIdFromWindow(aura::Window* window) {
225 DCHECK(window_to_node_id_map_.count(window));
226 return window_to_node_id_map_[window];
227 }
228
229 int AshDevToolsDOMAgent::GetNodeIdFromWidget(views::Widget* widget) {
230 DCHECK(widget_to_node_id_map_.count(widget));
231 return widget_to_node_id_map_[widget];
232 }
233
234 int AshDevToolsDOMAgent::GetNodeIdFromView(views::View* view) {
235 DCHECK(view_to_node_id_map_.count(view));
236 return view_to_node_id_map_[view];
197 } 237 }
198 238
199 void AshDevToolsDOMAgent::AddObserver(AshDevToolsDOMAgentObserver* observer) { 239 void AshDevToolsDOMAgent::AddObserver(AshDevToolsDOMAgentObserver* observer) {
200 observers_.AddObserver(observer); 240 observers_.AddObserver(observer);
201 } 241 }
202 242
203 void AshDevToolsDOMAgent::RemoveObserver( 243 void AshDevToolsDOMAgent::RemoveObserver(
204 AshDevToolsDOMAgentObserver* observer) { 244 AshDevToolsDOMAgentObserver* observer) {
205 observers_.RemoveObserver(observer); 245 observers_.RemoveObserver(observer);
206 } 246 }
207 247
208 UIElement* AshDevToolsDOMAgent::GetElementFromNodeId(int node_id) {
209 return node_id_to_ui_element_[node_id];
210 }
211
212 void AshDevToolsDOMAgent::OnNodeBoundsChanged(int node_id) {
213 for (auto& observer : observers_)
214 observer.OnNodeBoundsChanged(node_id);
215 }
216
217 std::unique_ptr<ui::devtools::protocol::DOM::Node> 248 std::unique_ptr<ui::devtools::protocol::DOM::Node>
218 AshDevToolsDOMAgent::BuildInitialTree() { 249 AshDevToolsDOMAgent::BuildInitialTree() {
219 is_building_tree_ = true; 250 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
220 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); 251 for (aura::Window* window : Shell::GetAllRootWindows())
221 252 children->addItem(BuildTreeForWindow(window));
222 // TODO(thanhph): Root of UIElement tree shoudn't be WindowElement 253 return BuildNode("root", nullptr, std::move(children));
223 // but maybe a new different element type. 254 }
224 window_element_root_ = new WindowElement(nullptr, this, nullptr); 255
225 256 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForWindow(
226 for (aura::Window* window : Shell::GetAllRootWindows()) { 257 aura::Window* window) {
227 UIElement* window_element = 258 DCHECK(!window_to_node_id_map_.count(window));
228 new WindowElement(window, this, window_element_root_); 259 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
229 260 views::Widget* widget = GetWidgetFromWindow(window);
230 children->addItem(BuildTreeForUIElement(window_element)); 261 if (widget)
231 window_element_root_->AddChild(window_element); 262 children->addItem(BuildTreeForRootWidget(widget));
263 for (aura::Window* child : window->children()) {
264 if (!IsHighlightingWindow(child))
265 children->addItem(BuildTreeForWindow(child));
232 } 266 }
233 std::unique_ptr<ui::devtools::protocol::DOM::Node> root_node = BuildNode( 267
234 "root", nullptr, std::move(children), window_element_root_->node_id()); 268 std::unique_ptr<ui::devtools::protocol::DOM::Node> node =
235 is_building_tree_ = false; 269 BuildNode("Window", GetAttributes(window), std::move(children));
236 return root_node; 270 if (!window->HasObserver(this))
237 } 271 window->AddObserver(this);
238 272 window_to_node_id_map_[window] = node->getNodeId();
239 std::unique_ptr<ui::devtools::protocol::DOM::Node> 273 node_id_to_window_map_[node->getNodeId()] = window;
240 AshDevToolsDOMAgent::BuildTreeForUIElement(UIElement* ui_element) { 274 return node;
241 if (ui_element->type() == UIElementType::WINDOW) { 275 }
242 return BuildTreeForWindow( 276
243 ui_element, 277 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForRootWidget(
244 UIElement::GetBackingElement<aura::Window, WindowElement>(ui_element)); 278 views::Widget* widget) {
245 } else if (ui_element->type() == UIElementType::WIDGET) { 279 DCHECK(!widget_to_node_id_map_.count(widget));
246 return BuildTreeForRootWidget( 280 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
247 ui_element, 281 children->addItem(BuildTreeForView(widget->GetRootView()));
248 UIElement::GetBackingElement<views::Widget, WidgetElement>(ui_element)); 282 std::unique_ptr<ui::devtools::protocol::DOM::Node> node =
249 } else if (ui_element->type() == UIElementType::VIEW) { 283 BuildNode("Widget", GetAttributes(widget), std::move(children));
250 return BuildTreeForView( 284 if (!widget->HasRemovalsObserver(this))
251 ui_element, 285 widget->AddRemovalsObserver(this);
252 UIElement::GetBackingElement<views::View, ViewElement>(ui_element)); 286 widget_to_node_id_map_[widget] = node->getNodeId();
287 node_id_to_widget_map_[node->getNodeId()] = widget;
288 return node;
289 }
290
291 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForView(
292 views::View* view) {
293 DCHECK(!view_to_node_id_map_.count(view));
294 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
295 for (int i = 0, count = view->child_count(); i < count; i++)
296 children->addItem(BuildTreeForView(view->child_at(i)));
297 std::unique_ptr<ui::devtools::protocol::DOM::Node> node =
298 BuildNode("View", GetAttributes(view), std::move(children));
299 if (!view->HasObserver(this))
300 view->AddObserver(this);
301 view_to_node_id_map_[view] = node->getNodeId();
302 node_id_to_view_map_[node->getNodeId()] = view;
303 return node;
304 }
305
306 void AshDevToolsDOMAgent::AddWindowTree(aura::Window* window) {
307 if (IsHighlightingWindow(window))
308 return;
309
310 DCHECK(window_to_node_id_map_.count(window->parent()));
311 aura::Window* prev_sibling = FindPreviousSibling(window);
312 frontend()->childNodeInserted(
313 window_to_node_id_map_[window->parent()],
314 prev_sibling ? window_to_node_id_map_[prev_sibling] : 0,
315 BuildTreeForWindow(window));
316 }
317
318 void AshDevToolsDOMAgent::RemoveWindowTree(aura::Window* window,
319 bool remove_observer) {
320 DCHECK(window);
321 if (IsHighlightingWindow(window))
322 return;
323
324 if (GetWidgetFromWindow(window))
325 RemoveWidgetTree(GetWidgetFromWindow(window), remove_observer);
326
327 for (aura::Window* child : window->children())
328 RemoveWindowTree(child, remove_observer);
329
330 RemoveWindowNode(window, remove_observer);
331 }
332
333 void AshDevToolsDOMAgent::RemoveWindowNode(aura::Window* window,
334 bool remove_observer) {
335 WindowToNodeIdMap::iterator window_to_node_id_it =
336 window_to_node_id_map_.find(window);
337 DCHECK(window_to_node_id_it != window_to_node_id_map_.end());
338
339 int node_id = window_to_node_id_it->second;
340 int parent_id = GetNodeIdFromWindow(window->parent());
341
342 NodeIdToWindowMap::iterator node_id_to_window_it =
343 node_id_to_window_map_.find(node_id);
344 DCHECK(node_id_to_window_it != node_id_to_window_map_.end());
345
346 if (remove_observer)
347 window->RemoveObserver(this);
348
349 node_id_to_window_map_.erase(node_id_to_window_it);
350 window_to_node_id_map_.erase(window_to_node_id_it);
351 frontend()->childNodeRemoved(parent_id, node_id);
352 }
353
354 void AshDevToolsDOMAgent::RemoveWidgetTree(views::Widget* widget,
355 bool remove_observer) {
356 DCHECK(widget);
357 if (widget->GetRootView())
358 RemoveViewTree(widget->GetRootView(), nullptr, remove_observer);
359 RemoveWidgetNode(widget, remove_observer);
360 }
361
362 void AshDevToolsDOMAgent::RemoveWidgetNode(views::Widget* widget,
363 bool remove_observer) {
364 WidgetToNodeIdMap::iterator widget_to_node_id_it =
365 widget_to_node_id_map_.find(widget);
366 DCHECK(widget_to_node_id_it != widget_to_node_id_map_.end());
367
368 int node_id = widget_to_node_id_it->second;
369 int parent_id = GetNodeIdFromWindow(widget->GetNativeWindow());
370
371 if (remove_observer)
372 widget->RemoveRemovalsObserver(this);
373
374 NodeIdToWidgetMap::iterator node_id_to_widget_it =
375 node_id_to_widget_map_.find(node_id);
376 DCHECK(node_id_to_widget_it != node_id_to_widget_map_.end());
377
378 widget_to_node_id_map_.erase(widget_to_node_id_it);
379 node_id_to_widget_map_.erase(node_id_to_widget_it);
380 frontend()->childNodeRemoved(parent_id, node_id);
381 }
382
383 void AshDevToolsDOMAgent::AddViewTree(views::View* view) {
384 DCHECK(view_to_node_id_map_.count(view->parent()));
385 views::View* prev_sibling = FindPreviousSibling(view);
386 frontend()->childNodeInserted(
387 view_to_node_id_map_[view->parent()],
388 prev_sibling ? view_to_node_id_map_[prev_sibling] : 0,
389 BuildTreeForView(view));
390 }
391
392 void AshDevToolsDOMAgent::RemoveViewTree(views::View* view,
393 views::View* parent,
394 bool remove_observer) {
395 DCHECK(view);
396 for (int i = 0, count = view->child_count(); i < count; i++)
397 RemoveViewTree(view->child_at(i), view, remove_observer);
398 RemoveViewNode(view, parent, remove_observer);
399 }
400
401 void AshDevToolsDOMAgent::RemoveViewNode(views::View* view,
402 views::View* parent,
403 bool remove_observer) {
404 ViewToNodeIdMap::iterator view_to_node_id_it =
405 view_to_node_id_map_.find(view);
406 DCHECK(view_to_node_id_it != view_to_node_id_map_.end());
407
408 int node_id = view_to_node_id_it->second;
409 int parent_id = 0;
410 if (parent)
411 parent_id = GetNodeIdFromView(parent);
412 else // views::RootView
413 parent_id = GetNodeIdFromWidget(view->GetWidget());
414
415 if (remove_observer)
416 view->RemoveObserver(this);
417
418 NodeIdToViewMap::iterator node_id_to_view_it =
419 node_id_to_view_map_.find(node_id);
420 DCHECK(node_id_to_view_it != node_id_to_view_map_.end());
421
422 view_to_node_id_map_.erase(view_to_node_id_it);
423 node_id_to_view_map_.erase(node_id_to_view_it);
424 frontend()->childNodeRemoved(parent_id, node_id);
425 }
426
427 void AshDevToolsDOMAgent::RemoveObservers() {
428 for (auto& pair : window_to_node_id_map_)
429 pair.first->RemoveObserver(this);
430 for (auto& pair : widget_to_node_id_map_)
431 pair.first->RemoveRemovalsObserver(this);
432 for (auto& pair : view_to_node_id_map_)
433 pair.first->RemoveObserver(this);
434 }
435
436 void AshDevToolsDOMAgent::Reset() {
437 RemoveObservers();
438 widget_for_highlighting_.reset();
439 window_to_node_id_map_.clear();
440 widget_to_node_id_map_.clear();
441 view_to_node_id_map_.clear();
442 node_id_to_window_map_.clear();
443 node_id_to_widget_map_.clear();
444 node_id_to_view_map_.clear();
445 node_ids = 1;
446 }
447
448 AshDevToolsDOMAgent::WindowAndBoundsPair
449 AshDevToolsDOMAgent::GetNodeWindowAndBounds(int node_id) {
450 aura::Window* window = GetWindowFromNodeId(node_id);
451 if (window)
452 return std::make_pair(window, window->GetBoundsInScreen());
453
454 views::Widget* widget = GetWidgetFromNodeId(node_id);
455 if (widget) {
456 return std::make_pair(widget->GetNativeWindow(),
457 widget->GetWindowBoundsInScreen());
253 } 458 }
254 return nullptr; 459
255 } 460 views::View* view = GetViewFromNodeId(node_id);
256 461 if (view) {
257 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForWindow( 462 gfx::Rect bounds = view->GetBoundsInScreen();
258 UIElement* window_element_root, 463 return std::make_pair(view->GetWidget()->GetNativeWindow(), bounds);
259 aura::Window* window) {
260 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
261 views::Widget* widget = GetWidgetFromWindow(window);
262 if (widget) {
263 UIElement* widget_element =
264 new WidgetElement(widget, this, window_element_root);
265
266 children->addItem(BuildTreeForRootWidget(widget_element, widget));
267 window_element_root->AddChild(widget_element);
268 } 464 }
269 for (aura::Window* child : window->children()) { 465
270 UIElement* window_element = 466 return std::make_pair(nullptr, gfx::Rect());
271 new WindowElement(child, this, window_element_root);
272
273 children->addItem(BuildTreeForWindow(window_element, child));
274 window_element_root->AddChild(window_element);
275 }
276 std::unique_ptr<ui::devtools::protocol::DOM::Node> node =
277 BuildNode("Window", GetAttributes(window_element_root),
278 std::move(children), window_element_root->node_id());
279 return node;
280 }
281
282 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForRootWidget(
283 UIElement* widget_element,
284 views::Widget* widget) {
285 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
286
287 UIElement* view_element =
288 new ViewElement(widget->GetRootView(), this, widget_element);
289
290 children->addItem(BuildTreeForView(view_element, widget->GetRootView()));
291 widget_element->AddChild(view_element);
292
293 std::unique_ptr<ui::devtools::protocol::DOM::Node> node =
294 BuildNode("Widget", GetAttributes(widget_element), std::move(children),
295 widget_element->node_id());
296 return node;
297 }
298
299 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForView(
300 UIElement* view_element,
301 views::View* view) {
302 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
303
304 for (auto* child : view->GetChildrenInZOrder()) {
305 UIElement* view_element_child = new ViewElement(child, this, view_element);
306
307 children->addItem(BuildTreeForView(view_element_child, child));
308 view_element->AddChild(view_element_child);
309 }
310 std::unique_ptr<ui::devtools::protocol::DOM::Node> node =
311 BuildNode("View", GetAttributes(view_element), std::move(children),
312 view_element->node_id());
313 return node;
314 }
315
316 void AshDevToolsDOMAgent::RemoveDomNode(UIElement* ui_element) {
317 for (auto* child_element : ui_element->children())
318 RemoveDomNode(child_element);
319 frontend()->childNodeRemoved(ui_element->parent()->node_id(),
320 ui_element->node_id());
321 }
322
323 void AshDevToolsDOMAgent::Reset() {
324 is_building_tree_ = false;
325 widget_for_highlighting_.reset();
326 delete window_element_root_;
327 node_id_to_ui_element_.clear();
328 observers_.Clear();
329 } 467 }
330 468
331 void AshDevToolsDOMAgent::InitializeHighlightingWidget() { 469 void AshDevToolsDOMAgent::InitializeHighlightingWidget() {
332 DCHECK(!widget_for_highlighting_); 470 DCHECK(!widget_for_highlighting_);
333 widget_for_highlighting_.reset(new views::Widget); 471 widget_for_highlighting_.reset(new views::Widget);
334 views::Widget::InitParams params; 472 views::Widget::InitParams params;
335 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; 473 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
336 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; 474 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
337 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 475 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
338 params.opacity = views::Widget::InitParams::WindowOpacity::TRANSLUCENT_WINDOW; 476 params.opacity = views::Widget::InitParams::WindowOpacity::TRANSLUCENT_WINDOW;
339 params.name = "HighlightingWidget"; 477 params.name = "HighlightingWidget";
340 Shell::GetPrimaryRootWindowController() 478 Shell::GetPrimaryRootWindowController()
341 ->ConfigureWidgetInitParamsForContainer(widget_for_highlighting_.get(), 479 ->ConfigureWidgetInitParamsForContainer(widget_for_highlighting_.get(),
342 kShellWindowId_OverlayContainer, 480 kShellWindowId_OverlayContainer,
343 &params); 481 &params);
344 params.keep_on_top = true; 482 params.keep_on_top = true;
345 params.accept_events = false; 483 params.accept_events = false;
346 widget_for_highlighting_->Init(params); 484 widget_for_highlighting_->Init(params);
347 } 485 }
348 486
349 void AshDevToolsDOMAgent::UpdateHighlight( 487 void AshDevToolsDOMAgent::UpdateHighlight(
350 const std::pair<aura::Window*, gfx::Rect>& window_and_bounds, 488 const WindowAndBoundsPair& window_and_bounds,
351 SkColor background, 489 SkColor background,
352 SkColor border) { 490 SkColor border) {
353 constexpr int kBorderThickness = 1; 491 constexpr int kBorderThickness = 1;
354 views::View* root_view = widget_for_highlighting_->GetRootView(); 492 views::View* root_view = widget_for_highlighting_->GetRootView();
355 root_view->SetBorder(views::CreateSolidBorder(kBorderThickness, border)); 493 root_view->SetBorder(views::CreateSolidBorder(kBorderThickness, border));
356 root_view->set_background( 494 root_view->set_background(
357 views::Background::CreateSolidBackground(background)); 495 views::Background::CreateSolidBackground(background));
358 display::Display display = 496 display::Display display =
359 display::Screen::GetScreen()->GetDisplayNearestWindow( 497 display::Screen::GetScreen()->GetDisplayNearestWindow(
360 window_and_bounds.first); 498 window_and_bounds.first);
361 widget_for_highlighting_->GetNativeWindow()->SetBoundsInScreen( 499 widget_for_highlighting_->GetNativeWindow()->SetBoundsInScreen(
362 window_and_bounds.second, display); 500 window_and_bounds.second, display);
363 } 501 }
364 502
365 ui::devtools::protocol::Response AshDevToolsDOMAgent::HighlightNode( 503 ui::devtools::protocol::Response AshDevToolsDOMAgent::HighlightNode(
366 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> 504 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig>
367 highlight_config, 505 highlight_config,
368 int node_id) { 506 int node_id) {
369 if (!widget_for_highlighting_) 507 if (!widget_for_highlighting_)
370 InitializeHighlightingWidget(); 508 InitializeHighlightingWidget();
371 509
372 std::pair<aura::Window*, gfx::Rect> window_and_bounds = 510 WindowAndBoundsPair window_and_bounds(GetNodeWindowAndBounds(node_id));
373 node_id_to_ui_element_.count(node_id)
374 ? node_id_to_ui_element_[node_id]->GetNodeWindowAndBounds()
375 : std::make_pair<aura::Window*, gfx::Rect>(nullptr, gfx::Rect());
376 511
377 if (!window_and_bounds.first) { 512 if (!window_and_bounds.first)
378 return ui::devtools::protocol::Response::Error( 513 return ui::devtools::protocol::Response::Error(
379 "No node found with that id"); 514 "No node found with that id");
380 } 515
381 SkColor border_color = 516 SkColor border_color =
382 RGBAToSkColor(highlight_config->getBorderColor(nullptr)); 517 RGBAToSkColor(highlight_config->getBorderColor(nullptr));
383 SkColor content_color = 518 SkColor content_color =
384 RGBAToSkColor(highlight_config->getContentColor(nullptr)); 519 RGBAToSkColor(highlight_config->getContentColor(nullptr));
385 UpdateHighlight(window_and_bounds, content_color, border_color); 520 UpdateHighlight(window_and_bounds, content_color, border_color);
386 521
387 if (!widget_for_highlighting_->IsVisible()) 522 if (!widget_for_highlighting_->IsVisible())
388 widget_for_highlighting_->Show(); 523 widget_for_highlighting_->Show();
389 524
390 return ui::devtools::protocol::Response::OK(); 525 return ui::devtools::protocol::Response::OK();
391 } 526 }
392 527
528 bool AshDevToolsDOMAgent::IsHighlightingWindow(aura::Window* window) {
529 return widget_for_highlighting_ &&
530 GetWidgetFromWindow(window) == widget_for_highlighting_.get();
531 }
532
393 } // namespace devtools 533 } // namespace devtools
394 } // namespace ash 534 } // namespace ash
OLDNEW
« no previous file with comments | « ash/devtools/ash_devtools_dom_agent.h ('k') | ash/devtools/ash_devtools_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698