2019-04-12 01:15:07 +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 "ContentBlockingLog.h"
|
|
|
|
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 10:46:35 +03:00
|
|
|
#include "nsStringStream.h"
|
2019-05-24 20:08:23 +03:00
|
|
|
#include "nsTArray.h"
|
2019-05-03 18:37:14 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2019-05-24 20:08:23 +03:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2019-04-12 01:15:07 +03:00
|
|
|
#include "mozilla/RandomNum.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
2019-05-03 18:37:14 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2019-04-12 01:15:07 +03:00
|
|
|
#include "mozilla/XorShift128PlusRNG.h"
|
2019-05-31 18:13:33 +03:00
|
|
|
#include "mozilla/ipc/IPCStreamUtils.h"
|
|
|
|
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 10:46:35 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using ipc::AutoIPCStream;
|
2019-04-12 01:15:07 +03:00
|
|
|
|
2019-05-03 18:37:14 +03:00
|
|
|
static LazyLogModule gContentBlockingLog("ContentBlockingLog");
|
|
|
|
#define LOG(fmt, ...) \
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 10:46:35 +03:00
|
|
|
MOZ_LOG(gContentBlockingLog, LogLevel::Debug, (fmt, ##__VA_ARGS__))
|
2019-05-03 18:37:14 +03:00
|
|
|
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 10:46:35 +03:00
|
|
|
typedef Telemetry::OriginMetricID OriginMetricID;
|
2019-05-03 18:37:14 +03:00
|
|
|
|
2019-04-12 01:15:07 +03:00
|
|
|
namespace dom {
|
|
|
|
|
2019-05-25 20:21:26 +03:00
|
|
|
// sync with TelemetryOriginData.inc
|
|
|
|
NS_NAMED_LITERAL_CSTRING(ContentBlockingLog::kDummyOriginHash, "PAGELOAD");
|
|
|
|
|
2019-04-12 01:15:07 +03:00
|
|
|
// randomly choose 1% users included in the content blocking measurement
|
|
|
|
// based on their client id.
|
|
|
|
static constexpr double kRatioReportUser = 0.01;
|
|
|
|
|
|
|
|
// randomly choose 0.14% documents when the page is unload.
|
|
|
|
static constexpr double kRatioReportDocument = 0.0014;
|
|
|
|
|
|
|
|
static bool IsReportingPerUserEnabled() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
static Maybe<bool> sIsReportingEnabled;
|
|
|
|
|
|
|
|
if (sIsReportingEnabled.isSome()) {
|
|
|
|
return sIsReportingEnabled.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString cachedClientId;
|
|
|
|
if (NS_FAILED(Preferences::GetCString("toolkit.telemetry.cachedClientID",
|
|
|
|
cachedClientId))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsID clientId;
|
|
|
|
if (!clientId.Parse(cachedClientId.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UUID might not be uniform-distributed (although some part of it could be).
|
|
|
|
* In order to generate more random result, usually we use a hash function,
|
|
|
|
* but here we hope it's fast and doesn't have to be cryptographic-safe.
|
|
|
|
* |XorShift128PlusRNG| looks like a good alternative because it takes a
|
|
|
|
* 128-bit data as its seed and always generate identical sequence if the
|
|
|
|
* initial seed is the same.
|
|
|
|
*/
|
|
|
|
static_assert(sizeof(nsID) == 16, "nsID is 128-bit");
|
|
|
|
uint64_t* init = reinterpret_cast<uint64_t*>(&clientId);
|
|
|
|
non_crypto::XorShift128PlusRNG rng(init[0], init[1]);
|
|
|
|
sIsReportingEnabled.emplace(rng.nextDouble() <= kRatioReportUser);
|
|
|
|
|
|
|
|
return sIsReportingEnabled.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsReportingPerDocumentEnabled() {
|
|
|
|
constexpr double boundary =
|
|
|
|
kRatioReportDocument * std::numeric_limits<uint64_t>::max();
|
|
|
|
Maybe<uint64_t> randomNum = RandomUint64();
|
|
|
|
return randomNum.isSome() && randomNum.value() <= boundary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsReportingEnabled() {
|
|
|
|
if (StaticPrefs::telemetry_origin_telemetry_test_mode_enabled()) {
|
|
|
|
return true;
|
|
|
|
} else if (!StaticPrefs::
|
|
|
|
privacy_trackingprotection_origin_telemetry_enabled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IsReportingPerUserEnabled() && IsReportingPerDocumentEnabled();
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:37:14 +03:00
|
|
|
static void ReportOriginSingleHash(OriginMetricID aId,
|
|
|
|
const nsACString& aOrigin) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
LOG("ReportOriginSingleHash metric=%s",
|
|
|
|
Telemetry::MetricIDToString[static_cast<uint32_t>(aId)]);
|
|
|
|
LOG("ReportOriginSingleHash origin=%s", PromiseFlatCString(aOrigin).get());
|
|
|
|
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
Telemetry::RecordOrigin(aId, aOrigin);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::ContentChild* contentChild = dom::ContentChild::GetSingleton();
|
|
|
|
if (NS_WARN_IF(!contentChild)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unused << contentChild->SendRecordOrigin(static_cast<uint32_t>(aId),
|
|
|
|
nsCString(aOrigin));
|
|
|
|
}
|
|
|
|
|
2019-05-31 18:13:33 +03:00
|
|
|
void ContentBlockingLog::ReportLog(nsIPrincipal* aFirstPartyPrincipal) {
|
2019-04-12 01:15:07 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-05-31 18:13:33 +03:00
|
|
|
MOZ_ASSERT(aFirstPartyPrincipal);
|
|
|
|
|
|
|
|
if (!StaticPrefs::browser_contentblocking_database_enabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLog.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::ContentChild* contentChild = dom::ContentChild::GetSingleton();
|
|
|
|
if (NS_WARN_IF(!contentChild)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString json = Stringify();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
nsresult rv = NS_NewCStringInputStream(getter_AddRefs(stream), json);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoIPCStream ipcStream;
|
|
|
|
ipcStream.Serialize(stream, contentChild);
|
|
|
|
|
2019-06-22 01:45:47 +03:00
|
|
|
Unused << contentChild->SendReportContentBlockingLog(ipcStream.TakeValue());
|
2019-05-31 18:13:33 +03:00
|
|
|
}
|
2019-04-12 01:15:07 +03:00
|
|
|
|
2019-05-31 18:13:33 +03:00
|
|
|
void ContentBlockingLog::ReportOrigins() {
|
2019-04-12 01:15:07 +03:00
|
|
|
if (!IsReportingEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-31 18:13:33 +03:00
|
|
|
LOG("ContentBlockingLog::ReportOrigins [this=%p]", this);
|
2019-05-25 20:21:26 +03:00
|
|
|
const bool testMode =
|
|
|
|
StaticPrefs::telemetry_origin_telemetry_test_mode_enabled();
|
|
|
|
OriginMetricID metricId =
|
|
|
|
testMode ? OriginMetricID::ContentBlocking_Blocked_TestOnly
|
|
|
|
: OriginMetricID::ContentBlocking_Blocked;
|
|
|
|
ReportOriginSingleHash(metricId, kDummyOriginHash);
|
2019-04-12 01:15:07 +03:00
|
|
|
|
2019-05-24 20:08:23 +03:00
|
|
|
nsTArray<HashNumber> lookupTable;
|
2019-04-12 01:15:07 +03:00
|
|
|
for (const auto& originEntry : mLog) {
|
|
|
|
if (!originEntry.mData) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& logEntry : Reversed(originEntry.mData->mLogs)) {
|
|
|
|
if (logEntry.mType !=
|
2019-05-03 18:37:14 +03:00
|
|
|
nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER ||
|
|
|
|
logEntry.mTrackingFullHashes.IsEmpty()) {
|
2019-04-12 01:15:07 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-12 03:38:57 +03:00
|
|
|
const bool isBlocked = logEntry.mBlocked;
|
|
|
|
Maybe<StorageAccessGrantedReason> reason = logEntry.mReason;
|
|
|
|
|
2019-05-25 20:21:26 +03:00
|
|
|
metricId = testMode ? OriginMetricID::ContentBlocking_Blocked_TestOnly
|
|
|
|
: OriginMetricID::ContentBlocking_Blocked;
|
2019-04-12 03:38:57 +03:00
|
|
|
if (!isBlocked) {
|
|
|
|
MOZ_ASSERT(reason.isSome());
|
|
|
|
switch (reason.value()) {
|
|
|
|
case StorageAccessGrantedReason::eStorageAccessAPI:
|
|
|
|
metricId =
|
|
|
|
testMode
|
|
|
|
? OriginMetricID::
|
|
|
|
ContentBlocking_StorageAccessAPIExempt_TestOnly
|
|
|
|
: OriginMetricID::ContentBlocking_StorageAccessAPIExempt;
|
|
|
|
break;
|
|
|
|
case StorageAccessGrantedReason::eOpenerAfterUserInteraction:
|
|
|
|
metricId =
|
|
|
|
testMode
|
|
|
|
? OriginMetricID::
|
|
|
|
ContentBlocking_OpenerAfterUserInteractionExempt_TestOnly
|
|
|
|
: OriginMetricID::
|
|
|
|
ContentBlocking_OpenerAfterUserInteractionExempt;
|
|
|
|
break;
|
|
|
|
case StorageAccessGrantedReason::eOpener:
|
|
|
|
metricId =
|
|
|
|
testMode ? OriginMetricID::ContentBlocking_OpenerExempt_TestOnly
|
|
|
|
: OriginMetricID::ContentBlocking_OpenerExempt;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown StorageAccessGrantedReason");
|
|
|
|
}
|
2019-04-12 01:15:07 +03:00
|
|
|
}
|
2019-04-12 03:38:57 +03:00
|
|
|
|
2019-05-24 20:08:23 +03:00
|
|
|
for (const auto& hash : logEntry.mTrackingFullHashes) {
|
|
|
|
HashNumber key = AddToHash(HashString(hash.get(), hash.Length()),
|
|
|
|
static_cast<uint32_t>(metricId));
|
|
|
|
if (lookupTable.Contains(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lookupTable.AppendElement(key);
|
2019-05-03 18:37:14 +03:00
|
|
|
ReportOriginSingleHash(metricId, hash);
|
|
|
|
}
|
2019-04-12 01:15:07 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|