1/*
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#pragma once
27
28#if ENABLE(INDEXED_DATABASE)
29
30#include "ClientOrigin.h"
31#include "SecurityOriginData.h"
32#include <pal/SessionID.h>
33#include <wtf/text/StringHash.h>
34#include <wtf/text/WTFString.h>
35
36namespace WebCore {
37
38class SecurityOrigin;
39
40class IDBDatabaseIdentifier {
41public:
42 IDBDatabaseIdentifier() { }
43 IDBDatabaseIdentifier(WTF::HashTableDeletedValueType)
44 : m_databaseName(WTF::HashTableDeletedValue)
45 {
46 }
47
48 WEBCORE_EXPORT IDBDatabaseIdentifier(const String& databaseName, const PAL::SessionID&, SecurityOriginData&& openingOrigin, SecurityOriginData&& mainFrameOrigin);
49
50 IDBDatabaseIdentifier isolatedCopy() const;
51
52 bool isHashTableDeletedValue() const
53 {
54 return m_databaseName.isHashTableDeletedValue();
55 }
56
57 unsigned hash() const
58 {
59 unsigned nameHash = StringHash::hash(m_databaseName);
60 unsigned sessionIDHash = WTF::SessionIDHash::hash(m_sessionID);
61 unsigned originHash = m_origin.hash();
62
63 unsigned hashCodes[3] = { nameHash, sessionIDHash, originHash };
64 return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
65 }
66
67 bool isValid() const
68 {
69 return !m_databaseName.isNull()
70 && !m_databaseName.isHashTableDeletedValue();
71 }
72
73 bool isEmpty() const
74 {
75 return m_databaseName.isNull();
76 }
77
78 bool operator==(const IDBDatabaseIdentifier& other) const
79 {
80 return other.m_databaseName == m_databaseName && other.m_origin == m_origin;
81 }
82
83 const String& databaseName() const { return m_databaseName; }
84 const PAL::SessionID& sessionID() const { return m_sessionID; }
85 const ClientOrigin& origin() const { return m_origin; }
86
87 String databaseDirectoryRelativeToRoot(const String& rootDirectory, const String& versionString="v1") const;
88 static String databaseDirectoryRelativeToRoot(const SecurityOriginData& topLevelOrigin, const SecurityOriginData& openingOrigin, const String& rootDirectory, const String& versionString);
89
90 template<class Encoder> void encode(Encoder&) const;
91 template<class Decoder> static Optional<IDBDatabaseIdentifier> decode(Decoder&);
92
93#if !LOG_DISABLED
94 String debugString() const;
95#endif
96
97 bool isRelatedToOrigin(const SecurityOriginData& other) const { return m_origin.isRelated(other); }
98
99private:
100 String m_databaseName;
101 PAL::SessionID m_sessionID;
102 ClientOrigin m_origin;
103 SecurityOriginData m_mainFrameOrigin;
104};
105
106struct IDBDatabaseIdentifierHash {
107 static unsigned hash(const IDBDatabaseIdentifier& a) { return a.hash(); }
108 static bool equal(const IDBDatabaseIdentifier& a, const IDBDatabaseIdentifier& b) { return a == b; }
109 static const bool safeToCompareToEmptyOrDeleted = false;
110};
111
112struct IDBDatabaseIdentifierHashTraits : WTF::SimpleClassHashTraits<IDBDatabaseIdentifier> {
113 static const bool hasIsEmptyValueFunction = true;
114 static const bool emptyValueIsZero = false;
115 static bool isEmptyValue(const IDBDatabaseIdentifier& info) { return info.isEmpty(); }
116};
117
118template<class Encoder>
119void IDBDatabaseIdentifier::encode(Encoder& encoder) const
120{
121 encoder << m_databaseName << m_sessionID << m_origin;
122}
123
124template<class Decoder>
125Optional<IDBDatabaseIdentifier> IDBDatabaseIdentifier::decode(Decoder& decoder)
126{
127 Optional<String> databaseName;
128 decoder >> databaseName;
129 if (!databaseName)
130 return WTF::nullopt;
131
132 Optional<PAL::SessionID> sessionID;
133 decoder >> sessionID;
134 if (!sessionID)
135 return WTF::nullopt;
136
137 Optional<ClientOrigin> origin;
138 decoder >> origin;
139 if (!origin)
140 return WTF::nullopt;
141
142 IDBDatabaseIdentifier identifier;
143 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.
144 identifier.m_sessionID = WTFMove(*sessionID);
145 identifier.m_origin = WTFMove(*origin);
146 return identifier;
147}
148
149} // namespace WebCore
150
151namespace WTF {
152
153template<> struct HashTraits<WebCore::IDBDatabaseIdentifier> : WebCore::IDBDatabaseIdentifierHashTraits { };
154template<> struct DefaultHash<WebCore::IDBDatabaseIdentifier> {
155 typedef WebCore::IDBDatabaseIdentifierHash Hash;
156};
157
158} // namespace WTF
159
160#endif // ENABLE(INDEXED_DATABASE)
161