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 https://mozilla.org/MPL/2.0/. */
7 #include "mozilla/WinDllServices.h"
12 #include "mozilla/ClearOnShutdown.h"
13 #include "mozilla/SchedulerGroup.h"
14 #include "mozilla/Services.h"
15 #include "mozilla/StaticLocalPtr.h"
16 #include "mozilla/UntrustedModulesProcessor.h"
18 #include "nsIObserverService.h"
20 #include "nsXULAppAPI.h"
24 const char* DllServices::kTopicDllLoadedMainThread
= "dll-loaded-main-thread";
25 const char* DllServices::kTopicDllLoadedNonMainThread
=
26 "dll-loaded-non-main-thread";
29 DllServices
* DllServices::Get() {
30 static StaticLocalRefPtr
<DllServices
> sInstance(
31 []() -> already_AddRefed
<DllServices
> {
32 RefPtr
<DllServices
> dllSvc(new DllServices());
35 auto setClearOnShutdown
= [ptr
= &sInstance
]() -> void {
39 if (NS_IsMainThread()) {
41 return dllSvc
.forget();
44 SchedulerGroup::Dispatch(NS_NewRunnableFunction(
45 "mozilla::DllServices::Get", std::move(setClearOnShutdown
)));
47 return dllSvc
.forget();
53 DllServices::~DllServices() { DisableFull(); }
55 void DllServices::StartUntrustedModulesProcessor(bool aIsStartingUp
) {
56 MOZ_ASSERT(NS_IsMainThread());
57 MOZ_ASSERT(!mUntrustedModulesProcessor
);
58 mUntrustedModulesProcessor
= UntrustedModulesProcessor::Create(aIsStartingUp
);
61 bool DllServices::IsReadyForBackgroundProcessing() const {
62 return mUntrustedModulesProcessor
&&
63 mUntrustedModulesProcessor
->IsReadyForBackgroundProcessing();
66 RefPtr
<UntrustedModulesPromise
> DllServices::GetUntrustedModulesData() {
67 if (!mUntrustedModulesProcessor
) {
68 return UntrustedModulesPromise::CreateAndReject(NS_ERROR_NOT_IMPLEMENTED
,
72 return mUntrustedModulesProcessor
->GetProcessedData();
75 void DllServices::DisableFull() {
76 if (mUntrustedModulesProcessor
) {
77 mUntrustedModulesProcessor
->Disable();
80 glue::DllServices::DisableFull();
83 RefPtr
<ModulesTrustPromise
> DllServices::GetModulesTrust(
84 ModulePaths
&& aModPaths
, bool aRunAtNormalPriority
) {
85 if (!mUntrustedModulesProcessor
) {
86 return ModulesTrustPromise::CreateAndReject(NS_ERROR_NOT_IMPLEMENTED
,
90 return mUntrustedModulesProcessor
->GetModulesTrust(std::move(aModPaths
),
91 aRunAtNormalPriority
);
94 void DllServices::NotifyDllLoad(glue::EnhancedModuleLoadInfo
&& aModLoadInfo
) {
95 MOZ_ASSERT(NS_IsMainThread());
99 if (aModLoadInfo
.mNtLoadInfo
.mThreadId
== ::GetCurrentThreadId()) {
100 topic
= kTopicDllLoadedMainThread
;
102 topic
= kTopicDllLoadedNonMainThread
;
105 // We save the path to a nsAutoString because once we have submitted
106 // aModLoadInfo for processing there is no guarantee that the original
107 // buffer will continue to be valid.
108 nsAutoString
dllFilePath(aModLoadInfo
.GetSectionName());
110 if (mUntrustedModulesProcessor
) {
111 mUntrustedModulesProcessor
->Enqueue(std::move(aModLoadInfo
));
114 nsCOMPtr
<nsIObserverService
> obsServ(mozilla::services::GetObserverService());
119 obsServ
->NotifyObservers(nullptr, topic
, dllFilePath
.get());
122 void DllServices::NotifyModuleLoadBacklog(ModuleLoadInfoVec
&& aEvents
) {
123 MOZ_ASSERT(NS_IsMainThread());
124 if (!mUntrustedModulesProcessor
) {
128 mUntrustedModulesProcessor
->Enqueue(std::move(aEvents
));
131 } // namespace mozilla