Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Evan Stade | 1a8d9d4 | 2024-09-10 19:37:19 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_INDEXED_DB_INSTANCE_CURSOR_H_ |
| 6 | #define CONTENT_BROWSER_INDEXED_DB_INSTANCE_CURSOR_H_ |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 7 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | 531cca9 | 2016-04-09 03:03:23 | [diff] [blame] | 10 | #include <memory> |
[email protected] | 907a8bc5 | 2013-06-07 16:32:34 | [diff] [blame] | 11 | |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
Evan Stade | d9529ea5 | 2025-04-11 17:02:50 | [diff] [blame] | 14 | #include "content/browser/indexed_db/instance/backing_store.h" |
Evan Stade | 1a8d9d4 | 2024-09-10 19:37:19 | [diff] [blame] | 15 | #include "content/browser/indexed_db/instance/database.h" |
| 16 | #include "content/browser/indexed_db/instance/transaction.h" |
Evan Stade | 6584a49 | 2023-09-25 21:02:30 | [diff] [blame] | 17 | #include "mojo/public/cpp/bindings/associated_receiver.h" |
| 18 | #include "mojo/public/cpp/bindings/pending_associated_remote.h" |
Henrique Ferreiro | da0a55c | 2019-11-12 14:06:04 | [diff] [blame] | 19 | #include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-forward.h" |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 20 | |
Ari Chivukula | 664bf8d | 2022-04-27 23:28:18 | [diff] [blame] | 21 | namespace storage { |
| 22 | struct BucketLocator; |
| 23 | } // namespace storage |
| 24 | |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 25 | namespace content::indexed_db { |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 26 | |
Evan Stade | 6265dcd | 2024-02-27 20:50:04 | [diff] [blame] | 27 | enum class CursorType { kKeyAndValue = 0, kKeyOnly = 1 }; |
Evan Stade | 6265dcd | 2024-02-27 20:50:04 | [diff] [blame] | 28 | |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 29 | class Cursor : public blink::mojom::IDBCursor { |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 30 | public: |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 31 | // Creates a new self-owned instance and binds to `pending_remote`. |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 32 | static Cursor* CreateAndBind( |
Evan Stade | d9529ea5 | 2025-04-11 17:02:50 | [diff] [blame] | 33 | std::unique_ptr<BackingStore::Cursor> cursor, |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 34 | indexed_db::CursorType cursor_type, |
| 35 | blink::mojom::IDBTaskType task_type, |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 36 | base::WeakPtr<Transaction> transaction, |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 37 | mojo::PendingAssociatedRemote<blink::mojom::IDBCursor>& pending_remote); |
| 38 | |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 39 | ~Cursor() override; |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 40 | |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 41 | Cursor(const Cursor&) = delete; |
| 42 | Cursor& operator=(const Cursor&) = delete; |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 43 | |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 44 | // blink::mojom::IDBCursor implementation |
Chase Phillips | 9a58b89 | 2019-01-11 22:04:27 | [diff] [blame] | 45 | void Advance(uint32_t count, |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 46 | blink::mojom::IDBCursor::AdvanceCallback callback) override; |
Evan Stade | ca999b1 | 2025-05-09 19:09:11 | [diff] [blame] | 47 | void Continue(blink::IndexedDBKey key, |
| 48 | blink::IndexedDBKey primary_key, |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 49 | 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 Phillips | 4674aa34 | 2019-03-05 01:55:17 | [diff] [blame] | 53 | |
Evan Stade | d9529ea5 | 2025-04-11 17:02:50 | [diff] [blame] | 54 | const blink::IndexedDBKey& key() const { return cursor_->GetKey(); } |
Chase Phillips | 68ecf551 | 2018-08-16 01:59:13 | [diff] [blame] | 55 | const blink::IndexedDBKey& primary_key() const { |
Evan Stade | d9529ea5 | 2025-04-11 17:02:50 | [diff] [blame] | 56 | return cursor_->GetPrimaryKey(); |
Chase Phillips | 68ecf551 | 2018-08-16 01:59:13 | [diff] [blame] | 57 | } |
[email protected] | 120875f | 2014-03-19 23:08:52 | [diff] [blame] | 58 | IndexedDBValue* Value() const { |
Evan Stade | 6265dcd | 2024-02-27 20:50:04 | [diff] [blame] | 59 | return (cursor_type_ == indexed_db::CursorType::kKeyOnly) |
| 60 | ? nullptr |
Evan Stade | d9529ea5 | 2025-04-11 17:02:50 | [diff] [blame] | 61 | : &cursor_->GetValue(); |
[email protected] | 907a8bc5 | 2013-06-07 16:32:34 | [diff] [blame] | 62 | } |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 63 | |
[email protected] | 907a8bc5 | 2013-06-07 16:32:34 | [diff] [blame] | 64 | void Close(); |
| 65 | |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 66 | private: |
Evan Stade | d9529ea5 | 2025-04-11 17:02:50 | [diff] [blame] | 67 | Cursor(std::unique_ptr<BackingStore::Cursor> cursor, |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 68 | indexed_db::CursorType cursor_type, |
| 69 | blink::mojom::IDBTaskType task_type, |
| 70 | base::WeakPtr<Transaction> transaction); |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 71 | |
Evan Stade | ca999b1 | 2025-05-09 19:09:11 | [diff] [blame] | 72 | Status ContinueOperation(blink::IndexedDBKey key, |
| 73 | blink::IndexedDBKey primary_key, |
Mike Wasserman | 0d5da52 | 2024-09-27 07:47:03 | [diff] [blame] | 74 | 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] | 65880a8 | 2013-08-16 21:30:08 | [diff] [blame] | 80 | int number_to_fetch, |
Chase Phillips | f74e517 | 2019-03-13 20:44:43 | [diff] [blame] | 81 | blink::mojom::IDBCursor::PrefetchCallback callback, |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 82 | Transaction* transaction); |
[email protected] | 65880a8 | 2013-08-16 21:30:08 | [diff] [blame] | 83 | |
Ari Chivukula | 664bf8d | 2022-04-27 23:28:18 | [diff] [blame] | 84 | const storage::BucketLocator bucket_locator_; |
Chase Phillips | b2851f32 | 2018-11-16 00:27:48 | [diff] [blame] | 85 | blink::mojom::IDBTaskType task_type_; |
[email protected] | 907a8bc5 | 2013-06-07 16:32:34 | [diff] [blame] | 86 | indexed_db::CursorType cursor_type_; |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 87 | |
| 88 | // We rely on the transaction calling Close() to clear this. |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 89 | base::WeakPtr<Transaction> transaction_; |
[email protected] | 907a8bc5 | 2013-06-07 16:32:34 | [diff] [blame] | 90 | |
| 91 | // Must be destroyed before transaction_. |
Evan Stade | d9529ea5 | 2025-04-11 17:02:50 | [diff] [blame] | 92 | std::unique_ptr<BackingStore::Cursor> cursor_; |
Abhishek Shanthkumar | d9bed3af | 2025-07-01 08:37:03 | [diff] [blame] | 93 | |
| 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] | 907a8bc5 | 2013-06-07 16:32:34 | [diff] [blame] | 99 | |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 100 | bool closed_ = false; |
Chase Phillips | 4674aa34 | 2019-03-05 01:55:17 | [diff] [blame] | 101 | |
Evan Stade | b568dbdc | 2023-08-10 05:21:24 | [diff] [blame] | 102 | mojo::AssociatedReceiver<blink::mojom::IDBCursor> receiver_{this}; |
[email protected] | e8ca0f5 | 2014-06-07 08:46:34 | [diff] [blame] | 103 | |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 104 | base::WeakPtrFactory<Cursor> ptr_factory_{this}; |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 105 | }; |
| 106 | |
Evan Stade | cbb1e00 | 2024-09-13 20:06:57 | [diff] [blame] | 107 | } // namespace content::indexed_db |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 108 | |
Evan Stade | 1a8d9d4 | 2024-09-10 19:37:19 | [diff] [blame] | 109 | #endif // CONTENT_BROWSER_INDEXED_DB_INSTANCE_CURSOR_H_ |