File: dom_agent_aura.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (112 lines) | stat: -rw-r--r-- 3,554 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/ui_devtools/views/dom_agent_aura.h"

#include <algorithm>
#include <vector>

#include "components/ui_devtools/views/widget_element.h"
#include "components/ui_devtools/views/window_element.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"

namespace ui_devtools {

namespace {
using ui_devtools::protocol::Array;
using ui_devtools::protocol::DOM::Node;
}  // namespace

DOMAgentAura* DOMAgentAura::dom_agent_aura_ = nullptr;

DOMAgentAura::DOMAgentAura() {
  DCHECK(!dom_agent_aura_);
  dom_agent_aura_ = this;
  aura::Env::GetInstance()->AddObserver(this);
  for (aura::WindowTreeHost* window_tree_host :
       aura::Env::GetInstance()->window_tree_hosts()) {
    OnHostInitialized(window_tree_host);
  }
}

DOMAgentAura::~DOMAgentAura() {
  for (aura::Window* window : roots_)
    window->RemoveObserver(this);
  aura::Env::GetInstance()->RemoveObserver(this);
  dom_agent_aura_ = nullptr;
}

void DOMAgentAura::OnHostInitialized(aura::WindowTreeHost* host) {
  roots_.push_back(host->window());
  host->window()->AddObserver(this);

  if (element_root() && !element_root()->is_updating()) {
    // The tree is already built, needs to update.
    UIElement* window_element =
        new WindowElement(host->window(), this, element_root());
    element_root()->AddChild(window_element);
  }
}

void DOMAgentAura::OnWindowDestroying(aura::Window* window) {
  std::erase(roots_, window);

  if (element_root() && !element_root()->is_updating()) {
    const auto& children = element_root()->children();
    auto iter = std::ranges::find(children, window, &WindowElement::From);
    if (iter != children.end()) {
      UIElement* child_element = *iter;
      element_root()->RemoveChild(child_element);
      delete child_element;
    }
  }
}

std::vector<UIElement*> DOMAgentAura::CreateChildrenForRoot() {
  std::vector<UIElement*> children;
  for (aura::Window* window : roots_) {
    UIElement* window_element = new WindowElement(window, this, element_root());
    children.push_back(window_element);
  }
  return children;
}

std::unique_ptr<Node> DOMAgentAura::BuildTreeForWindow(
    UIElement* window_element_root) {
  DCHECK(window_element_root->type() == UIElementType::WINDOW);
  aura::Window* window =
      UIElement::GetBackingElement<aura::Window, WindowElement>(
          window_element_root);
  auto children = std::make_unique<protocol::Array<Node>>();
  views::Widget* widget = views::Widget::GetWidgetForNativeView(window);
  if (widget) {
    UIElement* widget_element =
        new WidgetElement(widget, this, window_element_root);

    children->emplace_back(BuildTreeForRootWidget(widget_element));
    window_element_root->AddChild(widget_element);
  }
  for (aura::Window* child : window->children()) {
    UIElement* window_element =
        new WindowElement(child, this, window_element_root);

    children->emplace_back(BuildTreeForWindow(window_element));
    window_element_root->AddChild(window_element);
  }
  std::unique_ptr<Node> node =
      BuildNode("Window",
                std::make_unique<std::vector<std::string>>(
                    window_element_root->GetAttributes()),
                std::move(children), window_element_root->node_id());
  return node;
}

// static
std::unique_ptr<DOMAgentViews> DOMAgentViews::Create() {
  return std::make_unique<DOMAgentAura>();
}

}  // namespace ui_devtools