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#ifndef IDBCursor_h
27#define IDBCursor_h
28
29#if ENABLE(INDEXED_DATABASE)
30
31#include "IDBKey.h"
32#include "IDBTransaction.h"
33#include "IndexedDB.h"
34#include "ScriptWrappable.h"
35#include <bindings/ScriptValue.h>
36#include <wtf/PassRefPtr.h>
37#include <wtf/RefCounted.h>
38#include <wtf/RefPtr.h>
39
40namespace WebCore {
41
42class DOMRequestState;
43class IDBAny;
44class IDBCallbacks;
45class IDBCursorBackend;
46class IDBRequest;
47class ScriptExecutionContext;
48
49typedef int ExceptionCode;
50
51class IDBCursor : public ScriptWrappable, public RefCounted<IDBCursor> {
52public:
53    static const AtomicString& directionNext();
54    static const AtomicString& directionNextUnique();
55    static const AtomicString& directionPrev();
56    static const AtomicString& directionPrevUnique();
57
58    static IndexedDB::CursorDirection stringToDirection(const String& modeString, ExceptionCode&);
59    static const AtomicString& directionToString(IndexedDB::CursorDirection mode);
60
61    static PassRefPtr<IDBCursor> create(PassRefPtr<IDBCursorBackend>, IndexedDB::CursorDirection, IDBRequest*, IDBAny* source, IDBTransaction*);
62    virtual ~IDBCursor();
63
64    // Implement the IDL
65    const String& direction() const;
66    const Deprecated::ScriptValue& key() const;
67    const Deprecated::ScriptValue& primaryKey() const;
68    const Deprecated::ScriptValue& value() const;
69    IDBAny* source() const;
70
71    PassRefPtr<IDBRequest> update(JSC::ExecState*, Deprecated::ScriptValue&, ExceptionCode&);
72    void advance(unsigned long, ExceptionCode&);
73    // FIXME: Try to modify the code generator so this overload is unneeded.
74    void continueFunction(ScriptExecutionContext*, ExceptionCode& ec) { continueFunction(static_cast<IDBKey*>(0), ec); }
75    void continueFunction(ScriptExecutionContext*, const Deprecated::ScriptValue& key, ExceptionCode&);
76    PassRefPtr<IDBRequest> deleteFunction(ScriptExecutionContext*, ExceptionCode&);
77
78    void continueFunction(PassRefPtr<IDBKey>, ExceptionCode&);
79    void postSuccessHandlerCallback();
80    void close();
81    void setValueReady(DOMRequestState*, PassRefPtr<IDBKey>, PassRefPtr<IDBKey> primaryKey, Deprecated::ScriptValue&);
82    PassRefPtr<IDBKey> idbPrimaryKey() { return m_currentPrimaryKey; }
83
84protected:
85    IDBCursor(PassRefPtr<IDBCursorBackend>, IndexedDB::CursorDirection, IDBRequest*, IDBAny* source, IDBTransaction*);
86    virtual bool isKeyCursor() const { return true; }
87
88private:
89    PassRefPtr<IDBObjectStore> effectiveObjectStore();
90
91    RefPtr<IDBCursorBackend> m_backend;
92    RefPtr<IDBRequest> m_request;
93    const IndexedDB::CursorDirection m_direction;
94    RefPtr<IDBAny> m_source;
95    RefPtr<IDBTransaction> m_transaction;
96    IDBTransaction::OpenCursorNotifier m_transactionNotifier;
97    bool m_gotValue;
98    // These values are held because m_backend may advance while they
99    // are still valid for the current success handlers.
100    Deprecated::ScriptValue m_currentKeyValue;
101    Deprecated::ScriptValue m_currentPrimaryKeyValue;
102    RefPtr<IDBKey> m_currentKey;
103    RefPtr<IDBKey> m_currentPrimaryKey;
104    Deprecated::ScriptValue m_currentValue;
105};
106
107} // namespace WebCore
108
109#endif
110
111#endif // IDBCursor_h
112