2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "modules/indexeddb/IDBKeyRange.h"
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "bindings/modules/v8/ToV8ForModules.h"
31 #include "bindings/modules/v8/V8BindingForModules.h"
32 #include "core/dom/ExceptionCode.h"
33 #include "modules/indexeddb/IDBDatabase.h"
37 IDBKeyRange
* IDBKeyRange::fromScriptValue(ExecutionContext
* context
, const ScriptValue
& value
, ExceptionState
& exceptionState
)
39 if (value
.isUndefined() || value
.isNull())
42 IDBKeyRange
* range
= ScriptValue::to
<IDBKeyRange
*>(toIsolate(context
), value
, exceptionState
);
46 IDBKey
* key
= ScriptValue::to
<IDBKey
*>(toIsolate(context
), value
, exceptionState
);
47 if (exceptionState
.hadException())
49 if (!key
|| !key
->isValid()) {
50 exceptionState
.throwDOMException(DataError
, IDBDatabase::notValidKeyErrorMessage
);
54 return new IDBKeyRange(key
, key
, LowerBoundClosed
, UpperBoundClosed
);
57 IDBKeyRange::IDBKeyRange(IDBKey
* lower
, IDBKey
* upper
, LowerBoundType lowerType
, UpperBoundType upperType
)
60 , m_lowerType(lowerType
)
61 , m_upperType(upperType
)
65 DEFINE_TRACE(IDBKeyRange
)
67 visitor
->trace(m_lower
);
68 visitor
->trace(m_upper
);
71 ScriptValue
IDBKeyRange::lowerValue(ScriptState
* scriptState
) const
73 return ScriptValue::from(scriptState
, m_lower
);
76 ScriptValue
IDBKeyRange::upperValue(ScriptState
* scriptState
) const
78 return ScriptValue::from(scriptState
, m_upper
);
81 IDBKeyRange
* IDBKeyRange::only(IDBKey
* key
, ExceptionState
& exceptionState
)
83 if (!key
|| !key
->isValid()) {
84 exceptionState
.throwDOMException(DataError
, IDBDatabase::notValidKeyErrorMessage
);
88 return IDBKeyRange::create(key
, key
, LowerBoundClosed
, UpperBoundClosed
);
91 IDBKeyRange
* IDBKeyRange::only(ExecutionContext
* context
, const ScriptValue
& keyValue
, ExceptionState
& exceptionState
)
93 IDBKey
* key
= ScriptValue::to
<IDBKey
*>(toIsolate(context
), keyValue
, exceptionState
);
94 if (exceptionState
.hadException())
96 if (!key
|| !key
->isValid()) {
97 exceptionState
.throwDOMException(DataError
, IDBDatabase::notValidKeyErrorMessage
);
101 return IDBKeyRange::create(key
, key
, LowerBoundClosed
, UpperBoundClosed
);
104 IDBKeyRange
* IDBKeyRange::lowerBound(ExecutionContext
* context
, const ScriptValue
& boundValue
, bool open
, ExceptionState
& exceptionState
)
106 IDBKey
* bound
= ScriptValue::to
<IDBKey
*>(toIsolate(context
), boundValue
, exceptionState
);
107 if (exceptionState
.hadException())
109 if (!bound
|| !bound
->isValid()) {
110 exceptionState
.throwDOMException(DataError
, IDBDatabase::notValidKeyErrorMessage
);
114 return IDBKeyRange::create(bound
, nullptr, open
? LowerBoundOpen
: LowerBoundClosed
, UpperBoundOpen
);
117 IDBKeyRange
* IDBKeyRange::upperBound(ExecutionContext
* context
, const ScriptValue
& boundValue
, bool open
, ExceptionState
& exceptionState
)
119 IDBKey
* bound
= ScriptValue::to
<IDBKey
*>(toIsolate(context
), boundValue
, exceptionState
);
120 if (exceptionState
.hadException())
122 if (!bound
|| !bound
->isValid()) {
123 exceptionState
.throwDOMException(DataError
, IDBDatabase::notValidKeyErrorMessage
);
127 return IDBKeyRange::create(nullptr, bound
, LowerBoundOpen
, open
? UpperBoundOpen
: UpperBoundClosed
);
130 IDBKeyRange
* IDBKeyRange::bound(ExecutionContext
* context
, const ScriptValue
& lowerValue
, const ScriptValue
& upperValue
, bool lowerOpen
, bool upperOpen
, ExceptionState
& exceptionState
)
132 IDBKey
* lower
= ScriptValue::to
<IDBKey
*>(toIsolate(context
), lowerValue
, exceptionState
);
133 if (exceptionState
.hadException())
135 if (!lower
|| !lower
->isValid()) {
136 exceptionState
.throwDOMException(DataError
, IDBDatabase::notValidKeyErrorMessage
);
140 IDBKey
* upper
= ScriptValue::to
<IDBKey
*>(toIsolate(context
), upperValue
, exceptionState
);
141 if (exceptionState
.hadException())
143 if (!upper
|| !upper
->isValid()) {
144 exceptionState
.throwDOMException(DataError
, IDBDatabase::notValidKeyErrorMessage
);
148 if (upper
->isLessThan(lower
)) {
149 exceptionState
.throwDOMException(DataError
, "The lower key is greater than the upper key.");
152 if (upper
->isEqual(lower
) && (lowerOpen
|| upperOpen
)) {
153 exceptionState
.throwDOMException(DataError
, "The lower key and upper key are equal and one of the bounds is open.");
157 return IDBKeyRange::create(lower
, upper
, lowerOpen
? LowerBoundOpen
: LowerBoundClosed
, upperOpen
? UpperBoundOpen
: UpperBoundClosed
);