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/. */
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 ////////////////////////////////////////////////////////////////////////
22 struct UserInteractionKey
{
28 ////////////////////////////////////////////////////////////////////////
29 ////////////////////////////////////////////////////////////////////////
31 // PRIVATE STATE, SHARED BY ALL THREADS
34 // Set to true once this global state has been initialized.
35 bool gTelemetryUserInteractionInitDone
= false;
37 bool gTelemetryUserInteractionCanRecord
;
41 // Implements the methods for UserInteractionInfo.
42 const char* UserInteractionInfo::name() const {
43 return &gUserInteractionsStringTable
[this->name_offset
];
48 ////////////////////////////////////////////////////////////////////////
49 ////////////////////////////////////////////////////////////////////////
51 // EXTERNALLY VISIBLE FUNCTIONS in namespace TelemetryUserInteraction::
53 void TelemetryUserInteraction::InitializeGlobalState(bool aCanRecord
) {
54 if (!XRE_IsParentProcess()) {
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()) {
72 MOZ_ASSERT(NS_IsMainThread());
73 MOZ_ASSERT(gTelemetryUserInteractionInitDone
);
74 if (!gTelemetryUserInteractionInitDone
) {
78 gTelemetryUserInteractionInitDone
= false;
81 bool TelemetryUserInteraction::CanRecord(const nsAString
& aName
) {
82 if (!gTelemetryUserInteractionCanRecord
) {
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())) {