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 "mozilla/net/UrlClassifierFeatureFactory.h"
10 #include "UrlClassifierFeatureCryptominingAnnotation.h"
11 #include "UrlClassifierFeatureCryptominingProtection.h"
12 #include "UrlClassifierFeatureEmailTrackingDataCollection.h"
13 #include "UrlClassifierFeatureEmailTrackingProtection.h"
14 #include "UrlClassifierFeatureFingerprintingAnnotation.h"
15 #include "UrlClassifierFeatureFingerprintingProtection.h"
16 #include "UrlClassifierFeaturePhishingProtection.h"
17 #include "UrlClassifierFeatureSocialTrackingAnnotation.h"
18 #include "UrlClassifierFeatureSocialTrackingProtection.h"
19 #include "UrlClassifierFeatureTrackingProtection.h"
20 #include "UrlClassifierFeatureTrackingAnnotation.h"
21 #include "UrlClassifierFeatureCustomTables.h"
23 #include "nsIWebProgressListener.h"
24 #include "nsAppRunner.h"
30 void UrlClassifierFeatureFactory::Shutdown() {
31 // We want to expose Features only in the parent process.
32 if (!XRE_IsParentProcess()) {
36 UrlClassifierFeatureCryptominingAnnotation::MaybeShutdown();
37 UrlClassifierFeatureCryptominingProtection::MaybeShutdown();
38 UrlClassifierFeatureEmailTrackingDataCollection::MaybeShutdown();
39 UrlClassifierFeatureEmailTrackingProtection::MaybeShutdown();
40 UrlClassifierFeatureFingerprintingAnnotation::MaybeShutdown();
41 UrlClassifierFeatureFingerprintingProtection::MaybeShutdown();
42 UrlClassifierFeaturePhishingProtection::MaybeShutdown();
43 UrlClassifierFeatureSocialTrackingAnnotation::MaybeShutdown();
44 UrlClassifierFeatureSocialTrackingProtection::MaybeShutdown();
45 UrlClassifierFeatureTrackingAnnotation::MaybeShutdown();
46 UrlClassifierFeatureTrackingProtection::MaybeShutdown();
50 void UrlClassifierFeatureFactory::GetFeaturesFromChannel(
52 nsTArray
<nsCOMPtr
<nsIUrlClassifierFeature
>>& aFeatures
) {
53 MOZ_ASSERT(XRE_IsParentProcess());
56 nsCOMPtr
<nsIUrlClassifierFeature
> feature
;
58 // Note that the order of the features is extremely important! When more than
59 // 1 feature classifies the channel, we call ::ProcessChannel() following this
60 // feature order, and this could produce different results with a different
63 // Email Tracking Data Collection
64 // This needs to be run before other features so that other blocking features
65 // won't stop us to collect data for email trackers. Note that this feature
66 // is not a blocking feature.
68 UrlClassifierFeatureEmailTrackingDataCollection::MaybeCreate(aChannel
);
70 aFeatures
.AppendElement(feature
);
73 // Email Tracking Protection
74 feature
= UrlClassifierFeatureEmailTrackingProtection::MaybeCreate(aChannel
);
76 aFeatures
.AppendElement(feature
);
79 // Cryptomining Protection
80 feature
= UrlClassifierFeatureCryptominingProtection::MaybeCreate(aChannel
);
82 aFeatures
.AppendElement(feature
);
85 // Fingerprinting Protection
86 feature
= UrlClassifierFeatureFingerprintingProtection::MaybeCreate(aChannel
);
88 aFeatures
.AppendElement(feature
);
91 // SocialTracking Protection
92 feature
= UrlClassifierFeatureSocialTrackingProtection::MaybeCreate(aChannel
);
94 aFeatures
.AppendElement(feature
);
97 // Tracking Protection
98 feature
= UrlClassifierFeatureTrackingProtection::MaybeCreate(aChannel
);
100 aFeatures
.AppendElement(feature
);
103 // Cryptomining Annotation
104 feature
= UrlClassifierFeatureCryptominingAnnotation::MaybeCreate(aChannel
);
106 aFeatures
.AppendElement(feature
);
109 // Fingerprinting Annotation
110 feature
= UrlClassifierFeatureFingerprintingAnnotation::MaybeCreate(aChannel
);
112 aFeatures
.AppendElement(feature
);
115 // SocialTracking Annotation
116 feature
= UrlClassifierFeatureSocialTrackingAnnotation::MaybeCreate(aChannel
);
118 aFeatures
.AppendElement(feature
);
121 // Tracking Annotation
122 feature
= UrlClassifierFeatureTrackingAnnotation::MaybeCreate(aChannel
);
124 aFeatures
.AppendElement(feature
);
129 void UrlClassifierFeatureFactory::GetPhishingProtectionFeatures(
130 nsTArray
<RefPtr
<nsIUrlClassifierFeature
>>& aFeatures
) {
131 UrlClassifierFeaturePhishingProtection::MaybeCreate(aFeatures
);
135 already_AddRefed
<nsIUrlClassifierFeature
>
136 UrlClassifierFeatureFactory::GetFeatureByName(const nsACString
& aName
) {
137 if (!XRE_IsParentProcess()) {
141 nsCOMPtr
<nsIUrlClassifierFeature
> feature
;
143 // Cryptomining Annotation
144 feature
= UrlClassifierFeatureCryptominingAnnotation::GetIfNameMatches(aName
);
146 return feature
.forget();
149 // Cryptomining Protection
150 feature
= UrlClassifierFeatureCryptominingProtection::GetIfNameMatches(aName
);
152 return feature
.forget();
155 // Email Tracking Data Collection
157 UrlClassifierFeatureEmailTrackingDataCollection::GetIfNameMatches(aName
);
159 return feature
.forget();
162 // Email Tracking Protection
164 UrlClassifierFeatureEmailTrackingProtection::GetIfNameMatches(aName
);
166 return feature
.forget();
169 // Fingerprinting Annotation
171 UrlClassifierFeatureFingerprintingAnnotation::GetIfNameMatches(aName
);
173 return feature
.forget();
176 // Fingerprinting Protection
178 UrlClassifierFeatureFingerprintingProtection::GetIfNameMatches(aName
);
180 return feature
.forget();
183 // SocialTracking Annotation
185 UrlClassifierFeatureSocialTrackingAnnotation::GetIfNameMatches(aName
);
187 return feature
.forget();
190 // SocialTracking Protection
192 UrlClassifierFeatureSocialTrackingProtection::GetIfNameMatches(aName
);
194 return feature
.forget();
197 // Tracking Protection
198 feature
= UrlClassifierFeatureTrackingProtection::GetIfNameMatches(aName
);
200 return feature
.forget();
203 // Tracking Annotation
204 feature
= UrlClassifierFeatureTrackingAnnotation::GetIfNameMatches(aName
);
206 return feature
.forget();
209 // PhishingProtection features
210 feature
= UrlClassifierFeaturePhishingProtection::GetIfNameMatches(aName
);
212 return feature
.forget();
219 void UrlClassifierFeatureFactory::GetFeatureNames(nsTArray
<nsCString
>& aArray
) {
220 if (!XRE_IsParentProcess()) {
226 // Cryptomining Annotation
227 name
.Assign(UrlClassifierFeatureCryptominingAnnotation::Name());
228 if (!name
.IsEmpty()) {
229 aArray
.AppendElement(name
);
232 // Cryptomining Protection
233 name
.Assign(UrlClassifierFeatureCryptominingProtection::Name());
234 if (!name
.IsEmpty()) {
235 aArray
.AppendElement(name
);
238 // Email Tracking Data Collection
239 name
.Assign(UrlClassifierFeatureEmailTrackingDataCollection::Name());
240 if (!name
.IsEmpty()) {
241 aArray
.AppendElement(name
);
244 // Email Tracking Protection
245 name
.Assign(UrlClassifierFeatureEmailTrackingProtection::Name());
246 if (!name
.IsEmpty()) {
247 aArray
.AppendElement(name
);
250 // Fingerprinting Annotation
251 name
.Assign(UrlClassifierFeatureFingerprintingAnnotation::Name());
252 if (!name
.IsEmpty()) {
253 aArray
.AppendElement(name
);
256 // Fingerprinting Protection
257 name
.Assign(UrlClassifierFeatureFingerprintingProtection::Name());
258 if (!name
.IsEmpty()) {
259 aArray
.AppendElement(name
);
262 // SocialTracking Annotation
263 name
.Assign(UrlClassifierFeatureSocialTrackingAnnotation::Name());
264 if (!name
.IsEmpty()) {
265 aArray
.AppendElement(name
);
268 // SocialTracking Protection
269 name
.Assign(UrlClassifierFeatureSocialTrackingProtection::Name());
270 if (!name
.IsEmpty()) {
271 aArray
.AppendElement(name
);
274 // Tracking Protection
275 name
.Assign(UrlClassifierFeatureTrackingProtection::Name());
276 if (!name
.IsEmpty()) {
277 aArray
.AppendElement(name
);
280 // Tracking Annotation
281 name
.Assign(UrlClassifierFeatureTrackingAnnotation::Name());
282 if (!name
.IsEmpty()) {
283 aArray
.AppendElement(name
);
286 // PhishingProtection features
288 nsTArray
<nsCString
> features
;
289 UrlClassifierFeaturePhishingProtection::GetFeatureNames(features
);
290 aArray
.AppendElements(features
);
295 already_AddRefed
<nsIUrlClassifierFeature
>
296 UrlClassifierFeatureFactory::CreateFeatureWithTables(
297 const nsACString
& aName
, const nsTArray
<nsCString
>& aBlocklistTables
,
298 const nsTArray
<nsCString
>& aEntitylistTables
) {
299 nsCOMPtr
<nsIUrlClassifierFeature
> feature
=
300 new UrlClassifierFeatureCustomTables(aName
, aBlocklistTables
,
302 return feature
.forget();
307 struct BlockingErrorCode
{
309 uint32_t mBlockingEventCode
;
310 const char* mConsoleMessage
;
311 nsLiteralCString mConsoleCategory
;
314 static constexpr BlockingErrorCode sBlockingErrorCodes
[] = {
315 {NS_ERROR_TRACKING_URI
,
316 nsIWebProgressListener::STATE_BLOCKED_TRACKING_CONTENT
,
317 "TrackerUriBlocked", "Tracking Protection"_ns
},
318 {NS_ERROR_FINGERPRINTING_URI
,
319 nsIWebProgressListener::STATE_BLOCKED_FINGERPRINTING_CONTENT
,
320 "TrackerUriBlocked", "Tracking Protection"_ns
},
321 {NS_ERROR_CRYPTOMINING_URI
,
322 nsIWebProgressListener::STATE_BLOCKED_CRYPTOMINING_CONTENT
,
323 "TrackerUriBlocked", "Tracking Protection"_ns
},
324 {NS_ERROR_SOCIALTRACKING_URI
,
325 nsIWebProgressListener::STATE_BLOCKED_SOCIALTRACKING_CONTENT
,
326 "TrackerUriBlocked", "Tracking Protection"_ns
},
327 {NS_ERROR_EMAILTRACKING_URI
,
328 nsIWebProgressListener::STATE_BLOCKED_EMAILTRACKING_CONTENT
,
329 "TrackerUriBlocked", "Tracking Protection"_ns
},
335 bool UrlClassifierFeatureFactory::IsClassifierBlockingErrorCode(
337 // In theory we can iterate through the features, but at the moment, we can
338 // just have a simple check here.
339 for (const auto& blockingErrorCode
: sBlockingErrorCodes
) {
340 if (aError
== blockingErrorCode
.mErrorCode
) {
349 bool UrlClassifierFeatureFactory::IsClassifierBlockingEventCode(
350 uint32_t aEventCode
) {
351 for (const auto& blockingErrorCode
: sBlockingErrorCodes
) {
352 if (aEventCode
== blockingErrorCode
.mBlockingEventCode
) {
360 uint32_t UrlClassifierFeatureFactory::GetClassifierBlockingEventCode(
361 nsresult aErrorCode
) {
362 for (const auto& blockingErrorCode
: sBlockingErrorCodes
) {
363 if (aErrorCode
== blockingErrorCode
.mErrorCode
) {
364 return blockingErrorCode
.mBlockingEventCode
;
370 /* static */ const char*
371 UrlClassifierFeatureFactory::ClassifierBlockingErrorCodeToConsoleMessage(
372 nsresult aError
, nsACString
& aCategory
) {
373 for (const auto& blockingErrorCode
: sBlockingErrorCodes
) {
374 if (aError
== blockingErrorCode
.mErrorCode
) {
375 aCategory
= blockingErrorCode
.mConsoleCategory
;
376 return blockingErrorCode
.mConsoleMessage
;
384 } // namespace mozilla