1 | /* |
2 | * Copyright (C) 2015, 2016 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. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #if ENABLE(INDEXED_DATABASE) |
29 | |
30 | #include <wtf/text/StringHash.h> |
31 | |
32 | namespace WebCore { |
33 | |
34 | class IDBRequest; |
35 | |
36 | namespace IDBClient { |
37 | class IDBConnectionProxy; |
38 | } |
39 | |
40 | namespace IDBServer { |
41 | class IDBConnectionToClient; |
42 | } |
43 | |
44 | class IDBResourceIdentifier { |
45 | WTF_MAKE_FAST_ALLOCATED; |
46 | public: |
47 | explicit IDBResourceIdentifier(const IDBClient::IDBConnectionProxy&); |
48 | IDBResourceIdentifier(const IDBClient::IDBConnectionProxy&, const IDBRequest&); |
49 | explicit IDBResourceIdentifier(const IDBServer::IDBConnectionToClient&); |
50 | |
51 | static IDBResourceIdentifier deletedValue(); |
52 | WEBCORE_EXPORT bool isHashTableDeletedValue() const; |
53 | |
54 | static IDBResourceIdentifier emptyValue(); |
55 | bool isEmpty() const |
56 | { |
57 | return !m_resourceNumber && !m_idbConnectionIdentifier; |
58 | } |
59 | |
60 | unsigned hash() const |
61 | { |
62 | uint64_t hashCodes[2] = { m_idbConnectionIdentifier, m_resourceNumber }; |
63 | return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes); |
64 | } |
65 | |
66 | bool operator==(const IDBResourceIdentifier& other) const |
67 | { |
68 | return m_idbConnectionIdentifier == other.m_idbConnectionIdentifier |
69 | && m_resourceNumber == other.m_resourceNumber; |
70 | } |
71 | |
72 | uint64_t connectionIdentifier() const { return m_idbConnectionIdentifier; } |
73 | |
74 | IDBResourceIdentifier isolatedCopy() const; |
75 | |
76 | #if !LOG_DISABLED |
77 | String loggingString() const; |
78 | #endif |
79 | |
80 | WEBCORE_EXPORT IDBResourceIdentifier(); |
81 | |
82 | template<class Encoder> void encode(Encoder&) const; |
83 | template<class Decoder> static bool decode(Decoder&, IDBResourceIdentifier&); |
84 | |
85 | private: |
86 | IDBResourceIdentifier(uint64_t connectionIdentifier, uint64_t resourceIdentifier); |
87 | uint64_t m_idbConnectionIdentifier { 0 }; |
88 | uint64_t m_resourceNumber { 0 }; |
89 | }; |
90 | |
91 | struct IDBResourceIdentifierHash { |
92 | static unsigned hash(const IDBResourceIdentifier& a) { return a.hash(); } |
93 | static bool equal(const IDBResourceIdentifier& a, const IDBResourceIdentifier& b) { return a == b; } |
94 | static const bool safeToCompareToEmptyOrDeleted = false; |
95 | }; |
96 | |
97 | struct IDBResourceIdentifierHashTraits : WTF::CustomHashTraits<IDBResourceIdentifier> { |
98 | static const bool hasIsEmptyValueFunction = true; |
99 | static const bool emptyValueIsZero = false; |
100 | |
101 | static IDBResourceIdentifier emptyValue() |
102 | { |
103 | return IDBResourceIdentifier::emptyValue(); |
104 | } |
105 | |
106 | static bool isEmptyValue(const IDBResourceIdentifier& identifier) |
107 | { |
108 | return identifier.isEmpty(); |
109 | } |
110 | |
111 | static void constructDeletedValue(IDBResourceIdentifier& identifier) |
112 | { |
113 | identifier = IDBResourceIdentifier::deletedValue(); |
114 | } |
115 | |
116 | static bool isDeletedValue(const IDBResourceIdentifier& identifier) |
117 | { |
118 | return identifier.isHashTableDeletedValue(); |
119 | } |
120 | }; |
121 | |
122 | template<class Encoder> |
123 | void IDBResourceIdentifier::encode(Encoder& encoder) const |
124 | { |
125 | encoder << m_idbConnectionIdentifier << m_resourceNumber; |
126 | } |
127 | |
128 | template<class Decoder> |
129 | bool IDBResourceIdentifier::decode(Decoder& decoder, IDBResourceIdentifier& identifier) |
130 | { |
131 | if (!decoder.decode(identifier.m_idbConnectionIdentifier)) |
132 | return false; |
133 | |
134 | if (!decoder.decode(identifier.m_resourceNumber)) |
135 | return false; |
136 | |
137 | return true; |
138 | } |
139 | |
140 | } // namespace WebCore |
141 | |
142 | namespace WTF { |
143 | |
144 | template<> struct HashTraits<WebCore::IDBResourceIdentifier> : WebCore::IDBResourceIdentifierHashTraits { }; |
145 | template<> struct DefaultHash<WebCore::IDBResourceIdentifier> { |
146 | typedef WebCore::IDBResourceIdentifierHash Hash; |
147 | }; |
148 | |
149 | } // namespace WTF |
150 | |
151 | #endif // ENABLE(INDEXED_DATABASE) |
152 | |