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/. */
8 #include "WorkletThread.h"
10 #include "mozilla/dom/WorkletFetchHandler.h"
11 #include "mozilla/dom/WorkletImpl.h"
12 #include "xpcprivate.h"
14 using JS::loader::ResolveError
;
15 using JS::loader::ResolveErrorInfo
;
17 namespace mozilla::dom
{
18 // ---------------------------------------------------------------------------
21 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Worklet
)
23 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Worklet
)
24 NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow
)
25 NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwnedObject
)
26 NS_IMPL_CYCLE_COLLECTION_UNLINK(mImportHandlers
)
27 tmp
->mImpl
->NotifyWorkletFinished();
28 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
29 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Worklet
)
32 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow
)
33 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwnedObject
)
34 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mImportHandlers
)
35 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
37 NS_IMPL_CYCLE_COLLECTING_ADDREF(Worklet
)
38 NS_IMPL_CYCLE_COLLECTING_RELEASE(Worklet
)
40 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Worklet
)
41 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
42 NS_INTERFACE_MAP_ENTRY(nsISupports
)
45 Worklet::Worklet(nsPIDOMWindowInner
* aWindow
, RefPtr
<WorkletImpl
> aImpl
,
46 nsISupports
* aOwnedObject
)
47 : mWindow(aWindow
), mOwnedObject(aOwnedObject
), mImpl(std::move(aImpl
)) {
50 MOZ_ASSERT(NS_IsMainThread());
53 Worklet::~Worklet() { mImpl
->NotifyWorkletFinished(); }
55 JSObject
* Worklet::WrapObject(JSContext
* aCx
,
56 JS::Handle
<JSObject
*> aGivenProto
) {
57 return mImpl
->WrapWorklet(aCx
, this, aGivenProto
);
60 static bool LoadLocalizedStrings(nsTArray
<nsString
>& aStrings
) {
61 // All enumes in ResolveError.
62 ResolveError errors
[] = {ResolveError::Failure
,
63 ResolveError::FailureMayBeBare
,
64 ResolveError::BlockedByNullEntry
,
65 ResolveError::BlockedByAfterPrefix
,
66 ResolveError::BlockedByBacktrackingPrefix
,
67 ResolveError::InvalidBareSpecifier
};
70 ArrayLength(errors
) == static_cast<size_t>(ResolveError::Length
),
71 "The array 'errors' has missing entries in the enum class ResolveError.");
73 for (auto i
: errors
) {
75 nsresult rv
= nsContentUtils::GetLocalizedString(
76 nsContentUtils::eDOM_PROPERTIES
, ResolveErrorInfo::GetString(i
),
78 if (NS_WARN_IF(NS_FAILED(rv
))) {
79 if (NS_WARN_IF(!aStrings
.AppendElement(EmptyString(), fallible
))) {
83 if (NS_WARN_IF(!aStrings
.AppendElement(message
, fallible
))) {
92 already_AddRefed
<Promise
> Worklet::AddModule(JSContext
* aCx
,
93 const nsAString
& aModuleURL
,
94 const WorkletOptions
& aOptions
,
95 CallerType aCallerType
,
97 MOZ_ASSERT(NS_IsMainThread());
98 if (mLocalizedStrings
.IsEmpty()) {
99 bool result
= LoadLocalizedStrings(mLocalizedStrings
);
105 return WorkletFetchHandler::AddModule(this, aCx
, aModuleURL
, aOptions
, aRv
);
108 WorkletFetchHandler
* Worklet::GetImportFetchHandler(const nsACString
& aURI
) {
109 MOZ_ASSERT(NS_IsMainThread());
110 return mImportHandlers
.GetWeak(aURI
);
113 void Worklet::AddImportFetchHandler(const nsACString
& aURI
,
114 WorkletFetchHandler
* aHandler
) {
115 MOZ_ASSERT(aHandler
);
116 MOZ_ASSERT(!mImportHandlers
.GetWeak(aURI
));
117 MOZ_ASSERT(NS_IsMainThread());
119 mImportHandlers
.InsertOrUpdate(aURI
, RefPtr
{aHandler
});
122 } // namespace mozilla::dom