tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

IndexedDatabase.h (8254B)


      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/. */
      6 
      7 #ifndef mozilla_dom_indexeddatabase_h__
      8 #define mozilla_dom_indexeddatabase_h__
      9 
     10 #include "DatabaseFileInfoFwd.h"
     11 #include "SafeRefPtr.h"
     12 #include "js/StructuredClone.h"
     13 #include "mozilla/InitializedOnce.h"
     14 #include "mozilla/Variant.h"
     15 #include "nsCOMPtr.h"
     16 #include "nsTArray.h"
     17 
     18 namespace mozilla::dom {
     19 
     20 class Blob;
     21 class IDBDatabase;
     22 
     23 namespace indexedDB {
     24 
     25 struct StructuredCloneFileBase {
     26  enum FileType {
     27    eBlob,
     28    eMutableFile,
     29    eStructuredClone,
     30    eWasmBytecode,
     31    eWasmCompiled,
     32    eEndGuard
     33  };
     34 
     35  FileType Type() const { return mType; }
     36 
     37 protected:
     38  explicit StructuredCloneFileBase(FileType aType) : mType{aType} {}
     39 
     40  FileType mType;
     41 };
     42 
     43 struct StructuredCloneFileChild : StructuredCloneFileBase {
     44  StructuredCloneFileChild(const StructuredCloneFileChild&) = delete;
     45  StructuredCloneFileChild& operator=(const StructuredCloneFileChild&) = delete;
     46 #ifdef NS_BUILD_REFCNT_LOGGING
     47  // In IndexedDatabaseInlines.h
     48  StructuredCloneFileChild(StructuredCloneFileChild&&);
     49 #else
     50  StructuredCloneFileChild(StructuredCloneFileChild&&) = default;
     51 #endif
     52  StructuredCloneFileChild& operator=(StructuredCloneFileChild&&) = delete;
     53 
     54  // In IndexedDatabaseInlines.h
     55  ~StructuredCloneFileChild();
     56 
     57  // In IndexedDatabaseInlines.h
     58  explicit StructuredCloneFileChild(FileType aType);
     59 
     60  // In IndexedDatabaseInlines.h
     61  StructuredCloneFileChild(FileType aType, RefPtr<Blob> aBlob);
     62 
     63  const dom::Blob& Blob() const { return *mContents->as<RefPtr<dom::Blob>>(); }
     64 
     65  // XXX This is currently used for a number of reasons. Bug 1620560 will remove
     66  // the need for one of them, but the uses of do_GetWeakReference in
     67  // IDBDatabase::GetOrCreateFileActorForBlob and WrapAsJSObject in
     68  // CopyingStructuredCloneReadCallback are probably harder to change.
     69  dom::Blob& MutableBlob() const { return *mContents->as<RefPtr<dom::Blob>>(); }
     70 
     71  // In IndexedDatabaseInlines.h
     72  RefPtr<dom::Blob> BlobPtr() const;
     73 
     74  bool HasBlob() const { return mContents->is<RefPtr<dom::Blob>>(); }
     75 
     76 private:
     77  InitializedOnce<const Variant<Nothing, RefPtr<dom::Blob>>> mContents;
     78 };
     79 
     80 struct StructuredCloneFileParent : StructuredCloneFileBase {
     81  StructuredCloneFileParent(const StructuredCloneFileParent&) = delete;
     82  StructuredCloneFileParent& operator=(const StructuredCloneFileParent&) =
     83      delete;
     84 #ifdef NS_BUILD_REFCNT_LOGGING
     85  // In IndexedDatabaseInlines.h
     86  StructuredCloneFileParent(StructuredCloneFileParent&&);
     87 #else
     88  StructuredCloneFileParent(StructuredCloneFileParent&&) = default;
     89 #endif
     90  StructuredCloneFileParent& operator=(StructuredCloneFileParent&&) = delete;
     91 
     92  // In IndexedDatabaseInlines.h
     93  StructuredCloneFileParent(FileType aType,
     94                            SafeRefPtr<DatabaseFileInfo> aFileInfo);
     95 
     96  // In IndexedDatabaseInlines.h
     97  ~StructuredCloneFileParent();
     98 
     99  // XXX This is used for a schema upgrade hack in UpgradeSchemaFrom19_0To20_0.
    100  // When this is eventually removed, this function can be removed, and mType
    101  // can be declared const in the base class.
    102  void MutateType(FileType aNewType) { mType = aNewType; }
    103 
    104  const DatabaseFileInfo& FileInfo() const { return ***mContents; }
    105 
    106  // In IndexedDatabaseInlines.h
    107  SafeRefPtr<DatabaseFileInfo> FileInfoPtr() const;
    108 
    109 private:
    110  InitializedOnce<const Maybe<SafeRefPtr<DatabaseFileInfo>>> mContents;
    111 };
    112 
    113 struct StructuredCloneReadInfoBase {
    114  // In IndexedDatabaseInlines.h
    115  explicit StructuredCloneReadInfoBase(JSStructuredCloneData&& aData)
    116      : mData{std::move(aData)} {}
    117 
    118  const JSStructuredCloneData& Data() const { return mData; }
    119  JSStructuredCloneData ReleaseData() { return std::move(mData); }
    120 
    121 private:
    122  JSStructuredCloneData mData;
    123 };
    124 
    125 template <typename StructuredCloneFileT>
    126 struct StructuredCloneReadInfo : StructuredCloneReadInfoBase {
    127  using StructuredCloneFile = StructuredCloneFileT;
    128 
    129  // In IndexedDatabaseInlines.h
    130  explicit StructuredCloneReadInfo(JS::StructuredCloneScope aScope);
    131 
    132  // In IndexedDatabaseInlines.h
    133  StructuredCloneReadInfo();
    134 
    135  // In IndexedDatabaseInlines.h
    136  StructuredCloneReadInfo(JSStructuredCloneData&& aData,
    137                          nsTArray<StructuredCloneFile> aFiles);
    138 
    139 #ifdef NS_BUILD_REFCNT_LOGGING
    140  // In IndexedDatabaseInlines.h
    141  ~StructuredCloneReadInfo();
    142 
    143  // In IndexedDatabaseInlines.h
    144  //
    145  // This custom implementation of the move ctor is only necessary because of
    146  // MOZ_COUNT_CTOR. It is less efficient as the compiler-generated move ctor,
    147  // since it unnecessarily clears elements on the source.
    148  StructuredCloneReadInfo(StructuredCloneReadInfo&& aOther) noexcept;
    149 #else
    150  StructuredCloneReadInfo(StructuredCloneReadInfo&& aOther) = default;
    151 #endif
    152  StructuredCloneReadInfo& operator=(StructuredCloneReadInfo&& aOther) =
    153      default;
    154 
    155  StructuredCloneReadInfo(const StructuredCloneReadInfo& aOther) = delete;
    156  StructuredCloneReadInfo& operator=(const StructuredCloneReadInfo& aOther) =
    157      delete;
    158 
    159  // Returns the estimated size used for IPC message size calculations, not the
    160  // actual size of the data. This estimation accounts for serialization
    161  // behavior of large data.
    162  // Defined in IndexedDatabaseInlines.h
    163  size_t Size() const;
    164 
    165  // XXX This is only needed for a schema upgrade (UpgradeSchemaFrom19_0To20_0).
    166  // If support for older schemas is dropped, we can probably remove this method
    167  // and make mFiles InitializedOnce.
    168  StructuredCloneFile& MutableFile(const size_t aIndex) {
    169    return mFiles[aIndex];
    170  }
    171  const nsTArray<StructuredCloneFile>& Files() const { return mFiles; }
    172 
    173  nsTArray<StructuredCloneFile> ReleaseFiles() { return std::move(mFiles); }
    174 
    175  bool HasFiles() const { return !mFiles.IsEmpty(); }
    176 
    177 private:
    178  nsTArray<StructuredCloneFile> mFiles;
    179 };
    180 
    181 struct StructuredCloneReadInfoChild
    182    : StructuredCloneReadInfo<StructuredCloneFileChild> {
    183  inline StructuredCloneReadInfoChild(JSStructuredCloneData&& aData,
    184                                      nsTArray<StructuredCloneFileChild> aFiles,
    185                                      IDBDatabase* aDatabase);
    186 
    187  IDBDatabase* Database() const { return mDatabase; }
    188 
    189 private:
    190  IDBDatabase* mDatabase;
    191 };
    192 
    193 // This is only defined in the header file to satisfy the clang-plugin static
    194 // analysis, it could be placed in ActorsParent.cpp otherwise.
    195 struct StructuredCloneReadInfoParent
    196    : StructuredCloneReadInfo<StructuredCloneFileParent> {
    197  StructuredCloneReadInfoParent(JSStructuredCloneData&& aData,
    198                                nsTArray<StructuredCloneFileParent> aFiles,
    199                                bool aHasPreprocessInfo)
    200      : StructuredCloneReadInfo{std::move(aData), std::move(aFiles)},
    201        mHasPreprocessInfo{aHasPreprocessInfo} {}
    202 
    203  bool HasPreprocessInfo() const { return mHasPreprocessInfo; }
    204 
    205 private:
    206  bool mHasPreprocessInfo;
    207 };
    208 
    209 template <typename StructuredCloneReadInfo>
    210 JSObject* CommonStructuredCloneReadCallback(
    211    JSContext* aCx, JSStructuredCloneReader* aReader,
    212    const JS::CloneDataPolicy& aCloneDataPolicy, uint32_t aTag, uint32_t aData,
    213    StructuredCloneReadInfo* aCloneReadInfo, IDBDatabase* aDatabase);
    214 
    215 template <typename StructuredCloneReadInfoType>
    216 JSObject* StructuredCloneReadCallback(
    217    JSContext* aCx, JSStructuredCloneReader* aReader,
    218    const JS::CloneDataPolicy& aCloneDataPolicy, uint32_t aTag, uint32_t aData,
    219    void* aClosure);
    220 
    221 }  // namespace indexedDB
    222 }  // namespace mozilla::dom
    223 
    224 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
    225    mozilla::dom::indexedDB::StructuredCloneReadInfo<
    226        mozilla::dom::indexedDB::StructuredCloneFileChild>);
    227 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
    228    mozilla::dom::indexedDB::StructuredCloneReadInfo<
    229        mozilla::dom::indexedDB::StructuredCloneFileParent>);
    230 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
    231    mozilla::dom::indexedDB::StructuredCloneReadInfoChild);
    232 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
    233    mozilla::dom::indexedDB::StructuredCloneReadInfoParent);
    234 
    235 #endif  // mozilla_dom_indexeddatabase_h__