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 "modules/indexeddb/IDBKeyRange.h" 28 29#include "bindings/v8/DOMRequestState.h" 30#include "bindings/v8/ExceptionState.h" 31#include "bindings/v8/IDBBindingUtilities.h" 32#include "core/dom/ExceptionCode.h" 33#include "modules/indexeddb/IDBDatabase.h" 34 35namespace WebCore { 36 37PassRefPtr<IDBKeyRange> IDBKeyRange::fromScriptValue(ExecutionContext* context, const ScriptValue& value, ExceptionState& exceptionState) 38{ 39 DOMRequestState requestState(context); 40 if (value.isUndefined() || value.isNull()) 41 return 0; 42 43 RefPtr<IDBKeyRange> range = scriptValueToIDBKeyRange(&requestState, value); 44 if (range) 45 return range.release(); 46 47 RefPtr<IDBKey> key = scriptValueToIDBKey(&requestState, value); 48 if (!key || !key->isValid()) { 49 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage); 50 return 0; 51 } 52 53 return adoptRef(new IDBKeyRange(key, key, LowerBoundClosed, UpperBoundClosed)); 54} 55 56IDBKeyRange::IDBKeyRange(PassRefPtr<IDBKey> lower, PassRefPtr<IDBKey> upper, LowerBoundType lowerType, UpperBoundType upperType) 57 : m_lower(lower) 58 , m_upper(upper) 59 , m_lowerType(lowerType) 60 , m_upperType(upperType) 61{ 62 ScriptWrappable::init(this); 63} 64 65ScriptValue IDBKeyRange::lowerValue(ExecutionContext* context) const 66{ 67 DOMRequestState requestState(context); 68 return idbKeyToScriptValue(&requestState, m_lower); 69} 70 71ScriptValue IDBKeyRange::upperValue(ExecutionContext* context) const 72{ 73 DOMRequestState requestState(context); 74 return idbKeyToScriptValue(&requestState, m_upper); 75} 76 77PassRefPtr<IDBKeyRange> IDBKeyRange::only(PassRefPtr<IDBKey> prpKey, ExceptionState& exceptionState) 78{ 79 RefPtr<IDBKey> key = prpKey; 80 if (!key || !key->isValid()) { 81 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage); 82 return 0; 83 } 84 85 return IDBKeyRange::create(key, key, LowerBoundClosed, UpperBoundClosed); 86} 87 88PassRefPtr<IDBKeyRange> IDBKeyRange::only(ExecutionContext* context, const ScriptValue& keyValue, ExceptionState& exceptionState) 89{ 90 DOMRequestState requestState(context); 91 RefPtr<IDBKey> key = scriptValueToIDBKey(&requestState, keyValue); 92 if (!key || !key->isValid()) { 93 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage); 94 return 0; 95 } 96 97 return IDBKeyRange::create(key, key, LowerBoundClosed, UpperBoundClosed); 98} 99 100PassRefPtr<IDBKeyRange> IDBKeyRange::lowerBound(ExecutionContext* context, const ScriptValue& boundValue, bool open, ExceptionState& exceptionState) 101{ 102 DOMRequestState requestState(context); 103 RefPtr<IDBKey> bound = scriptValueToIDBKey(&requestState, boundValue); 104 if (!bound || !bound->isValid()) { 105 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage); 106 return 0; 107 } 108 109 return IDBKeyRange::create(bound, 0, open ? LowerBoundOpen : LowerBoundClosed, UpperBoundOpen); 110} 111 112PassRefPtr<IDBKeyRange> IDBKeyRange::upperBound(ExecutionContext* context, const ScriptValue& boundValue, bool open, ExceptionState& exceptionState) 113{ 114 DOMRequestState requestState(context); 115 RefPtr<IDBKey> bound = scriptValueToIDBKey(&requestState, boundValue); 116 if (!bound || !bound->isValid()) { 117 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage); 118 return 0; 119 } 120 121 return IDBKeyRange::create(0, bound, LowerBoundOpen, open ? UpperBoundOpen : UpperBoundClosed); 122} 123 124PassRefPtr<IDBKeyRange> IDBKeyRange::bound(ExecutionContext* context, const ScriptValue& lowerValue, const ScriptValue& upperValue, bool lowerOpen, bool upperOpen, ExceptionState& exceptionState) 125{ 126 DOMRequestState requestState(context); 127 RefPtr<IDBKey> lower = scriptValueToIDBKey(&requestState, lowerValue); 128 RefPtr<IDBKey> upper = scriptValueToIDBKey(&requestState, upperValue); 129 130 if (!lower || !lower->isValid() || !upper || !upper->isValid()) { 131 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage); 132 return 0; 133 } 134 if (upper->isLessThan(lower.get())) { 135 exceptionState.throwDOMException(DataError, "The lower key is greater than the upper key."); 136 return 0; 137 } 138 if (upper->isEqual(lower.get()) && (lowerOpen || upperOpen)) { 139 exceptionState.throwDOMException(DataError, "The lower key and upper key are equal and one of the bounds is open."); 140 return 0; 141 } 142 143 return IDBKeyRange::create(lower, upper, lowerOpen ? LowerBoundOpen : LowerBoundClosed, upperOpen ? UpperBoundOpen : UpperBoundClosed); 144} 145 146} // namespace WebCore 147