summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/IDBCursorType.h
blob: 79ecd2881a545a2492b3c96d5914f610f210bd6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_idbcursortype_h__
#define mozilla_dom_idbcursortype_h__

#include "IndexedDatabase.h"
#include "mozilla/dom/indexedDB/Key.h"

namespace mozilla::dom {
namespace indexedDB {
class ObjectStoreCursorResponse;
class ObjectStoreKeyCursorResponse;
class IndexCursorResponse;
class IndexKeyCursorResponse;
}  // namespace indexedDB

enum struct IDBCursorType {
  ObjectStore,
  ObjectStoreKey,
  Index,
  IndexKey,
};

template <IDBCursorType CursorType>
struct CursorTypeTraits;

class IDBIndex;
class IDBObjectStore;

class IDBIndexCursor;
class IDBIndexKeyCursor;
class IDBObjectStoreCursor;
class IDBObjectStoreKeyCursor;

template <>
struct CursorTypeTraits<IDBCursorType::Index> {
  using Type = IDBIndexCursor;
  using ResponseType = indexedDB::IndexCursorResponse;
  static constexpr bool IsObjectStoreCursor = false;
  static constexpr bool IsKeyOnlyCursor = false;
};

template <>
struct CursorTypeTraits<IDBCursorType::IndexKey> {
  using Type = IDBIndexKeyCursor;
  using ResponseType = indexedDB::IndexKeyCursorResponse;
  static constexpr bool IsObjectStoreCursor = false;
  static constexpr bool IsKeyOnlyCursor = true;
};

template <>
struct CursorTypeTraits<IDBCursorType::ObjectStore> {
  using Type = IDBObjectStoreCursor;
  using ResponseType = indexedDB::ObjectStoreCursorResponse;
  static constexpr bool IsObjectStoreCursor = true;
  static constexpr bool IsKeyOnlyCursor = false;
};

template <>
struct CursorTypeTraits<IDBCursorType::ObjectStoreKey> {
  using Type = IDBObjectStoreKeyCursor;
  using ResponseType = indexedDB::ObjectStoreKeyCursorResponse;
  static constexpr bool IsObjectStoreCursor = true;
  static constexpr bool IsKeyOnlyCursor = true;
};

template <IDBCursorType CursorType>
using CursorSourceType =
    std::conditional_t<CursorTypeTraits<CursorType>::IsObjectStoreCursor,
                       IDBObjectStore, IDBIndex>;

using Key = indexedDB::Key;
using StructuredCloneReadInfoChild = indexedDB::StructuredCloneReadInfoChild;

struct CommonCursorDataBase {
  CommonCursorDataBase() = delete;

  explicit CommonCursorDataBase(Key aKey);

  Key mKey;  ///< The current key, i.e. the key representing the cursor's
             ///< position
             ///< (https://w3c.github.io/IndexedDB/#cursor-position).
};

template <IDBCursorType CursorType>
struct CursorData;

struct ObjectStoreCursorDataBase : CommonCursorDataBase {
  using CommonCursorDataBase::CommonCursorDataBase;

  const Key& GetSortKey(const bool aIsLocaleAware) const {
    MOZ_ASSERT(!aIsLocaleAware);
    return GetObjectStoreKey();
  }
  const Key& GetObjectStoreKey() const { return mKey; }
  static constexpr const char* GetObjectStoreKeyForLogging() { return "NA"; }
};

struct IndexCursorDataBase : CommonCursorDataBase {
  IndexCursorDataBase(Key aKey, Key aLocaleAwareKey, Key aObjectStoreKey);

  const Key& GetSortKey(const bool aIsLocaleAware) const {
    return aIsLocaleAware ? mLocaleAwareKey : mKey;
  }
  const Key& GetObjectStoreKey() const { return mObjectStoreKey; }
  const char* GetObjectStoreKeyForLogging() const {
    return GetObjectStoreKey().GetBuffer().get();
  }

  Key mLocaleAwareKey;  ///< If the index's mLocale is set, this is mKey
                        ///< converted to mLocale. Otherwise, it is unset.
  Key mObjectStoreKey;  ///< The key representing the cursor's object store
                        ///< position
  ///< (https://w3c.github.io/IndexedDB/#cursor-object-store-position).
};

struct ValueCursorDataBase {
  explicit ValueCursorDataBase(StructuredCloneReadInfoChild&& aCloneInfo);

  StructuredCloneReadInfoChild mCloneInfo;
};

template <>
struct CursorData<IDBCursorType::ObjectStoreKey> : ObjectStoreCursorDataBase {
  using ObjectStoreCursorDataBase::ObjectStoreCursorDataBase;
};

template <>
struct CursorData<IDBCursorType::ObjectStore> : ObjectStoreCursorDataBase,
                                                ValueCursorDataBase {
  CursorData(Key aKey, StructuredCloneReadInfoChild&& aCloneInfo);
};

template <>
struct CursorData<IDBCursorType::IndexKey> : IndexCursorDataBase {
  using IndexCursorDataBase::IndexCursorDataBase;
};

template <>
struct CursorData<IDBCursorType::Index> : IndexCursorDataBase,
                                          ValueCursorDataBase {
  CursorData(Key aKey, Key aLocaleAwareKey, Key aObjectStoreKey,
             StructuredCloneReadInfoChild&& aCloneInfo);
};

}  // namespace mozilla::dom

#endif