1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CURSOR_H_
6#define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CURSOR_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "content/browser/indexed_db/indexed_db_backing_store.h"
14#include "content/browser/indexed_db/indexed_db_database.h"
15#include "content/common/indexed_db/indexed_db_key_range.h"
16
17namespace content {
18
19class IndexedDBTransaction;
20
21class CONTENT_EXPORT IndexedDBCursor
22    : NON_EXPORTED_BASE(public base::RefCounted<IndexedDBCursor>) {
23 public:
24  IndexedDBCursor(scoped_ptr<IndexedDBBackingStore::Cursor> cursor,
25                  indexed_db::CursorType cursor_type,
26                  IndexedDBDatabase::TaskType task_type,
27                  IndexedDBTransaction* transaction);
28
29  void Advance(uint32 count, scoped_refptr<IndexedDBCallbacks> callbacks);
30  void Continue(scoped_ptr<IndexedDBKey> key,
31                scoped_refptr<IndexedDBCallbacks> callbacks);
32  void PrefetchContinue(int number_to_fetch,
33                        scoped_refptr<IndexedDBCallbacks> callbacks);
34  void PrefetchReset(int used_prefetches, int unused_prefetches);
35
36  const IndexedDBKey& key() const { return cursor_->key(); }
37  const IndexedDBKey& primary_key() const { return cursor_->primary_key(); }
38  std::string* Value() const {
39    return (cursor_type_ == indexed_db::CURSOR_KEY_ONLY) ? NULL
40                                                         : cursor_->Value();
41  }
42  void Close();
43
44 private:
45  friend class base::RefCounted<IndexedDBCursor>;
46
47  ~IndexedDBCursor();
48
49  class CursorIterationOperation;
50  class CursorAdvanceOperation;
51  class CursorPrefetchIterationOperation;
52
53  IndexedDBDatabase::TaskType task_type_;
54  indexed_db::CursorType cursor_type_;
55  const scoped_refptr<IndexedDBTransaction> transaction_;
56
57  // Must be destroyed before transaction_.
58  scoped_ptr<IndexedDBBackingStore::Cursor> cursor_;
59  // Must be destroyed before transaction_.
60  scoped_ptr<IndexedDBBackingStore::Cursor> saved_cursor_;
61
62  bool closed_;
63};
64
65}  // namespace content
66
67#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CURSOR_H_
68