| 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include "config.h" |
| 27 | #include "JSIDBRequest.h" |
| 28 | |
| 29 | #if ENABLE(INDEXED_DATABASE) |
| 30 | |
| 31 | #include "IDBBindingUtilities.h" |
| 32 | #include "JSDOMConvertInterface.h" |
| 33 | #include "JSIDBCursor.h" |
| 34 | #include "JSIDBDatabase.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | using namespace JSC; |
| 38 | |
| 39 | JSC::JSValue JSIDBRequest::result(JSC::ExecState& state) const |
| 40 | { |
| 41 | return cachedPropertyValue(state, *this, wrapped().resultWrapper(), [&] { |
| 42 | auto result = wrapped().result(); |
| 43 | if (UNLIKELY(result.hasException())) { |
| 44 | auto throwScope = DECLARE_THROW_SCOPE(state.vm()); |
| 45 | propagateException(state, throwScope, result.releaseException()); |
| 46 | return jsNull(); |
| 47 | } |
| 48 | |
| 49 | IDBRequest::Result resultValue = result.releaseReturnValue(); |
| 50 | return WTF::switchOn(resultValue, [&state] (RefPtr<IDBCursor>& cursor) { |
| 51 | auto throwScope = DECLARE_THROW_SCOPE(state.vm()); |
| 52 | return toJS<IDLInterface<IDBCursor>>(state, *jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject()), throwScope, cursor.get()); |
| 53 | }, [&state] (RefPtr<IDBDatabase>& database) { |
| 54 | auto throwScope = DECLARE_THROW_SCOPE(state.vm()); |
| 55 | return toJS<IDLInterface<IDBDatabase>>(state, *jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject()), throwScope, database.get()); |
| 56 | }, [&state] (IDBKeyData keyData) { |
| 57 | return toJS<IDLIDBKeyData>(state, *jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject()), keyData); |
| 58 | }, [&state] (Vector<IDBKeyData> keyDatas) { |
| 59 | return toJS<IDLSequence<IDLIDBKeyData>>(state, *jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject()), keyDatas); |
| 60 | }, [&state] (IDBGetResult getResult) { |
| 61 | auto result = deserializeIDBValueWithKeyInjection(state, getResult.value(), getResult.keyData(), getResult.keyPath()); |
| 62 | return result ? result.value() : jsNull(); |
| 63 | }, [&state] (IDBGetAllResult getAllResult) { |
| 64 | auto& keys = getAllResult.keys(); |
| 65 | auto& values = getAllResult.values(); |
| 66 | auto& keyPath = getAllResult.keyPath(); |
| 67 | auto scope = DECLARE_THROW_SCOPE(state.vm()); |
| 68 | JSC::MarkedArgumentBuffer list; |
| 69 | for (unsigned i = 0; i < values.size(); i ++) { |
| 70 | auto result = deserializeIDBValueWithKeyInjection(state, values[i], keys[i], keyPath); |
| 71 | if (!result) |
| 72 | return jsNull(); |
| 73 | list.append(result.value()); |
| 74 | if (UNLIKELY(list.hasOverflowed())) { |
| 75 | propagateException(state, scope, Exception(UnknownError)); |
| 76 | return jsNull(); |
| 77 | } |
| 78 | } |
| 79 | return JSValue(JSC::constructArray(&state, nullptr, state.lexicalGlobalObject(), list)); |
| 80 | }, [] (uint64_t number) { |
| 81 | return toJS<IDLUnsignedLongLong>(number); |
| 82 | }, [] (IDBRequest::NullResultType other) { |
| 83 | if (other == IDBRequest::NullResultType::Empty) |
| 84 | return JSC::jsNull(); |
| 85 | return JSC::jsUndefined(); |
| 86 | }); |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | void JSIDBRequest::visitAdditionalChildren(SlotVisitor& visitor) |
| 91 | { |
| 92 | auto& request = wrapped(); |
| 93 | request.resultWrapper().visit(visitor); |
| 94 | request.cursorWrapper().visit(visitor); |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | #endif // ENABLE(INDEXED_DATABASE) |
| 99 | |