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 "IDBCursorBackend.h"
28
29#if ENABLE(INDEXED_DATABASE)
30
31#include "IDBCallbacks.h"
32#include "IDBCursorBackendOperations.h"
33#include "IDBDatabaseBackend.h"
34#include "IDBDatabaseCallbacks.h"
35#include "IDBDatabaseError.h"
36#include "IDBDatabaseException.h"
37#include "IDBKeyRange.h"
38#include "IDBOperation.h"
39#include "IDBServerConnection.h"
40#include "Logging.h"
41#include "SharedBuffer.h"
42
43namespace WebCore {
44
45IDBCursorBackend::IDBCursorBackend(int64_t cursorID, IndexedDB::CursorType cursorType, IDBDatabaseBackend::TaskType taskType, IDBTransactionBackend& transaction, int64_t objectStoreID)
46    : m_taskType(taskType)
47    , m_cursorType(cursorType)
48    , m_transaction(&transaction)
49    , m_objectStoreID(objectStoreID)
50    , m_cursorID(cursorID)
51    , m_savedCursorID(0)
52    , m_closed(false)
53{
54    m_transaction->registerOpenCursor(this);
55}
56
57IDBCursorBackend::~IDBCursorBackend()
58{
59    m_transaction->unregisterOpenCursor(this);
60}
61
62void IDBCursorBackend::continueFunction(PassRefPtr<IDBKey> key, PassRefPtr<IDBCallbacks> prpCallbacks, ExceptionCode&)
63{
64    LOG(StorageAPI, "IDBCursorBackend::continue");
65    RefPtr<IDBCallbacks> callbacks = prpCallbacks;
66    m_transaction->scheduleTask(m_taskType, CursorIterationOperation::create(this, key, callbacks));
67}
68
69void IDBCursorBackend::advance(unsigned long count, PassRefPtr<IDBCallbacks> prpCallbacks, ExceptionCode&)
70{
71    LOG(StorageAPI, "IDBCursorBackend::advance");
72    RefPtr<IDBCallbacks> callbacks = prpCallbacks;
73    m_transaction->scheduleTask(CursorAdvanceOperation::create(this, count, callbacks));
74}
75
76void IDBCursorBackend::deleteFunction(PassRefPtr<IDBCallbacks> prpCallbacks, ExceptionCode&)
77{
78    LOG(StorageAPI, "IDBCursorBackend::delete");
79    ASSERT(m_transaction->mode() != IndexedDB::TransactionMode::ReadOnly);
80    RefPtr<IDBKeyRange> keyRange = IDBKeyRange::create(primaryKey());
81    m_transaction->database().deleteRange(m_transaction->id(), m_objectStoreID, keyRange.release(), prpCallbacks);
82}
83
84void IDBCursorBackend::close()
85{
86    LOG(StorageAPI, "IDBCursorBackend::close");
87    clear();
88    m_closed = true;
89    m_savedCursorID = 0;
90}
91
92void IDBCursorBackend::updateCursorData(IDBKey* key, IDBKey* primaryKey, SharedBuffer* valueBuffer)
93{
94    m_currentKey = key;
95    m_currentPrimaryKey = primaryKey;
96    m_currentValueBuffer = valueBuffer;
97}
98
99void IDBCursorBackend::clear()
100{
101    m_cursorID = 0;
102    m_currentKey = nullptr;
103    m_currentPrimaryKey = nullptr;
104    m_currentValueBuffer = nullptr;
105}
106
107} // namespace WebCore
108
109#endif // ENABLE(INDEXED_DATABASE)
110