Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / modules / indexeddb / IDBKeyRange.cpp
blob4ee2a65d3990aa0b78c1347fcc5fa0d0e79a739f
1 /*
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
6 * are met:
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.
26 #include "config.h"
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"
35 namespace blink {
37 IDBKeyRange* IDBKeyRange::fromScriptValue(ExecutionContext* context, const ScriptValue& value, ExceptionState& exceptionState)
39 if (value.isUndefined() || value.isNull())
40 return nullptr;
42 IDBKeyRange* range = ScriptValue::to<IDBKeyRange*>(toIsolate(context), value, exceptionState);
43 if (range)
44 return range;
46 IDBKey* key = ScriptValue::to<IDBKey*>(toIsolate(context), value, exceptionState);
47 if (exceptionState.hadException())
48 return nullptr;
49 if (!key || !key->isValid()) {
50 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
51 return nullptr;
54 return new IDBKeyRange(key, key, LowerBoundClosed, UpperBoundClosed);
57 IDBKeyRange::IDBKeyRange(IDBKey* lower, IDBKey* upper, LowerBoundType lowerType, UpperBoundType upperType)
58 : m_lower(lower)
59 , m_upper(upper)
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);
85 return nullptr;
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())
95 return nullptr;
96 if (!key || !key->isValid()) {
97 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
98 return nullptr;
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())
108 return nullptr;
109 if (!bound || !bound->isValid()) {
110 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
111 return nullptr;
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())
121 return nullptr;
122 if (!bound || !bound->isValid()) {
123 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
124 return nullptr;
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())
134 return nullptr;
135 if (!lower || !lower->isValid()) {
136 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
137 return nullptr;
140 IDBKey* upper = ScriptValue::to<IDBKey*>(toIsolate(context), upperValue, exceptionState);
141 if (exceptionState.hadException())
142 return nullptr;
143 if (!upper || !upper->isValid()) {
144 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
145 return nullptr;
148 if (upper->isLessThan(lower)) {
149 exceptionState.throwDOMException(DataError, "The lower key is greater than the upper key.");
150 return nullptr;
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.");
154 return nullptr;
157 return IDBKeyRange::create(lower, upper, lowerOpen ? LowerBoundOpen : LowerBoundClosed, upperOpen ? UpperBoundOpen : UpperBoundClosed);
160 } // namespace blink