File: page_agent_views.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (121 lines) | stat: -rw-r--r-- 3,786 bytes parent folder | download | duplicates (5)
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
113
114
115
116
117
118
119
120
121
// 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/page_agent_views.h"

#include <unordered_set>

#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "components/ui_devtools/agent_util.h"
#include "components/ui_devtools/ui_element.h"

namespace ui_devtools {

namespace {

void PaintRectVector(
    std::vector<raw_ptr<UIElement, VectorExperimental>> child_elements) {
  for (ui_devtools::UIElement* element : child_elements) {
    if (element->type() == UIElementType::VIEW) {
      element->PaintRect();
    }
    PaintRectVector(element->children());
  }
}

std::unordered_set<std::string> GetSources(UIElement* root) {
  std::unordered_set<std::string> ret;

  for (auto& source : root->GetSources()) {
    ret.insert(source.path_ + "?l=" + base::NumberToString(source.line_));
  }

  for (ui_devtools::UIElement* child : root->children()) {
    for (auto& child_source : GetSources(child)) {
      ret.insert(child_source);
    }
  }

  return ret;
}

void AddFrameResources(
    std::unique_ptr<protocol::Array<protocol::Page::FrameResource>>&
        frame_resources,
    const std::unordered_set<std::string>& all_sources) {
  for (const auto& source : all_sources) {
    frame_resources->emplace_back(
        protocol::Page::FrameResource::create()
            .setUrl(kChromiumCodeSearchSrcURL + source)
            .setType("Document")
            .setMimeType("text/x-c++hdr")
            .build());
  }
}

}  // namespace

PageAgentViews::PageAgentViews(DOMAgent* dom_agent) : PageAgent(dom_agent) {}

PageAgentViews::~PageAgentViews() = default;

protocol::Response PageAgentViews::disable() {
  return protocol::Response::Success();
}

protocol::Response PageAgentViews::getResourceTree(
    std::unique_ptr<protocol::Page::FrameResourceTree>* object) {
  std::unique_ptr<protocol::Page::Frame> frame_object =
      protocol::Page::Frame::create()
          .setId("1")
          .setUrl(kChromiumCodeSearchURL)
          .build();
  auto subresources =
      std::make_unique<protocol::Array<protocol::Page::FrameResource>>();

  // Ensure that the DOM tree has been initialized, so all sources have
  // been added.
  if (dom_agent_->element_root() == nullptr) {
    std::unique_ptr<protocol::DOM::Node> node;
    dom_agent_->getDocument(&node);
  }

  std::unordered_set<std::string> all_sources =
      GetSources(dom_agent_->element_root());

  AddFrameResources(subresources, all_sources);
  std::unique_ptr<protocol::Page::FrameResourceTree> result =
      protocol::Page::FrameResourceTree::create()
          .setFrame(std::move(frame_object))
          .setResources(std::move(subresources))
          .build();
  *object = std::move(result);
  return protocol::Response::Success();
}

protocol::Response PageAgentViews::getResourceContent(
    const protocol::String& in_frameId,
    const protocol::String& in_url,
    protocol::String* out_content,
    bool* out_base64Encoded) {
  auto split_url = base::SplitStringUsingSubstr(
      in_url, "src/", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  if (split_url.size() != 2)
    return protocol::Response::ServerError("Invalid URL");

  auto split_path = base::SplitStringUsingSubstr(
      split_url[1], "?l=", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  if (split_path.size() != 2)
    return protocol::Response::ServerError("Invalid URL");

  if (GetSourceCode(split_path[0], out_content))
    return protocol::Response::Success();
  else
    return protocol::Response::ServerError("Could not read source file");
}

}  // namespace ui_devtools