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 IndexedDatabaseInlines_h
8 #define IndexedDatabaseInlines_h
10 #ifndef mozilla_dom_indexeddatabase_h__
11 # error Must include IndexedDatabase.h first
14 #include "DatabaseFileInfo.h"
15 #include "IDBMutableFile.h"
16 #include "mozilla/dom/ToJSValue.h"
17 #include "mozilla/dom/indexedDB/PBackgroundIDBSharedTypes.h"
18 #include "mozilla/dom/DOMStringList.h"
19 #include "mozilla/dom/File.h"
25 #ifdef NS_BUILD_REFCNT_LOGGING
26 inline StructuredCloneFileChild::StructuredCloneFileChild(
27 StructuredCloneFileChild
&& aOther
)
28 : StructuredCloneFileBase
{std::move(aOther
)},
29 mContents
{std::move(aOther
.mContents
)} {
30 MOZ_COUNT_CTOR(StructuredCloneFileChild
);
34 inline StructuredCloneFileChild::~StructuredCloneFileChild() {
35 MOZ_COUNT_DTOR(StructuredCloneFileChild
);
38 inline StructuredCloneFileChild::StructuredCloneFileChild(FileType aType
)
39 : StructuredCloneFileBase
{aType
}, mContents
{Nothing()} {
40 MOZ_COUNT_CTOR(StructuredCloneFileChild
);
43 inline StructuredCloneFileChild::StructuredCloneFileChild(
44 FileType aType
, RefPtr
<dom::Blob
> aBlob
)
45 : StructuredCloneFileBase
{aType
}, mContents
{std::move(aBlob
)} {
46 MOZ_ASSERT(eBlob
== aType
|| eStructuredClone
== aType
);
47 MOZ_ASSERT(mContents
->as
<RefPtr
<dom::Blob
>>());
48 MOZ_COUNT_CTOR(StructuredCloneFileChild
);
51 inline StructuredCloneFileParent::StructuredCloneFileParent(
52 FileType aType
, SafeRefPtr
<DatabaseFileInfo
> aFileInfo
)
53 : StructuredCloneFileBase
{aType
}, mContents
{Some(std::move(aFileInfo
))} {
54 MOZ_ASSERT(**mContents
);
55 MOZ_COUNT_CTOR(StructuredCloneFileParent
);
58 inline StructuredCloneFileChild::StructuredCloneFileChild(
59 RefPtr
<IDBMutableFile
> aMutableFile
)
60 : StructuredCloneFileBase
{eMutableFile
},
61 mContents
{std::move(aMutableFile
)} {
62 MOZ_ASSERT(mContents
->as
<RefPtr
<IDBMutableFile
>>());
63 MOZ_COUNT_CTOR(StructuredCloneFileChild
);
66 #ifdef NS_BUILD_REFCNT_LOGGING
67 inline StructuredCloneFileParent::StructuredCloneFileParent(
68 StructuredCloneFileParent
&& aOther
)
69 : StructuredCloneFileBase
{std::move(aOther
)},
70 mContents
{std::move(aOther
.mContents
)} {
71 MOZ_COUNT_CTOR(StructuredCloneFileParent
);
75 inline StructuredCloneFileParent::~StructuredCloneFileParent() {
76 MOZ_COUNT_DTOR(StructuredCloneFileParent
);
79 inline SafeRefPtr
<DatabaseFileInfo
> StructuredCloneFileParent::FileInfoPtr()
81 return (*mContents
)->clonePtr();
84 inline RefPtr
<dom::Blob
> StructuredCloneFileChild::BlobPtr() const {
85 return mContents
->as
<RefPtr
<dom::Blob
>>();
88 template <typename StructuredCloneFile
>
89 inline StructuredCloneReadInfo
<StructuredCloneFile
>::StructuredCloneReadInfo(
90 JS::StructuredCloneScope aScope
)
91 : StructuredCloneReadInfoBase(JSStructuredCloneData
{aScope
}) {
92 MOZ_COUNT_CTOR(StructuredCloneReadInfo
);
95 template <typename StructuredCloneFile
>
96 inline StructuredCloneReadInfo
<StructuredCloneFile
>::StructuredCloneReadInfo()
97 : StructuredCloneReadInfo(
98 JS::StructuredCloneScope::DifferentProcessForIndexedDB
) {}
100 template <typename StructuredCloneFile
>
101 inline StructuredCloneReadInfo
<StructuredCloneFile
>::StructuredCloneReadInfo(
102 JSStructuredCloneData
&& aData
, nsTArray
<StructuredCloneFile
> aFiles
)
103 : StructuredCloneReadInfoBase
{std::move(aData
)}, mFiles
{std::move(aFiles
)} {
104 MOZ_COUNT_CTOR(StructuredCloneReadInfo
);
107 #ifdef NS_BUILD_REFCNT_LOGGING
108 template <typename StructuredCloneFile
>
109 inline StructuredCloneReadInfo
<StructuredCloneFile
>::StructuredCloneReadInfo(
110 StructuredCloneReadInfo
&& aOther
) noexcept
111 : StructuredCloneReadInfoBase
{std::move(aOther
)},
112 mFiles
{std::move(aOther
.mFiles
)} {
113 MOZ_COUNT_CTOR(StructuredCloneReadInfo
);
116 template <typename StructuredCloneFile
>
117 inline StructuredCloneReadInfo
<
118 StructuredCloneFile
>::~StructuredCloneReadInfo() {
119 MOZ_COUNT_DTOR(StructuredCloneReadInfo
);
124 template <typename StructuredCloneFile
>
125 inline size_t StructuredCloneReadInfo
<StructuredCloneFile
>::Size() const {
126 size_t size
= Data().Size();
128 for (uint32_t i
= 0, count
= mFiles
.Length(); i
< count
; ++i
) {
129 // We don't want to calculate the size of files and so on, because are
130 // mainly file descriptors.
131 size
+= sizeof(uint64_t);
137 inline StructuredCloneReadInfoChild::StructuredCloneReadInfoChild(
138 JSStructuredCloneData
&& aData
, nsTArray
<StructuredCloneFileChild
> aFiles
,
139 IDBDatabase
* aDatabase
)
140 : StructuredCloneReadInfo
{std::move(aData
), std::move(aFiles
)},
141 mDatabase
{aDatabase
} {}
143 template <typename E
, typename Map
>
144 RefPtr
<DOMStringList
> CreateSortedDOMStringList(const nsTArray
<E
>& aArray
,
146 auto list
= MakeRefPtr
<DOMStringList
>();
148 if (!aArray
.IsEmpty()) {
149 nsTArray
<nsString
>& mapped
= list
->StringArray();
150 mapped
.SetCapacity(aArray
.Length());
152 std::transform(aArray
.cbegin(), aArray
.cend(), MakeBackInserter(mapped
),
161 template <typename StructuredCloneReadInfoType
>
162 JSObject
* StructuredCloneReadCallback(
163 JSContext
* const aCx
, JSStructuredCloneReader
* const aReader
,
164 const JS::CloneDataPolicy
& aCloneDataPolicy
, const uint32_t aTag
,
165 const uint32_t aData
, void* const aClosure
) {
166 auto* const database
= [aClosure
]() -> IDBDatabase
* {
167 if constexpr (std::is_same_v
<StructuredCloneReadInfoType
,
168 StructuredCloneReadInfoChild
>) {
169 return static_cast<StructuredCloneReadInfoChild
*>(aClosure
)->Database();
174 return CommonStructuredCloneReadCallback(
175 aCx
, aReader
, aCloneDataPolicy
, aTag
, aData
,
176 static_cast<StructuredCloneReadInfoType
*>(aClosure
), database
);
179 template <typename T
>
180 bool WrapAsJSObject(JSContext
* const aCx
, T
& aBaseObject
,
181 JS::MutableHandle
<JSObject
*> aResult
) {
182 JS::Rooted
<JS::Value
> wrappedValue(aCx
);
183 if (!ToJSValue(aCx
, aBaseObject
, &wrappedValue
)) {
187 aResult
.set(&wrappedValue
.toObject());
191 } // namespace indexedDB
193 } // namespace mozilla
195 #endif // IndexedDatabaseInlines_h