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 "mozilla/dom/ToJSValue.h"
16 #include "mozilla/dom/indexedDB/PBackgroundIDBSharedTypes.h"
17 #include "mozilla/dom/DOMStringList.h"
18 #include "mozilla/dom/File.h"
20 namespace mozilla::dom::indexedDB
{
22 #ifdef NS_BUILD_REFCNT_LOGGING
23 inline StructuredCloneFileChild::StructuredCloneFileChild(
24 StructuredCloneFileChild
&& aOther
)
25 : StructuredCloneFileBase
{std::move(aOther
)},
26 mContents
{std::move(aOther
.mContents
)} {
27 MOZ_COUNT_CTOR(StructuredCloneFileChild
);
31 inline StructuredCloneFileChild::~StructuredCloneFileChild() {
32 MOZ_COUNT_DTOR(StructuredCloneFileChild
);
35 inline StructuredCloneFileChild::StructuredCloneFileChild(FileType aType
)
36 : StructuredCloneFileBase
{aType
}, mContents
{Nothing()} {
37 MOZ_COUNT_CTOR(StructuredCloneFileChild
);
40 inline StructuredCloneFileChild::StructuredCloneFileChild(
41 FileType aType
, RefPtr
<dom::Blob
> aBlob
)
42 : StructuredCloneFileBase
{aType
}, mContents
{std::move(aBlob
)} {
43 MOZ_ASSERT(eBlob
== aType
|| eStructuredClone
== aType
);
44 MOZ_ASSERT(mContents
->as
<RefPtr
<dom::Blob
>>());
45 MOZ_COUNT_CTOR(StructuredCloneFileChild
);
48 inline StructuredCloneFileParent::StructuredCloneFileParent(
49 FileType aType
, SafeRefPtr
<DatabaseFileInfo
> aFileInfo
)
50 : StructuredCloneFileBase
{aType
}, mContents
{Some(std::move(aFileInfo
))} {
51 MOZ_ASSERT(**mContents
);
52 MOZ_COUNT_CTOR(StructuredCloneFileParent
);
55 #ifdef NS_BUILD_REFCNT_LOGGING
56 inline StructuredCloneFileParent::StructuredCloneFileParent(
57 StructuredCloneFileParent
&& aOther
)
58 : StructuredCloneFileBase
{std::move(aOther
)},
59 mContents
{std::move(aOther
.mContents
)} {
60 MOZ_COUNT_CTOR(StructuredCloneFileParent
);
64 inline StructuredCloneFileParent::~StructuredCloneFileParent() {
65 MOZ_COUNT_DTOR(StructuredCloneFileParent
);
68 inline SafeRefPtr
<DatabaseFileInfo
> StructuredCloneFileParent::FileInfoPtr()
70 return (*mContents
)->clonePtr();
73 inline RefPtr
<dom::Blob
> StructuredCloneFileChild::BlobPtr() const {
74 return mContents
->as
<RefPtr
<dom::Blob
>>();
77 template <typename StructuredCloneFile
>
78 inline StructuredCloneReadInfo
<StructuredCloneFile
>::StructuredCloneReadInfo(
79 JS::StructuredCloneScope aScope
)
80 : StructuredCloneReadInfoBase(JSStructuredCloneData
{aScope
}) {
81 MOZ_COUNT_CTOR(StructuredCloneReadInfo
);
84 template <typename StructuredCloneFile
>
85 inline StructuredCloneReadInfo
<StructuredCloneFile
>::StructuredCloneReadInfo()
86 : StructuredCloneReadInfo(
87 JS::StructuredCloneScope::DifferentProcessForIndexedDB
) {}
89 template <typename StructuredCloneFile
>
90 inline StructuredCloneReadInfo
<StructuredCloneFile
>::StructuredCloneReadInfo(
91 JSStructuredCloneData
&& aData
, nsTArray
<StructuredCloneFile
> aFiles
)
92 : StructuredCloneReadInfoBase
{std::move(aData
)}, mFiles
{std::move(aFiles
)} {
93 MOZ_COUNT_CTOR(StructuredCloneReadInfo
);
96 #ifdef NS_BUILD_REFCNT_LOGGING
97 template <typename StructuredCloneFile
>
98 inline StructuredCloneReadInfo
<StructuredCloneFile
>::StructuredCloneReadInfo(
99 StructuredCloneReadInfo
&& aOther
) noexcept
100 : StructuredCloneReadInfoBase
{std::move(aOther
)},
101 mFiles
{std::move(aOther
.mFiles
)} {
102 MOZ_COUNT_CTOR(StructuredCloneReadInfo
);
105 template <typename StructuredCloneFile
>
106 inline StructuredCloneReadInfo
<
107 StructuredCloneFile
>::~StructuredCloneReadInfo() {
108 MOZ_COUNT_DTOR(StructuredCloneReadInfo
);
113 template <typename StructuredCloneFile
>
114 inline size_t StructuredCloneReadInfo
<StructuredCloneFile
>::Size() const {
115 size_t size
= Data().Size();
117 for (uint32_t i
= 0, count
= mFiles
.Length(); i
< count
; ++i
) {
118 // We don't want to calculate the size of files and so on, because are
119 // mainly file descriptors.
120 size
+= sizeof(uint64_t);
126 inline StructuredCloneReadInfoChild::StructuredCloneReadInfoChild(
127 JSStructuredCloneData
&& aData
, nsTArray
<StructuredCloneFileChild
> aFiles
,
128 IDBDatabase
* aDatabase
)
129 : StructuredCloneReadInfo
{std::move(aData
), std::move(aFiles
)},
130 mDatabase
{aDatabase
} {}
132 template <typename E
, typename Map
>
133 RefPtr
<DOMStringList
> CreateSortedDOMStringList(const nsTArray
<E
>& aArray
,
135 auto list
= MakeRefPtr
<DOMStringList
>();
137 if (!aArray
.IsEmpty()) {
138 nsTArray
<nsString
>& mapped
= list
->StringArray();
139 mapped
.SetCapacity(aArray
.Length());
141 std::transform(aArray
.cbegin(), aArray
.cend(), MakeBackInserter(mapped
),
150 template <typename StructuredCloneReadInfoType
>
151 JSObject
* StructuredCloneReadCallback(
152 JSContext
* const aCx
, JSStructuredCloneReader
* const aReader
,
153 const JS::CloneDataPolicy
& aCloneDataPolicy
, const uint32_t aTag
,
154 const uint32_t aData
, void* const aClosure
) {
155 auto* const database
= [aClosure
]() -> IDBDatabase
* {
156 if constexpr (std::is_same_v
<StructuredCloneReadInfoType
,
157 StructuredCloneReadInfoChild
>) {
158 return static_cast<StructuredCloneReadInfoChild
*>(aClosure
)->Database();
163 return CommonStructuredCloneReadCallback(
164 aCx
, aReader
, aCloneDataPolicy
, aTag
, aData
,
165 static_cast<StructuredCloneReadInfoType
*>(aClosure
), database
);
168 template <typename T
>
169 bool WrapAsJSObject(JSContext
* const aCx
, T
& aBaseObject
,
170 JS::MutableHandle
<JSObject
*> aResult
) {
171 JS::Rooted
<JS::Value
> wrappedValue(aCx
);
172 if (!ToJSValue(aCx
, aBaseObject
, &wrappedValue
)) {
176 aResult
.set(&wrappedValue
.toObject());
180 } // namespace mozilla::dom::indexedDB
182 #endif // IndexedDatabaseInlines_h