1/*
2 * Copyright (C) 2011 Google 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * 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 "IndexedDB.h"
31#include "ThreadSafeDataBuffer.h"
32#include <wtf/Forward.h>
33#include <wtf/RefCounted.h>
34#include <wtf/Variant.h>
35#include <wtf/Vector.h>
36#include <wtf/text/WTFString.h>
37
38namespace JSC {
39class JSArrayBuffer;
40class JSArrayBufferView;
41}
42
43namespace WebCore {
44
45class IDBKey : public RefCounted<IDBKey> {
46public:
47 static Ref<IDBKey> createInvalid()
48 {
49 return adoptRef(*new IDBKey());
50 }
51
52 static Ref<IDBKey> createNumber(double number)
53 {
54 return adoptRef(*new IDBKey(IndexedDB::KeyType::Number, number));
55 }
56
57 static Ref<IDBKey> createString(const String& string)
58 {
59 return adoptRef(*new IDBKey(string));
60 }
61
62 static Ref<IDBKey> createDate(double date)
63 {
64 return adoptRef(*new IDBKey(IndexedDB::KeyType::Date, date));
65 }
66
67 static Ref<IDBKey> createMultiEntryArray(const Vector<RefPtr<IDBKey>>& array)
68 {
69 Vector<RefPtr<IDBKey>> result;
70
71 size_t sizeEstimate = 0;
72 for (auto& key : array) {
73 if (!key->isValid())
74 continue;
75
76 bool skip = false;
77 for (auto& resultKey : result) {
78 if (key->isEqual(*resultKey)) {
79 skip = true;
80 break;
81 }
82 }
83 if (!skip) {
84 result.append(key);
85 sizeEstimate += key->m_sizeEstimate;
86 }
87 }
88 auto idbKey = adoptRef(*new IDBKey(result, sizeEstimate));
89 ASSERT(idbKey->isValid());
90 return idbKey;
91 }
92
93 static Ref<IDBKey> createArray(const Vector<RefPtr<IDBKey>>& array)
94 {
95 size_t sizeEstimate = 0;
96 for (auto& key : array)
97 sizeEstimate += key->m_sizeEstimate;
98
99 return adoptRef(*new IDBKey(array, sizeEstimate));
100 }
101
102 static Ref<IDBKey> createBinary(const ThreadSafeDataBuffer&);
103 static Ref<IDBKey> createBinary(JSC::JSArrayBuffer&);
104 static Ref<IDBKey> createBinary(JSC::JSArrayBufferView&);
105
106 WEBCORE_EXPORT ~IDBKey();
107
108 IndexedDB::KeyType type() const { return m_type; }
109 WEBCORE_EXPORT bool isValid() const;
110
111 const Vector<RefPtr<IDBKey>>& array() const
112 {
113 ASSERT(m_type == IndexedDB::KeyType::Array);
114 return WTF::get<Vector<RefPtr<IDBKey>>>(m_value);
115 }
116
117 const String& string() const
118 {
119 ASSERT(m_type == IndexedDB::KeyType::String);
120 return WTF::get<String>(m_value);
121 }
122
123 double date() const
124 {
125 ASSERT(m_type == IndexedDB::KeyType::Date);
126 return WTF::get<double>(m_value);
127 }
128
129 double number() const
130 {
131 ASSERT(m_type == IndexedDB::KeyType::Number);
132 return WTF::get<double>(m_value);
133 }
134
135 const ThreadSafeDataBuffer& binary() const
136 {
137 ASSERT(m_type == IndexedDB::KeyType::Binary);
138 return WTF::get<ThreadSafeDataBuffer>(m_value);
139 }
140
141 int compare(const IDBKey& other) const;
142 bool isLessThan(const IDBKey& other) const;
143 bool isEqual(const IDBKey& other) const;
144
145 size_t sizeEstimate() const { return m_sizeEstimate; }
146
147 static int compareTypes(IndexedDB::KeyType a, IndexedDB::KeyType b)
148 {
149 return b - a;
150 }
151
152 using RefCounted<IDBKey>::ref;
153 using RefCounted<IDBKey>::deref;
154
155#if !LOG_DISABLED
156 String loggingString() const;
157#endif
158
159private:
160 IDBKey()
161 : m_type(IndexedDB::KeyType::Invalid)
162 , m_sizeEstimate(OverheadSize)
163 {
164 }
165
166 IDBKey(IndexedDB::KeyType, double number);
167 explicit IDBKey(const String& value);
168 IDBKey(const Vector<RefPtr<IDBKey>>& keyArray, size_t arraySize);
169 explicit IDBKey(const ThreadSafeDataBuffer&);
170
171 const IndexedDB::KeyType m_type;
172 Variant<Vector<RefPtr<IDBKey>>, String, double, ThreadSafeDataBuffer> m_value;
173
174 const size_t m_sizeEstimate;
175
176 // Very rough estimate of minimum key size overhead.
177 enum { OverheadSize = 16 };
178};
179
180inline int compareBinaryKeyData(const Vector<uint8_t>& a, const Vector<uint8_t>& b)
181{
182 size_t length = std::min(a.size(), b.size());
183
184 for (size_t i = 0; i < length; ++i) {
185 if (a[i] > b[i])
186 return 1;
187 if (a[i] < b[i])
188 return -1;
189 }
190
191 if (a.size() == b.size())
192 return 0;
193
194 if (a.size() > b.size())
195 return 1;
196
197 return -1;
198}
199
200inline int compareBinaryKeyData(const ThreadSafeDataBuffer& a, const ThreadSafeDataBuffer& b)
201{
202 auto* aData = a.data();
203 auto* bData = b.data();
204
205 // Covers the cases where both pointers are null as well as both pointing to the same buffer.
206 if (aData == bData)
207 return 0;
208
209 if (aData && !bData)
210 return 1;
211 if (!aData && bData)
212 return -1;
213
214 return compareBinaryKeyData(*aData, *bData);
215}
216
217} // namespace WebCore
218
219#endif // ENABLE(INDEXED_DATABASE)
220