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