File: log_buffer.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 (276 lines) | stat: -rw-r--r-- 7,814 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// 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/autofill/core/common/logging/log_buffer.h"

#include <string>
#include <string_view>

#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"

namespace autofill {

namespace {

bool IsElement(const base::Value::Dict& value) {
  const std::string* type = value.FindString("type");
  return type && *type == "element";
}

bool IsTextNode(const base::Value::Dict& value) {
  const std::string* type = value.FindString("type");
  return type && *type == "text";
}

bool IsFragment(const base::Value::Dict& value) {
  const std::string* type = value.FindString("type");
  return type && *type == "fragment";
}

void AppendChildToLastNode(std::vector<base::Value::Dict>* buffer,
                           base::Value::Dict&& new_child) {
  if (buffer->empty()) {
    buffer->push_back(std::move(new_child));
    return;
  }

  base::Value::Dict& parent = buffer->back();
  // Elements and Fragments can have children, but TextNodes cannot.
  DCHECK(!IsTextNode(parent));

  if (auto* children = parent.FindList("children")) {
    children->Append(std::move(new_child));
    return;
  }

  base::Value::List list;
  list.Append(std::move(new_child));
  parent.Set("children", std::move(list));
}

// This is an optimization to reduce the number of text nodes in the DOM.
// Sequences of appended string_views are coalesced into one. If many strings
// are appended, this has quadratic runtime. But the number of strings
// and the lengths of strings should be relatively small and we reduce the
// memory consumption of the DOM, which may grow rather large.
//
// If the last child of the element in buffer is a text node, append |text| to
// it and return true (successful coalescing). Otherwise return false.
bool TryCoalesceString(std::vector<base::Value::Dict>* buffer,
                       std::string_view text) {
  if (buffer->empty())
    return false;
  base::Value::Dict& parent = buffer->back();
  auto* children = parent.FindList("children");
  if (!children)
    return false;
  DCHECK(!children->empty());
  auto& last_child = children->back().GetDict();
  if (!IsTextNode(last_child))
    return false;
  std::string* old_text = last_child.FindString("value");
  old_text->append(text);
  return true;
}

base::Value::Dict CreateEmptyFragment() {
  base::Value::Dict dict;
  dict.Set("type", "fragment");
  return dict;
}

}  // namespace

LogBuffer::LogBuffer(IsActive active) : active_(*active) {
  if (active_)
    buffer_.push_back(CreateEmptyFragment());
}

LogBuffer::LogBuffer(LogBuffer&& other) noexcept = default;
LogBuffer& LogBuffer::operator=(LogBuffer&& other) = default;
LogBuffer::~LogBuffer() = default;

std::optional<base::Value::Dict> LogBuffer::RetrieveResult() {
  // The buffer should always start with a fragment.
  DCHECK(buffer_.size() >= 1);

  // Close not-yet-closed tags.
  while (buffer_.size() > 1)
    *this << CTag{};

  auto* children = buffer_[0].FindList("children");
  if (!children || children->empty())
    return std::nullopt;

  // If the fragment has a single child, remove it from |children| and return
  // that directly.
  if (children->size() == 1) {
    return std::optional<base::Value::Dict>(
        std::move((*children).back().GetDict()));
  }

  return std::exchange(buffer_.back(), CreateEmptyFragment());
}

LogBuffer& operator<<(LogBuffer& buf, Tag&& tag) {
  if (!buf.active())
    return buf;

  base::Value::Dict dict;
  dict.Set("type", "element");
  dict.Set("value", std::move(tag.name));
  buf.buffer_.emplace_back(std::move(dict));
  return buf;
}

LogBuffer& operator<<(LogBuffer& buf, CTag&& tag) {
  if (!buf.active())
    return buf;
  // Don't close the fragment. It stays and gets returned in the end.
  if (buf.buffer_.size() <= 1)
    return buf;

  base::Value::Dict node_to_add = std::move(buf.buffer_.back());
  buf.buffer_.pop_back();

  AppendChildToLastNode(&buf.buffer_, std::move(node_to_add));
  return buf;
}

LogBuffer& operator<<(LogBuffer& buf, Attrib&& attrib) {
  if (!buf.active())
    return buf;

  base::Value::Dict& node = buf.buffer_.back();
  DCHECK(IsElement(node));

  if (auto* attributes = node.FindDict("attributes")) {
    attributes->Set(attrib.name, std::move(attrib.value));
  } else {
    base::Value::Dict dict;
    dict.Set(attrib.name, std::move(attrib.value));
    node.Set("attributes", std::move(dict));
  }

  return buf;
}

LogBuffer& operator<<(LogBuffer& buf, Br&& tag) {
  if (!buf.active())
    return buf;
  return buf << Tag{"br"} << CTag{};
}

LogBuffer& operator<<(LogBuffer& buf, std::string_view text) {
  if (!buf.active())
    return buf;

  if (text.empty())
    return buf;

  if (TryCoalesceString(&buf.buffer_, text))
    return buf;

  base::Value::Dict node_to_add;
  node_to_add.Set("type", "text");
  // This text is not HTML escaped because the rest of the frame work takes care
  // of that and it must not be escaped twice.
  node_to_add.Set("value", text);
  AppendChildToLastNode(&buf.buffer_, std::move(node_to_add));
  return buf;
}

LogBuffer& operator<<(LogBuffer& buf, std::u16string_view text) {
  return buf << base::UTF16ToUTF8(text);
}

LogBuffer& operator<<(LogBuffer& buf, LogBuffer&& buffer) {
  if (!buf.active())
    return buf;

  std::optional<base::Value::Dict> node_to_add = buffer.RetrieveResult();
  if (!node_to_add)
    return buf;

  if (IsFragment(*node_to_add)) {
    auto* children = node_to_add->FindList("children");
    if (!children)
      return buf;
    for (auto& child : *children) {
      AppendChildToLastNode(
          &buf.buffer_, std::exchange(child.GetDict(), base::Value::Dict()));
    }
    return buf;
  }
  AppendChildToLastNode(&buf.buffer_, std::move(*node_to_add));
  return buf;
}

LogBuffer& operator<<(LogBuffer& buf, const GURL& url) {
  if (!buf.active())
    return buf;
  if (!url.is_valid())
    return buf << "Invalid URL";
  return buf << url.DeprecatedGetOriginAsURL().spec();
}

LogTableRowBuffer::LogTableRowBuffer(LogBuffer* parent) : parent_(parent) {
  *parent_ << Tag{"tr"};
}

LogTableRowBuffer::LogTableRowBuffer(LogTableRowBuffer&& buffer) noexcept
    : parent_(buffer.parent_) {
  // Prevent double closing of the <tr> tag.
  buffer.parent_ = nullptr;
}

LogTableRowBuffer::~LogTableRowBuffer() {
  if (parent_)
    *parent_ << CTag{};
}

LogTableRowBuffer operator<<(LogBuffer& buf, Tr&& tr) {
  return LogTableRowBuffer(&buf);
}

LogTableRowBuffer&& operator<<(LogTableRowBuffer&& buf, Attrib&& attrib) {
  *buf.parent_ << std::move(attrib);
  return std::move(buf);
}

LogTableRowBuffer&& operator<<(LogTableRowBuffer&& buf,
                               SetParentTagContainsPII&& attrib) {
  *buf.parent_ << Attrib{"data-pii", "true"};
  return std::move(buf);
}

namespace {
// Highlights the first |needle| in |haystack| by wrapping it in <b> tags.
template <typename T, typename CharT = typename T::value_type>
LogBuffer HighlightValueInternal(T haystack, T needle) {
  using StringViewT = std::basic_string_view<CharT>;
  LogBuffer buffer(LogBuffer::IsActive(true));
  size_t pos = haystack.find(needle);
  if (pos == StringViewT::npos || needle.empty()) {
    buffer << haystack;
    return buffer;
  }
  buffer << haystack.substr(0, pos);
  buffer << Tag{"b"} << needle << CTag{"b"};
  buffer << haystack.substr(pos + needle.size());
  return buffer;
}
}  // namespace

LogBuffer HighlightValue(std::string_view haystack, std::string_view needle) {
  return HighlightValueInternal(haystack, needle);
}

LogBuffer HighlightValue(std::u16string_view haystack,
                         std::u16string_view needle) {
  return HighlightValueInternal(haystack, needle);
}

}  // namespace autofill