Update configs. IGNORE BROKEN CHANGESETS CLOSED TREE NO BUG a=release ba=release
[gecko.git] / toolkit / xre / dllservices / WinDllServices.cpp
blob31e466c5f1ca227fa87a90d35d5cd6a3b79aca9c
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"
9 #include <windows.h>
10 #include <psapi.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"
17 #include "nsCOMPtr.h"
18 #include "nsIObserverService.h"
19 #include "nsString.h"
20 #include "nsXULAppAPI.h"
22 namespace mozilla {
24 const char* DllServices::kTopicDllLoadedMainThread = "dll-loaded-main-thread";
25 const char* DllServices::kTopicDllLoadedNonMainThread =
26 "dll-loaded-non-main-thread";
28 /* static */
29 DllServices* DllServices::Get() {
30 static StaticLocalRefPtr<DllServices> sInstance(
31 []() -> already_AddRefed<DllServices> {
32 RefPtr<DllServices> dllSvc(new DllServices());
33 dllSvc->EnableFull();
35 auto setClearOnShutdown = [ptr = &sInstance]() -> void {
36 ClearOnShutdown(ptr);
39 if (NS_IsMainThread()) {
40 setClearOnShutdown();
41 return dllSvc.forget();
44 SchedulerGroup::Dispatch(NS_NewRunnableFunction(
45 "mozilla::DllServices::Get", std::move(setClearOnShutdown)));
47 return dllSvc.forget();
48 }());
50 return sInstance;
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,
69 __func__);
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,
87 __func__);
90 return mUntrustedModulesProcessor->GetModulesTrust(std::move(aModPaths),
91 aRunAtNormalPriority);
94 void DllServices::NotifyDllLoad(glue::EnhancedModuleLoadInfo&& aModLoadInfo) {
95 MOZ_ASSERT(NS_IsMainThread());
97 const char* topic;
99 if (aModLoadInfo.mNtLoadInfo.mThreadId == ::GetCurrentThreadId()) {
100 topic = kTopicDllLoadedMainThread;
101 } else {
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());
115 if (!obsServ) {
116 return;
119 obsServ->NotifyObservers(nullptr, topic, dllFilePath.get());
122 void DllServices::NotifyModuleLoadBacklog(ModuleLoadInfoVec&& aEvents) {
123 MOZ_ASSERT(NS_IsMainThread());
124 if (!mUntrustedModulesProcessor) {
125 return;
128 mUntrustedModulesProcessor->Enqueue(std::move(aEvents));
131 } // namespace mozilla