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 "ExceptionOr.h"
31#include "IDBCursorDirection.h"
32#include "IDBCursorInfo.h"
33#include "IDBKeyPath.h"
34#include "IDBRequest.h"
35#include "IDBValue.h"
36#include "JSValueInWrappedObject.h"
37#include <JavaScriptCore/Strong.h>
38#include <wtf/Variant.h>
39#include <wtf/WeakPtr.h>
40
41namespace WebCore {
42
43class IDBGetResult;
44class IDBIndex;
45class IDBObjectStore;
46class IDBTransaction;
47
48class IDBCursor : public ScriptWrappable, public RefCounted<IDBCursor> {
49 WTF_MAKE_ISO_ALLOCATED(IDBCursor);
50public:
51 static Ref<IDBCursor> create(IDBObjectStore&, const IDBCursorInfo&);
52 static Ref<IDBCursor> create(IDBIndex&, const IDBCursorInfo&);
53
54 virtual ~IDBCursor();
55
56 using Source = Variant<RefPtr<IDBObjectStore>, RefPtr<IDBIndex>>;
57
58 const Source& source() const;
59 IDBCursorDirection direction() const;
60
61 IDBKey* key() { return m_key.get(); };
62 IDBKey* primaryKey() { return m_primaryKey.get(); };
63 IDBValue value() { return m_value; };
64 const Optional<IDBKeyPath>& primaryKeyPath() { return m_keyPath; };
65 JSValueInWrappedObject& keyWrapper() { return m_keyWrapper; }
66 JSValueInWrappedObject& primaryKeyWrapper() { return m_primaryKeyWrapper; }
67 JSValueInWrappedObject& valueWrapper() { return m_valueWrapper; }
68
69 ExceptionOr<Ref<IDBRequest>> update(JSC::ExecState&, JSC::JSValue);
70 ExceptionOr<void> advance(unsigned);
71 ExceptionOr<void> continueFunction(JSC::ExecState&, JSC::JSValue key);
72 ExceptionOr<void> continuePrimaryKey(JSC::ExecState&, JSC::JSValue key, JSC::JSValue primaryKey);
73 ExceptionOr<Ref<IDBRequest>> deleteFunction(JSC::ExecState&);
74
75 ExceptionOr<void> continueFunction(const IDBKeyData&);
76
77 const IDBCursorInfo& info() const { return m_info; }
78
79 void setRequest(IDBRequest& request) { m_request = makeWeakPtr(&request); }
80 void clearRequest() { m_request.clear(); }
81 void clearWrappers();
82 IDBRequest* request() { return m_request.get(); }
83
84 bool setGetResult(IDBRequest&, const IDBGetResult&);
85
86 virtual bool isKeyCursorWithValue() const { return false; }
87
88protected:
89 IDBCursor(IDBObjectStore&, const IDBCursorInfo&);
90 IDBCursor(IDBIndex&, const IDBCursorInfo&);
91
92private:
93 bool sourcesDeleted() const;
94 IDBObjectStore& effectiveObjectStore() const;
95 IDBTransaction& transaction() const;
96
97 void uncheckedIterateCursor(const IDBKeyData&, unsigned count);
98 void uncheckedIterateCursor(const IDBKeyData&, const IDBKeyData&);
99
100 IDBCursorInfo m_info;
101 Source m_source;
102 WeakPtr<IDBRequest> m_request;
103
104 bool m_gotValue { false };
105
106 RefPtr<IDBKey> m_key;
107 RefPtr<IDBKey> m_primaryKey;
108 IDBKeyData m_keyData;
109 IDBKeyData m_primaryKeyData;
110 IDBValue m_value;
111 Optional<IDBKeyPath> m_keyPath;
112
113 JSValueInWrappedObject m_keyWrapper;
114 JSValueInWrappedObject m_primaryKeyWrapper;
115 JSValueInWrappedObject m_valueWrapper;
116};
117
118
119inline const IDBCursor::Source& IDBCursor::source() const
120{
121 return m_source;
122}
123
124inline IDBCursorDirection IDBCursor::direction() const
125{
126 return m_info.cursorDirection();
127}
128
129} // namespace WebCore
130
131#endif // ENABLE(INDEXED_DATABASE)
132