summaryrefslogtreecommitdiffstats
path: root/dom/worklet/Worklet.cpp
blob: 7c37bdbba960cde5dfc0651fd7a28e294fc382b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Worklet.h"
#include "WorkletThread.h"

#include "mozilla/dom/WorkletFetchHandler.h"
#include "mozilla/dom/WorkletImpl.h"
#include "xpcprivate.h"

using JS::loader::ResolveError;
using JS::loader::ResolveErrorInfo;

namespace mozilla::dom {
// ---------------------------------------------------------------------------
// Worklet

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Worklet)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Worklet)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwnedObject)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mImportHandlers)
  tmp->mImpl->NotifyWorkletFinished();
  NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Worklet)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwnedObject)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mImportHandlers)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(Worklet)
NS_IMPL_CYCLE_COLLECTING_RELEASE(Worklet)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Worklet)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

Worklet::Worklet(nsPIDOMWindowInner* aWindow, RefPtr<WorkletImpl> aImpl,
                 nsISupports* aOwnedObject)
    : mWindow(aWindow), mOwnedObject(aOwnedObject), mImpl(std::move(aImpl)) {
  MOZ_ASSERT(aWindow);
  MOZ_ASSERT(mImpl);
  MOZ_ASSERT(NS_IsMainThread());
}

Worklet::~Worklet() { mImpl->NotifyWorkletFinished(); }

JSObject* Worklet::WrapObject(JSContext* aCx,
                              JS::Handle<JSObject*> aGivenProto) {
  return mImpl->WrapWorklet(aCx, this, aGivenProto);
}

static bool LoadLocalizedStrings(nsTArray<nsString>& aStrings) {
  // All enumes in ResolveError.
  ResolveError errors[] = {ResolveError::Failure,
                           ResolveError::FailureMayBeBare,
                           ResolveError::BlockedByNullEntry,
                           ResolveError::BlockedByAfterPrefix,
                           ResolveError::BlockedByBacktrackingPrefix,
                           ResolveError::InvalidBareSpecifier};

  static_assert(
      ArrayLength(errors) == static_cast<size_t>(ResolveError::Length),
      "The array 'errors' has missing entries in the enum class ResolveError.");

  for (auto i : errors) {
    nsAutoString message;
    nsresult rv = nsContentUtils::GetLocalizedString(
        nsContentUtils::eDOM_PROPERTIES, ResolveErrorInfo::GetString(i),
        message);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      if (NS_WARN_IF(!aStrings.AppendElement(EmptyString(), fallible))) {
        return false;
      }
    } else {
      if (NS_WARN_IF(!aStrings.AppendElement(message, fallible))) {
        return false;
      }
    }
  }

  return true;
}

already_AddRefed<Promise> Worklet::AddModule(JSContext* aCx,
                                             const nsAString& aModuleURL,
                                             const WorkletOptions& aOptions,
                                             CallerType aCallerType,
                                             ErrorResult& aRv) {
  MOZ_ASSERT(NS_IsMainThread());
  if (mLocalizedStrings.IsEmpty()) {
    bool result = LoadLocalizedStrings(mLocalizedStrings);
    if (!result) {
      return nullptr;
    }
  }

  return WorkletFetchHandler::AddModule(this, aCx, aModuleURL, aOptions, aRv);
}

WorkletFetchHandler* Worklet::GetImportFetchHandler(const nsACString& aURI) {
  MOZ_ASSERT(NS_IsMainThread());
  return mImportHandlers.GetWeak(aURI);
}

void Worklet::AddImportFetchHandler(const nsACString& aURI,
                                    WorkletFetchHandler* aHandler) {
  MOZ_ASSERT(aHandler);
  MOZ_ASSERT(!mImportHandlers.GetWeak(aURI));
  MOZ_ASSERT(NS_IsMainThread());

  mImportHandlers.InsertOrUpdate(aURI, RefPtr{aHandler});
}

}  // namespace mozilla::dom