2015-10-30 05:53:25 +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 "mozilla/ConsoleReportCollector.h"
|
2020-03-04 18:12:05 +03:00
|
|
|
#include "mozilla/net/NeckoChannelParams.h"
|
2015-10-30 05:53:25 +03:00
|
|
|
|
2018-01-12 20:16:04 +03:00
|
|
|
#include "ConsoleUtils.h"
|
2016-05-23 09:56:46 +03:00
|
|
|
#include "nsIScriptError.h"
|
2015-10-30 05:53:25 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2018-01-12 20:16:04 +03:00
|
|
|
using mozilla::dom::ConsoleUtils;
|
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
NS_IMPL_ISUPPORTS(ConsoleReportCollector, nsIConsoleReportCollector)
|
|
|
|
|
|
|
|
ConsoleReportCollector::ConsoleReportCollector()
|
|
|
|
: mMutex("mozilla::ConsoleReportCollector") {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
void ConsoleReportCollector::AddConsoleReport(
|
|
|
|
uint32_t aErrorFlags, const nsACString& aCategory,
|
|
|
|
nsContentUtils::PropertiesFile aPropertiesFile,
|
|
|
|
const nsACString& aSourceFileURI, uint32_t aLineNumber,
|
|
|
|
uint32_t aColumnNumber, const nsACString& aMessageName,
|
|
|
|
const nsTArray<nsString>& aStringParams) {
|
|
|
|
// any thread
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2020-05-06 14:49:25 +03:00
|
|
|
mPendingReports.EmplaceBack(aErrorFlags, aCategory, aPropertiesFile,
|
|
|
|
aSourceFileURI, aLineNumber, aColumnNumber,
|
|
|
|
aMessageName, aStringParams);
|
2015-10-30 05:53:25 +03:00
|
|
|
}
|
|
|
|
|
2017-02-06 04:19:34 +03:00
|
|
|
void ConsoleReportCollector::FlushReportsToConsole(uint64_t aInnerWindowID,
|
|
|
|
ReportAction aAction) {
|
2015-10-30 05:53:25 +03:00
|
|
|
nsTArray<PendingReport> reports;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2016-05-23 09:56:46 +03:00
|
|
|
if (aAction == ReportAction::Forget) {
|
2020-08-04 14:27:07 +03:00
|
|
|
reports = std::move(mPendingReports);
|
2016-05-23 09:56:46 +03:00
|
|
|
} else {
|
2020-05-06 14:49:25 +03:00
|
|
|
reports = mPendingReports.Clone();
|
2016-05-23 09:56:46 +03:00
|
|
|
}
|
2015-10-30 05:53:25 +03:00
|
|
|
}
|
2015-10-30 05:53:25 +03:00
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
for (uint32_t i = 0; i < reports.Length(); ++i) {
|
|
|
|
PendingReport& report = reports[i];
|
2015-10-30 05:53:25 +03:00
|
|
|
|
2017-08-04 07:40:52 +03:00
|
|
|
nsAutoString errorText;
|
2017-02-06 04:19:34 +03:00
|
|
|
nsresult rv;
|
|
|
|
if (!report.mStringParams.IsEmpty()) {
|
|
|
|
rv = nsContentUtils::FormatLocalizedString(
|
|
|
|
report.mPropertiesFile, report.mMessageName.get(),
|
|
|
|
report.mStringParams, errorText);
|
|
|
|
} else {
|
|
|
|
rv = nsContentUtils::GetLocalizedString(
|
|
|
|
report.mPropertiesFile, report.mMessageName.get(), errorText);
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
// It would be nice if we did not have to do this since ReportToConsole()
|
|
|
|
// just turns around and converts it back to a spec.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2016-05-23 09:56:46 +03:00
|
|
|
if (!report.mSourceFileURI.IsEmpty()) {
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), report.mSourceFileURI);
|
|
|
|
if (NS_FAILED(rv)) {
|
2020-04-16 14:39:29 +03:00
|
|
|
NS_WARNING(nsPrintfCString("Failed to transform %s to uri",
|
|
|
|
report.mSourceFileURI.get())
|
|
|
|
.get());
|
2016-05-23 09:56:46 +03:00
|
|
|
continue;
|
|
|
|
}
|
2015-10-30 05:53:25 +03:00
|
|
|
}
|
|
|
|
|
2017-02-06 04:19:34 +03:00
|
|
|
nsContentUtils::ReportToConsoleByWindowID(
|
|
|
|
errorText, report.mErrorFlags, report.mCategory, aInnerWindowID, uri,
|
|
|
|
EmptyString(), report.mLineNumber, report.mColumnNumber);
|
2015-10-30 05:53:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:16:04 +03:00
|
|
|
void ConsoleReportCollector::FlushReportsToConsoleForServiceWorkerScope(
|
|
|
|
const nsACString& aScope, ReportAction aAction) {
|
|
|
|
nsTArray<PendingReport> reports;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (aAction == ReportAction::Forget) {
|
2020-08-04 14:27:07 +03:00
|
|
|
reports = std::move(mPendingReports);
|
2018-01-12 20:16:04 +03:00
|
|
|
} else {
|
2020-05-06 14:49:25 +03:00
|
|
|
reports = mPendingReports.Clone();
|
2018-01-12 20:16:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < reports.Length(); ++i) {
|
|
|
|
PendingReport& report = reports[i];
|
|
|
|
|
|
|
|
nsAutoString errorText;
|
|
|
|
nsresult rv;
|
|
|
|
if (!report.mStringParams.IsEmpty()) {
|
|
|
|
rv = nsContentUtils::FormatLocalizedString(
|
|
|
|
report.mPropertiesFile, report.mMessageName.get(),
|
|
|
|
report.mStringParams, errorText);
|
|
|
|
} else {
|
|
|
|
rv = nsContentUtils::GetLocalizedString(
|
|
|
|
report.mPropertiesFile, report.mMessageName.get(), errorText);
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleUtils::Level level = ConsoleUtils::eLog;
|
|
|
|
switch (report.mErrorFlags) {
|
|
|
|
case nsIScriptError::errorFlag:
|
|
|
|
level = ConsoleUtils::eError;
|
|
|
|
break;
|
|
|
|
case nsIScriptError::warningFlag:
|
|
|
|
level = ConsoleUtils::eWarning;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// default to log otherwise
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleUtils::ReportForServiceWorkerScope(
|
|
|
|
NS_ConvertUTF8toUTF16(aScope), errorText,
|
|
|
|
NS_ConvertUTF8toUTF16(report.mSourceFileURI), report.mLineNumber,
|
|
|
|
report.mColumnNumber, level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void ConsoleReportCollector::FlushConsoleReports(dom::Document* aDocument,
|
2017-02-06 04:19:34 +03:00
|
|
|
ReportAction aAction) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
FlushReportsToConsole(aDocument ? aDocument->InnerWindowID() : 0, aAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleReportCollector::FlushConsoleReports(nsILoadGroup* aLoadGroup,
|
|
|
|
ReportAction aAction) {
|
|
|
|
FlushReportsToConsole(nsContentUtils::GetInnerWindowID(aLoadGroup), aAction);
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:20:56 +03:00
|
|
|
void ConsoleReportCollector::FlushConsoleReports(
|
|
|
|
nsIConsoleReportCollector* aCollector) {
|
|
|
|
MOZ_ASSERT(aCollector);
|
|
|
|
|
|
|
|
nsTArray<PendingReport> reports;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2020-08-04 14:27:07 +03:00
|
|
|
reports = std::move(mPendingReports);
|
2015-11-03 22:20:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < reports.Length(); ++i) {
|
|
|
|
PendingReport& report = reports[i];
|
2020-05-06 14:49:25 +03:00
|
|
|
aCollector->AddConsoleReport(
|
|
|
|
report.mErrorFlags, report.mCategory, report.mPropertiesFile,
|
|
|
|
report.mSourceFileURI, report.mLineNumber, report.mColumnNumber,
|
|
|
|
report.mMessageName,
|
|
|
|
static_cast<const nsTArray<nsString>&>(report.mStringParams));
|
2015-11-03 22:20:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 18:12:05 +03:00
|
|
|
void ConsoleReportCollector::StealConsoleReports(
|
|
|
|
nsTArray<net::ConsoleReportCollected>& aReports) {
|
|
|
|
aReports.Clear();
|
|
|
|
|
|
|
|
nsTArray<PendingReport> reports;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2020-08-04 14:27:07 +03:00
|
|
|
reports = std::move(mPendingReports);
|
2020-03-04 18:12:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const PendingReport& report : reports) {
|
|
|
|
aReports.AppendElement(net::ConsoleReportCollected(
|
|
|
|
report.mErrorFlags, report.mCategory, report.mPropertiesFile,
|
|
|
|
report.mSourceFileURI, report.mLineNumber, report.mColumnNumber,
|
|
|
|
report.mMessageName, report.mStringParams));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 09:56:46 +03:00
|
|
|
void ConsoleReportCollector::ClearConsoleReports() {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
mPendingReports.Clear();
|
|
|
|
}
|
|
|
|
|
2020-02-20 19:18:07 +03:00
|
|
|
ConsoleReportCollector::~ConsoleReportCollector() = default;
|
2015-10-30 05:53:25 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|