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());
33 // Full DLL services require XPCOM, which GMP doesn't have
34 if (XRE_IsGMPluginProcess()) {
35 dllSvc
->EnableBasic();
40 auto setClearOnShutdown
= [ptr
= &sInstance
]() -> void {
44 if (NS_IsMainThread()) {
46 return dllSvc
.forget();
49 SchedulerGroup::Dispatch(
51 NS_NewRunnableFunction("mozilla::DllServices::Get",
52 std::move(setClearOnShutdown
)));
54 return dllSvc
.forget();
60 DllServices::~DllServices() { DisableFull(); }
62 void DllServices::StartUntrustedModulesProcessor() {
63 MOZ_ASSERT(NS_IsMainThread());
64 MOZ_ASSERT(!mUntrustedModulesProcessor
);
65 mUntrustedModulesProcessor
= UntrustedModulesProcessor::Create();
68 RefPtr
<UntrustedModulesPromise
> DllServices::GetUntrustedModulesData() {
69 if (!mUntrustedModulesProcessor
) {
70 return UntrustedModulesPromise::CreateAndReject(NS_ERROR_NOT_IMPLEMENTED
,
74 return mUntrustedModulesProcessor
->GetProcessedData();
77 void DllServices::DisableFull() {
78 if (XRE_IsGMPluginProcess()) {
82 if (mUntrustedModulesProcessor
) {
83 mUntrustedModulesProcessor
->Disable();
86 glue::DllServices::DisableFull();
89 RefPtr
<ModulesTrustPromise
> DllServices::GetModulesTrust(
90 ModulePaths
&& aModPaths
, bool aRunAtNormalPriority
) {
91 if (!mUntrustedModulesProcessor
) {
92 return ModulesTrustPromise::CreateAndReject(NS_ERROR_NOT_IMPLEMENTED
,
96 return mUntrustedModulesProcessor
->GetModulesTrust(std::move(aModPaths
),
97 aRunAtNormalPriority
);
100 void DllServices::NotifyDllLoad(glue::EnhancedModuleLoadInfo
&& aModLoadInfo
) {
101 MOZ_ASSERT(NS_IsMainThread());
105 if (aModLoadInfo
.mNtLoadInfo
.mThreadId
== ::GetCurrentThreadId()) {
106 topic
= kTopicDllLoadedMainThread
;
108 topic
= kTopicDllLoadedNonMainThread
;
111 // We save the path to a nsAutoString because once we have submitted
112 // aModLoadInfo for processing there is no guarantee that the original
113 // buffer will continue to be valid.
114 nsAutoString
dllFilePath(aModLoadInfo
.GetSectionName());
116 if (mUntrustedModulesProcessor
) {
117 mUntrustedModulesProcessor
->Enqueue(std::move(aModLoadInfo
));
120 nsCOMPtr
<nsIObserverService
> obsServ(mozilla::services::GetObserverService());
125 obsServ
->NotifyObservers(nullptr, topic
, dllFilePath
.get());
128 void DllServices::NotifyModuleLoadBacklog(ModuleLoadInfoVec
&& aEvents
) {
129 MOZ_ASSERT(NS_IsMainThread());
130 if (!mUntrustedModulesProcessor
) {
134 mUntrustedModulesProcessor
->Enqueue(std::move(aEvents
));
137 } // namespace mozilla