summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/dom/qualified_name.h
blob: 2223714cd21b12aa6ef68e2b91d14b7367b4d6d8 (plain)
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
/*
 * Copyright (C) 2005, 2006, 2009 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_DOM_QUALIFIED_NAME_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_DOM_QUALIFIED_NAME_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/hash_table_deleted_value_type.h"
#include "third_party/blink/renderer/platform/wtf/hash_traits.h"
#include "third_party/blink/renderer/platform/wtf/ref_counted.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"

namespace blink {

struct QualifiedNameComponents {
  DISALLOW_NEW();
  StringImpl* prefix_;
  StringImpl* local_name_;
  StringImpl* namespace_;
};

// This struct is used to pass data between QualifiedName and the
// QNameTranslator.  For hashing and equality only the QualifiedNameComponents
// fields are used.
struct QualifiedNameData {
  DISALLOW_NEW();
  QualifiedNameComponents components_;
  bool is_static_;
};

CORE_EXPORT extern const class QualifiedName& g_null_name;

class CORE_EXPORT QualifiedName {
  USING_FAST_MALLOC(QualifiedName);

 public:
  class CORE_EXPORT QualifiedNameImpl : public RefCounted<QualifiedNameImpl> {
   public:
    static scoped_refptr<QualifiedNameImpl> Create(
        const AtomicString& prefix,
        const AtomicString& local_name,
        const AtomicString& namespace_uri,
        bool is_static) {
      return base::AdoptRef(
          new QualifiedNameImpl(prefix, local_name, namespace_uri, is_static));
    }

    ~QualifiedNameImpl();

    unsigned ComputeHash() const;

    void AddRef() {
      if (is_static_)
        return;
      RefCounted<QualifiedNameImpl>::AddRef();
    }

    void Release() {
      if (is_static_)
        return;
      RefCounted<QualifiedNameImpl>::Release();
    }

    // We rely on StringHasher's HashMemory clearing out the top 8 bits when
    // doing hashing and use one of the bits for the is_static_ value.
    mutable unsigned existing_hash_ : 24;
    unsigned is_static_ : 1;
    const AtomicString prefix_;
    const AtomicString local_name_;
    const AtomicString namespace_;
    mutable AtomicString local_name_upper_;

   private:
    QualifiedNameImpl(const AtomicString& prefix,
                      const AtomicString& local_name,
                      const AtomicString& namespace_uri,
                      bool is_static)
        : existing_hash_(0),
          is_static_(is_static),
          prefix_(prefix),
          local_name_(local_name),
          namespace_(namespace_uri)

    {
      DCHECK(!namespace_uri.IsEmpty() || namespace_uri.IsNull());
    }
  };

  QualifiedName(const AtomicString& prefix,
                const AtomicString& local_name,
                const AtomicString& namespace_uri);
  ~QualifiedName();

  QualifiedName(const QualifiedName& other) = default;
  const QualifiedName& operator=(const QualifiedName& other) {
    impl_ = other.impl_;
    return *this;
  }

  bool operator==(const QualifiedName& other) const {
    return impl_ == other.impl_;
  }
  bool operator!=(const QualifiedName& other) const {
    return !(*this == other);
  }

  bool Matches(const QualifiedName& other) const {
    return impl_ == other.impl_ || (LocalName() == other.LocalName() &&
                                    NamespaceURI() == other.NamespaceURI());
  }

  bool HasPrefix() const { return impl_->prefix_ != g_null_atom; }
  void SetPrefix(const AtomicString& prefix) {
    *this = QualifiedName(prefix, LocalName(), NamespaceURI());
  }

  const AtomicString& Prefix() const { return impl_->prefix_; }
  const AtomicString& LocalName() const { return impl_->local_name_; }
  const AtomicString& NamespaceURI() const { return impl_->namespace_; }

  // Uppercased localName, cached for efficiency
  const AtomicString& LocalNameUpper() const {
    if (impl_->local_name_upper_)
      return impl_->local_name_upper_;
    return LocalNameUpperSlow();
  }

  const AtomicString& LocalNameUpperSlow() const;

  String ToString() const;

  QualifiedNameImpl* Impl() const { return impl_.get(); }

  // Init routine for globals
  static void InitAndReserveCapacityForSize(unsigned size);

  static const QualifiedName& Null() { return g_null_name; }

  // The below methods are only for creating static global QNames that need no
  // ref counting.
  static void CreateStatic(void* target_address, StringImpl* name);
  static void CreateStatic(void* target_address,
                           StringImpl* name,
                           const AtomicString& name_namespace);

 private:
  friend struct WTF::HashTraits<blink::QualifiedName>;

  // This constructor is used only to create global/static QNames that don't
  // require any ref counting.
  QualifiedName(const AtomicString& prefix,
                const AtomicString& local_name,
                const AtomicString& namespace_uri,
                bool is_static);

  scoped_refptr<QualifiedNameImpl> impl_;
};

extern const QualifiedName& g_any_name;
inline const QualifiedName& AnyQName() {
  return g_any_name;
}

inline bool operator==(const AtomicString& a, const QualifiedName& q) {
  return a == q.LocalName();
}
inline bool operator!=(const AtomicString& a, const QualifiedName& q) {
  return a != q.LocalName();
}
inline bool operator==(const QualifiedName& q, const AtomicString& a) {
  return a == q.LocalName();
}
inline bool operator!=(const QualifiedName& q, const AtomicString& a) {
  return a != q.LocalName();
}

inline unsigned HashComponents(const QualifiedNameComponents& buf) {
  return StringHasher::HashMemory<sizeof(QualifiedNameComponents)>(&buf);
}

struct CORE_EXPORT QualifiedNameHash {
  STATIC_ONLY(QualifiedNameHash);
  static unsigned GetHash(const QualifiedName& name) {
    return GetHash(name.Impl());
  }

  static unsigned GetHash(const QualifiedName::QualifiedNameImpl* name) {
    if (!name->existing_hash_)
      name->existing_hash_ = name->ComputeHash();
    return name->existing_hash_;
  }

  static bool Equal(const QualifiedName& a, const QualifiedName& b) {
    return a == b;
  }
  static bool Equal(const QualifiedName::QualifiedNameImpl* a,
                    const QualifiedName::QualifiedNameImpl* b) {
    return a == b;
  }

  static const bool safe_to_compare_to_empty_or_deleted = false;
};

CORE_EXPORT std::ostream& operator<<(std::ostream&, const QualifiedName&);

}  // namespace blink

WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(blink::QualifiedName)

namespace WTF {

template <>
struct DefaultHash<blink::QualifiedName> {
  typedef blink::QualifiedNameHash Hash;
};

template <>
struct HashTraits<blink::QualifiedName>
    : SimpleClassHashTraits<blink::QualifiedName> {
  static const bool kEmptyValueIsZero = false;
  static const blink::QualifiedName& EmptyValue() {
    return blink::QualifiedName::Null();
  }

  static bool IsDeletedValue(const blink::QualifiedName& value) {
    using QualifiedNameImpl = blink::QualifiedName::QualifiedNameImpl;
    return HashTraits<scoped_refptr<QualifiedNameImpl>>::IsDeletedValue(
        value.impl_);
  }

  static void ConstructDeletedValue(blink::QualifiedName& slot,
                                    bool zero_value) {
    using QualifiedNameImpl = blink::QualifiedName::QualifiedNameImpl;
    HashTraits<scoped_refptr<QualifiedNameImpl>>::ConstructDeletedValue(
        slot.impl_, zero_value);
  }
};

}  // namespace WTF

#endif