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

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

Issue 2466073003: Push updates to inspector when windows are added, destroyed or reorganized (Closed)
Patch Set: Push updates to inspector when windows are added, destroyed or reorganized 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
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_window.h" 7 #include "ash/common/wm_window.h"
8 #include "components/ui_devtools/devtools_server.h" 8 #include "components/ui_devtools/devtools_server.h"
9 #include "ui/views/view.h" 9 #include "ui/views/view.h"
10 #include "ui/views/widget/widget.h" 10 #include "ui/views/widget/widget.h"
11 11
12 namespace ash { 12 namespace ash {
13 namespace devtools { 13 namespace devtools {
14 14
15 namespace { 15 namespace {
16 using namespace ui::devtools::protocol; 16 using namespace ui::devtools::protocol;
17 DOM::NodeId node_ids = 1;
17 18
18 std::unique_ptr<DOM::Node> BuildNode( 19 std::unique_ptr<DOM::Node> BuildNode(
19 const std::string& name, 20 const std::string& name,
20 std::unique_ptr<Array<std::string>> attributes, 21 std::unique_ptr<Array<std::string>> attributes,
21 std::unique_ptr<Array<DOM::Node>> children) { 22 std::unique_ptr<Array<DOM::Node>> children) {
22 static DOM::NodeId node_ids = 0;
23 constexpr int kDomElementNodeType = 1; 23 constexpr int kDomElementNodeType = 1;
24 std::unique_ptr<DOM::Node> node = DOM::Node::create() 24 std::unique_ptr<DOM::Node> node = DOM::Node::create()
25 .setNodeId(node_ids++) 25 .setNodeId(node_ids++)
26 .setNodeName(name) 26 .setNodeName(name)
27 .setNodeType(kDomElementNodeType) 27 .setNodeType(kDomElementNodeType)
28 .setAttributes(std::move(attributes)) 28 .setAttributes(std::move(attributes))
29 .build(); 29 .build();
30 node->setChildNodeCount(children->length()); 30 node->setChildNodeCount(children->length());
31 node->setChildren(std::move(children)); 31 node->setChildren(std::move(children));
32 return node; 32 return node;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 } 64 }
65 return BuildNode("View", GetAttributes(view), std::move(children)); 65 return BuildNode("View", GetAttributes(view), std::move(children));
66 } 66 }
67 67
68 std::unique_ptr<DOM::Node> BuildTreeForRootWidget(views::Widget* widget) { 68 std::unique_ptr<DOM::Node> BuildTreeForRootWidget(views::Widget* widget) {
69 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); 69 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
70 children->addItem(BuildTreeForView(widget->GetRootView())); 70 children->addItem(BuildTreeForView(widget->GetRootView()));
71 return BuildNode("Widget", GetAttributes(widget), std::move(children)); 71 return BuildNode("Widget", GetAttributes(widget), std::move(children));
72 } 72 }
73 73
74 std::unique_ptr<DOM::Node> BuildTreeForWindow(ash::WmWindow* window) { 74 } // namespace
75
76 AshDevToolsDOMAgent::AshDevToolsDOMAgent(ash::WmShell* shell) : shell_(shell) {
77 DCHECK(shell_);
78 }
79
80 AshDevToolsDOMAgent::~AshDevToolsDOMAgent() {
81 RemoveObserverFromRootWindows();
82 }
83
84 void AshDevToolsDOMAgent::disable(ui::devtools::protocol::ErrorString* error) {
85 RemoveObserverFromRootWindows();
86 }
87
88 void AshDevToolsDOMAgent::getDocument(
89 ui::devtools::protocol::ErrorString* error,
90 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) {
91 Reset();
92 *out_root = BuildInitialTree();
93 }
94
95 void AshDevToolsDOMAgent::OnWindowTreeChanging(WmWindow* window,
sadrul 2016/11/02 02:04:36 I don't think this gets called if sibling windows
Sarmad Hashmi 2016/11/03 01:42:17 Done.
96 const TreeChangeParams& params) {
97 // If there is an old_parent + new_parent, then this window is being moved
98 // which requires a remove followed by an add. If only old_parent exists, then
99 // this is being destroyed, only remove is called. Finally, if only new_parent
100 // exists, then this is being created so only add is called.
101 if (params.old_parent)
102 RemoveWindowNode(params.target);
103 if (params.new_parent)
104 AddWindowNode(params.target, params.new_parent);
105 }
106
107 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForWindow(
108 ash::WmWindow* window) {
75 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); 109 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
76 for (ash::WmWindow* child : window->GetChildren()) { 110 for (ash::WmWindow* child : window->GetChildren()) {
77 children->addItem(BuildTreeForWindow(child)); 111 children->addItem(BuildTreeForWindow(child));
78 views::Widget* widget = child->GetInternalWidget(); 112 views::Widget* widget = child->GetInternalWidget();
79 if (widget) 113 if (widget)
80 children->addItem(BuildTreeForRootWidget(widget)); 114 children->addItem(BuildTreeForRootWidget(widget));
81 } 115 }
82 return BuildNode("Window", GetAttributes(window), std::move(children)); 116 std::unique_ptr<ui::devtools::protocol::DOM::Node> node =
117 BuildNode("Window", GetAttributes(window), std::move(children));
118 window_to_node_id_map_[window] = node->getNodeId();
119 return node;
83 } 120 }
84 121
85 } // namespace
86
87 AshDevToolsDOMAgent::AshDevToolsDOMAgent(ash::WmShell* shell) : shell_(shell) {
88 DCHECK(shell_);
89 }
90
91 AshDevToolsDOMAgent::~AshDevToolsDOMAgent() {}
92
93 std::unique_ptr<ui::devtools::protocol::DOM::Node> 122 std::unique_ptr<ui::devtools::protocol::DOM::Node>
94 AshDevToolsDOMAgent::BuildInitialTree() { 123 AshDevToolsDOMAgent::BuildInitialTree() {
95 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); 124 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
96 for (ash::WmWindow* window : shell_->GetAllRootWindows()) { 125 for (ash::WmWindow* window : shell_->GetAllRootWindows()) {
97 children->addItem(BuildTreeForWindow(window)); 126 children->addItem(BuildTreeForWindow(window));
98 } 127 }
99 return BuildNode("root", nullptr, std::move(children)); 128 return BuildNode("root", nullptr, std::move(children));
100 } 129 }
101 130
102 void AshDevToolsDOMAgent::getDocument( 131 void AshDevToolsDOMAgent::AddWindowNode(WmWindow* window, WmWindow* parent) {
103 ui::devtools::protocol::ErrorString* error, 132 frontend()->childNodeInserted(window_to_node_id_map_[parent], 0,
sadrul 2016/11/02 02:04:36 Should 'prev_sibling' be correctly computed? (e.g.
Sarmad Hashmi 2016/11/03 01:42:17 Done.
104 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) { 133 BuildTreeForWindow(window));
105 *out_root = BuildInitialTree(); 134 }
135
136 void AshDevToolsDOMAgent::RemoveWindowNode(WmWindow* window) {
137 WindowToNodeIdMap::iterator it = window_to_node_id_map_.find(window);
138 DCHECK(it != window_to_node_id_map_.end());
139
140 int node_id = it->second;
141 int parent_id =
142 window->GetParent() ? window_to_node_id_map_[window->GetParent()] : 0;
sadrul 2016/11/02 02:04:36 window->GetParent() should never be null here, rig
Sarmad Hashmi 2016/11/03 01:42:18 Discussed in-person. It can be null for root windo
143
144 window_to_node_id_map_.erase(it);
145 frontend()->childNodeRemoved(parent_id, node_id);
146 }
147
148 void AshDevToolsDOMAgent::AddObserverToRootWindows() {
149 // TODO(mhashmi): Use ShellObserver events to add observers to new root
150 // windows
151 for (ash::WmWindow* window : shell_->GetAllRootWindows()) {
152 window->AddObserver(this);
sadrul 2016/11/02 02:04:36 No {}
Sarmad Hashmi 2016/11/03 01:42:17 Done.
153 }
154 }
155
156 void AshDevToolsDOMAgent::RemoveObserverFromRootWindows() {
157 for (ash::WmWindow* window : shell_->GetAllRootWindows()) {
sadrul 2016/11/02 02:04:36 ditto
Sarmad Hashmi 2016/11/03 01:42:18 Done.
158 window->RemoveObserver(this);
159 }
160 }
161
162 void AshDevToolsDOMAgent::Reset() {
163 AddObserverToRootWindows();
164 window_to_node_id_map_.clear();
165 node_ids = 1;
106 } 166 }
107 167
108 } // namespace devtools 168 } // namespace devtools
109 } // namespace ash 169 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698