1 // Copyright 2016 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 #include "content/browser/indexed_db/cursor_impl.h"
6 
7 #include <utility>
8 
9 #include "base/bind.h"
10 #include "base/sequenced_task_runner.h"
11 #include "content/browser/indexed_db/indexed_db_callbacks.h"
12 #include "content/browser/indexed_db/indexed_db_cursor.h"
13 #include "content/browser/indexed_db/indexed_db_dispatcher_host.h"
14 
15 namespace content {
16 
CursorImpl(std::unique_ptr<IndexedDBCursor> cursor,const url::Origin & origin,IndexedDBDispatcherHost * dispatcher_host,scoped_refptr<base::SequencedTaskRunner> idb_runner)17 CursorImpl::CursorImpl(std::unique_ptr<IndexedDBCursor> cursor,
18                        const url::Origin& origin,
19                        IndexedDBDispatcherHost* dispatcher_host,
20                        scoped_refptr<base::SequencedTaskRunner> idb_runner)
21     : dispatcher_host_(dispatcher_host),
22       origin_(origin),
23       idb_runner_(std::move(idb_runner)),
24       cursor_(std::move(cursor)) {
25   DCHECK(idb_runner_);
26   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
27 }
28 
~CursorImpl()29 CursorImpl::~CursorImpl() {
30   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
31 }
32 
Advance(uint32_t count,blink::mojom::IDBCursor::AdvanceCallback callback)33 void CursorImpl::Advance(uint32_t count,
34                          blink::mojom::IDBCursor::AdvanceCallback callback) {
35   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
36   cursor_->Advance(count, dispatcher_host_->AsWeakPtr(), std::move(callback));
37 }
38 
CursorContinue(const blink::IndexedDBKey & key,const blink::IndexedDBKey & primary_key,blink::mojom::IDBCursor::CursorContinueCallback callback)39 void CursorImpl::CursorContinue(
40     const blink::IndexedDBKey& key,
41     const blink::IndexedDBKey& primary_key,
42     blink::mojom::IDBCursor::CursorContinueCallback callback) {
43   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
44   cursor_->Continue(
45       dispatcher_host_->AsWeakPtr(),
46       key.IsValid() ? std::make_unique<blink::IndexedDBKey>(key) : nullptr,
47       primary_key.IsValid() ? std::make_unique<blink::IndexedDBKey>(primary_key)
48                             : nullptr,
49       std::move(callback));
50 }
51 
Prefetch(int32_t count,blink::mojom::IDBCursor::PrefetchCallback callback)52 void CursorImpl::Prefetch(int32_t count,
53                           blink::mojom::IDBCursor::PrefetchCallback callback) {
54   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
55   cursor_->PrefetchContinue(dispatcher_host_->AsWeakPtr(), count,
56                             std::move(callback));
57 }
58 
PrefetchReset(int32_t used_prefetches,int32_t unused_prefetches)59 void CursorImpl::PrefetchReset(int32_t used_prefetches,
60                                int32_t unused_prefetches) {
61   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
62   leveldb::Status s =
63       cursor_->PrefetchReset(used_prefetches, unused_prefetches);
64   // TODO(cmumford): Handle this error (crbug.com/363397)
65   if (!s.ok())
66     DLOG(ERROR) << "Unable to reset prefetch";
67 }
68 
OnRemoveBinding(base::OnceClosure remove_binding_cb)69 void CursorImpl::OnRemoveBinding(base::OnceClosure remove_binding_cb) {
70   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
71   cursor_->OnRemoveBinding(std::move(remove_binding_cb));
72 }
73 
74 }  // namespace content
75