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.
28 #if ENABLE(INDEXED_DATABASE)
30 #include "SecurityOriginData.h"
31 #include <pal/SessionID.h>
32 #include <wtf/text/StringHash.h>
33 #include <wtf/text/WTFString.h>
39 class IDBDatabaseIdentifier {
41 IDBDatabaseIdentifier() { }
42 IDBDatabaseIdentifier(WTF::HashTableDeletedValueType)
43 : m_databaseName(WTF::HashTableDeletedValue)
47 WEBCORE_EXPORT IDBDatabaseIdentifier(const String& databaseName, const PAL::SessionID&, SecurityOriginData&& openingOrigin, SecurityOriginData&& mainFrameOrigin);
49 IDBDatabaseIdentifier isolatedCopy() const;
51 bool isHashTableDeletedValue() const
53 return m_databaseName.isHashTableDeletedValue();
58 unsigned nameHash = StringHash::hash(m_databaseName);
59 unsigned sessionIDHash = WTF::SessionIDHash::hash(m_sessionID);
60 unsigned openingProtocolHash = StringHash::hash(m_openingOrigin.protocol);
61 unsigned openingHostHash = StringHash::hash(m_openingOrigin.host);
62 unsigned mainFrameProtocolHash = StringHash::hash(m_mainFrameOrigin.protocol);
63 unsigned mainFrameHostHash = StringHash::hash(m_mainFrameOrigin.host);
65 unsigned hashCodes[8] = { nameHash, sessionIDHash, openingProtocolHash, openingHostHash, m_openingOrigin.port.valueOr(0), mainFrameProtocolHash, mainFrameHostHash, m_mainFrameOrigin.port.valueOr(0) };
66 return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
71 return !m_databaseName.isNull()
72 && !m_databaseName.isHashTableDeletedValue();
77 return m_databaseName.isNull();
80 bool operator==(const IDBDatabaseIdentifier& other) const
82 return other.m_databaseName == m_databaseName
83 && other.m_openingOrigin == m_openingOrigin
84 && other.m_mainFrameOrigin == m_mainFrameOrigin;
87 const String& databaseName() const { return m_databaseName; }
88 const PAL::SessionID& sessionID() const { return m_sessionID; }
90 String databaseDirectoryRelativeToRoot(const String& rootDirectory) const;
91 static String databaseDirectoryRelativeToRoot(const SecurityOriginData& topLevelOrigin, const SecurityOriginData& openingOrigin, const String& rootDirectory);
93 template<class Encoder> void encode(Encoder&) const;
94 template<class Decoder> static Optional<IDBDatabaseIdentifier> decode(Decoder&);
97 String debugString() const;
100 bool isRelatedToOrigin(const SecurityOriginData& other) const
102 return m_openingOrigin == other || m_mainFrameOrigin == other;
106 String m_databaseName;
107 PAL::SessionID m_sessionID;
108 SecurityOriginData m_openingOrigin;
109 SecurityOriginData m_mainFrameOrigin;
112 struct IDBDatabaseIdentifierHash {
113 static unsigned hash(const IDBDatabaseIdentifier& a) { return a.hash(); }
114 static bool equal(const IDBDatabaseIdentifier& a, const IDBDatabaseIdentifier& b) { return a == b; }
115 static const bool safeToCompareToEmptyOrDeleted = false;
118 struct IDBDatabaseIdentifierHashTraits : WTF::SimpleClassHashTraits<IDBDatabaseIdentifier> {
119 static const bool hasIsEmptyValueFunction = true;
120 static const bool emptyValueIsZero = false;
121 static bool isEmptyValue(const IDBDatabaseIdentifier& info) { return info.isEmpty(); }
124 template<class Encoder>
125 void IDBDatabaseIdentifier::encode(Encoder& encoder) const
127 encoder << m_databaseName << m_sessionID << m_openingOrigin << m_mainFrameOrigin;
130 template<class Decoder>
131 Optional<IDBDatabaseIdentifier> IDBDatabaseIdentifier::decode(Decoder& decoder)
133 Optional<String> databaseName;
134 decoder >> databaseName;
138 Optional<PAL::SessionID> sessionID;
139 decoder >> sessionID;
143 Optional<SecurityOriginData> openingOrigin;
144 decoder >> openingOrigin;
148 Optional<SecurityOriginData> mainFrameOrigin;
149 decoder >> mainFrameOrigin;
150 if (!mainFrameOrigin)
153 IDBDatabaseIdentifier identifier;
154 identifier.m_databaseName = WTFMove(*databaseName); // FIXME: When decoding from IPC, databaseName can be null, and the non-empty constructor asserts that this is not the case.
155 identifier.m_sessionID = WTFMove(*sessionID);
156 identifier.m_openingOrigin = WTFMove(*openingOrigin);
157 identifier.m_mainFrameOrigin = WTFMove(*mainFrameOrigin);
158 return WTFMove(identifier);
161 } // namespace WebCore
165 template<> struct HashTraits<WebCore::IDBDatabaseIdentifier> : WebCore::IDBDatabaseIdentifierHashTraits { };
166 template<> struct DefaultHash<WebCore::IDBDatabaseIdentifier> {
167 typedef WebCore::IDBDatabaseIdentifierHash Hash;
172 #endif // ENABLE(INDEXED_DATABASE)