1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/browser/indexed_db/indexed_db_cursor.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include <vector>
8f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
93551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/bind.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/logging.h"
11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "content/browser/indexed_db/indexed_db_callbacks.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/browser/indexed_db/indexed_db_database_error.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/browser/indexed_db/indexed_db_tracing.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/browser/indexed_db/indexed_db_transaction.h"
1523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "content/browser/indexed_db/indexed_db_value.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace content {
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)IndexedDBCursor::IndexedDBCursor(
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    scoped_ptr<IndexedDBBackingStore::Cursor> cursor,
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    indexed_db::CursorType cursor_type,
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    blink::WebIDBTaskType task_type,
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IndexedDBTransaction* transaction)
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : task_type_(task_type),
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      cursor_type_(cursor_type),
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      transaction_(transaction),
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      cursor_(cursor.Pass()),
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      closed_(false) {
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  transaction_->RegisterOpenCursor(this);
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)IndexedDBCursor::~IndexedDBCursor() {
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  transaction_->UnregisterOpenCursor(this);
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid IndexedDBCursor::Continue(scoped_ptr<IndexedDBKey> key,
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               scoped_ptr<IndexedDBKey> primary_key,
387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                               scoped_refptr<IndexedDBCallbacks> callbacks) {
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  IDB_TRACE("IndexedDBCursor::Continue");
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  transaction_->ScheduleTask(
423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      task_type_,
433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::Bind(&IndexedDBCursor::CursorIterationOperation,
443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 this,
453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 base::Passed(&key),
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                 base::Passed(&primary_key),
473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 callbacks));
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid IndexedDBCursor::Advance(uint32 count,
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                              scoped_refptr<IndexedDBCallbacks> callbacks) {
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  IDB_TRACE("IndexedDBCursor::Advance");
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  transaction_->ScheduleTask(
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      task_type_,
563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::Bind(
573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          &IndexedDBCursor::CursorAdvanceOperation, this, count, callbacks));
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void IndexedDBCursor::CursorAdvanceOperation(
613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    uint32 count,
623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    scoped_refptr<IndexedDBCallbacks> callbacks,
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IndexedDBTransaction* /*transaction*/) {
643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  IDB_TRACE("IndexedDBCursor::CursorAdvanceOperation");
650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  leveldb::Status s;
660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // TODO(cmumford): Handle this error (crbug.com/363397). Although this will
670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  //                 properly fail, caller will not know why, and any corruption
680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  //                 will be ignored.
690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!cursor_ || !cursor_->Advance(count, &s)) {
703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    cursor_.reset();
7123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    callbacks->OnSuccess(static_cast<IndexedDBValue*>(NULL));
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  callbacks->OnSuccess(key(), primary_key(), Value());
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void IndexedDBCursor::CursorIterationOperation(
793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    scoped_ptr<IndexedDBKey> key,
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    scoped_ptr<IndexedDBKey> primary_key,
813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    scoped_refptr<IndexedDBCallbacks> callbacks,
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IndexedDBTransaction* /*transaction*/) {
833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  IDB_TRACE("IndexedDBCursor::CursorIterationOperation");
840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  leveldb::Status s;
850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // TODO(cmumford): Handle this error (crbug.com/363397). Although this will
860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  //                 properly fail, caller will not know why, and any corruption
870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  //                 will be ignored.
880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!cursor_ || !cursor_->Continue(key.get(),
890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                     primary_key.get(),
900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                     IndexedDBBackingStore::Cursor::SEEK,
910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                     &s) || !s.ok()) {
923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    cursor_.reset();
9323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    callbacks->OnSuccess(static_cast<IndexedDBValue*>(NULL));
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  callbacks->OnSuccess(this->key(), this->primary_key(), Value());
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void IndexedDBCursor::PrefetchContinue(
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    int number_to_fetch,
102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    scoped_refptr<IndexedDBCallbacks> callbacks) {
103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  IDB_TRACE("IndexedDBCursor::PrefetchContinue");
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  transaction_->ScheduleTask(
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      task_type_,
1073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::Bind(&IndexedDBCursor::CursorPrefetchIterationOperation,
1083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 this,
1093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 number_to_fetch,
1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 callbacks));
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void IndexedDBCursor::CursorPrefetchIterationOperation(
1143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    int number_to_fetch,
1153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    scoped_refptr<IndexedDBCallbacks> callbacks,
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IndexedDBTransaction* /*transaction*/) {
1173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  IDB_TRACE("IndexedDBCursor::CursorPrefetchIterationOperation");
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::vector<IndexedDBKey> found_keys;
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::vector<IndexedDBKey> found_primary_keys;
12123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  std::vector<IndexedDBValue> found_values;
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  saved_cursor_.reset();
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const size_t max_size_estimate = 10 * 1024 * 1024;
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  size_t size_estimate = 0;
1260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  leveldb::Status s;
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // TODO(cmumford): Handle this error (crbug.com/363397). Although this will
1290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  //                 properly fail, caller will not know why, and any corruption
1300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  //                 will be ignored.
1313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  for (int i = 0; i < number_to_fetch; ++i) {
1320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (!cursor_ || !cursor_->Continue(&s)) {
1333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      cursor_.reset();
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (i == 0) {
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // First prefetched result is always used, so that's the position
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // a cursor should be reset to if the prefetch is invalidated.
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      saved_cursor_.reset(cursor_->Clone());
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    found_keys.push_back(cursor_->key());
1443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    found_primary_keys.push_back(cursor_->primary_key());
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    switch (cursor_type_) {
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      case indexed_db::CURSOR_KEY_ONLY:
14823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        found_values.push_back(IndexedDBValue());
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        break;
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      case indexed_db::CURSOR_KEY_AND_VALUE: {
15123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        IndexedDBValue value;
152a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        value.swap(*cursor_->value());
15323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        size_estimate += value.SizeEstimate();
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        found_values.push_back(value);
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        break;
156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      default:
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        NOTREACHED();
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
1603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    size_estimate += cursor_->key().size_estimate();
1613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    size_estimate += cursor_->primary_key().size_estimate();
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (size_estimate > max_size_estimate)
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!found_keys.size()) {
16823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    callbacks->OnSuccess(static_cast<IndexedDBValue*>(NULL));
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  callbacks->OnSuccessWithPrefetch(
1735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      found_keys, found_primary_keys, &found_values);
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochleveldb::Status IndexedDBCursor::PrefetchReset(int used_prefetches,
1770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                               int /* unused_prefetches */) {
178eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  IDB_TRACE("IndexedDBCursor::PrefetchReset");
179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  cursor_.swap(saved_cursor_);
180868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  saved_cursor_.reset();
1810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  leveldb::Status s;
182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (closed_)
1840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return s;
185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (cursor_) {
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // First prefetched result is always used.
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DCHECK_GT(used_prefetches, 0);
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for (int i = 0; i < used_prefetches - 1; ++i) {
1890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      bool ok = cursor_->Continue(&s);
190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DCHECK(ok);
191868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return s;
195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
196868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
197868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void IndexedDBCursor::Close() {
198eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  IDB_TRACE("IndexedDBCursor::Close");
199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  closed_ = true;
200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  cursor_.reset();
201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  saved_cursor_.reset();
202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace content
205