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 #include "StaticComponents.h"
9 #include "mozilla/ArrayUtils.h"
10 #ifdef MOZ_BACKGROUNDTASKS
11 # include "mozilla/BackgroundTasks.h"
13 #include "mozilla/PerfectHash.h"
14 #include "mozilla/ResultExtensions.h"
15 #include "mozilla/StaticPtr.h"
16 #include "mozilla/UniquePtr.h"
17 #include "mozilla/dom/ScriptSettings.h"
18 #include "mozJSComponentLoader.h"
20 #include "nsComponentManager.h"
21 #include "nsContentUtils.h"
22 #include "nsIFactory.h"
23 #include "nsISupports.h"
24 #include "nsIXPConnect.h"
26 #include "nsStringEnumerator.h"
30 #include "js/PropertyAndElement.h" // JS_GetProperty
32 // Cleanup pollution from zipstruct.h
39 //# @relative_includes@
49 static constexpr uint32_t kNoContractID
= 0xffffffff;
52 // Template helpers for constructor function sanity checks.
54 struct RemoveAlreadyAddRefed
{
59 struct RemoveAlreadyAddRefed
<already_AddRefed
<T
>> {
62 } // anonymous namespace
65 uint8_t gInvalidContracts
[kContractCount
/ 8 + 1];
67 static StaticRefPtr
<nsISupports
> gServiceInstances
[kStaticModuleCount
];
69 uint8_t gInitCalled
[kModuleInitCount
/ 8 + 1];
71 static const char gStrings
[] =
75 const StaticCategory gStaticCategories
[kStaticCategoryCount
] = {
78 const StaticCategoryEntry gStaticCategoryEntries
[] = {
79 //# @category_entries@
82 const nsXPTInterface gInterfaces
[] = {
86 const StringOffset gComponentJSMs
[] = {
91 * Returns a nsCString corresponding to the given entry in the `gStrings` string
92 * table. The resulting nsCString points directly to static storage, and does
93 * not incur any memory allocation overhead.
95 static inline nsCString
GetString(const StringOffset
& aOffset
) {
96 const char* str
= &gStrings
[aOffset
.mOffset
];
98 result
.AssignLiteral(str
, strlen(str
));
102 nsCString
ContractEntry::ContractID() const {
103 return GetString(mContractID
);
106 bool ContractEntry::Matches(const nsACString
& aContractID
) const {
107 return aContractID
== ContractID() && Module().Active();
111 static nsresult
ConstructJSMComponent(const nsACString
& aURI
,
112 const char* aConstructor
,
113 nsISupports
** aResult
) {
114 if (!nsComponentManagerImpl::JSLoaderReady()) {
115 return NS_ERROR_NOT_AVAILABLE
;
119 MOZ_ALWAYS_TRUE(jsapi
.Init(xpc::PrivilegedJunkScope()));
120 JSContext
* cx
= jsapi
.cx();
122 JS::RootedObject
global(cx
);
123 JS::RootedObject
exports(cx
);
124 MOZ_TRY(mozJSComponentLoader::Get()->Import(cx
, aURI
, &global
, &exports
));
126 JS::RootedValue
ctor(cx
);
127 if (!JS_GetProperty(cx
, exports
, aConstructor
, &ctor
) ||
129 return NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED
;
132 JS::RootedObject
inst(cx
);
133 if (!JS::Construct(cx
, ctor
, JS::HandleValueArray::empty(), &inst
)) {
134 return NS_ERROR_FAILURE
;
137 return nsContentUtils::XPConnect()->WrapJS(cx
, inst
, NS_GET_IID(nsISupports
),
142 //# @module_cid_table@
144 //# @module_contract_id_table@
146 //# @js_services_table@
148 static inline bool CalledInit(size_t aIdx
) {
149 return GetBit(gInitCalled
, aIdx
);
152 static nsresult
CallInitFunc(size_t aIdx
) {
153 if (CalledInit(aIdx
)) {
162 SetBit(gInitCalled
, aIdx
);
164 MOZ_ASSERT(NS_SUCCEEDED(rv
));
168 static void CallUnloadFuncs() {
172 static nsresult
CreateInstanceImpl(ModuleID aID
, const nsIID
& aIID
,
174 // The full set of constructors for all static modules.
175 // This switch statement will be compiled to a relative address jump table
176 // with no runtime relocations and a single indirect jump.
181 MOZ_ASSERT_UNREACHABLE("Constructor didn't return");
182 return NS_ERROR_FAILURE
;
188 class StaticModuleFactory final
: public nsIFactory
{
192 explicit StaticModuleFactory(ModuleID aID
) : mID(aID
) {}
195 ~StaticModuleFactory() = default;
200 NS_IMPL_ISUPPORTS(StaticModuleFactory
, nsIFactory
)
202 NS_IMETHODIMP
StaticModuleFactory::CreateInstance(const nsIID
& aIID
,
204 return CreateInstanceImpl(mID
, aIID
, aResult
);
207 } // anonymous namespace
210 already_AddRefed
<nsIFactory
> StaticModule::GetFactory() const {
211 return do_AddRef(new StaticModuleFactory(ID()));
214 bool StaticModule::Active() const {
215 return FastProcessSelectorMatches(mProcessSelector
);
218 bool StaticModule::Overridable() const {
219 return mContractID
.mOffset
!= kNoContractID
;
222 nsCString
StaticModule::ContractID() const {
223 MOZ_ASSERT(Overridable());
224 return GetString(mContractID
);
227 nsresult
StaticModule::CreateInstance(const nsIID
& aIID
, void** aResult
) const {
228 return CreateInstanceImpl(ID(), aIID
, aResult
);
231 GetServiceHelper
StaticModule::GetService() const {
232 return { ID(), nullptr };
235 GetServiceHelper
StaticModule::GetService(nsresult
* aRv
) const {
236 return { ID(), aRv
};
240 nsISupports
* StaticModule::ServiceInstance() const {
241 return gServiceInstances
[Idx()];
244 void StaticModule::SetServiceInstance(
245 already_AddRefed
<nsISupports
> aInst
) const {
246 gServiceInstances
[Idx()] = aInst
;
250 nsCString
StaticCategoryEntry::Entry() const {
251 return GetString(mEntry
);
254 nsCString
StaticCategoryEntry::Value() const {
255 return GetString(mValue
);
258 bool StaticCategoryEntry::Active() const {
259 if (!FastProcessSelectorMatches(mProcessSelector
)) {
262 #ifdef MOZ_BACKGROUNDTASKS
263 if (MOZ_UNLIKELY(BackgroundTasks::IsBackgroundTaskMode())) {
264 return mBackgroundTasksSelector
!= Module::BackgroundTasksSelector::NO_TASKS
;
266 #endif /* MOZ_BACKGROUNDTASKS */
270 nsCString
StaticCategory::Name() const {
271 return GetString(mName
);
274 nsCString
JSServiceEntry::Name() const {
275 return GetString(mName
);
278 JSServiceEntry::InterfaceList
JSServiceEntry::Interfaces() const {
280 iids
.SetCapacity(mInterfaceCount
);
282 for (size_t i
= 0; i
< mInterfaceCount
; i
++) {
283 nsXPTInterface ifaceID
= gInterfaces
[mInterfaceOffset
.mOffset
+ i
];
284 iids
.AppendElement(&nsXPTInterfaceInfo::Get(ifaceID
)->IID());
291 const JSServiceEntry
* JSServiceEntry::Lookup(const nsACString
& aName
) {
292 return LookupJSService(aName
);
295 /* static */ const StaticModule
* StaticComponents::LookupByCID(
297 return ModuleByCID(aCID
);
300 /* static */ const StaticModule
* StaticComponents::LookupByContractID(
301 const nsACString
& aContractID
) {
302 if (const ContractEntry
* entry
= LookupContractID(aContractID
)) {
303 if (!entry
->Invalid()) {
304 return &entry
->Module();
310 /* static */ bool StaticComponents::InvalidateContractID(
311 const nsACString
& aContractID
, bool aInvalid
) {
312 if (const ContractEntry
* entry
= LookupContractID(aContractID
)) {
313 entry
->SetInvalid(aInvalid
);
319 /* static */ already_AddRefed
<nsIUTF8StringEnumerator
>
320 StaticComponents::GetComponentJSMs() {
321 auto jsms
= MakeUnique
<nsTArray
<nsCString
>>(MOZ_ARRAY_LENGTH(gComponentJSMs
));
323 for (const auto& entry
: gComponentJSMs
) {
324 jsms
->AppendElement(GetString(entry
));
327 nsCOMPtr
<nsIUTF8StringEnumerator
> result
;
328 MOZ_ALWAYS_SUCCEEDS(NS_NewAdoptingUTF8StringEnumerator(getter_AddRefs(result
),
330 return result
.forget();
333 /* static */ Span
<const JSServiceEntry
> StaticComponents::GetJSServices() {
334 return { gJSServices
, ArrayLength(gJSServices
) };
337 /* static */ void StaticComponents::Shutdown() {
341 /* static */ const nsID
& Components::GetCID(ModuleID aID
) {
342 return gStaticModules
[size_t(aID
)].CID();
345 nsresult
GetServiceHelper::operator()(const nsIID
& aIID
, void** aResult
) const {
347 nsComponentManagerImpl::gComponentManager
->GetService(mId
, aIID
, aResult
);
348 return SetResult(rv
);
351 nsresult
CreateInstanceHelper::operator()(const nsIID
& aIID
,
352 void** aResult
) const {
353 const auto& entry
= gStaticModules
[size_t(mId
)];
354 if (!entry
.Active()) {
355 return SetResult(NS_ERROR_FACTORY_NOT_REGISTERED
);
358 nsresult rv
= entry
.CreateInstance(aIID
, aResult
);
359 return SetResult(rv
);
363 } // namespace mozilla