Line data Source code
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/. */
6 :
7 : #include "nsITelemetry.h"
8 : #include "nsVersionComparator.h"
9 : #include "mozilla/TimeStamp.h"
10 : #include "nsIConsoleService.h"
11 : #include "nsThreadUtils.h"
12 :
13 : #include "TelemetryCommon.h"
14 : #include "TelemetryProcessData.h"
15 :
16 : #include <cstring>
17 :
18 : namespace mozilla {
19 : namespace Telemetry {
20 : namespace Common {
21 :
22 : bool
23 9774 : IsExpiredVersion(const char* aExpiration)
24 : {
25 9774 : MOZ_ASSERT(aExpiration);
26 : // Note: We intentionally don't construct a static Version object here as we
27 : // saw odd crashes around this (see bug 1334105).
28 15442 : return strcmp(aExpiration, "never") && strcmp(aExpiration, "default") &&
29 15442 : (mozilla::Version(aExpiration) <= MOZ_APP_VERSION);
30 : }
31 :
32 : bool
33 3589 : IsInDataset(uint32_t aDataset, uint32_t aContainingDataset)
34 : {
35 3589 : if (aDataset == aContainingDataset) {
36 23 : return true;
37 : }
38 :
39 : // The "optin on release channel" dataset is a superset of the
40 : // "optout on release channel one".
41 3566 : if (aContainingDataset == nsITelemetry::DATASET_RELEASE_CHANNEL_OPTIN &&
42 : aDataset == nsITelemetry::DATASET_RELEASE_CHANNEL_OPTOUT) {
43 0 : return true;
44 : }
45 :
46 3566 : return false;
47 : }
48 :
49 : bool
50 3655 : CanRecordDataset(uint32_t aDataset, bool aCanRecordBase, bool aCanRecordExtended)
51 : {
52 : // If we are extended telemetry is enabled, we are allowed to record
53 : // regardless of the dataset.
54 3655 : if (aCanRecordExtended) {
55 66 : return true;
56 : }
57 :
58 : // If base telemetry data is enabled and we're trying to record base
59 : // telemetry, allow it.
60 7178 : if (aCanRecordBase &&
61 3589 : IsInDataset(aDataset, nsITelemetry::DATASET_RELEASE_CHANNEL_OPTOUT)) {
62 23 : return true;
63 : }
64 :
65 : // We're not recording extended telemetry or this is not the base
66 : // dataset. Bail out.
67 3566 : return false;
68 : }
69 :
70 : bool
71 5193 : CanRecordInProcess(RecordedProcessType processes, GeckoProcessType processType)
72 : {
73 5193 : bool recordAllChild = !!(processes & RecordedProcessType::AllChilds);
74 : // We can use (1 << ProcessType) due to the way RecordedProcessType is defined.
75 : bool canRecordProcess =
76 5193 : !!(processes & static_cast<RecordedProcessType>(1 << processType));
77 :
78 10318 : return canRecordProcess ||
79 5241 : ((processType != GeckoProcessType_Default) && recordAllChild);
80 : }
81 :
82 : bool
83 0 : CanRecordInProcess(RecordedProcessType processes, ProcessID processId)
84 : {
85 0 : return CanRecordInProcess(processes, GetGeckoProcessType(processId));
86 : }
87 :
88 : nsresult
89 6 : MsSinceProcessStart(double* aResult)
90 : {
91 : bool error;
92 12 : *aResult = (TimeStamp::NowLoRes() -
93 18 : TimeStamp::ProcessCreation(&error)).ToMilliseconds();
94 6 : if (error) {
95 0 : return NS_ERROR_NOT_AVAILABLE;
96 : }
97 6 : return NS_OK;
98 : }
99 :
100 : void
101 0 : LogToBrowserConsole(uint32_t aLogLevel, const nsAString& aMsg)
102 : {
103 0 : if (!NS_IsMainThread()) {
104 0 : nsString msg(aMsg);
105 0 : nsCOMPtr<nsIRunnable> task = NS_NewRunnableFunction(
106 : "Telemetry::Common::LogToBrowserConsole",
107 0 : [aLogLevel, msg]() { LogToBrowserConsole(aLogLevel, msg); });
108 0 : NS_DispatchToMainThread(task.forget(), NS_DISPATCH_NORMAL);
109 0 : return;
110 : }
111 :
112 0 : nsCOMPtr<nsIConsoleService> console(do_GetService("@mozilla.org/consoleservice;1"));
113 0 : if (!console) {
114 0 : NS_WARNING("Failed to log message to console.");
115 0 : return;
116 : }
117 :
118 0 : nsCOMPtr<nsIScriptError> error(do_CreateInstance(NS_SCRIPTERROR_CONTRACTID));
119 0 : error->Init(aMsg, EmptyString(), EmptyString(), 0, 0, aLogLevel, "chrome javascript");
120 0 : console->LogMessage(error);
121 : }
122 :
123 : const char*
124 0 : GetNameForProcessID(ProcessID process)
125 : {
126 0 : MOZ_ASSERT(process < ProcessID::Count);
127 0 : return ProcessIDToString[static_cast<uint32_t>(process)];
128 : }
129 :
130 : GeckoProcessType
131 0 : GetGeckoProcessType(ProcessID process)
132 : {
133 0 : MOZ_ASSERT(process < ProcessID::Count);
134 0 : return ProcessIDToGeckoProcessType[static_cast<uint32_t>(process)];
135 : }
136 :
137 : } // namespace Common
138 : } // namespace Telemetry
139 : } // namespace mozilla
|