summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/IndexedDatabase.h
blob: 6fdf6415536d9c7c4ed9d369a34165dbb895b79a (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
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
/* -*- 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_indexeddatabase_h__
#define mozilla_dom_indexeddatabase_h__

#include "DatabaseFileInfoFwd.h"
#include "js/StructuredClone.h"
#include "mozilla/InitializedOnce.h"
#include "mozilla/Variant.h"
#include "nsCOMPtr.h"
#include "nsTArray.h"
#include "SafeRefPtr.h"

namespace mozilla::dom {

class Blob;
class IDBDatabase;

namespace indexedDB {

struct StructuredCloneFileBase {
  enum FileType {
    eBlob,
    eMutableFile,
    eStructuredClone,
    eWasmBytecode,
    eWasmCompiled,
    eEndGuard
  };

  FileType Type() const { return mType; }

 protected:
  explicit StructuredCloneFileBase(FileType aType) : mType{aType} {}

  FileType mType;
};

struct StructuredCloneFileChild : StructuredCloneFileBase {
  StructuredCloneFileChild(const StructuredCloneFileChild&) = delete;
  StructuredCloneFileChild& operator=(const StructuredCloneFileChild&) = delete;
#ifdef NS_BUILD_REFCNT_LOGGING
  // In IndexedDatabaseInlines.h
  StructuredCloneFileChild(StructuredCloneFileChild&&);
#else
  StructuredCloneFileChild(StructuredCloneFileChild&&) = default;
#endif
  StructuredCloneFileChild& operator=(StructuredCloneFileChild&&) = delete;

  // In IndexedDatabaseInlines.h
  ~StructuredCloneFileChild();

  // In IndexedDatabaseInlines.h
  explicit StructuredCloneFileChild(FileType aType);

  // In IndexedDatabaseInlines.h
  StructuredCloneFileChild(FileType aType, RefPtr<Blob> aBlob);

  const dom::Blob& Blob() const { return *mContents->as<RefPtr<dom::Blob>>(); }

  // XXX This is currently used for a number of reasons. Bug 1620560 will remove
  // the need for one of them, but the uses of do_GetWeakReference in
  // IDBDatabase::GetOrCreateFileActorForBlob and WrapAsJSObject in
  // CopyingStructuredCloneReadCallback are probably harder to change.
  dom::Blob& MutableBlob() const { return *mContents->as<RefPtr<dom::Blob>>(); }

  // In IndexedDatabaseInlines.h
  RefPtr<dom::Blob> BlobPtr() const;

  bool HasBlob() const { return mContents->is<RefPtr<dom::Blob>>(); }

 private:
  InitializedOnce<const Variant<Nothing, RefPtr<dom::Blob>>> mContents;
};

struct StructuredCloneFileParent : StructuredCloneFileBase {
  StructuredCloneFileParent(const StructuredCloneFileParent&) = delete;
  StructuredCloneFileParent& operator=(const StructuredCloneFileParent&) =
      delete;
#ifdef NS_BUILD_REFCNT_LOGGING
  // In IndexedDatabaseInlines.h
  StructuredCloneFileParent(StructuredCloneFileParent&&);
#else
  StructuredCloneFileParent(StructuredCloneFileParent&&) = default;
#endif
  StructuredCloneFileParent& operator=(StructuredCloneFileParent&&) = delete;

  // In IndexedDatabaseInlines.h
  StructuredCloneFileParent(FileType aType,
                            SafeRefPtr<DatabaseFileInfo> aFileInfo);

  // In IndexedDatabaseInlines.h
  ~StructuredCloneFileParent();

  // XXX This is used for a schema upgrade hack in UpgradeSchemaFrom19_0To20_0.
  // When this is eventually removed, this function can be removed, and mType
  // can be declared const in the base class.
  void MutateType(FileType aNewType) { mType = aNewType; }

  const DatabaseFileInfo& FileInfo() const { return ***mContents; }

  // In IndexedDatabaseInlines.h
  SafeRefPtr<DatabaseFileInfo> FileInfoPtr() const;

 private:
  InitializedOnce<const Maybe<SafeRefPtr<DatabaseFileInfo>>> mContents;
};

struct StructuredCloneReadInfoBase {
  // In IndexedDatabaseInlines.h
  explicit StructuredCloneReadInfoBase(JSStructuredCloneData&& aData)
      : mData{std::move(aData)} {}

  const JSStructuredCloneData& Data() const { return mData; }
  JSStructuredCloneData ReleaseData() { return std::move(mData); }

 private:
  JSStructuredCloneData mData;
};

template <typename StructuredCloneFileT>
struct StructuredCloneReadInfo : StructuredCloneReadInfoBase {
  using StructuredCloneFile = StructuredCloneFileT;

  // In IndexedDatabaseInlines.h
  explicit StructuredCloneReadInfo(JS::StructuredCloneScope aScope);

  // In IndexedDatabaseInlines.h
  StructuredCloneReadInfo();

  // In IndexedDatabaseInlines.h
  StructuredCloneReadInfo(JSStructuredCloneData&& aData,
                          nsTArray<StructuredCloneFile> aFiles);

#ifdef NS_BUILD_REFCNT_LOGGING
  // In IndexedDatabaseInlines.h
  ~StructuredCloneReadInfo();

  // In IndexedDatabaseInlines.h
  //
  // This custom implementation of the move ctor is only necessary because of
  // MOZ_COUNT_CTOR. It is less efficient as the compiler-generated move ctor,
  // since it unnecessarily clears elements on the source.
  StructuredCloneReadInfo(StructuredCloneReadInfo&& aOther) noexcept;
#else
  StructuredCloneReadInfo(StructuredCloneReadInfo&& aOther) = default;
#endif
  StructuredCloneReadInfo& operator=(StructuredCloneReadInfo&& aOther) =
      default;

  StructuredCloneReadInfo(const StructuredCloneReadInfo& aOther) = delete;
  StructuredCloneReadInfo& operator=(const StructuredCloneReadInfo& aOther) =
      delete;

  // In IndexedDatabaseInlines.h
  size_t Size() const;

  // XXX This is only needed for a schema upgrade (UpgradeSchemaFrom19_0To20_0).
  // If support for older schemas is dropped, we can probably remove this method
  // and make mFiles InitializedOnce.
  StructuredCloneFile& MutableFile(const size_t aIndex) {
    return mFiles[aIndex];
  }
  const nsTArray<StructuredCloneFile>& Files() const { return mFiles; }

  nsTArray<StructuredCloneFile> ReleaseFiles() { return std::move(mFiles); }

  bool HasFiles() const { return !mFiles.IsEmpty(); }

 private:
  nsTArray<StructuredCloneFile> mFiles;
};

struct StructuredCloneReadInfoChild
    : StructuredCloneReadInfo<StructuredCloneFileChild> {
  inline StructuredCloneReadInfoChild(JSStructuredCloneData&& aData,
                                      nsTArray<StructuredCloneFileChild> aFiles,
                                      IDBDatabase* aDatabase);

  IDBDatabase* Database() const { return mDatabase; }

 private:
  IDBDatabase* mDatabase;
};

// This is only defined in the header file to satisfy the clang-plugin static
// analysis, it could be placed in ActorsParent.cpp otherwise.
struct StructuredCloneReadInfoParent
    : StructuredCloneReadInfo<StructuredCloneFileParent> {
  StructuredCloneReadInfoParent(JSStructuredCloneData&& aData,
                                nsTArray<StructuredCloneFileParent> aFiles,
                                bool aHasPreprocessInfo)
      : StructuredCloneReadInfo{std::move(aData), std::move(aFiles)},
        mHasPreprocessInfo{aHasPreprocessInfo} {}

  bool HasPreprocessInfo() const { return mHasPreprocessInfo; }

 private:
  bool mHasPreprocessInfo;
};

template <typename StructuredCloneReadInfo>
JSObject* CommonStructuredCloneReadCallback(
    JSContext* aCx, JSStructuredCloneReader* aReader,
    const JS::CloneDataPolicy& aCloneDataPolicy, uint32_t aTag, uint32_t aData,
    StructuredCloneReadInfo* aCloneReadInfo, IDBDatabase* aDatabase);

template <typename StructuredCloneReadInfoType>
JSObject* StructuredCloneReadCallback(
    JSContext* aCx, JSStructuredCloneReader* aReader,
    const JS::CloneDataPolicy& aCloneDataPolicy, uint32_t aTag, uint32_t aData,
    void* aClosure);

}  // namespace indexedDB
}  // namespace mozilla::dom

MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
    mozilla::dom::indexedDB::StructuredCloneReadInfo<
        mozilla::dom::indexedDB::StructuredCloneFileChild>);
MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
    mozilla::dom::indexedDB::StructuredCloneReadInfo<
        mozilla::dom::indexedDB::StructuredCloneFileParent>);
MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
    mozilla::dom::indexedDB::StructuredCloneReadInfoChild);
MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
    mozilla::dom::indexedDB::StructuredCloneReadInfoParent);

#endif  // mozilla_dom_indexeddatabase_h__