Bug 1729395 - Handle message sender going away during message processing r=robwu
[gecko.git] / dom / indexedDB / IndexedDatabase.h
blob3bd1c846c16a2cb5f3c06d8045f3b125104822b3
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_indexeddatabase_h__
8 #define mozilla_dom_indexeddatabase_h__
10 #include "DatabaseFileInfoFwd.h"
11 #include "js/StructuredClone.h"
12 #include "mozilla/InitializedOnce.h"
13 #include "mozilla/Variant.h"
14 #include "nsCOMPtr.h"
15 #include "nsTArray.h"
16 #include "SafeRefPtr.h"
18 namespace mozilla {
19 namespace dom {
21 class Blob;
22 class IDBDatabase;
23 class IDBMutableFile;
25 namespace indexedDB {
27 struct StructuredCloneFileBase {
28 enum FileType {
29 eBlob,
30 eMutableFile,
31 eStructuredClone,
32 eWasmBytecode,
33 eWasmCompiled,
34 eEndGuard
37 FileType Type() const { return mType; }
39 protected:
40 explicit StructuredCloneFileBase(FileType aType) : mType{aType} {}
42 FileType mType;
45 struct StructuredCloneFileChild : StructuredCloneFileBase {
46 StructuredCloneFileChild(const StructuredCloneFileChild&) = delete;
47 StructuredCloneFileChild& operator=(const StructuredCloneFileChild&) = delete;
48 #ifdef NS_BUILD_REFCNT_LOGGING
49 // In IndexedDatabaseInlines.h
50 StructuredCloneFileChild(StructuredCloneFileChild&&);
51 #else
52 StructuredCloneFileChild(StructuredCloneFileChild&&) = default;
53 #endif
54 StructuredCloneFileChild& operator=(StructuredCloneFileChild&&) = delete;
56 // In IndexedDatabaseInlines.h
57 ~StructuredCloneFileChild();
59 // In IndexedDatabaseInlines.h
60 explicit StructuredCloneFileChild(FileType aType);
62 // In IndexedDatabaseInlines.h
63 StructuredCloneFileChild(FileType aType, RefPtr<Blob> aBlob);
65 // In IndexedDatabaseInlines.h
66 explicit StructuredCloneFileChild(RefPtr<IDBMutableFile> aMutableFile);
68 const dom::Blob& Blob() const { return *mContents->as<RefPtr<dom::Blob>>(); }
70 // XXX This is currently used for a number of reasons. Bug 1620560 will remove
71 // the need for one of them, but the uses of do_GetWeakReference in
72 // IDBDatabase::GetOrCreateFileActorForBlob and WrapAsJSObject in
73 // CopyingStructuredCloneReadCallback are probably harder to change.
74 dom::Blob& MutableBlob() const { return *mContents->as<RefPtr<dom::Blob>>(); }
76 // In IndexedDatabaseInlines.h
77 RefPtr<dom::Blob> BlobPtr() const;
79 bool HasBlob() const { return mContents->is<RefPtr<dom::Blob>>(); }
81 const IDBMutableFile& MutableFile() const {
82 return *mContents->as<RefPtr<IDBMutableFile>>();
85 IDBMutableFile& MutableMutableFile() const {
86 return *mContents->as<RefPtr<IDBMutableFile>>();
89 bool HasMutableFile() const {
90 return mContents->is<RefPtr<IDBMutableFile>>();
93 private:
94 InitializedOnce<
95 const Variant<Nothing, RefPtr<dom::Blob>, RefPtr<IDBMutableFile>>>
96 mContents;
99 struct StructuredCloneFileParent : StructuredCloneFileBase {
100 StructuredCloneFileParent(const StructuredCloneFileParent&) = delete;
101 StructuredCloneFileParent& operator=(const StructuredCloneFileParent&) =
102 delete;
103 #ifdef NS_BUILD_REFCNT_LOGGING
104 // In IndexedDatabaseInlines.h
105 StructuredCloneFileParent(StructuredCloneFileParent&&);
106 #else
107 StructuredCloneFileParent(StructuredCloneFileParent&&) = default;
108 #endif
109 StructuredCloneFileParent& operator=(StructuredCloneFileParent&&) = delete;
111 // In IndexedDatabaseInlines.h
112 StructuredCloneFileParent(FileType aType,
113 SafeRefPtr<DatabaseFileInfo> aFileInfo);
115 // In IndexedDatabaseInlines.h
116 ~StructuredCloneFileParent();
118 // XXX This is used for a schema upgrade hack in UpgradeSchemaFrom19_0To20_0.
119 // When this is eventually removed, this function can be removed, and mType
120 // can be declared const in the base class.
121 void MutateType(FileType aNewType) { mType = aNewType; }
123 const DatabaseFileInfo& FileInfo() const { return ***mContents; }
125 // In IndexedDatabaseInlines.h
126 SafeRefPtr<DatabaseFileInfo> FileInfoPtr() const;
128 private:
129 InitializedOnce<const Maybe<SafeRefPtr<DatabaseFileInfo>>> mContents;
132 struct StructuredCloneReadInfoBase {
133 // In IndexedDatabaseInlines.h
134 explicit StructuredCloneReadInfoBase(JSStructuredCloneData&& aData)
135 : mData{std::move(aData)} {}
137 const JSStructuredCloneData& Data() const { return mData; }
138 JSStructuredCloneData ReleaseData() { return std::move(mData); }
140 private:
141 JSStructuredCloneData mData;
144 template <typename StructuredCloneFileT>
145 struct StructuredCloneReadInfo : StructuredCloneReadInfoBase {
146 using StructuredCloneFile = StructuredCloneFileT;
148 // In IndexedDatabaseInlines.h
149 explicit StructuredCloneReadInfo(JS::StructuredCloneScope aScope);
151 // In IndexedDatabaseInlines.h
152 StructuredCloneReadInfo();
154 // In IndexedDatabaseInlines.h
155 StructuredCloneReadInfo(JSStructuredCloneData&& aData,
156 nsTArray<StructuredCloneFile> aFiles);
158 #ifdef NS_BUILD_REFCNT_LOGGING
159 // In IndexedDatabaseInlines.h
160 ~StructuredCloneReadInfo();
162 // In IndexedDatabaseInlines.h
164 // This custom implementation of the move ctor is only necessary because of
165 // MOZ_COUNT_CTOR. It is less efficient as the compiler-generated move ctor,
166 // since it unnecessarily clears elements on the source.
167 StructuredCloneReadInfo(StructuredCloneReadInfo&& aOther) noexcept;
168 #else
169 StructuredCloneReadInfo(StructuredCloneReadInfo&& aOther) = default;
170 #endif
171 StructuredCloneReadInfo& operator=(StructuredCloneReadInfo&& aOther) =
172 default;
174 StructuredCloneReadInfo(const StructuredCloneReadInfo& aOther) = delete;
175 StructuredCloneReadInfo& operator=(const StructuredCloneReadInfo& aOther) =
176 delete;
178 // In IndexedDatabaseInlines.h
179 size_t Size() const;
181 // XXX This is only needed for a schema upgrade (UpgradeSchemaFrom19_0To20_0).
182 // If support for older schemas is dropped, we can probably remove this method
183 // and make mFiles InitializedOnce.
184 StructuredCloneFile& MutableFile(const size_t aIndex) {
185 return mFiles[aIndex];
187 const nsTArray<StructuredCloneFile>& Files() const { return mFiles; }
189 nsTArray<StructuredCloneFile> ReleaseFiles() { return std::move(mFiles); }
191 bool HasFiles() const { return !mFiles.IsEmpty(); }
193 private:
194 nsTArray<StructuredCloneFile> mFiles;
197 struct StructuredCloneReadInfoChild
198 : StructuredCloneReadInfo<StructuredCloneFileChild> {
199 inline StructuredCloneReadInfoChild(JSStructuredCloneData&& aData,
200 nsTArray<StructuredCloneFileChild> aFiles,
201 IDBDatabase* aDatabase);
203 IDBDatabase* Database() const { return mDatabase; }
205 private:
206 IDBDatabase* mDatabase;
209 // This is only defined in the header file to satisfy the clang-plugin static
210 // analysis, it could be placed in ActorsParent.cpp otherwise.
211 struct StructuredCloneReadInfoParent
212 : StructuredCloneReadInfo<StructuredCloneFileParent> {
213 StructuredCloneReadInfoParent(JSStructuredCloneData&& aData,
214 nsTArray<StructuredCloneFileParent> aFiles,
215 bool aHasPreprocessInfo)
216 : StructuredCloneReadInfo{std::move(aData), std::move(aFiles)},
217 mHasPreprocessInfo{aHasPreprocessInfo} {}
219 bool HasPreprocessInfo() const { return mHasPreprocessInfo; }
221 private:
222 bool mHasPreprocessInfo;
225 template <typename StructuredCloneReadInfo>
226 JSObject* CommonStructuredCloneReadCallback(
227 JSContext* aCx, JSStructuredCloneReader* aReader,
228 const JS::CloneDataPolicy& aCloneDataPolicy, uint32_t aTag, uint32_t aData,
229 StructuredCloneReadInfo* aCloneReadInfo, IDBDatabase* aDatabase);
231 template <typename StructuredCloneReadInfoType>
232 JSObject* StructuredCloneReadCallback(
233 JSContext* aCx, JSStructuredCloneReader* aReader,
234 const JS::CloneDataPolicy& aCloneDataPolicy, uint32_t aTag, uint32_t aData,
235 void* aClosure);
237 } // namespace indexedDB
238 } // namespace dom
239 } // namespace mozilla
241 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
242 mozilla::dom::indexedDB::StructuredCloneReadInfo<
243 mozilla::dom::indexedDB::StructuredCloneFileChild>);
244 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
245 mozilla::dom::indexedDB::StructuredCloneReadInfo<
246 mozilla::dom::indexedDB::StructuredCloneFileParent>);
247 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
248 mozilla::dom::indexedDB::StructuredCloneReadInfoChild);
249 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
250 mozilla::dom::indexedDB::StructuredCloneReadInfoParent);
252 #endif // mozilla_dom_indexeddatabase_h__