2016-06-25 02:01:00 +03:00
|
|
|
/* -*- 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 http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "nsITelemetry.h"
|
|
|
|
#include "nsVersionComparator.h"
|
2016-11-18 17:51:58 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2016-06-25 02:01:00 +03:00
|
|
|
|
|
|
|
#include "TelemetryCommon.h"
|
2017-05-23 09:48:13 +03:00
|
|
|
#include "TelemetryProcessData.h"
|
2016-06-25 02:01:00 +03:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace Telemetry {
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsExpiredVersion(const char* aExpiration)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aExpiration);
|
2017-02-08 20:43:27 +03:00
|
|
|
// Note: We intentionally don't construct a static Version object here as we
|
|
|
|
// saw odd crashes around this (see bug 1334105).
|
2016-06-25 02:01:00 +03:00
|
|
|
return strcmp(aExpiration, "never") && strcmp(aExpiration, "default") &&
|
2017-02-08 20:43:27 +03:00
|
|
|
(mozilla::Version(aExpiration) <= MOZ_APP_VERSION);
|
2016-06-25 02:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsInDataset(uint32_t aDataset, uint32_t aContainingDataset)
|
|
|
|
{
|
|
|
|
if (aDataset == aContainingDataset) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "optin on release channel" dataset is a superset of the
|
|
|
|
// "optout on release channel one".
|
|
|
|
if (aContainingDataset == nsITelemetry::DATASET_RELEASE_CHANNEL_OPTIN &&
|
|
|
|
aDataset == nsITelemetry::DATASET_RELEASE_CHANNEL_OPTOUT) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CanRecordDataset(uint32_t aDataset, bool aCanRecordBase, bool aCanRecordExtended)
|
|
|
|
{
|
|
|
|
// If we are extended telemetry is enabled, we are allowed to record
|
|
|
|
// regardless of the dataset.
|
|
|
|
if (aCanRecordExtended) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If base telemetry data is enabled and we're trying to record base
|
|
|
|
// telemetry, allow it.
|
|
|
|
if (aCanRecordBase &&
|
|
|
|
IsInDataset(aDataset, nsITelemetry::DATASET_RELEASE_CHANNEL_OPTOUT)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're not recording extended telemetry or this is not the base
|
|
|
|
// dataset. Bail out.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-06 18:12:49 +03:00
|
|
|
bool
|
|
|
|
CanRecordInProcess(RecordedProcessType processes, GeckoProcessType processType)
|
|
|
|
{
|
|
|
|
bool recordAllChild = !!(processes & RecordedProcessType::AllChilds);
|
|
|
|
// We can use (1 << ProcessType) due to the way RecordedProcessType is defined.
|
|
|
|
bool canRecordProcess =
|
|
|
|
!!(processes & static_cast<RecordedProcessType>(1 << processType));
|
|
|
|
|
|
|
|
return canRecordProcess ||
|
|
|
|
((processType != GeckoProcessType_Default) && recordAllChild);
|
|
|
|
}
|
|
|
|
|
2017-05-23 09:48:13 +03:00
|
|
|
bool
|
|
|
|
CanRecordInProcess(RecordedProcessType processes, ProcessID processId)
|
|
|
|
{
|
|
|
|
return CanRecordInProcess(processes, GetGeckoProcessType(processId));
|
|
|
|
}
|
|
|
|
|
2016-11-18 17:51:58 +03:00
|
|
|
nsresult
|
|
|
|
MsSinceProcessStart(double* aResult)
|
|
|
|
{
|
|
|
|
bool error;
|
|
|
|
*aResult = (TimeStamp::NowLoRes() -
|
2017-04-26 07:55:54 +03:00
|
|
|
TimeStamp::ProcessCreation(&error)).ToMilliseconds();
|
2016-11-18 17:51:58 +03:00
|
|
|
if (error) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LogToBrowserConsole(uint32_t aLogLevel, const nsAString& aMsg)
|
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
nsString msg(aMsg);
|
2017-06-12 22:34:10 +03:00
|
|
|
nsCOMPtr<nsIRunnable> task = NS_NewRunnableFunction(
|
|
|
|
"Telemetry::Common::LogToBrowserConsole",
|
|
|
|
[aLogLevel, msg]() { LogToBrowserConsole(aLogLevel, msg); });
|
2016-11-18 17:51:58 +03:00
|
|
|
NS_DispatchToMainThread(task.forget(), NS_DISPATCH_NORMAL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIConsoleService> console(do_GetService("@mozilla.org/consoleservice;1"));
|
|
|
|
if (!console) {
|
|
|
|
NS_WARNING("Failed to log message to console.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptError> error(do_CreateInstance(NS_SCRIPTERROR_CONTRACTID));
|
|
|
|
error->Init(aMsg, EmptyString(), EmptyString(), 0, 0, aLogLevel, "chrome javascript");
|
|
|
|
console->LogMessage(error);
|
|
|
|
}
|
|
|
|
|
2017-05-23 09:48:13 +03:00
|
|
|
const char*
|
|
|
|
GetNameForProcessID(ProcessID process)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(process < ProcessID::Count);
|
|
|
|
return ProcessIDToString[static_cast<uint32_t>(process)];
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoProcessType
|
|
|
|
GetGeckoProcessType(ProcessID process)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(process < ProcessID::Count);
|
|
|
|
return ProcessIDToGeckoProcessType[static_cast<uint32_t>(process)];
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:05:40 +03:00
|
|
|
bool
|
|
|
|
IsStringCharValid(const char aChar, const bool aAllowInfixPeriod,
|
|
|
|
const bool aAllowInfixUnderscore)
|
|
|
|
{
|
|
|
|
return (aChar >= 'A' && aChar <= 'Z')
|
|
|
|
|| (aChar >= 'a' && aChar <= 'z')
|
|
|
|
|| (aChar >= '0' && aChar <= '9')
|
|
|
|
|| (aAllowInfixPeriod && (aChar == '.'))
|
|
|
|
|| (aAllowInfixUnderscore && (aChar == '_'));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsValidIdentifierString(const nsACString& aStr, const size_t aMaxLength,
|
|
|
|
const bool aAllowInfixPeriod, const bool aAllowInfixUnderscore)
|
|
|
|
{
|
|
|
|
// Check string length.
|
|
|
|
if (aStr.Length() > aMaxLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check string characters.
|
|
|
|
const char* first = aStr.BeginReading();
|
|
|
|
const char* end = aStr.EndReading();
|
|
|
|
|
|
|
|
for (const char* cur = first; cur < end; ++cur) {
|
|
|
|
const bool infix = (cur != first) && (cur != (end - 1));
|
|
|
|
if (!IsStringCharValid(*cur,
|
|
|
|
aAllowInfixPeriod && infix,
|
|
|
|
aAllowInfixUnderscore && infix)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-25 02:01:00 +03:00
|
|
|
} // namespace Common
|
|
|
|
} // namespace Telemetry
|
|
|
|
} // namespace mozilla
|