1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_idbcursortype_h__
8 #define mozilla_dom_idbcursortype_h__
10 #include "IndexedDatabase.h"
11 #include "mozilla/dom/indexedDB/Key.h"
13 namespace mozilla::dom
{
15 class ObjectStoreCursorResponse
;
16 class ObjectStoreKeyCursorResponse
;
17 class IndexCursorResponse
;
18 class IndexKeyCursorResponse
;
19 } // namespace indexedDB
21 enum struct IDBCursorType
{
28 template <IDBCursorType CursorType
>
29 struct CursorTypeTraits
;
35 class IDBIndexKeyCursor
;
36 class IDBObjectStoreCursor
;
37 class IDBObjectStoreKeyCursor
;
40 struct CursorTypeTraits
<IDBCursorType::Index
> {
41 using Type
= IDBIndexCursor
;
42 using ResponseType
= indexedDB::IndexCursorResponse
;
43 static constexpr bool IsObjectStoreCursor
= false;
44 static constexpr bool IsKeyOnlyCursor
= false;
48 struct CursorTypeTraits
<IDBCursorType::IndexKey
> {
49 using Type
= IDBIndexKeyCursor
;
50 using ResponseType
= indexedDB::IndexKeyCursorResponse
;
51 static constexpr bool IsObjectStoreCursor
= false;
52 static constexpr bool IsKeyOnlyCursor
= true;
56 struct CursorTypeTraits
<IDBCursorType::ObjectStore
> {
57 using Type
= IDBObjectStoreCursor
;
58 using ResponseType
= indexedDB::ObjectStoreCursorResponse
;
59 static constexpr bool IsObjectStoreCursor
= true;
60 static constexpr bool IsKeyOnlyCursor
= false;
64 struct CursorTypeTraits
<IDBCursorType::ObjectStoreKey
> {
65 using Type
= IDBObjectStoreKeyCursor
;
66 using ResponseType
= indexedDB::ObjectStoreKeyCursorResponse
;
67 static constexpr bool IsObjectStoreCursor
= true;
68 static constexpr bool IsKeyOnlyCursor
= true;
71 template <IDBCursorType CursorType
>
72 using CursorSourceType
=
73 std::conditional_t
<CursorTypeTraits
<CursorType
>::IsObjectStoreCursor
,
74 IDBObjectStore
, IDBIndex
>;
76 using Key
= indexedDB::Key
;
77 using StructuredCloneReadInfoChild
= indexedDB::StructuredCloneReadInfoChild
;
79 struct CommonCursorDataBase
{
80 CommonCursorDataBase() = delete;
82 explicit CommonCursorDataBase(Key aKey
);
84 Key mKey
; ///< The current key, i.e. the key representing the cursor's
86 ///< (https://w3c.github.io/IndexedDB/#cursor-position).
89 template <IDBCursorType CursorType
>
92 struct ObjectStoreCursorDataBase
: CommonCursorDataBase
{
93 using CommonCursorDataBase::CommonCursorDataBase
;
95 const Key
& GetSortKey(const bool aIsLocaleAware
) const {
96 MOZ_ASSERT(!aIsLocaleAware
);
97 return GetObjectStoreKey();
99 const Key
& GetObjectStoreKey() const { return mKey
; }
100 static constexpr const char* GetObjectStoreKeyForLogging() { return "NA"; }
103 struct IndexCursorDataBase
: CommonCursorDataBase
{
104 IndexCursorDataBase(Key aKey
, Key aLocaleAwareKey
, Key aObjectStoreKey
);
106 const Key
& GetSortKey(const bool aIsLocaleAware
) const {
107 return aIsLocaleAware
? mLocaleAwareKey
: mKey
;
109 const Key
& GetObjectStoreKey() const { return mObjectStoreKey
; }
110 const char* GetObjectStoreKeyForLogging() const {
111 return GetObjectStoreKey().GetBuffer().get();
114 Key mLocaleAwareKey
; ///< If the index's mLocale is set, this is mKey
115 ///< converted to mLocale. Otherwise, it is unset.
116 Key mObjectStoreKey
; ///< The key representing the cursor's object store
118 ///< (https://w3c.github.io/IndexedDB/#cursor-object-store-position).
121 struct ValueCursorDataBase
{
122 explicit ValueCursorDataBase(StructuredCloneReadInfoChild
&& aCloneInfo
);
124 StructuredCloneReadInfoChild mCloneInfo
;
128 struct CursorData
<IDBCursorType::ObjectStoreKey
> : ObjectStoreCursorDataBase
{
129 using ObjectStoreCursorDataBase::ObjectStoreCursorDataBase
;
133 struct CursorData
<IDBCursorType::ObjectStore
> : ObjectStoreCursorDataBase
,
134 ValueCursorDataBase
{
135 CursorData(Key aKey
, StructuredCloneReadInfoChild
&& aCloneInfo
);
139 struct CursorData
<IDBCursorType::IndexKey
> : IndexCursorDataBase
{
140 using IndexCursorDataBase::IndexCursorDataBase
;
144 struct CursorData
<IDBCursorType::Index
> : IndexCursorDataBase
,
145 ValueCursorDataBase
{
146 CursorData(Key aKey
, Key aLocaleAwareKey
, Key aObjectStoreKey
,
147 StructuredCloneReadInfoChild
&& aCloneInfo
);
150 } // namespace mozilla::dom