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/. */
7 #include "UrlClassifierFeatureEmailTrackingDataCollection.h"
9 #include "mozilla/AntiTrackingUtils.h"
10 #include "mozilla/ClearOnShutdown.h"
11 #include "mozilla/ContentBlockingNotifier.h"
12 #include "mozilla/dom/BrowsingContext.h"
13 #include "mozilla/dom/WindowGlobalParent.h"
14 #include "mozilla/net/UrlClassifierCommon.h"
15 #include "mozilla/StaticPrefs_privacy.h"
16 #include "mozilla/StaticPtr.h"
17 #include "mozilla/Telemetry.h"
19 #include "nsIChannel.h"
20 #include "nsIClassifiedChannel.h"
21 #include "nsILoadInfo.h"
23 namespace mozilla::net
{
27 #define EMAILTRACKING_DATACOLLECTION_FEATURE_NAME \
28 "emailtracking-data-collection"
30 #define URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_BLOCKLIST \
31 "urlclassifier.features.emailtracking.datacollection.blocklistTables"
32 #define URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_BLOCKLIST_TEST_ENTRIES \
33 "urlclassifier.features.emailtracking.datacollection.blocklistHosts"
34 #define URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_ENTITYLIST \
35 "urlclassifier.features.emailtracking.datacollection.allowlistTables"
36 #define URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_ENTITYLIST_TEST_ENTRIES \
37 "urlclassifier.features.emailtracking.datacollection.allowlistHosts"
38 #define URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_EXCEPTION_URLS \
39 "urlclassifier.features.emailtracking.datacollection.skipURLs"
40 #define TABLE_EMAILTRACKING_DATACOLLECTION_BLOCKLIST_PREF \
41 "emailtracking-data-collection-blocklist-pref"
42 #define TABLE_EMAILTRACKING_DATACOLLECTION_ENTITYLIST_PREF \
43 "emailtracking-data-collection-allowlist-pref"
45 StaticRefPtr
<UrlClassifierFeatureEmailTrackingDataCollection
>
46 gFeatureEmailTrackingDataCollection
;
47 StaticAutoPtr
<nsCString
> gEmailWebAppDomainsPref
;
48 static constexpr char kEmailWebAppDomainPrefName
[] =
49 "privacy.trackingprotection.emailtracking.webapp.domains";
51 void EmailWebAppDomainPrefChangeCallback(const char* aPrefName
, void*) {
52 MOZ_ASSERT(NS_IsMainThread());
53 MOZ_ASSERT(!strcmp(aPrefName
, kEmailWebAppDomainPrefName
));
54 MOZ_ASSERT(gEmailWebAppDomainsPref
);
56 Preferences::GetCString(kEmailWebAppDomainPrefName
, *gEmailWebAppDomainsPref
);
61 UrlClassifierFeatureEmailTrackingDataCollection::
62 UrlClassifierFeatureEmailTrackingDataCollection()
63 : UrlClassifierFeatureAntiTrackingBase(
64 nsLiteralCString(EMAILTRACKING_DATACOLLECTION_FEATURE_NAME
),
66 URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_BLOCKLIST
),
68 URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_ENTITYLIST
),
70 URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_BLOCKLIST_TEST_ENTRIES
),
72 URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_ENTITYLIST_TEST_ENTRIES
),
73 nsLiteralCString(TABLE_EMAILTRACKING_DATACOLLECTION_BLOCKLIST_PREF
),
74 nsLiteralCString(TABLE_EMAILTRACKING_DATACOLLECTION_ENTITYLIST_PREF
),
76 URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_EXCEPTION_URLS
)) {}
79 const char* UrlClassifierFeatureEmailTrackingDataCollection::Name() {
80 return EMAILTRACKING_DATACOLLECTION_FEATURE_NAME
;
84 void UrlClassifierFeatureEmailTrackingDataCollection::MaybeInitialize() {
86 ("UrlClassifierFeatureEmailTrackingDataCollection::MaybeInitialize"));
88 if (!gFeatureEmailTrackingDataCollection
) {
89 gFeatureEmailTrackingDataCollection
=
90 new UrlClassifierFeatureEmailTrackingDataCollection();
91 gFeatureEmailTrackingDataCollection
->InitializePreferences();
96 void UrlClassifierFeatureEmailTrackingDataCollection::MaybeShutdown() {
98 ("UrlClassifierFeatureEmailTrackingDataCollection::MaybeShutdown"));
100 if (gFeatureEmailTrackingDataCollection
) {
101 gFeatureEmailTrackingDataCollection
->ShutdownPreferences();
102 gFeatureEmailTrackingDataCollection
= nullptr;
107 already_AddRefed
<UrlClassifierFeatureEmailTrackingDataCollection
>
108 UrlClassifierFeatureEmailTrackingDataCollection::MaybeCreate(
109 nsIChannel
* aChannel
) {
110 MOZ_ASSERT(aChannel
);
113 ("UrlClassifierFeatureEmailTrackingDataCollection::MaybeCreate - channel "
118 privacy_trackingprotection_emailtracking_data_collection_enabled()) {
122 bool isThirdParty
= AntiTrackingUtils::IsThirdPartyChannel(aChannel
);
124 // We don't need to collect data if the email tracker is loaded in first-party
131 MOZ_ASSERT(gFeatureEmailTrackingDataCollection
);
133 RefPtr
<UrlClassifierFeatureEmailTrackingDataCollection
> self
=
134 gFeatureEmailTrackingDataCollection
;
135 return self
.forget();
139 already_AddRefed
<nsIUrlClassifierFeature
>
140 UrlClassifierFeatureEmailTrackingDataCollection::GetIfNameMatches(
141 const nsACString
& aName
) {
142 if (!aName
.EqualsLiteral(EMAILTRACKING_DATACOLLECTION_FEATURE_NAME
)) {
147 MOZ_ASSERT(gFeatureEmailTrackingDataCollection
);
149 RefPtr
<UrlClassifierFeatureEmailTrackingDataCollection
> self
=
150 gFeatureEmailTrackingDataCollection
;
151 return self
.forget();
155 UrlClassifierFeatureEmailTrackingDataCollection::ProcessChannel(
156 nsIChannel
* aChannel
, const nsTArray
<nsCString
>& aList
,
157 const nsTArray
<nsCString
>& aHashes
, bool* aShouldContinue
) {
158 NS_ENSURE_ARG_POINTER(aChannel
);
159 NS_ENSURE_ARG_POINTER(aShouldContinue
);
161 // This is not a blocking feature.
162 *aShouldContinue
= true;
165 ("UrlClassifierFeatureEmailTrackingDataCollection::ProcessChannel - "
166 "collecting data from channel %p",
169 static std::vector
<UrlClassifierCommon::ClassificationData
>
170 sClassificationData
= {
171 {"base-email-track-"_ns
,
172 nsIClassifiedChannel::ClassificationFlags::CLASSIFIED_EMAILTRACKING
},
173 {"content-email-track-"_ns
,
174 nsIClassifiedChannel::ClassificationFlags::
175 CLASSIFIED_EMAILTRACKING_CONTENT
},
178 // Get if the top window is a email webapp.
179 nsCOMPtr
<nsILoadInfo
> loadInfo
= aChannel
->LoadInfo();
181 RefPtr
<dom::BrowsingContext
> bc
;
182 loadInfo
->GetBrowsingContext(getter_AddRefs(bc
));
183 if (!bc
|| bc
->IsDiscarded()) {
187 RefPtr
<dom::WindowGlobalParent
> topWindowParent
=
188 bc
->Canonical()->GetTopWindowContext();
189 if (!topWindowParent
|| topWindowParent
->IsDiscarded()) {
193 // Cache the email webapp domains pref value and register the callback
194 // function to update the cached value when the pref changes.
195 if (!gEmailWebAppDomainsPref
) {
196 gEmailWebAppDomainsPref
= new nsCString();
198 Preferences::RegisterCallbackAndCall(EmailWebAppDomainPrefChangeCallback
,
199 kEmailWebAppDomainPrefName
);
201 Preferences::UnregisterCallback(EmailWebAppDomainPrefChangeCallback
,
202 kEmailWebAppDomainPrefName
);
203 gEmailWebAppDomainsPref
= nullptr;
207 bool isTopEmailWebApp
= topWindowParent
->DocumentPrincipal()->IsURIInList(
208 *gEmailWebAppDomainsPref
);
210 uint32_t flags
= UrlClassifierCommon::TablesToClassificationFlags(
211 aList
, sClassificationData
,
212 nsIClassifiedChannel::ClassificationFlags::CLASSIFIED_EMAILTRACKING
);
214 if (flags
& nsIClassifiedChannel::ClassificationFlags::
215 CLASSIFIED_EMAILTRACKING_CONTENT
) {
216 Telemetry::AccumulateCategorical(
218 ? Telemetry::LABELS_EMAIL_TRACKER_COUNT::content_email_webapp
219 : Telemetry::LABELS_EMAIL_TRACKER_COUNT::content_normal
);
221 // Notify the load event to record the content blocking log.
223 // Note that we need to change the code here if we decided to block content
224 // email trackers in the future.
225 ContentBlockingNotifier::OnEvent(
227 nsIWebProgressListener::STATE_LOADED_EMAILTRACKING_LEVEL_2_CONTENT
);
229 Telemetry::AccumulateCategorical(
231 ? Telemetry::LABELS_EMAIL_TRACKER_COUNT::base_email_webapp
232 : Telemetry::LABELS_EMAIL_TRACKER_COUNT::base_normal
);
233 // Notify to record content blocking log. Note that we don't need to notify
234 // if email tracking is enabled because the email tracking protection
235 // feature will be responsible for notifying the blocking event.
237 // Note that we need to change the code here if we decided to block content
238 // email trackers in the future.
239 if (!StaticPrefs::privacy_trackingprotection_emailtracking_enabled()) {
240 ContentBlockingNotifier::OnEvent(
242 nsIWebProgressListener::STATE_LOADED_EMAILTRACKING_LEVEL_1_CONTENT
);
250 UrlClassifierFeatureEmailTrackingDataCollection::GetURIByListType(
251 nsIChannel
* aChannel
, nsIUrlClassifierFeature::listType aListType
,
252 nsIUrlClassifierFeature::URIType
* aURIType
, nsIURI
** aURI
) {
253 NS_ENSURE_ARG_POINTER(aChannel
);
254 NS_ENSURE_ARG_POINTER(aURIType
);
255 NS_ENSURE_ARG_POINTER(aURI
);
257 if (aListType
== nsIUrlClassifierFeature::blocklist
) {
258 *aURIType
= nsIUrlClassifierFeature::blocklistURI
;
259 return aChannel
->GetURI(aURI
);
262 MOZ_ASSERT(aListType
== nsIUrlClassifierFeature::entitylist
);
264 *aURIType
= nsIUrlClassifierFeature::pairwiseEntitylistURI
;
265 return UrlClassifierCommon::CreatePairwiseEntityListURI(aChannel
, aURI
);
268 } // namespace mozilla::net