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"
16 #include "SafeRefPtr.h"
18 namespace mozilla::dom
{
25 struct StructuredCloneFileBase
{
35 FileType
Type() const { return mType
; }
38 explicit StructuredCloneFileBase(FileType aType
) : mType
{aType
} {}
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
&&);
50 StructuredCloneFileChild(StructuredCloneFileChild
&&) = default;
52 StructuredCloneFileChild
& operator=(StructuredCloneFileChild
&&) = delete;
54 // In IndexedDatabaseInlines.h
55 ~StructuredCloneFileChild();
57 // In IndexedDatabaseInlines.h
58 explicit StructuredCloneFileChild(FileType aType
);
60 // In IndexedDatabaseInlines.h
61 StructuredCloneFileChild(FileType aType
, RefPtr
<Blob
> aBlob
);
63 const dom::Blob
& Blob() const { return *mContents
->as
<RefPtr
<dom::Blob
>>(); }
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
>>(); }
71 // In IndexedDatabaseInlines.h
72 RefPtr
<dom::Blob
> BlobPtr() const;
74 bool HasBlob() const { return mContents
->is
<RefPtr
<dom::Blob
>>(); }
77 InitializedOnce
<const Variant
<Nothing
, RefPtr
<dom::Blob
>>> mContents
;
80 struct StructuredCloneFileParent
: StructuredCloneFileBase
{
81 StructuredCloneFileParent(const StructuredCloneFileParent
&) = delete;
82 StructuredCloneFileParent
& operator=(const StructuredCloneFileParent
&) =
84 #ifdef NS_BUILD_REFCNT_LOGGING
85 // In IndexedDatabaseInlines.h
86 StructuredCloneFileParent(StructuredCloneFileParent
&&);
88 StructuredCloneFileParent(StructuredCloneFileParent
&&) = default;
90 StructuredCloneFileParent
& operator=(StructuredCloneFileParent
&&) = delete;
92 // In IndexedDatabaseInlines.h
93 StructuredCloneFileParent(FileType aType
,
94 SafeRefPtr
<DatabaseFileInfo
> aFileInfo
);
96 // In IndexedDatabaseInlines.h
97 ~StructuredCloneFileParent();
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
; }
104 const DatabaseFileInfo
& FileInfo() const { return ***mContents
; }
106 // In IndexedDatabaseInlines.h
107 SafeRefPtr
<DatabaseFileInfo
> FileInfoPtr() const;
110 InitializedOnce
<const Maybe
<SafeRefPtr
<DatabaseFileInfo
>>> mContents
;
113 struct StructuredCloneReadInfoBase
{
114 // In IndexedDatabaseInlines.h
115 explicit StructuredCloneReadInfoBase(JSStructuredCloneData
&& aData
)
116 : mData
{std::move(aData
)} {}
118 const JSStructuredCloneData
& Data() const { return mData
; }
119 JSStructuredCloneData
ReleaseData() { return std::move(mData
); }
122 JSStructuredCloneData mData
;
125 template <typename StructuredCloneFileT
>
126 struct StructuredCloneReadInfo
: StructuredCloneReadInfoBase
{
127 using StructuredCloneFile
= StructuredCloneFileT
;
129 // In IndexedDatabaseInlines.h
130 explicit StructuredCloneReadInfo(JS::StructuredCloneScope aScope
);
132 // In IndexedDatabaseInlines.h
133 StructuredCloneReadInfo();
135 // In IndexedDatabaseInlines.h
136 StructuredCloneReadInfo(JSStructuredCloneData
&& aData
,
137 nsTArray
<StructuredCloneFile
> aFiles
);
139 #ifdef NS_BUILD_REFCNT_LOGGING
140 // In IndexedDatabaseInlines.h
141 ~StructuredCloneReadInfo();
143 // In IndexedDatabaseInlines.h
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
;
150 StructuredCloneReadInfo(StructuredCloneReadInfo
&& aOther
) = default;
152 StructuredCloneReadInfo
& operator=(StructuredCloneReadInfo
&& aOther
) =
155 StructuredCloneReadInfo(const StructuredCloneReadInfo
& aOther
) = delete;
156 StructuredCloneReadInfo
& operator=(const StructuredCloneReadInfo
& aOther
) =
159 // In IndexedDatabaseInlines.h
162 // XXX This is only needed for a schema upgrade (UpgradeSchemaFrom19_0To20_0).
163 // If support for older schemas is dropped, we can probably remove this method
164 // and make mFiles InitializedOnce.
165 StructuredCloneFile
& MutableFile(const size_t aIndex
) {
166 return mFiles
[aIndex
];
168 const nsTArray
<StructuredCloneFile
>& Files() const { return mFiles
; }
170 nsTArray
<StructuredCloneFile
> ReleaseFiles() { return std::move(mFiles
); }
172 bool HasFiles() const { return !mFiles
.IsEmpty(); }
175 nsTArray
<StructuredCloneFile
> mFiles
;
178 struct StructuredCloneReadInfoChild
179 : StructuredCloneReadInfo
<StructuredCloneFileChild
> {
180 inline StructuredCloneReadInfoChild(JSStructuredCloneData
&& aData
,
181 nsTArray
<StructuredCloneFileChild
> aFiles
,
182 IDBDatabase
* aDatabase
);
184 IDBDatabase
* Database() const { return mDatabase
; }
187 IDBDatabase
* mDatabase
;
190 // This is only defined in the header file to satisfy the clang-plugin static
191 // analysis, it could be placed in ActorsParent.cpp otherwise.
192 struct StructuredCloneReadInfoParent
193 : StructuredCloneReadInfo
<StructuredCloneFileParent
> {
194 StructuredCloneReadInfoParent(JSStructuredCloneData
&& aData
,
195 nsTArray
<StructuredCloneFileParent
> aFiles
,
196 bool aHasPreprocessInfo
)
197 : StructuredCloneReadInfo
{std::move(aData
), std::move(aFiles
)},
198 mHasPreprocessInfo
{aHasPreprocessInfo
} {}
200 bool HasPreprocessInfo() const { return mHasPreprocessInfo
; }
203 bool mHasPreprocessInfo
;
206 template <typename StructuredCloneReadInfo
>
207 JSObject
* CommonStructuredCloneReadCallback(
208 JSContext
* aCx
, JSStructuredCloneReader
* aReader
,
209 const JS::CloneDataPolicy
& aCloneDataPolicy
, uint32_t aTag
, uint32_t aData
,
210 StructuredCloneReadInfo
* aCloneReadInfo
, IDBDatabase
* aDatabase
);
212 template <typename StructuredCloneReadInfoType
>
213 JSObject
* StructuredCloneReadCallback(
214 JSContext
* aCx
, JSStructuredCloneReader
* aReader
,
215 const JS::CloneDataPolicy
& aCloneDataPolicy
, uint32_t aTag
, uint32_t aData
,
218 } // namespace indexedDB
219 } // namespace mozilla::dom
221 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
222 mozilla::dom::indexedDB::StructuredCloneReadInfo
<
223 mozilla::dom::indexedDB::StructuredCloneFileChild
>);
224 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
225 mozilla::dom::indexedDB::StructuredCloneReadInfo
<
226 mozilla::dom::indexedDB::StructuredCloneFileParent
>);
227 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
228 mozilla::dom::indexedDB::StructuredCloneReadInfoChild
);
229 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(
230 mozilla::dom::indexedDB::StructuredCloneReadInfoParent
);
232 #endif // mozilla_dom_indexeddatabase_h__