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

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

Issue 2486543003: Add CSS agent for various window/widget/view attributes in devtools (Closed)
Patch Set: sadruls comments Created 4 years, 1 month 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/common/devtools/ash_devtools_dom_agent.h ('k') | ash/common/wm_shell.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/common/devtools/ash_devtools_dom_agent.h" 5 #include "ash/common/devtools/ash_devtools_dom_agent.h"
6 6
7 #include "ash/common/wm_lookup.h"
7 #include "ash/common/wm_window.h" 8 #include "ash/common/wm_window.h"
8 #include "components/ui_devtools/devtools_server.h" 9 #include "components/ui_devtools/devtools_server.h"
9 #include "ui/views/view.h"
10 #include "ui/views/widget/widget.h"
11 10
12 namespace ash { 11 namespace ash {
13 namespace devtools { 12 namespace devtools {
14 13
15 namespace { 14 namespace {
16 using namespace ui::devtools::protocol; 15 using namespace ui::devtools::protocol;
17 DOM::NodeId node_ids = 1; 16 DOM::NodeId node_ids = 1;
18 17
19 std::unique_ptr<DOM::Node> BuildNode( 18 std::unique_ptr<DOM::Node> BuildNode(
20 const std::string& name, 19 const std::string& name,
(...skipping 29 matching lines...) Expand all
50 return attributes; 49 return attributes;
51 } 50 }
52 51
53 std::unique_ptr<Array<std::string>> GetAttributes(const views::View* view) { 52 std::unique_ptr<Array<std::string>> GetAttributes(const views::View* view) {
54 std::unique_ptr<Array<std::string>> attributes = Array<std::string>::create(); 53 std::unique_ptr<Array<std::string>> attributes = Array<std::string>::create();
55 attributes->addItem("name"); 54 attributes->addItem("name");
56 attributes->addItem(view->GetClassName()); 55 attributes->addItem(view->GetClassName());
57 return attributes; 56 return attributes;
58 } 57 }
59 58
60 std::unique_ptr<DOM::Node> BuildTreeForView(views::View* view) {
61 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
62 for (int i = 0, count = view->child_count(); i < count; i++) {
63 children->addItem(BuildTreeForView(view->child_at(i)));
64 }
65 return BuildNode("View", GetAttributes(view), std::move(children));
66 }
67
68 std::unique_ptr<DOM::Node> BuildTreeForRootWidget(views::Widget* widget) {
69 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
70 children->addItem(BuildTreeForView(widget->GetRootView()));
71 return BuildNode("Widget", GetAttributes(widget), std::move(children));
72 }
73 59
74 WmWindow* FindPreviousSibling(WmWindow* window) { 60 WmWindow* FindPreviousSibling(WmWindow* window) {
75 std::vector<WmWindow*> siblings = window->GetParent()->GetChildren(); 61 std::vector<WmWindow*> siblings = window->GetParent()->GetChildren();
76 std::vector<WmWindow*>::iterator it = 62 std::vector<WmWindow*>::iterator it =
77 std::find(siblings.begin(), siblings.end(), window); 63 std::find(siblings.begin(), siblings.end(), window);
78 DCHECK(it != siblings.end()); 64 DCHECK(it != siblings.end());
79 // If this is the first child of its parent, the previous sibling is null 65 // If this is the first child of its parent, the previous sibling is null
80 return it == siblings.begin() ? nullptr : *std::prev(it); 66 return it == siblings.begin() ? nullptr : *std::prev(it);
81 } 67 }
82 68
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 // the window is only added here. 111 // the window is only added here.
126 if (window == params.new_parent) 112 if (window == params.new_parent)
127 AddWindowNode(params.target); 113 AddWindowNode(params.target);
128 } 114 }
129 115
130 void AshDevToolsDOMAgent::OnWindowStackingChanged(WmWindow* window) { 116 void AshDevToolsDOMAgent::OnWindowStackingChanged(WmWindow* window) {
131 RemoveWindowNode(window); 117 RemoveWindowNode(window);
132 AddWindowNode(window); 118 AddWindowNode(window);
133 } 119 }
134 120
121 WmWindow* AshDevToolsDOMAgent::GetWindowFromNodeId(int nodeId) {
122 return node_id_to_window_map_.count(nodeId) ? node_id_to_window_map_[nodeId]
123 : nullptr;
124 }
125
126 views::Widget* AshDevToolsDOMAgent::GetWidgetFromNodeId(int nodeId) {
127 return node_id_to_widget_map_.count(nodeId) ? node_id_to_widget_map_[nodeId]
128 : nullptr;
129 }
130
131 views::View* AshDevToolsDOMAgent::GetViewFromNodeId(int nodeId) {
132 return node_id_to_view_map_.count(nodeId) ? node_id_to_view_map_[nodeId]
133 : nullptr;
134 }
135
136 int AshDevToolsDOMAgent::GetNodeIdFromWindow(WmWindow* window) {
137 DCHECK(window_to_node_id_map_.count(window));
138 return window_to_node_id_map_[window];
139 }
140
141 int AshDevToolsDOMAgent::GetNodeIdFromWidget(views::Widget* widget) {
142 DCHECK(widget_to_node_id_map_.count(widget));
143 return widget_to_node_id_map_[widget];
144 }
145
146 int AshDevToolsDOMAgent::GetNodeIdFromView(views::View* view) {
147 DCHECK(view_to_node_id_map_.count(view));
148 return view_to_node_id_map_[view];
149 }
150
151 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForView(
152 views::View* view) {
153 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
154 for (int i = 0, count = view->child_count(); i < count; i++) {
155 children->addItem(BuildTreeForView(view->child_at(i)));
156 }
157 std::unique_ptr<ui::devtools::protocol::DOM::Node> node =
158 BuildNode("View", GetAttributes(view), std::move(children));
159 view_to_node_id_map_[view] = node->getNodeId();
160 node_id_to_view_map_[node->getNodeId()] = view;
161 return node;
162 }
163
164 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForRootWidget(
165 views::Widget* widget) {
166 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
167 children->addItem(BuildTreeForView(widget->GetRootView()));
168 std::unique_ptr<ui::devtools::protocol::DOM::Node> node =
169 BuildNode("Widget", GetAttributes(widget), std::move(children));
170 widget_to_node_id_map_[widget] = node->getNodeId();
171 node_id_to_widget_map_[node->getNodeId()] = widget;
172 return node;
173 }
174
135 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForWindow( 175 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForWindow(
136 ash::WmWindow* window) { 176 ash::WmWindow* window) {
137 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); 177 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
138 views::Widget* widget = window->GetInternalWidget(); 178 views::Widget* widget = window->GetInternalWidget();
139 if (widget) 179 if (widget)
140 children->addItem(BuildTreeForRootWidget(widget)); 180 children->addItem(BuildTreeForRootWidget(widget));
141 for (ash::WmWindow* child : window->GetChildren()) { 181 for (ash::WmWindow* child : window->GetChildren()) {
142 children->addItem(BuildTreeForWindow(child)); 182 children->addItem(BuildTreeForWindow(child));
143 } 183 }
144 std::unique_ptr<ui::devtools::protocol::DOM::Node> node = 184 std::unique_ptr<ui::devtools::protocol::DOM::Node> node =
145 BuildNode("Window", GetAttributes(window), std::move(children)); 185 BuildNode("Window", GetAttributes(window), std::move(children));
146 // Only add as observer if window is not in map 186 // Only add as observer if window is not in map
147 if (!window_to_node_id_map_.count(window)) 187 if (!window_to_node_id_map_.count(window))
148 window->AddObserver(this); 188 window->AddObserver(this);
149 window_to_node_id_map_[window] = node->getNodeId(); 189 window_to_node_id_map_[window] = node->getNodeId();
190 node_id_to_window_map_[node->getNodeId()] = window;
150 return node; 191 return node;
151 } 192 }
152 193
153 std::unique_ptr<ui::devtools::protocol::DOM::Node> 194 std::unique_ptr<ui::devtools::protocol::DOM::Node>
154 AshDevToolsDOMAgent::BuildInitialTree() { 195 AshDevToolsDOMAgent::BuildInitialTree() {
155 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); 196 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
156 for (ash::WmWindow* window : shell_->GetAllRootWindows()) { 197 for (ash::WmWindow* window : shell_->GetAllRootWindows()) {
157 children->addItem(BuildTreeForWindow(window)); 198 children->addItem(BuildTreeForWindow(window));
158 } 199 }
159 return BuildNode("root", nullptr, std::move(children)); 200 return BuildNode("root", nullptr, std::move(children));
160 } 201 }
161 202
162 void AshDevToolsDOMAgent::AddWindowNode(WmWindow* window) { 203 void AshDevToolsDOMAgent::AddWindowNode(WmWindow* window) {
163 DCHECK(window_to_node_id_map_.count(window->GetParent())); 204 DCHECK(window_to_node_id_map_.count(window->GetParent()));
164 WmWindow* prev_sibling = FindPreviousSibling(window); 205 WmWindow* prev_sibling = FindPreviousSibling(window);
165 frontend()->childNodeInserted( 206 frontend()->childNodeInserted(
166 window_to_node_id_map_[window->GetParent()], 207 window_to_node_id_map_[window->GetParent()],
167 prev_sibling ? window_to_node_id_map_[prev_sibling] : 0, 208 prev_sibling ? window_to_node_id_map_[prev_sibling] : 0,
168 BuildTreeForWindow(window)); 209 BuildTreeForWindow(window));
169 } 210 }
170 211
171 void AshDevToolsDOMAgent::RemoveWindowNode(WmWindow* window) { 212 void AshDevToolsDOMAgent::RemoveWindowNode(WmWindow* window) {
172 WmWindow* parent = window->GetParent(); 213 WmWindow* parent = window->GetParent();
173 DCHECK(parent); 214 DCHECK(parent);
174 DCHECK(window_to_node_id_map_.count(parent)); 215 WindowToNodeIdMap::iterator window_to_node_id_it =
175 WindowToNodeIdMap::iterator it = window_to_node_id_map_.find(window); 216 window_to_node_id_map_.find(window);
176 DCHECK(it != window_to_node_id_map_.end()); 217 DCHECK(window_to_node_id_it != window_to_node_id_map_.end());
177 218
178 int node_id = it->second; 219 int node_id = window_to_node_id_it->second;
179 int parent_id = window_to_node_id_map_[parent]; 220 int parent_id = GetNodeIdFromWindow(parent);
221
222 NodeIdToWindowMap::iterator node_id_to_window_it =
223 node_id_to_window_map_.find(node_id);
224 DCHECK(node_id_to_window_it != node_id_to_window_map_.end());
225
226 views::Widget* widget = window->GetInternalWidget();
227 if (widget)
228 RemoveWidgetNode(widget);
180 229
181 window->RemoveObserver(this); 230 window->RemoveObserver(this);
182 window_to_node_id_map_.erase(it); 231 node_id_to_window_map_.erase(node_id_to_window_it);
232 window_to_node_id_map_.erase(window_to_node_id_it);
183 frontend()->childNodeRemoved(parent_id, node_id); 233 frontend()->childNodeRemoved(parent_id, node_id);
184 } 234 }
185 235
236 void AshDevToolsDOMAgent::RemoveWidgetNode(views::Widget* widget) {
237 WidgetToNodeIdMap::iterator widget_to_node_id_it =
238 widget_to_node_id_map_.find(widget);
239 DCHECK(widget_to_node_id_it != widget_to_node_id_map_.end());
240
241 int node_id = widget_to_node_id_it->second;
242 int parent_id =
243 GetNodeIdFromWindow(WmLookup::Get()->GetWindowForWidget(widget));
244
245 RemoveViewNode(widget->GetRootView());
246
247 NodeIdToWidgetMap::iterator node_id_to_widget_it =
248 node_id_to_widget_map_.find(node_id);
249 DCHECK(node_id_to_widget_it != node_id_to_widget_map_.end());
250
251 widget_to_node_id_map_.erase(widget_to_node_id_it);
252 node_id_to_widget_map_.erase(node_id_to_widget_it);
253 frontend()->childNodeRemoved(parent_id, node_id);
254 }
255
256 void AshDevToolsDOMAgent::RemoveViewNode(views::View* view) {
257 // TODO(mhashmi): Add observers to views/widgets so new views exist
258 // in the map and can be removed here
259 }
260
186 void AshDevToolsDOMAgent::RemoveObserverFromAllWindows() { 261 void AshDevToolsDOMAgent::RemoveObserverFromAllWindows() {
187 for (auto& pair : window_to_node_id_map_) 262 for (auto& pair : window_to_node_id_map_)
188 pair.first->RemoveObserver(this); 263 pair.first->RemoveObserver(this);
189 } 264 }
190 265
191 void AshDevToolsDOMAgent::Reset() { 266 void AshDevToolsDOMAgent::Reset() {
192 RemoveObserverFromAllWindows(); 267 RemoveObserverFromAllWindows();
193 window_to_node_id_map_.clear(); 268 window_to_node_id_map_.clear();
269 widget_to_node_id_map_.clear();
270 view_to_node_id_map_.clear();
271 node_id_to_window_map_.clear();
272 node_id_to_widget_map_.clear();
273 node_id_to_view_map_.clear();
194 node_ids = 1; 274 node_ids = 1;
195 } 275 }
196 276
197 } // namespace devtools 277 } // namespace devtools
198 } // namespace ash 278 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/devtools/ash_devtools_dom_agent.h ('k') | ash/common/wm_shell.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698