Chromium Code Reviews| Index: ash/common/devtools/devtools_client_dom.cc |
| diff --git a/ash/common/devtools/devtools_client_dom.cc b/ash/common/devtools/devtools_client_dom.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5e0e3ee875799328df1c16af991e8782b5e9ed48 |
| --- /dev/null |
| +++ b/ash/common/devtools/devtools_client_dom.cc |
| @@ -0,0 +1,127 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/common/devtools/devtools_client_dom.h" |
| + |
| +#include "ash/common/wm_shell.h" |
| +#include "ash/common/wm_window.h" |
| +#include "components/ui_devtools/DOM.h" |
| +#include "components/ui_devtools/devtools_server.h" |
| +#include "ui/views/view.h" |
| +#include "ui/views/widget/widget.h" |
| + |
| +namespace ui { |
| +namespace devtools { |
| + |
| +namespace { |
| +using namespace protocol; |
| + |
| +std::unique_ptr<DOM::Node> BuildNode( |
| + const String& name, |
|
sadrul
2016/10/26 19:20:41
Is this std::string? If so, use std::string in all
Sarmad Hashmi
2016/10/26 20:22:58
Done.
|
| + std::unique_ptr<Array<String>> attributes, |
| + std::unique_ptr<Array<DOM::Node>> children) { |
| + static DOM::NodeId node_ids = 0; |
| + return DOM::Node::create() |
| + .setNodeId(node_ids++) |
| + .setNodeName(name) |
| + .setNodeType(1) |
|
sadrul
2016/10/26 19:20:41
Let's give this a name so it's easier to read:
Sarmad Hashmi
2016/10/26 20:22:57
Done.
|
| + .setAttributes(std::move(attributes)) |
| + .setChildNodeCount(children->length()) |
| + .setChildren(std::move(children)) |
| + .build(); |
| +} |
| + |
| +std::unique_ptr<Array<String>> GetAttributes(const ash::WmWindow* window) { |
| + std::unique_ptr<Array<String>> attributes = Array<String>::create(); |
| + attributes->addItem("name"); |
| + attributes->addItem(window->GetName()); |
| + attributes->addItem("visible"); |
| + attributes->addItem(window->IsVisible() ? "true" : "false"); |
| + attributes->addItem("active"); |
| + attributes->addItem(window->IsActive() ? "true" : "false"); |
| + attributes->addItem("bounds_height"); |
| + attributes->addItem(base::IntToString(window->GetBounds().height())); |
| + attributes->addItem("bounds_width"); |
| + attributes->addItem(base::IntToString(window->GetBounds().width())); |
|
sadrul
2016/10/26 19:20:41
A number of these attributes (e.g. bounds, visibil
Sarmad Hashmi
2016/10/26 20:22:58
Yep you're right. Done.
|
| + return attributes; |
| +} |
| + |
| +std::unique_ptr<Array<String>> GetAttributes(const views::Widget* widget) { |
| + std::unique_ptr<Array<String>> attributes = Array<String>::create(); |
| + attributes->addItem("name"); |
| + attributes->addItem(widget->GetName()); |
| + attributes->addItem("visible"); |
| + attributes->addItem(widget->IsVisible() ? "true" : "false"); |
| + attributes->addItem("active"); |
| + attributes->addItem(widget->IsActive() ? "true" : "false"); |
| + return attributes; |
| +} |
| + |
| +std::unique_ptr<Array<String>> GetAttributes(const views::View* view) { |
| + std::unique_ptr<Array<String>> attributes = Array<String>::create(); |
| + attributes->addItem("name"); |
| + attributes->addItem(view->GetClassName()); |
| + attributes->addItem("visible"); |
| + attributes->addItem(view->visible() ? "true" : "false"); |
| + attributes->addItem("x"); |
| + attributes->addItem(base::IntToString(view->x())); |
| + attributes->addItem("y"); |
| + attributes->addItem(base::IntToString(view->y())); |
| + attributes->addItem("bounds_height"); |
| + attributes->addItem(base::IntToString(view->bounds().bottom())); |
| + attributes->addItem("bounds_width"); |
| + attributes->addItem(base::IntToString(view->bounds().right())); |
| + return attributes; |
| +} |
| + |
| +std::unique_ptr<DOM::Node> BuildTreeForRootView(views::View* view) { |
|
sadrul
2016/10/26 19:20:41
BuildTreeForView, since this is called for all Vie
Sarmad Hashmi
2016/10/26 20:22:57
Done.
|
| + std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); |
| + for (int i = 0, count = view->child_count(); i < count; i++) { |
| + children->addItem(BuildTreeForRootView(view->child_at(i))); |
| + } |
| + return BuildNode("View", GetAttributes(view), std::move(children)); |
| +} |
| + |
| +std::unique_ptr<DOM::Node> BuildTreeForRootWidget(views::Widget* widget) { |
|
sadrul
2016/10/26 19:20:41
BuildTreeForWidget
Sarmad Hashmi
2016/10/26 20:22:58
Done.
|
| + std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); |
| + children->addItem(BuildTreeForRootView(widget->GetRootView())); |
| + return BuildNode("Widget", GetAttributes(widget), std::move(children)); |
| +} |
| + |
| +std::unique_ptr<DOM::Node> BuildTreeForRootWindow(ash::WmWindow* window) { |
|
sadrul
2016/10/26 19:20:41
BuildTreeForWindow
Sarmad Hashmi
2016/10/26 20:22:57
Done.
|
| + std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); |
| + for (ash::WmWindow* child : window->GetChildren()) { |
| + children->addItem(BuildTreeForRootWindow(child)); |
| + views::Widget* widget = child->GetInternalWidget(); |
| + if (widget) |
| + children->addItem(BuildTreeForRootWidget(widget)); |
| + } |
| + return BuildNode("Window", GetAttributes(window), std::move(children)); |
| +} |
| + |
| +} // namespace |
| + |
| +AshDevToolsClientDOM::AshDevToolsClientDOM(ash::WmShell* shell) |
| + : shell_(shell) { |
| + DCHECK(shell_); |
| +} |
| + |
| +AshDevToolsClientDOM::~AshDevToolsClientDOM() {} |
| + |
| +std::unique_ptr<protocol::DOM::Node> AshDevToolsClientDOM::BuildInitialTree() { |
| + std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); |
| + for (ash::WmWindow* window : shell_->GetAllRootWindows()) { |
| + children->addItem(BuildTreeForRootWindow(window)); |
| + } |
| + return BuildNode("root", nullptr, std::move(children)); |
| +} |
| + |
| +void AshDevToolsClientDOM::getDocument( |
| + protocol::ErrorString* error, |
| + std::unique_ptr<protocol::DOM::Node>* out_root) { |
| + *out_root = BuildInitialTree(); |
| +} |
| + |
| +} // namespace devtools |
| +} // namespace ui |