summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/other/UntrustedModulesBackupService.cpp
blob: a0638e5aecc3e6ef87e421402999f049601de8ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "UntrustedModulesBackupService.h"

#include "mozilla/ClearOnShutdown.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/SchedulerGroup.h"
#include "mozilla/StaticLocalPtr.h"

namespace mozilla {

ProcessHashKey::ProcessHashKey(GeckoProcessType aType, DWORD aPid)
    : mType(aType), mPid(aPid) {}

bool ProcessHashKey::operator==(const ProcessHashKey& aOther) const {
  return mPid == aOther.mPid && mType == aOther.mType;
}

PLDHashNumber ProcessHashKey::Hash() const { return HashGeneric(mPid, mType); }

void UntrustedModulesBackupData::Add(UntrustedModulesData&& aData) {
  WithEntryHandle(
      ProcessHashKey(aData.mProcessType, aData.mPid), [&](auto&& p) {
        if (p) {
          p.Data()->mData.Merge(std::move(aData));
        } else {
          p.Insert(MakeRefPtr<UntrustedModulesDataContainer>(std::move(aData)));
        }
      });
}

void UntrustedModulesBackupData::AddWithoutStacks(
    UntrustedModulesData&& aData) {
  WithEntryHandle(
      ProcessHashKey(aData.mProcessType, aData.mPid), [&](auto&& p) {
        if (p) {
          p.Data()->mData.MergeWithoutStacks(std::move(aData));
        } else {
          aData.Truncate(true);
          p.Insert(MakeRefPtr<UntrustedModulesDataContainer>(std::move(aData)));
        }
      });
}

/* static */
UntrustedModulesBackupService* UntrustedModulesBackupService::Get() {
  if (!XRE_IsParentProcess()) {
    return nullptr;
  }

  static StaticLocalRefPtr<UntrustedModulesBackupService> sInstance(
      []() -> already_AddRefed<UntrustedModulesBackupService> {
        RefPtr<UntrustedModulesBackupService> instance(
            new UntrustedModulesBackupService());

        auto setClearOnShutdown = [ptr = &sInstance]() -> void {
          ClearOnShutdown(ptr);
        };

        if (NS_IsMainThread()) {
          setClearOnShutdown();
          return instance.forget();
        }

        SchedulerGroup::Dispatch(
            TaskCategory::Other,
            NS_NewRunnableFunction(
                "mozilla::UntrustedModulesBackupService::Get",
                std::move(setClearOnShutdown)));

        return instance.forget();
      }());

  return sInstance;
}

void UntrustedModulesBackupService::Backup(UntrustedModulesData&& aData) {
  mStaging.Add(std::move(aData));
}

void UntrustedModulesBackupService::SettleAllStagingData() {
  UntrustedModulesBackupData staging;
  staging.SwapElements(mStaging);

  for (auto&& iter = staging.Iter(); !iter.Done(); iter.Next()) {
    if (!iter.Data()) {
      continue;
    }
    mSettled.AddWithoutStacks(std::move(iter.Data()->mData));
  }
}

}  // namespace mozilla