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 "UntrustedModulesBackupService.h"
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/HashFunctions.h"
11 #include "mozilla/SchedulerGroup.h"
12 #include "mozilla/StaticLocalPtr.h"
16 ProcessHashKey::ProcessHashKey(GeckoProcessType aType
, DWORD aPid
)
17 : mType(aType
), mPid(aPid
) {}
19 bool ProcessHashKey::operator==(const ProcessHashKey
& aOther
) const {
20 return mPid
== aOther
.mPid
&& mType
== aOther
.mType
;
23 PLDHashNumber
ProcessHashKey::Hash() const { return HashGeneric(mPid
, mType
); }
25 void UntrustedModulesBackupData::Add(UntrustedModulesData
&& aData
) {
27 ProcessHashKey(aData
.mProcessType
, aData
.mPid
), [&](auto&& p
) {
29 p
.Data()->mData
.Merge(std::move(aData
));
31 p
.Insert(MakeRefPtr
<UntrustedModulesDataContainer
>(std::move(aData
)));
36 void UntrustedModulesBackupData::AddWithoutStacks(
37 UntrustedModulesData
&& aData
) {
39 ProcessHashKey(aData
.mProcessType
, aData
.mPid
), [&](auto&& p
) {
41 p
.Data()->mData
.MergeWithoutStacks(std::move(aData
));
44 p
.Insert(MakeRefPtr
<UntrustedModulesDataContainer
>(std::move(aData
)));
50 UntrustedModulesBackupService
* UntrustedModulesBackupService::Get() {
51 if (!XRE_IsParentProcess()) {
55 static StaticLocalRefPtr
<UntrustedModulesBackupService
> sInstance(
56 []() -> already_AddRefed
<UntrustedModulesBackupService
> {
57 RefPtr
<UntrustedModulesBackupService
> instance(
58 new UntrustedModulesBackupService());
60 auto setClearOnShutdown
= [ptr
= &sInstance
]() -> void {
64 if (NS_IsMainThread()) {
66 return instance
.forget();
69 SchedulerGroup::Dispatch(NS_NewRunnableFunction(
70 "mozilla::UntrustedModulesBackupService::Get",
71 std::move(setClearOnShutdown
)));
73 return instance
.forget();
79 void UntrustedModulesBackupService::Backup(UntrustedModulesData
&& aData
) {
80 mStaging
.Add(std::move(aData
));
83 void UntrustedModulesBackupService::SettleAllStagingData() {
84 UntrustedModulesBackupData staging
;
85 staging
.SwapElements(mStaging
);
87 for (auto&& iter
= staging
.Iter(); !iter
.Done(); iter
.Next()) {
91 mSettled
.AddWithoutStacks(std::move(iter
.Data()->mData
));
95 } // namespace mozilla