2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef IDBDatabaseIdentifier_h
27 #define IDBDatabaseIdentifier_h
29 #if ENABLE(INDEXED_DATABASE)
31 #include "SecurityOriginData.h"
33 #include <wtf/RefCounted.h>
34 #include <wtf/text/StringHash.h>
35 #include <wtf/text/WTFString.h>
41 class IDBDatabaseIdentifier {
43 IDBDatabaseIdentifier()
45 m_openingOrigin.port = -2;
46 m_mainFrameOrigin.port = -2;
49 IDBDatabaseIdentifier(WTF::HashTableDeletedValueType)
51 m_openingOrigin.port = -1;
52 m_mainFrameOrigin.port = -1;
55 IDBDatabaseIdentifier isolatedCopy() const;
57 bool isHashTableDeletedValue() const
59 return m_openingOrigin.port == -1 && m_mainFrameOrigin.port == -1;
64 unsigned nameHash = StringHash::hash(m_databaseName);
65 unsigned openingProtocolHash = StringHash::hash(m_openingOrigin.protocol);
66 unsigned openingHostHash = StringHash::hash(m_openingOrigin.host);
67 unsigned mainFrameProtocolHash = StringHash::hash(m_mainFrameOrigin.protocol);
68 unsigned mainFrameHostHash = StringHash::hash(m_mainFrameOrigin.host);
70 unsigned hashCodes[7] = { nameHash, openingProtocolHash, openingHostHash, static_cast<unsigned>(m_openingOrigin.port), mainFrameProtocolHash, mainFrameHostHash, static_cast<unsigned>(m_mainFrameOrigin.port) };
71 return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
74 IDBDatabaseIdentifier(const String& databaseName, const SecurityOrigin& openingOrigin, const SecurityOrigin& mainFrameOrigin);
78 return !m_databaseName.isNull() && m_openingOrigin.port >= 0 && m_mainFrameOrigin.port >= 0;
83 return m_openingOrigin.port == -2 && m_mainFrameOrigin.port == -2;
86 bool operator==(const IDBDatabaseIdentifier& other) const
88 return other.m_databaseName == m_databaseName
89 && other.m_openingOrigin == m_openingOrigin
90 && other.m_mainFrameOrigin == m_mainFrameOrigin;
93 const String& databaseName() const { return m_databaseName; }
95 String databaseDirectoryRelativeToRoot(const String& rootDirectory) const;
96 static String databaseDirectoryRelativeToRoot(const SecurityOriginData& topLevelOrigin, const SecurityOriginData& openingOrigin, const String& rootDirectory);
98 template<class Encoder> void encode(Encoder&) const;
99 template<class Decoder> static bool decode(Decoder&, IDBDatabaseIdentifier&);
102 String debugString() const;
106 String m_databaseName;
107 SecurityOriginData m_openingOrigin;
108 SecurityOriginData m_mainFrameOrigin;
111 struct IDBDatabaseIdentifierHash {
112 static unsigned hash(const IDBDatabaseIdentifier& a) { return a.hash(); }
113 static bool equal(const IDBDatabaseIdentifier& a, const IDBDatabaseIdentifier& b) { return a == b; }
114 static const bool safeToCompareToEmptyOrDeleted = false;
117 struct IDBDatabaseIdentifierHashTraits : WTF::SimpleClassHashTraits<IDBDatabaseIdentifier> {
118 static const bool hasIsEmptyValueFunction = true;
119 static const bool emptyValueIsZero = false;
120 static bool isEmptyValue(const IDBDatabaseIdentifier& info) { return info.isEmpty(); }
123 template<class Encoder>
124 void IDBDatabaseIdentifier::encode(Encoder& encoder) const
126 encoder << m_databaseName << m_openingOrigin << m_mainFrameOrigin;
129 template<class Decoder>
130 bool IDBDatabaseIdentifier::decode(Decoder& decoder, IDBDatabaseIdentifier& identifier)
132 if (!decoder.decode(identifier.m_databaseName))
135 if (!decoder.decode(identifier.m_openingOrigin))
138 if (!decoder.decode(identifier.m_mainFrameOrigin))
144 } // namespace WebCore
148 template<> struct HashTraits<WebCore::IDBDatabaseIdentifier> : WebCore::IDBDatabaseIdentifierHashTraits { };
149 template<> struct DefaultHash<WebCore::IDBDatabaseIdentifier> {
150 typedef WebCore::IDBDatabaseIdentifierHash Hash;
155 #endif // ENABLE(INDEXED_DATABASE)
156 #endif // IDBDatabaseIdentifier_h