File: element_identifier.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (93 lines) | stat: -rw-r--r-- 2,600 bytes parent folder | download | duplicates (4)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/interaction/element_identifier.h"

#include <string_view>

#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/no_destructor.h"

namespace ui {

std::string ElementIdentifier::GetName() const {
  if (!handle_)
    return std::string();
  RegisterKnownIdentifier(*this);
  return handle_->name;
}

intptr_t ElementIdentifier::GetRawValue() const {
  if (!handle_)
    return 0;
  RegisterKnownIdentifier(*this);
  return reinterpret_cast<intptr_t>(handle_);
}

// static
ElementIdentifier ElementIdentifier::FromRawValue(intptr_t value) {
  if (!value)
    return ElementIdentifier();
  const auto* impl =
      reinterpret_cast<const internal::ElementIdentifierImpl*>(value);
  CHECK(GetKnownIdentifiers().contains(impl));
  return ElementIdentifier(impl);
}

// static
ElementIdentifier ElementIdentifier::FromName(const char* name) {
  for (const auto* impl : GetKnownIdentifiers()) {
    if (std::string_view(impl->name) == name) {
      return ElementIdentifier(impl);
    }
  }
  return ElementIdentifier();
}

// static
void ElementIdentifier::RegisterKnownIdentifier(
    ElementIdentifier element_identifier) {
  CHECK(element_identifier);

#if DCHECK_IS_ON()
  // Enforce uniqueness in DCHECK builds.
  const ElementIdentifier existing = FromName(element_identifier.handle_->name);
  DCHECK(!existing || existing == element_identifier)
      << "Duplicate identifier: " << element_identifier.handle_->name;
#endif

  GetKnownIdentifiers().insert(element_identifier.handle_);
}

// static
ElementIdentifier::KnownIdentifiers& ElementIdentifier::GetKnownIdentifiers() {
  static base::NoDestructor<KnownIdentifiers> known_identifiers;
  return *known_identifiers.get();
}

COMPONENT_EXPORT(UI_BASE_INTERACTION)
void PrintTo(ElementIdentifier element_identifier, std::ostream* os) {
  *os << "ElementIdentifier " << element_identifier.GetName();
}

COMPONENT_EXPORT(UI_BASE_INTERACTION)
void PrintTo(ElementContext element_context, std::ostream* os) {
  *os << "ElementContext " << static_cast<const void*>(element_context);
}

COMPONENT_EXPORT(UI_BASE_INTERACTION)
std::ostream& operator<<(std::ostream& os,
                         ElementIdentifier element_identifier) {
  PrintTo(element_identifier, &os);
  return os;
}

COMPONENT_EXPORT(UI_BASE_INTERACTION)
std::ostream& operator<<(std::ostream& os, ElementContext element_context) {
  PrintTo(element_context, &os);
  return os;
}

}  // namespace ui