Bug 1708422: part 13) Factor code out to `mozInlineSpellChecker::SpellCheckerTimeSlic...
[gecko.git] / toolkit / xre / WinDllServices.cpp
blobcc00227be351e532cc43bf0b1a3d872255f0406d
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 // Full DLL services require XPCOM, which GMP doesn't have
34 if (XRE_IsGMPluginProcess()) {
35 dllSvc->EnableBasic();
36 } else {
37 dllSvc->EnableFull();
40 auto setClearOnShutdown = [ptr = &sInstance]() -> void {
41 ClearOnShutdown(ptr);
44 if (NS_IsMainThread()) {
45 setClearOnShutdown();
46 return dllSvc.forget();
49 SchedulerGroup::Dispatch(
50 TaskCategory::Other,
51 NS_NewRunnableFunction("mozilla::DllServices::Get",
52 std::move(setClearOnShutdown)));
54 return dllSvc.forget();
55 }());
57 return sInstance;
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,
71 __func__);
74 return mUntrustedModulesProcessor->GetProcessedData();
77 void DllServices::DisableFull() {
78 if (XRE_IsGMPluginProcess()) {
79 return;
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,
93 __func__);
96 return mUntrustedModulesProcessor->GetModulesTrust(std::move(aModPaths),
97 aRunAtNormalPriority);
100 void DllServices::NotifyDllLoad(glue::EnhancedModuleLoadInfo&& aModLoadInfo) {
101 MOZ_ASSERT(NS_IsMainThread());
103 const char* topic;
105 if (aModLoadInfo.mNtLoadInfo.mThreadId == ::GetCurrentThreadId()) {
106 topic = kTopicDllLoadedMainThread;
107 } else {
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());
121 if (!obsServ) {
122 return;
125 obsServ->NotifyObservers(nullptr, topic, dllFilePath.get());
128 void DllServices::NotifyModuleLoadBacklog(ModuleLoadInfoVec&& aEvents) {
129 MOZ_ASSERT(NS_IsMainThread());
130 if (!mUntrustedModulesProcessor) {
131 return;
134 mUntrustedModulesProcessor->Enqueue(std::move(aEvents));
137 } // namespace mozilla