1/*
2 * Copyright (C) 2010 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#include "config.h"
27#include "IDBKeyRange.h"
28
29#if ENABLE(INDEXED_DATABASE)
30
31#include "IDBBindingUtilities.h"
32#include "IDBKey.h"
33#include "IDBKeyData.h"
34#include "ScriptExecutionContext.h"
35#include <JavaScriptCore/JSCJSValue.h>
36#include <wtf/IsoMallocInlines.h>
37
38namespace WebCore {
39using namespace JSC;
40
41WTF_MAKE_ISO_ALLOCATED_IMPL(IDBKeyRange);
42
43Ref<IDBKeyRange> IDBKeyRange::create(RefPtr<IDBKey>&& lower, RefPtr<IDBKey>&& upper, bool isLowerOpen, bool isUpperOpen)
44{
45 return adoptRef(*new IDBKeyRange(WTFMove(lower), WTFMove(upper), isLowerOpen, isUpperOpen));
46}
47
48Ref<IDBKeyRange> IDBKeyRange::create(RefPtr<IDBKey>&& key)
49{
50 auto upper = key;
51 return create(WTFMove(key), WTFMove(upper), false, false);
52}
53
54IDBKeyRange::IDBKeyRange(RefPtr<IDBKey>&& lower, RefPtr<IDBKey>&& upper, bool isLowerOpen, bool isUpperOpen)
55 : m_lower(WTFMove(lower))
56 , m_upper(WTFMove(upper))
57 , m_isLowerOpen(isLowerOpen)
58 , m_isUpperOpen(isUpperOpen)
59{
60}
61
62IDBKeyRange::~IDBKeyRange() = default;
63
64ExceptionOr<Ref<IDBKeyRange>> IDBKeyRange::only(RefPtr<IDBKey>&& key)
65{
66 if (!key || !key->isValid())
67 return Exception { DataError };
68
69 return create(WTFMove(key));
70}
71
72ExceptionOr<Ref<IDBKeyRange>> IDBKeyRange::only(ExecState& state, JSValue keyValue)
73{
74 return only(scriptValueToIDBKey(state, keyValue));
75}
76
77ExceptionOr<Ref<IDBKeyRange>> IDBKeyRange::lowerBound(ExecState& state, JSValue boundValue, bool open)
78{
79 auto bound = scriptValueToIDBKey(state, boundValue);
80 if (!bound->isValid())
81 return Exception { DataError };
82
83 return create(WTFMove(bound), nullptr, open, true);
84}
85
86ExceptionOr<Ref<IDBKeyRange>> IDBKeyRange::upperBound(ExecState& state, JSValue boundValue, bool open)
87{
88 auto bound = scriptValueToIDBKey(state, boundValue);
89 if (!bound->isValid())
90 return Exception { DataError };
91
92 return create(nullptr, WTFMove(bound), true, open);
93}
94
95ExceptionOr<Ref<IDBKeyRange>> IDBKeyRange::bound(ExecState& state, JSValue lowerValue, JSValue upperValue, bool lowerOpen, bool upperOpen)
96{
97 auto lower = scriptValueToIDBKey(state, lowerValue);
98 if (!lower->isValid())
99 return Exception { DataError };
100 auto upper = scriptValueToIDBKey(state, upperValue);
101 if (!upper->isValid())
102 return Exception { DataError };
103 if (upper->isLessThan(lower.get()))
104 return Exception { DataError };
105 if (upper->isEqual(lower.get()) && (lowerOpen || upperOpen))
106 return Exception { DataError };
107
108 return create(WTFMove(lower), WTFMove(upper), lowerOpen, upperOpen);
109}
110
111bool IDBKeyRange::isOnlyKey() const
112{
113 return m_lower && m_upper && !m_isLowerOpen && !m_isUpperOpen && m_lower->isEqual(*m_upper);
114}
115
116ExceptionOr<bool> IDBKeyRange::includes(JSC::ExecState& state, JSC::JSValue keyValue)
117{
118 auto key = scriptValueToIDBKey(state, keyValue);
119 if (!key->isValid())
120 return Exception { DataError, "Failed to execute 'includes' on 'IDBKeyRange': The passed-in value is not a valid IndexedDB key." };
121
122 if (m_lower) {
123 int compare = m_lower->compare(key.get());
124
125 if (compare > 0)
126 return false;
127 if (m_isLowerOpen && !compare)
128 return false;
129 }
130
131 if (m_upper) {
132 int compare = m_upper->compare(key.get());
133
134 if (compare < 0)
135 return false;
136 if (m_isUpperOpen && !compare)
137 return false;
138 }
139
140 return true;
141}
142
143} // namespace WebCore
144
145#endif // ENABLE(INDEXED_DATABASE)
146