Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / netwerk / url-classifier / UrlClassifierFeatureFactory.cpp
blob747e938f069bc0fc6ce2081f25629ad59985a75f
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"
9 // List of Features
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"
26 namespace mozilla {
27 namespace net {
29 /* static */
30 void UrlClassifierFeatureFactory::Shutdown() {
31 // We want to expose Features only in the parent process.
32 if (!XRE_IsParentProcess()) {
33 return;
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();
49 /* static */
50 void UrlClassifierFeatureFactory::GetFeaturesFromChannel(
51 nsIChannel* aChannel,
52 nsTArray<nsCOMPtr<nsIUrlClassifierFeature>>& aFeatures) {
53 MOZ_ASSERT(XRE_IsParentProcess());
54 MOZ_ASSERT(aChannel);
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
61 // feature ordering.
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.
67 feature =
68 UrlClassifierFeatureEmailTrackingDataCollection::MaybeCreate(aChannel);
69 if (feature) {
70 aFeatures.AppendElement(feature);
73 // Email Tracking Protection
74 feature = UrlClassifierFeatureEmailTrackingProtection::MaybeCreate(aChannel);
75 if (feature) {
76 aFeatures.AppendElement(feature);
79 // Cryptomining Protection
80 feature = UrlClassifierFeatureCryptominingProtection::MaybeCreate(aChannel);
81 if (feature) {
82 aFeatures.AppendElement(feature);
85 // Fingerprinting Protection
86 feature = UrlClassifierFeatureFingerprintingProtection::MaybeCreate(aChannel);
87 if (feature) {
88 aFeatures.AppendElement(feature);
91 // SocialTracking Protection
92 feature = UrlClassifierFeatureSocialTrackingProtection::MaybeCreate(aChannel);
93 if (feature) {
94 aFeatures.AppendElement(feature);
97 // Tracking Protection
98 feature = UrlClassifierFeatureTrackingProtection::MaybeCreate(aChannel);
99 if (feature) {
100 aFeatures.AppendElement(feature);
103 // Cryptomining Annotation
104 feature = UrlClassifierFeatureCryptominingAnnotation::MaybeCreate(aChannel);
105 if (feature) {
106 aFeatures.AppendElement(feature);
109 // Fingerprinting Annotation
110 feature = UrlClassifierFeatureFingerprintingAnnotation::MaybeCreate(aChannel);
111 if (feature) {
112 aFeatures.AppendElement(feature);
115 // SocialTracking Annotation
116 feature = UrlClassifierFeatureSocialTrackingAnnotation::MaybeCreate(aChannel);
117 if (feature) {
118 aFeatures.AppendElement(feature);
121 // Tracking Annotation
122 feature = UrlClassifierFeatureTrackingAnnotation::MaybeCreate(aChannel);
123 if (feature) {
124 aFeatures.AppendElement(feature);
128 /* static */
129 void UrlClassifierFeatureFactory::GetPhishingProtectionFeatures(
130 nsTArray<RefPtr<nsIUrlClassifierFeature>>& aFeatures) {
131 UrlClassifierFeaturePhishingProtection::MaybeCreate(aFeatures);
134 /* static */
135 already_AddRefed<nsIUrlClassifierFeature>
136 UrlClassifierFeatureFactory::GetFeatureByName(const nsACString& aName) {
137 if (!XRE_IsParentProcess()) {
138 return nullptr;
141 nsCOMPtr<nsIUrlClassifierFeature> feature;
143 // Cryptomining Annotation
144 feature = UrlClassifierFeatureCryptominingAnnotation::GetIfNameMatches(aName);
145 if (feature) {
146 return feature.forget();
149 // Cryptomining Protection
150 feature = UrlClassifierFeatureCryptominingProtection::GetIfNameMatches(aName);
151 if (feature) {
152 return feature.forget();
155 // Email Tracking Data Collection
156 feature =
157 UrlClassifierFeatureEmailTrackingDataCollection::GetIfNameMatches(aName);
158 if (feature) {
159 return feature.forget();
162 // Email Tracking Protection
163 feature =
164 UrlClassifierFeatureEmailTrackingProtection::GetIfNameMatches(aName);
165 if (feature) {
166 return feature.forget();
169 // Fingerprinting Annotation
170 feature =
171 UrlClassifierFeatureFingerprintingAnnotation::GetIfNameMatches(aName);
172 if (feature) {
173 return feature.forget();
176 // Fingerprinting Protection
177 feature =
178 UrlClassifierFeatureFingerprintingProtection::GetIfNameMatches(aName);
179 if (feature) {
180 return feature.forget();
183 // SocialTracking Annotation
184 feature =
185 UrlClassifierFeatureSocialTrackingAnnotation::GetIfNameMatches(aName);
186 if (feature) {
187 return feature.forget();
190 // SocialTracking Protection
191 feature =
192 UrlClassifierFeatureSocialTrackingProtection::GetIfNameMatches(aName);
193 if (feature) {
194 return feature.forget();
197 // Tracking Protection
198 feature = UrlClassifierFeatureTrackingProtection::GetIfNameMatches(aName);
199 if (feature) {
200 return feature.forget();
203 // Tracking Annotation
204 feature = UrlClassifierFeatureTrackingAnnotation::GetIfNameMatches(aName);
205 if (feature) {
206 return feature.forget();
209 // PhishingProtection features
210 feature = UrlClassifierFeaturePhishingProtection::GetIfNameMatches(aName);
211 if (feature) {
212 return feature.forget();
215 return nullptr;
218 /* static */
219 void UrlClassifierFeatureFactory::GetFeatureNames(nsTArray<nsCString>& aArray) {
220 if (!XRE_IsParentProcess()) {
221 return;
224 nsAutoCString name;
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);
294 /* static */
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,
301 aEntitylistTables);
302 return feature.forget();
305 namespace {
307 struct BlockingErrorCode {
308 nsresult mErrorCode;
309 uint32_t mBlockingEventCode;
310 const char* mConsoleMessage;
311 nsCString mConsoleCategory;
314 static const 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},
332 } // namespace
334 /* static */
335 bool UrlClassifierFeatureFactory::IsClassifierBlockingErrorCode(
336 nsresult aError) {
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) {
341 return true;
345 return false;
348 /* static */
349 bool UrlClassifierFeatureFactory::IsClassifierBlockingEventCode(
350 uint32_t aEventCode) {
351 for (const auto& blockingErrorCode : sBlockingErrorCodes) {
352 if (aEventCode == blockingErrorCode.mBlockingEventCode) {
353 return true;
356 return false;
359 /* static */
360 uint32_t UrlClassifierFeatureFactory::GetClassifierBlockingEventCode(
361 nsresult aErrorCode) {
362 for (const auto& blockingErrorCode : sBlockingErrorCodes) {
363 if (aErrorCode == blockingErrorCode.mErrorCode) {
364 return blockingErrorCode.mBlockingEventCode;
367 return 0;
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;
380 return nullptr;
383 } // namespace net
384 } // namespace mozilla