Backed out 7 changesets (bug 1942424) for causing frequent crashes. a=backout
[gecko.git] / toolkit / components / telemetry / core / TelemetryCommon.h
blob47feef94d26396fc78d116dc27d6200301ab6d67
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef TelemetryCommon_h__
7 #define TelemetryCommon_h__
9 #include "PLDHashTable.h"
10 #include "js/RootingAPI.h"
11 #include "js/TypeDecls.h"
12 #include "mozilla/TypedEnumBits.h"
13 #include "mozilla/TelemetryProcessEnums.h"
14 #include "nsHashtablesFwd.h"
15 #include "nsTHashSet.h"
16 #include "nsTHashtable.h"
17 #include "nsIScriptError.h"
18 #include "nsXULAppAPI.h"
20 namespace mozilla {
21 namespace Telemetry {
22 namespace Common {
24 typedef nsTHashSet<nsCString> StringHashSet;
26 enum class RecordedProcessType : uint16_t {
27 Main = (1 << GeckoProcessType_Default), // Also known as "parent process"
28 Content = (1 << GeckoProcessType_Content),
29 Gpu = (1 << GeckoProcessType_GPU),
30 Rdd = (1 << GeckoProcessType_RDD),
31 Socket = (1 << GeckoProcessType_Socket),
32 Utility = (1 << GeckoProcessType_Utility),
33 AllChildren = 0xFFFF - 1, // All the child processes (i.e. content, gpu, ...)
34 // Always `All-Main` to allow easy matching.
35 All = 0xFFFF // All the processes
37 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(RecordedProcessType);
38 static_assert(static_cast<uint16_t>(RecordedProcessType::Main) == 1,
39 "Main process type must be equal to 1 to allow easy matching in "
40 "CanRecordInProcess");
42 enum class SupportedProduct : uint8_t {
43 Firefox = (1 << 0),
44 Fennec = (1 << 1),
45 // Note that `1 << 2` and `1 << 3` (former GeckoView, GeckoviewStreaming) are
46 // missing in the representation. We see no point in filling it at this time.
47 Thunderbird = (1 << 4),
49 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(SupportedProduct);
51 template <class EntryType>
52 class AutoHashtable : public nsTHashtable<EntryType> {
53 public:
54 explicit AutoHashtable(
55 uint32_t initLength = PLDHashTable::kDefaultInitialLength);
56 typedef bool (*ReflectEntryFunc)(EntryType* entry, JSContext* cx,
57 JS::Handle<JSObject*> obj);
58 bool ReflectIntoJS(ReflectEntryFunc entryFunc, JSContext* cx,
59 JS::Handle<JSObject*> obj);
62 template <class EntryType>
63 AutoHashtable<EntryType>::AutoHashtable(uint32_t initLength)
64 : nsTHashtable<EntryType>(initLength) {}
66 /**
67 * Reflect the individual entries of table into JS, usually by defining
68 * some property and value of obj. entryFunc is called for each entry.
70 template <typename EntryType>
71 bool AutoHashtable<EntryType>::ReflectIntoJS(ReflectEntryFunc entryFunc,
72 JSContext* cx,
73 JS::Handle<JSObject*> obj) {
74 for (auto iter = this->Iter(); !iter.Done(); iter.Next()) {
75 if (!entryFunc(iter.Get(), cx, obj)) {
76 return false;
79 return true;
82 bool IsExpiredVersion(const char* aExpiration);
83 bool IsInDataset(uint32_t aDataset, uint32_t aContainingDataset);
84 bool CanRecordDataset(uint32_t aDataset, bool aCanRecordBase,
85 bool aCanRecordExtended);
86 bool CanRecordInProcess(RecordedProcessType aProcesses,
87 GeckoProcessType aProcess);
88 bool CanRecordInProcess(RecordedProcessType aProcesses, ProcessID aProcess);
89 bool CanRecordProduct(SupportedProduct aProducts);
91 /**
92 * Return the number of milliseconds since process start using monotonic
93 * timestamps (unaffected by system clock changes). Depending on the platform,
94 * this can include the time the device was suspended (Windows) or not (Linux,
95 * macOS).
97 * @return NS_OK on success.
99 nsresult MsSinceProcessStart(double* aResult);
102 * Return the number of milliseconds since process start using monotonic
103 * timestamps (unaffected by system clock changes), including the time the
104 * system was suspended.
106 * @return NS_OK on success, NS_ERROR_NOT_AVAILABLE if the data is unavailable
107 * (this can happen on old operating systems).
109 nsresult MsSinceProcessStartIncludingSuspend(double* aResult);
112 * Return the number of milliseconds since process start using monotonic
113 * timestamps (unaffected by system clock changes), excluding the time the
114 * system was suspended.
116 * @return NS_OK on success, NS_ERROR_NOT_AVAILABLE if the data is unavailable
117 * (this can happen on old operating systems).
119 nsresult MsSinceProcessStartExcludingSuspend(double* aResult);
122 * Dumps a log message to the Browser Console using the provided level.
124 * @param aLogLevel The level to use when displaying the message in the browser
125 * console (e.g. nsIScriptError::warningFlag, ...).
126 * @param aMsg The text message to print to the console.
128 void LogToBrowserConsole(uint32_t aLogLevel, const nsAString& aMsg);
131 * Get the name string for a ProcessID.
132 * This is the name we use for the Telemetry payloads.
134 const char* GetNameForProcessID(ProcessID process);
137 * Get the process id give a process name.
139 * @param aProcessName - the name of the process.
140 * @returns {ProcessID} one value from ProcessID::* or ProcessID::Count if the
141 * name of the process was not found.
143 ProcessID GetIDForProcessName(const char* aProcessName);
146 * Get the GeckoProcessType for a ProcessID.
147 * Telemetry distinguishes between more process types than the GeckoProcessType,
148 * so the mapping is not direct.
150 GeckoProcessType GetGeckoProcessType(ProcessID process);
153 * Check if the passed telemetry identifier is valid.
155 * @param aStr The string identifier.
156 * @param aMaxLength The maximum length of the identifier.
157 * @param aAllowInfixPeriod Whether or not to allow infix dots.
158 * @param aAllowInfixUnderscore Whether or not to allow infix underscores.
159 * @returns true if the string validates correctly, false otherwise.
161 bool IsValidIdentifierString(const nsACString& aStr, const size_t aMaxLength,
162 const bool aAllowInfixPeriod,
163 const bool aAllowInfixUnderscore);
166 * Convert the given UTF8 string to a JavaScript string. The returned
167 * string's contents will be the UTF16 conversion of the given string.
169 * @param cx The JS context.
170 * @param aStr The UTF8 string.
171 * @returns a JavaScript string.
173 JSString* ToJSString(JSContext* cx, const nsACString& aStr);
176 * Convert the given UTF16 string to a JavaScript string.
178 * @param cx The JS context.
179 * @param aStr The UTF16 string.
180 * @returns a JavaScript string.
182 JSString* ToJSString(JSContext* cx, const nsAString& aStr);
185 * Get an identifier for the currently-running product.
186 * This is not stable over time and may change.
188 * @returns the product identifier
190 SupportedProduct GetCurrentProduct();
192 } // namespace Common
193 } // namespace Telemetry
194 } // namespace mozilla
196 #endif // TelemetryCommon_h__