Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 2005, 2006, 2009 Apple Inc. All rights reserved.
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  */
     19 
     20 #include "config.h"
     21 
     22 #ifdef SKIP_STATIC_CONSTRUCTORS_ON_GCC
     23 #define WEBCORE_QUALIFIEDNAME_HIDE_GLOBALS 1
     24 #else
     25 #define QNAME_DEFAULT_CONSTRUCTOR
     26 #endif
     27 
     28 #include "HTMLNames.h"
     29 #include "SVGNames.h"
     30 #include "XLinkNames.h"
     31 #include "XMLNSNames.h"
     32 #include "XMLNames.h"
     33 #include "core/dom/QualifiedName.h"
     34 #include "wtf/Assertions.h"
     35 #include "wtf/HashSet.h"
     36 #include "wtf/MainThread.h"
     37 #include "wtf/StaticConstructors.h"
     38 
     39 namespace WebCore {
     40 
     41 static const int staticQualifiedNamesCount = HTMLNames::HTMLTagsCount + HTMLNames::HTMLAttrsCount
     42     + SVGNames::SVGTagsCount + SVGNames::SVGAttrsCount
     43     + XLinkNames::XLinkAttrsCount
     44     + XMLNSNames::XMLNSAttrsCount
     45     + XMLNames::XMLAttrsCount;
     46 
     47 struct QualifiedNameHashTraits : public HashTraits<QualifiedName::QualifiedNameImpl*> {
     48     static const unsigned minimumTableSize = WTF::HashTableCapacityForSize<staticQualifiedNamesCount>::value;
     49 };
     50 
     51 typedef HashSet<QualifiedName::QualifiedNameImpl*, QualifiedNameHash, QualifiedNameHashTraits> QualifiedNameCache;
     52 
     53 static QualifiedNameCache& qualifiedNameCache()
     54 {
     55     // This code is lockless and thus assumes it all runs on one thread!
     56     ASSERT(isMainThread());
     57     static QualifiedNameCache* gNameCache = new QualifiedNameCache;
     58     return *gNameCache;
     59 }
     60 
     61 struct QNameComponentsTranslator {
     62     static unsigned hash(const QualifiedNameComponents& components)
     63     {
     64         return hashComponents(components);
     65     }
     66     static bool equal(QualifiedName::QualifiedNameImpl* name, const QualifiedNameComponents& c)
     67     {
     68         return c.m_prefix == name->m_prefix.impl() && c.m_localName == name->m_localName.impl() && c.m_namespace == name->m_namespace.impl();
     69     }
     70     static void translate(QualifiedName::QualifiedNameImpl*& location, const QualifiedNameComponents& components, unsigned)
     71     {
     72         location = QualifiedName::QualifiedNameImpl::create(AtomicString(components.m_prefix), AtomicString(components.m_localName), AtomicString(components.m_namespace)).leakRef();
     73     }
     74 };
     75 
     76 QualifiedName::QualifiedName(const AtomicString& p, const AtomicString& l, const AtomicString& n)
     77 {
     78     QualifiedNameComponents components = { p.impl(), l.impl(), n.isEmpty() ? nullAtom.impl() : n.impl() };
     79     QualifiedNameCache::AddResult addResult = qualifiedNameCache().add<QNameComponentsTranslator>(components);
     80     m_impl = *addResult.iterator;
     81     if (!addResult.isNewEntry)
     82         m_impl->ref();
     83 }
     84 
     85 QualifiedName::~QualifiedName()
     86 {
     87     deref();
     88 }
     89 
     90 void QualifiedName::deref()
     91 {
     92 #ifdef QNAME_DEFAULT_CONSTRUCTOR
     93     if (!m_impl)
     94         return;
     95 #endif
     96     ASSERT(!isHashTableDeletedValue());
     97     m_impl->deref();
     98 }
     99 
    100 QualifiedName::QualifiedNameImpl::~QualifiedNameImpl()
    101 {
    102     qualifiedNameCache().remove(this);
    103 }
    104 
    105 String QualifiedName::toString() const
    106 {
    107     String local = localName();
    108     if (hasPrefix())
    109         return prefix().string() + ":" + local;
    110     return local;
    111 }
    112 
    113 // Global init routines
    114 DEFINE_GLOBAL(QualifiedName, anyName, nullAtom, starAtom, starAtom)
    115 
    116 void QualifiedName::init()
    117 {
    118     ASSERT(starAtom.impl());
    119     new ((void*)&anyName) QualifiedName(nullAtom, starAtom, starAtom);
    120 }
    121 
    122 const QualifiedName& nullQName()
    123 {
    124     DEFINE_STATIC_LOCAL(QualifiedName, nullName, (nullAtom, nullAtom, nullAtom));
    125     return nullName;
    126 }
    127 
    128 const AtomicString& QualifiedName::localNameUpper() const
    129 {
    130     if (!m_impl->m_localNameUpper)
    131         m_impl->m_localNameUpper = m_impl->m_localName.upper();
    132     return m_impl->m_localNameUpper;
    133 }
    134 
    135 unsigned QualifiedName::QualifiedNameImpl::computeHash() const
    136 {
    137     QualifiedNameComponents components = { m_prefix.impl(), m_localName.impl(), m_namespace.impl() };
    138     return hashComponents(components);
    139 }
    140 
    141 void createQualifiedName(void* targetAddress, StringImpl* name, const AtomicString& nameNamespace)
    142 {
    143     new (targetAddress) QualifiedName(nullAtom, AtomicString(name), nameNamespace);
    144 }
    145 
    146 void createQualifiedName(void* targetAddress, StringImpl* name)
    147 {
    148     new (targetAddress) QualifiedName(nullAtom, AtomicString(name), nullAtom);
    149 }
    150 
    151 }
    152