Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / toolkit / components / telemetry / other / UntrustedModulesBackupService.cpp
blob1cbe35e8c057fe45462d2e6a8f70a54305cf9eb0
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"
14 namespace mozilla {
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) {
26 WithEntryHandle(
27 ProcessHashKey(aData.mProcessType, aData.mPid), [&](auto&& p) {
28 if (p) {
29 p.Data()->mData.Merge(std::move(aData));
30 } else {
31 p.Insert(MakeRefPtr<UntrustedModulesDataContainer>(std::move(aData)));
33 });
36 void UntrustedModulesBackupData::AddWithoutStacks(
37 UntrustedModulesData&& aData) {
38 WithEntryHandle(
39 ProcessHashKey(aData.mProcessType, aData.mPid), [&](auto&& p) {
40 if (p) {
41 p.Data()->mData.MergeWithoutStacks(std::move(aData));
42 } else {
43 aData.Truncate(true);
44 p.Insert(MakeRefPtr<UntrustedModulesDataContainer>(std::move(aData)));
46 });
49 /* static */
50 UntrustedModulesBackupService* UntrustedModulesBackupService::Get() {
51 if (!XRE_IsParentProcess()) {
52 return nullptr;
55 static StaticLocalRefPtr<UntrustedModulesBackupService> sInstance(
56 []() -> already_AddRefed<UntrustedModulesBackupService> {
57 RefPtr<UntrustedModulesBackupService> instance(
58 new UntrustedModulesBackupService());
60 auto setClearOnShutdown = [ptr = &sInstance]() -> void {
61 ClearOnShutdown(ptr);
64 if (NS_IsMainThread()) {
65 setClearOnShutdown();
66 return instance.forget();
69 SchedulerGroup::Dispatch(NS_NewRunnableFunction(
70 "mozilla::UntrustedModulesBackupService::Get",
71 std::move(setClearOnShutdown)));
73 return instance.forget();
74 }());
76 return sInstance;
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()) {
88 if (!iter.Data()) {
89 continue;
91 mSettled.AddWithoutStacks(std::move(iter.Data()->mData));
95 } // namespace mozilla