1/*
2 * Copyright (C) 2015 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#pragma once
27
28#if ENABLE(INDEXED_DATABASE)
29
30#include "IDBCursor.h"
31#include "IDBIndexInfo.h"
32#include "IDBRequest.h"
33
34namespace JSC {
35class ExecState;
36}
37
38namespace WebCore {
39
40class IDBKeyRange;
41
42struct IDBKeyRangeData;
43
44class IDBIndex final : private ActiveDOMObject {
45 WTF_MAKE_NONCOPYABLE(IDBIndex);
46 WTF_MAKE_FAST_ALLOCATED;
47public:
48 IDBIndex(ScriptExecutionContext&, const IDBIndexInfo&, IDBObjectStore&);
49
50 virtual ~IDBIndex();
51
52 const String& name() const;
53 ExceptionOr<void> setName(const String&);
54 IDBObjectStore& objectStore();
55 const IDBKeyPath& keyPath() const;
56 bool unique() const;
57 bool multiEntry() const;
58
59 void rollbackInfoForVersionChangeAbort();
60
61 ExceptionOr<Ref<IDBRequest>> openCursor(JSC::ExecState&, RefPtr<IDBKeyRange>&&, IDBCursorDirection);
62 ExceptionOr<Ref<IDBRequest>> openCursor(JSC::ExecState&, JSC::JSValue key, IDBCursorDirection);
63 ExceptionOr<Ref<IDBRequest>> openKeyCursor(JSC::ExecState&, RefPtr<IDBKeyRange>&&, IDBCursorDirection);
64 ExceptionOr<Ref<IDBRequest>> openKeyCursor(JSC::ExecState&, JSC::JSValue key, IDBCursorDirection);
65
66 ExceptionOr<Ref<IDBRequest>> count(JSC::ExecState&, IDBKeyRange*);
67 ExceptionOr<Ref<IDBRequest>> count(JSC::ExecState&, JSC::JSValue key);
68
69 ExceptionOr<Ref<IDBRequest>> get(JSC::ExecState&, IDBKeyRange*);
70 ExceptionOr<Ref<IDBRequest>> get(JSC::ExecState&, JSC::JSValue key);
71 ExceptionOr<Ref<IDBRequest>> getKey(JSC::ExecState&, IDBKeyRange*);
72 ExceptionOr<Ref<IDBRequest>> getKey(JSC::ExecState&, JSC::JSValue key);
73
74 ExceptionOr<Ref<IDBRequest>> getAll(JSC::ExecState&, RefPtr<IDBKeyRange>&&, Optional<uint32_t> count);
75 ExceptionOr<Ref<IDBRequest>> getAll(JSC::ExecState&, JSC::JSValue key, Optional<uint32_t> count);
76 ExceptionOr<Ref<IDBRequest>> getAllKeys(JSC::ExecState&, RefPtr<IDBKeyRange>&&, Optional<uint32_t> count);
77 ExceptionOr<Ref<IDBRequest>> getAllKeys(JSC::ExecState&, JSC::JSValue key, Optional<uint32_t> count);
78
79 const IDBIndexInfo& info() const { return m_info; }
80
81 void markAsDeleted();
82 bool isDeleted() const { return m_deleted; }
83
84 void ref();
85 void deref();
86
87 void* objectStoreAsOpaqueRoot() { return &m_objectStore; }
88
89private:
90 ExceptionOr<Ref<IDBRequest>> doCount(JSC::ExecState&, const IDBKeyRangeData&);
91 ExceptionOr<Ref<IDBRequest>> doGet(JSC::ExecState&, ExceptionOr<IDBKeyRangeData>);
92 ExceptionOr<Ref<IDBRequest>> doGetKey(JSC::ExecState&, ExceptionOr<IDBKeyRangeData>);
93 ExceptionOr<Ref<IDBRequest>> doOpenCursor(JSC::ExecState&, IDBCursorDirection, WTF::Function<ExceptionOr<RefPtr<IDBKeyRange>>()> &&);
94 ExceptionOr<Ref<IDBRequest>> doOpenKeyCursor(JSC::ExecState&, IDBCursorDirection, WTF::Function<ExceptionOr<RefPtr<IDBKeyRange>>()> &&);
95 ExceptionOr<Ref<IDBRequest>> doGetAll(JSC::ExecState&, Optional<uint32_t> count, WTF::Function<ExceptionOr<RefPtr<IDBKeyRange>>()> &&);
96 ExceptionOr<Ref<IDBRequest>> doGetAllKeys(JSC::ExecState&, Optional<uint32_t> count, WTF::Function<ExceptionOr<RefPtr<IDBKeyRange>>()> &&);
97
98 const char* activeDOMObjectName() const final;
99 bool canSuspendForDocumentSuspension() const final;
100 bool hasPendingActivity() const final;
101
102 IDBIndexInfo m_info;
103 IDBIndexInfo m_originalInfo;
104
105 bool m_deleted { false };
106
107 // IDBIndex objects are always owned by their referencing IDBObjectStore.
108 // Indexes will never outlive ObjectStores so its okay to keep a raw C++ reference here.
109 IDBObjectStore& m_objectStore;
110};
111
112} // namespace WebCore
113
114#endif // ENABLE(INDEXED_DATABASE)
115