DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (cdf352f02ac4)

VCS Links

Line Code
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
/* -*- 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_idbcursor_h__
#define mozilla_dom_idbcursor_h__

#include "IDBCursorType.h"
#include "IndexedDatabase.h"
#include "js/RootingAPI.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/IDBCursorBinding.h"
#include "mozilla/dom/IDBTransaction.h"
#include "mozilla/dom/indexedDB/Key.h"
#include "mozilla/dom/quota/CheckedUnsafePtr.h"
#include "mozilla/InitializedOnce.h"
#include "nsCycleCollectionParticipant.h"
#include "nsWrapperCache.h"

class nsIGlobalObject;

namespace mozilla {

class ErrorResult;

namespace dom {

class IDBIndex;
class IDBObjectStore;
class IDBRequest;
class OwningIDBObjectStoreOrIDBIndex;

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

namespace indexedDB {
class BackgroundCursorChildBase;
template <IDBCursorType CursorType>
class BackgroundCursorChild;
}  // namespace indexedDB

class IDBCursor : public nsISupports, public nsWrapperCache {
 public:
  using Key = indexedDB::Key;
  using StructuredCloneReadInfoChild = indexedDB::StructuredCloneReadInfoChild;

  using Direction = IDBCursorDirection;
  using Type = IDBCursorType;

 protected:
  InitializedOnceNotNull<indexedDB::BackgroundCursorChildBase* const>
      mBackgroundActor;

  // TODO: mRequest could be made const if Bug 1575173 is resolved. It is
  // initialized in the constructor and never modified/cleared.
  RefPtr<IDBRequest> mRequest;

  // Sub-classes' mSource will hold this alive.
  const CheckedUnsafePtr<IDBTransaction> mTransaction;

 protected:
  // These are cycle-collected!
  JS::Heap<JS::Value> mCachedKey;
  JS::Heap<JS::Value> mCachedPrimaryKey;
  JS::Heap<JS::Value> mCachedValue;

  const Direction mDirection;

  bool mHaveCachedKey : 1;
  bool mHaveCachedPrimaryKey : 1;
  bool mHaveCachedValue : 1;
  bool mRooted : 1;
  bool mContinueCalled : 1;
  bool mHaveValue : 1;

 public:
  static MOZ_MUST_USE RefPtr<IDBObjectStoreCursor> Create(
      indexedDB::BackgroundCursorChild<Type::ObjectStore>* aBackgroundActor,
      Key aKey, StructuredCloneReadInfoChild&& aCloneInfo);

  static MOZ_MUST_USE RefPtr<IDBObjectStoreKeyCursor> Create(
      indexedDB::BackgroundCursorChild<Type::ObjectStoreKey>* aBackgroundActor,
      Key aKey);

  static MOZ_MUST_USE RefPtr<IDBIndexCursor> Create(
      indexedDB::BackgroundCursorChild<Type::Index>* aBackgroundActor, Key aKey,
      Key aSortKey, Key aPrimaryKey, StructuredCloneReadInfoChild&& aCloneInfo);

  static MOZ_MUST_USE RefPtr<IDBIndexKeyCursor> Create(
      indexedDB::BackgroundCursorChild<Type::IndexKey>* aBackgroundActor,
      Key aKey, Key aSortKey, Key aPrimaryKey);

  void AssertIsOnOwningThread() const
#ifdef DEBUG
      ;
#else
  {
  }
#endif

  nsIGlobalObject* GetParentObject() const;

  // XXX: The virtual methods that are used by the DOM binding could be removed
  // on the base class, if we provided a non-polymorphic wrapper instead, which
  // uses a custom dispatch to the actual implementation type. Don't know if
  // this is worth it.

  virtual void GetSource(OwningIDBObjectStoreOrIDBIndex& aSource) const = 0;

  IDBCursorDirection GetDirection() const;

  virtual void GetKey(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
                      ErrorResult& aRv) = 0;

  virtual void GetPrimaryKey(JSContext* aCx,
                             JS::MutableHandle<JS::Value> aResult,
                             ErrorResult& aRv) = 0;

  // XXX: We could move this to a sub-class, since this is only present on
  // IDBCursorWithValue.
  virtual void GetValue(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
                        ErrorResult& aRv) = 0;

  virtual void Continue(JSContext* aCx, JS::Handle<JS::Value> aKey,
                        ErrorResult& aRv) = 0;

  virtual void ContinuePrimaryKey(JSContext* aCx, JS::Handle<JS::Value> aKey,
                                  JS::Handle<JS::Value> aPrimaryKey,
                                  ErrorResult& aRv) = 0;

  virtual void Advance(uint32_t aCount, ErrorResult& aRv) = 0;

  virtual MOZ_MUST_USE RefPtr<IDBRequest> Update(JSContext* aCx,
                                                 JS::Handle<JS::Value> aValue,
                                                 ErrorResult& aRv) = 0;

  virtual MOZ_MUST_USE RefPtr<IDBRequest> Delete(JSContext* aCx,
                                                 ErrorResult& aRv) = 0;

  void ClearBackgroundActor() {
    AssertIsOnOwningThread();

    mBackgroundActor.destroy();
  }

  virtual void InvalidateCachedResponses() = 0;

  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBCursor)

 protected:
  IDBCursor(indexedDB::BackgroundCursorChildBase* aBackgroundActor);

  // TODO: Check if we can remove virtual by changing cycle collection.
  virtual ~IDBCursor() = default;

  void ResetBase();
};

template <IDBCursor::Type CursorType>
class IDBTypedCursor : public IDBCursor {
 public:
  template <typename... DataArgs>
  explicit IDBTypedCursor(
      indexedDB::BackgroundCursorChild<CursorType>* aBackgroundActor,
      DataArgs&&... aDataArgs);

  static constexpr Type GetType() { return CursorType; }

  // Checks if this is a locale aware cursor (ie. the index's sortKey is unset)
  bool IsLocaleAware() const;

  void GetSource(OwningIDBObjectStoreOrIDBIndex& aSource) const final;

  void GetKey(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
              ErrorResult& aRv) final;

  void GetPrimaryKey(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
                     ErrorResult& aRv) final;

  void GetValue(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
                ErrorResult& aRv) final;

  void Continue(JSContext* aCx, JS::Handle<JS::Value> aKey,
                ErrorResult& aRv) final;

  void ContinuePrimaryKey(JSContext* aCx, JS::Handle<JS::Value> aKey,
                          JS::Handle<JS::Value> aPrimaryKey,
                          ErrorResult& aRv) final;

  void Advance(uint32_t aCount, ErrorResult& aRv) final;

  MOZ_MUST_USE RefPtr<IDBRequest> Update(JSContext* aCx,
                                         JS::Handle<JS::Value> aValue,
                                         ErrorResult& aRv) final;

  MOZ_MUST_USE RefPtr<IDBRequest> Delete(JSContext* aCx,
                                         ErrorResult& aRv) final;

  // nsWrapperCache
  JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) final;

  void InvalidateCachedResponses() final;

  void Reset();

  void Reset(CursorData<CursorType>&& aCursorData);

 private:
  static constexpr bool IsObjectStoreCursor =
      CursorTypeTraits<CursorType>::IsObjectStoreCursor;
  static constexpr bool IsKeyOnlyCursor =
      CursorTypeTraits<CursorType>::IsKeyOnlyCursor;

  CursorSourceType<CursorType>& GetSourceRef() const {
    MOZ_ASSERT(mSource);
    return *mSource;
  }

  IDBObjectStore& GetSourceObjectStoreRef() const {
    if constexpr (IsObjectStoreCursor) {
      return GetSourceRef();
    } else {
      MOZ_ASSERT(!GetSourceRef().IsDeleted());

      auto res = GetSourceRef().ObjectStore();
      MOZ_ASSERT(res);
      return *res;
    }
  }

  indexedDB::BackgroundCursorChild<CursorType>& GetTypedBackgroundActorRef()
      const {
    // We can safely downcast to BackgroundCursorChild<CursorType>*, since we
    // initialized that in the constructor from that type. We just want to avoid
    // having a second typed field.
    return *static_cast<indexedDB::BackgroundCursorChild<CursorType>*>(
        *mBackgroundActor);
  }

  bool IsSourceDeleted() const;

 protected:
  virtual ~IDBTypedCursor() override;

  void DropJSObjects();

  CursorData<CursorType> mData;

  // TODO: mSource could be made const if Bug 1575173 is resolved. It is
  // initialized in the constructor and never modified/cleared.
  RefPtr<CursorSourceType<CursorType>> mSource;
};

// The subclasses defined by this macro are only needed to be able to use the
// cycle collector macros, which do not support templates. If spelled out, the
// cycle collection could be implemented directly on IDBTypedCursor, and these
// classes were not needed.
#define CONCRETE_IDBCURSOR_SUBCLASS(_subclassName, _cursorType)        \
  class _subclassName final : public IDBTypedCursor<_cursorType> {     \
   public:                                                             \
    NS_DECL_ISUPPORTS_INHERITED                                        \
    NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(_subclassName, IDBCursor) \
                                                                       \
    using IDBTypedCursor<_cursorType>::IDBTypedCursor;                 \
                                                                       \
   private:                                                            \
    ~_subclassName() final = default;                                  \
  };

CONCRETE_IDBCURSOR_SUBCLASS(IDBObjectStoreCursor, IDBCursor::Type::ObjectStore)
CONCRETE_IDBCURSOR_SUBCLASS(IDBObjectStoreKeyCursor,
                            IDBCursor::Type::ObjectStoreKey)
CONCRETE_IDBCURSOR_SUBCLASS(IDBIndexCursor, IDBCursor::Type::Index)
CONCRETE_IDBCURSOR_SUBCLASS(IDBIndexKeyCursor, IDBCursor::Type::IndexKey)

template <IDBCursor::Type CursorType>
using IDBCursorImpl = typename CursorTypeTraits<CursorType>::Type;

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_idbcursor_h__