Bug 1943650 - Command-line --help output misformatted after --dbus-service. r=emilio
[gecko.git] / toolkit / components / telemetry / core / TelemetryUserInteraction.cpp
blob3de53147975912c20a9782e7cafd5cae225db296
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 http://mozilla.org/MPL/2.0/. */
7 #include "nsString.h"
8 #include "MainThreadUtils.h"
9 #include "TelemetryUserInteraction.h"
10 #include "TelemetryUserInteractionData.h"
11 #include "TelemetryUserInteractionNameMap.h"
12 #include "UserInteractionInfo.h"
14 using mozilla::Telemetry::UserInteractionIDByNameLookup;
16 ////////////////////////////////////////////////////////////////////////
17 ////////////////////////////////////////////////////////////////////////
19 // PRIVATE TYPES
20 namespace {
22 struct UserInteractionKey {
23 uint32_t id;
26 } // namespace
28 ////////////////////////////////////////////////////////////////////////
29 ////////////////////////////////////////////////////////////////////////
31 // PRIVATE STATE, SHARED BY ALL THREADS
33 namespace {
34 // Set to true once this global state has been initialized.
35 bool gTelemetryUserInteractionInitDone = false;
37 bool gTelemetryUserInteractionCanRecord;
38 } // namespace
40 namespace {
41 // Implements the methods for UserInteractionInfo.
42 const char* UserInteractionInfo::name() const {
43 return &gUserInteractionsStringTable[this->name_offset];
46 } // namespace
48 ////////////////////////////////////////////////////////////////////////
49 ////////////////////////////////////////////////////////////////////////
51 // EXTERNALLY VISIBLE FUNCTIONS in namespace TelemetryUserInteraction::
53 void TelemetryUserInteraction::InitializeGlobalState(bool aCanRecord) {
54 if (!XRE_IsParentProcess()) {
55 return;
58 MOZ_ASSERT(NS_IsMainThread());
59 MOZ_ASSERT(!gTelemetryUserInteractionInitDone,
60 "TelemetryUserInteraction::InitializeGlobalState "
61 "may only be called once");
63 gTelemetryUserInteractionCanRecord = aCanRecord;
64 gTelemetryUserInteractionInitDone = true;
67 void TelemetryUserInteraction::DeInitializeGlobalState() {
68 if (!XRE_IsParentProcess()) {
69 return;
72 MOZ_ASSERT(NS_IsMainThread());
73 MOZ_ASSERT(gTelemetryUserInteractionInitDone);
74 if (!gTelemetryUserInteractionInitDone) {
75 return;
78 gTelemetryUserInteractionInitDone = false;
81 bool TelemetryUserInteraction::CanRecord(const nsAString& aName) {
82 if (!gTelemetryUserInteractionCanRecord) {
83 return false;
86 nsCString name = NS_ConvertUTF16toUTF8(aName);
87 const uint32_t idx = UserInteractionIDByNameLookup(name);
89 MOZ_DIAGNOSTIC_ASSERT(
90 idx < mozilla::Telemetry::UserInteractionID::UserInteractionCount,
91 "Intermediate lookup should always give a valid index.");
93 if (name.Equals(gUserInteractions[idx].name())) {
94 return true;
97 return false;