1/*
2 * Copyright (C) 2014 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 "IDBKeyData.h"
31#include "IDBKeyRange.h"
32
33namespace WebCore {
34
35class IDBKey;
36
37struct IDBKeyRangeData {
38 IDBKeyRangeData()
39 : isNull(true)
40 , lowerOpen(false)
41 , upperOpen(false)
42 {
43 }
44
45 static IDBKeyRangeData allKeys()
46 {
47 IDBKeyRangeData result;
48 result.isNull = false;
49 result.lowerKey = IDBKeyData::minimum();
50 result.upperKey = IDBKeyData::maximum();
51 return result;
52 }
53
54 IDBKeyRangeData(IDBKey*);
55 IDBKeyRangeData(const IDBKeyData&);
56
57 IDBKeyRangeData(IDBKeyRange* keyRange)
58 : isNull(!keyRange)
59 , lowerOpen(false)
60 , upperOpen(false)
61 {
62 if (isNull)
63 return;
64
65 lowerKey = keyRange->lower();
66 upperKey = keyRange->upper();
67 lowerOpen = keyRange->lowerOpen();
68 upperOpen = keyRange->upperOpen();
69 }
70
71 IDBKeyRangeData isolatedCopy() const;
72
73 WEBCORE_EXPORT RefPtr<IDBKeyRange> maybeCreateIDBKeyRange() const;
74
75 WEBCORE_EXPORT bool isExactlyOneKey() const;
76 bool containsKey(const IDBKeyData&) const;
77 bool isValid() const;
78
79 template<class Encoder> void encode(Encoder&) const;
80 template<class Decoder> static bool decode(Decoder&, IDBKeyRangeData&);
81
82 bool isNull;
83
84 IDBKeyData lowerKey;
85 IDBKeyData upperKey;
86
87 bool lowerOpen;
88 bool upperOpen;
89
90#if !LOG_DISABLED
91 String loggingString() const;
92#endif
93};
94
95template<class Encoder>
96void IDBKeyRangeData::encode(Encoder& encoder) const
97{
98 encoder << isNull;
99 if (isNull)
100 return;
101
102 encoder << upperKey << lowerKey << upperOpen << lowerOpen;
103}
104
105template<class Decoder>
106bool IDBKeyRangeData::decode(Decoder& decoder, IDBKeyRangeData& keyRange)
107{
108 if (!decoder.decode(keyRange.isNull))
109 return false;
110
111 if (keyRange.isNull)
112 return true;
113
114 Optional<IDBKeyData> upperKey;
115 decoder >> upperKey;
116 if (!upperKey)
117 return false;
118 keyRange.upperKey = WTFMove(*upperKey);
119
120 Optional<IDBKeyData> lowerKey;
121 decoder >> lowerKey;
122 if (!lowerKey)
123 return false;
124 keyRange.lowerKey = WTFMove(*lowerKey);
125
126 if (!decoder.decode(keyRange.upperOpen))
127 return false;
128
129 if (!decoder.decode(keyRange.lowerOpen))
130 return false;
131
132 return true;
133}
134
135} // namespace WebCore
136
137#endif // ENABLE(INDEXED_DATABASE)
138