blob: fa7de4b1b19488d0e2b229b2f5f5d324e1830ee8 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[email protected]72a4183d2013-05-31 18:33:102// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Evan Stade1a8d9d42024-09-10 19:37:195#ifndef CONTENT_BROWSER_INDEXED_DB_INSTANCE_CURSOR_H_
6#define CONTENT_BROWSER_INDEXED_DB_INSTANCE_CURSOR_H_
[email protected]72a4183d2013-05-31 18:33:107
avib7348942015-12-25 20:57:108#include <stdint.h>
9
dcheng531cca92016-04-09 03:03:2310#include <memory>
[email protected]907a8bc52013-06-07 16:32:3411
Evan Stadeb568dbdc2023-08-10 05:21:2412#include "base/memory/raw_ptr.h"
dmurph9d00e05d2016-12-01 23:00:3413#include "base/memory/weak_ptr.h"
Evan Staded9529ea52025-04-11 17:02:5014#include "content/browser/indexed_db/instance/backing_store.h"
Evan Stade1a8d9d42024-09-10 19:37:1915#include "content/browser/indexed_db/instance/database.h"
16#include "content/browser/indexed_db/instance/transaction.h"
Evan Stade6584a492023-09-25 21:02:3017#include "mojo/public/cpp/bindings/associated_receiver.h"
18#include "mojo/public/cpp/bindings/pending_associated_remote.h"
Henrique Ferreiroda0a55c2019-11-12 14:06:0419#include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-forward.h"
[email protected]72a4183d2013-05-31 18:33:1020
Ari Chivukula664bf8d2022-04-27 23:28:1821namespace storage {
22struct BucketLocator;
23} // namespace storage
24
Evan Stadecbb1e002024-09-13 20:06:5725namespace content::indexed_db {
[email protected]72a4183d2013-05-31 18:33:1026
Evan Stade6265dcd2024-02-27 20:50:0427enum class CursorType { kKeyAndValue = 0, kKeyOnly = 1 };
Evan Stade6265dcd2024-02-27 20:50:0428
Evan Stadecbb1e002024-09-13 20:06:5729class Cursor : public blink::mojom::IDBCursor {
[email protected]72a4183d2013-05-31 18:33:1030 public:
Evan Stadeb568dbdc2023-08-10 05:21:2431 // Creates a new self-owned instance and binds to `pending_remote`.
Evan Stadecbb1e002024-09-13 20:06:5732 static Cursor* CreateAndBind(
Evan Staded9529ea52025-04-11 17:02:5033 std::unique_ptr<BackingStore::Cursor> cursor,
Evan Stadeb568dbdc2023-08-10 05:21:2434 indexed_db::CursorType cursor_type,
35 blink::mojom::IDBTaskType task_type,
Evan Stadecbb1e002024-09-13 20:06:5736 base::WeakPtr<Transaction> transaction,
Evan Stadeb568dbdc2023-08-10 05:21:2437 mojo::PendingAssociatedRemote<blink::mojom::IDBCursor>& pending_remote);
38
Evan Stadecbb1e002024-09-13 20:06:5739 ~Cursor() override;
Peter Boström828b9022021-09-21 02:28:4340
Evan Stadecbb1e002024-09-13 20:06:5741 Cursor(const Cursor&) = delete;
42 Cursor& operator=(const Cursor&) = delete;
Peter Boström828b9022021-09-21 02:28:4343
Evan Stadeb568dbdc2023-08-10 05:21:2444 // blink::mojom::IDBCursor implementation
Chase Phillips9a58b892019-01-11 22:04:2745 void Advance(uint32_t count,
Evan Stadeb568dbdc2023-08-10 05:21:2446 blink::mojom::IDBCursor::AdvanceCallback callback) override;
Evan Stadeca999b12025-05-09 19:09:1147 void Continue(blink::IndexedDBKey key,
48 blink::IndexedDBKey primary_key,
Evan Stadeb568dbdc2023-08-10 05:21:2449 blink::mojom::IDBCursor::ContinueCallback callback) override;
50 void Prefetch(int32_t count,
51 blink::mojom::IDBCursor::PrefetchCallback callback) override;
52 void PrefetchReset(int32_t used_prefetches) override;
Chase Phillips4674aa342019-03-05 01:55:1753
Evan Staded9529ea52025-04-11 17:02:5054 const blink::IndexedDBKey& key() const { return cursor_->GetKey(); }
Chase Phillips68ecf5512018-08-16 01:59:1355 const blink::IndexedDBKey& primary_key() const {
Evan Staded9529ea52025-04-11 17:02:5056 return cursor_->GetPrimaryKey();
Chase Phillips68ecf5512018-08-16 01:59:1357 }
[email protected]120875f2014-03-19 23:08:5258 IndexedDBValue* Value() const {
Evan Stade6265dcd2024-02-27 20:50:0459 return (cursor_type_ == indexed_db::CursorType::kKeyOnly)
60 ? nullptr
Evan Staded9529ea52025-04-11 17:02:5061 : &cursor_->GetValue();
[email protected]907a8bc52013-06-07 16:32:3462 }
dmurph9d00e05d2016-12-01 23:00:3463
[email protected]907a8bc52013-06-07 16:32:3464 void Close();
65
Evan Stadeb568dbdc2023-08-10 05:21:2466 private:
Evan Staded9529ea52025-04-11 17:02:5067 Cursor(std::unique_ptr<BackingStore::Cursor> cursor,
Evan Stadecbb1e002024-09-13 20:06:5768 indexed_db::CursorType cursor_type,
69 blink::mojom::IDBTaskType task_type,
70 base::WeakPtr<Transaction> transaction);
Evan Stadeb568dbdc2023-08-10 05:21:2471
Evan Stadeca999b12025-05-09 19:09:1172 Status ContinueOperation(blink::IndexedDBKey key,
73 blink::IndexedDBKey primary_key,
Mike Wasserman0d5da522024-09-27 07:47:0374 blink::mojom::IDBCursor::ContinueCallback callback,
75 Transaction* transaction);
76 Status AdvanceOperation(uint32_t count,
77 blink::mojom::IDBCursor::AdvanceCallback callback,
78 Transaction* transaction);
79 Status PrefetchIterationOperation(
[email protected]65880a82013-08-16 21:30:0880 int number_to_fetch,
Chase Phillipsf74e5172019-03-13 20:44:4381 blink::mojom::IDBCursor::PrefetchCallback callback,
Evan Stadecbb1e002024-09-13 20:06:5782 Transaction* transaction);
[email protected]65880a82013-08-16 21:30:0883
Ari Chivukula664bf8d2022-04-27 23:28:1884 const storage::BucketLocator bucket_locator_;
Chase Phillipsb2851f322018-11-16 00:27:4885 blink::mojom::IDBTaskType task_type_;
[email protected]907a8bc52013-06-07 16:32:3486 indexed_db::CursorType cursor_type_;
dmurph9d00e05d2016-12-01 23:00:3487
88 // We rely on the transaction calling Close() to clear this.
Evan Stadecbb1e002024-09-13 20:06:5789 base::WeakPtr<Transaction> transaction_;
[email protected]907a8bc52013-06-07 16:32:3490
91 // Must be destroyed before transaction_.
Evan Staded9529ea52025-04-11 17:02:5092 std::unique_ptr<BackingStore::Cursor> cursor_;
Abhishek Shanthkumard9bed3af2025-07-01 08:37:0393
94 // Normally, `cursor_` is immediately destroyed when it reaches the end of its
95 // range since it cannot be used any more. But if this happens during a
96 // prefetch operation, it can be reset to its last-saved position, making it
97 // usable again. Hence, use a flag to keep track of this only during prefetch.
98 bool reached_end_during_prefetch_ = false;
[email protected]907a8bc52013-06-07 16:32:3499
Evan Stadeb568dbdc2023-08-10 05:21:24100 bool closed_ = false;
Chase Phillips4674aa342019-03-05 01:55:17101
Evan Stadeb568dbdc2023-08-10 05:21:24102 mojo::AssociatedReceiver<blink::mojom::IDBCursor> receiver_{this};
[email protected]e8ca0f52014-06-07 08:46:34103
Evan Stadecbb1e002024-09-13 20:06:57104 base::WeakPtrFactory<Cursor> ptr_factory_{this};
[email protected]72a4183d2013-05-31 18:33:10105};
106
Evan Stadecbb1e002024-09-13 20:06:57107} // namespace content::indexed_db
[email protected]72a4183d2013-05-31 18:33:10108
Evan Stade1a8d9d42024-09-10 19:37:19109#endif // CONTENT_BROWSER_INDEXED_DB_INSTANCE_CURSOR_H_