Bug 1874684 - Part 13: Pass DateDuration to AddDate/CalendarDateAdd. r=sfink
[gecko.git] / netwerk / url-classifier / UrlClassifierFeatureSocialTrackingProtection.cpp
bloba2ca7459db76c9541f3faf61a88ccd7bb869f7a3
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 "UrlClassifierFeatureSocialTrackingProtection.h"
9 #include "mozilla/AntiTrackingUtils.h"
10 #include "mozilla/net/UrlClassifierCommon.h"
11 #include "ChannelClassifierService.h"
12 #include "mozilla/StaticPrefs_privacy.h"
13 #include "nsNetUtil.h"
14 #include "mozilla/StaticPtr.h"
15 #include "nsIWebProgressListener.h"
16 #include "nsIHttpChannelInternal.h"
17 #include "nsIChannel.h"
19 namespace mozilla {
20 namespace net {
22 namespace {
24 #define SOCIALTRACKING_FEATURE_NAME "socialtracking-protection"
26 #define URLCLASSIFIER_SOCIALTRACKING_BLOCKLIST \
27 "urlclassifier.features.socialtracking.blacklistTables"
28 #define URLCLASSIFIER_SOCIALTRACKING_BLOCKLIST_TEST_ENTRIES \
29 "urlclassifier.features.socialtracking.blacklistHosts"
30 #define URLCLASSIFIER_SOCIALTRACKING_ENTITYLIST \
31 "urlclassifier.features.socialtracking.whitelistTables"
32 #define URLCLASSIFIER_SOCIALTRACKING_ENTITYLIST_TEST_ENTRIES \
33 "urlclassifier.features.socialtracking.whitelistHosts"
34 #define URLCLASSIFIER_SOCIALTRACKING_EXCEPTION_URLS \
35 "urlclassifier.features.socialtracking.skipURLs"
36 #define TABLE_SOCIALTRACKING_BLOCKLIST_PREF "socialtracking-blocklist-pref"
37 #define TABLE_SOCIALTRACKING_ENTITYLIST_PREF "socialtracking-entitylist-pref"
39 StaticRefPtr<UrlClassifierFeatureSocialTrackingProtection>
40 gFeatureSocialTrackingProtection;
42 } // namespace
44 UrlClassifierFeatureSocialTrackingProtection::
45 UrlClassifierFeatureSocialTrackingProtection()
46 : UrlClassifierFeatureAntiTrackingBase(
47 nsLiteralCString(SOCIALTRACKING_FEATURE_NAME),
48 nsLiteralCString(URLCLASSIFIER_SOCIALTRACKING_BLOCKLIST),
49 nsLiteralCString(URLCLASSIFIER_SOCIALTRACKING_ENTITYLIST),
50 nsLiteralCString(URLCLASSIFIER_SOCIALTRACKING_BLOCKLIST_TEST_ENTRIES),
51 nsLiteralCString(
52 URLCLASSIFIER_SOCIALTRACKING_ENTITYLIST_TEST_ENTRIES),
53 nsLiteralCString(TABLE_SOCIALTRACKING_BLOCKLIST_PREF),
54 nsLiteralCString(TABLE_SOCIALTRACKING_ENTITYLIST_PREF),
55 nsLiteralCString(URLCLASSIFIER_SOCIALTRACKING_EXCEPTION_URLS)) {}
57 /* static */ const char* UrlClassifierFeatureSocialTrackingProtection::Name() {
58 return SOCIALTRACKING_FEATURE_NAME;
61 /* static */
62 void UrlClassifierFeatureSocialTrackingProtection::MaybeInitialize() {
63 UC_LOG_LEAK(
64 ("UrlClassifierFeatureSocialTrackingProtection::MaybeInitialize"));
66 if (!gFeatureSocialTrackingProtection) {
67 gFeatureSocialTrackingProtection =
68 new UrlClassifierFeatureSocialTrackingProtection();
69 gFeatureSocialTrackingProtection->InitializePreferences();
73 /* static */
74 void UrlClassifierFeatureSocialTrackingProtection::MaybeShutdown() {
75 UC_LOG_LEAK(("UrlClassifierFeatureSocialTrackingProtection::MaybeShutdown"));
77 if (gFeatureSocialTrackingProtection) {
78 gFeatureSocialTrackingProtection->ShutdownPreferences();
79 gFeatureSocialTrackingProtection = nullptr;
83 /* static */
84 already_AddRefed<UrlClassifierFeatureSocialTrackingProtection>
85 UrlClassifierFeatureSocialTrackingProtection::MaybeCreate(
86 nsIChannel* aChannel) {
87 MOZ_ASSERT(aChannel);
89 UC_LOG_LEAK(
90 ("UrlClassifierFeatureSocialTrackingProtection::MaybeCreate - channel %p",
91 aChannel));
93 if (!StaticPrefs::privacy_trackingprotection_socialtracking_enabled()) {
94 return nullptr;
97 bool isThirdParty = AntiTrackingUtils::IsThirdPartyChannel(aChannel);
98 if (!isThirdParty) {
99 UC_LOG(
100 ("UrlClassifierFeatureSocialTrackingProtection::MaybeCreate - "
101 "skipping first party or top-level load for channel %p",
102 aChannel));
103 return nullptr;
106 if (!UrlClassifierCommon::ShouldEnableProtectionForChannel(aChannel)) {
107 return nullptr;
110 MaybeInitialize();
111 MOZ_ASSERT(gFeatureSocialTrackingProtection);
113 RefPtr<UrlClassifierFeatureSocialTrackingProtection> self =
114 gFeatureSocialTrackingProtection;
115 return self.forget();
118 /* static */
119 already_AddRefed<nsIUrlClassifierFeature>
120 UrlClassifierFeatureSocialTrackingProtection::GetIfNameMatches(
121 const nsACString& aName) {
122 if (!aName.EqualsLiteral(SOCIALTRACKING_FEATURE_NAME)) {
123 return nullptr;
126 MaybeInitialize();
127 MOZ_ASSERT(gFeatureSocialTrackingProtection);
129 RefPtr<UrlClassifierFeatureSocialTrackingProtection> self =
130 gFeatureSocialTrackingProtection;
131 return self.forget();
134 NS_IMETHODIMP
135 UrlClassifierFeatureSocialTrackingProtection::ProcessChannel(
136 nsIChannel* aChannel, const nsTArray<nsCString>& aList,
137 const nsTArray<nsCString>& aHashes, bool* aShouldContinue) {
138 NS_ENSURE_ARG_POINTER(aChannel);
139 NS_ENSURE_ARG_POINTER(aShouldContinue);
141 bool isAllowListed = UrlClassifierCommon::IsAllowListed(aChannel);
143 // This is a blocking feature.
144 *aShouldContinue = isAllowListed;
146 if (isAllowListed) {
147 return NS_OK;
150 nsAutoCString list;
151 UrlClassifierCommon::TablesToString(aList, list);
153 ChannelBlockDecision decision =
154 ChannelClassifierService::OnBeforeBlockChannel(aChannel, mName, list);
155 if (decision != ChannelBlockDecision::Blocked) {
156 uint32_t event =
157 decision == ChannelBlockDecision::Replaced
158 ? nsIWebProgressListener::STATE_REPLACED_TRACKING_CONTENT
159 : nsIWebProgressListener::STATE_ALLOWED_TRACKING_CONTENT;
161 // Need to set aBlocked to True if we replace the Social Tracker
162 // with a shim, since the shim is treated as a blocked event
163 // Note: If we need to account for which kind of tracker was replaced,
164 // we need to create a new event type in nsIWebProgressListener
165 if (event == nsIWebProgressListener::STATE_REPLACED_TRACKING_CONTENT) {
166 ContentBlockingNotifier::OnEvent(aChannel, event, true);
167 } else {
168 ContentBlockingNotifier::OnEvent(aChannel, event, false);
171 *aShouldContinue = true;
172 return NS_OK;
175 UrlClassifierCommon::SetBlockedContent(aChannel, NS_ERROR_SOCIALTRACKING_URI,
176 list, ""_ns, ""_ns);
178 UC_LOG(
179 ("UrlClassifierFeatureSocialTrackingProtection::ProcessChannel - "
180 "cancelling channel %p",
181 aChannel));
182 nsCOMPtr<nsIHttpChannelInternal> httpChannel = do_QueryInterface(aChannel);
184 if (httpChannel) {
185 Unused << httpChannel->CancelByURLClassifier(NS_ERROR_SOCIALTRACKING_URI);
186 } else {
187 Unused << aChannel->Cancel(NS_ERROR_SOCIALTRACKING_URI);
190 return NS_OK;
193 NS_IMETHODIMP
194 UrlClassifierFeatureSocialTrackingProtection::GetURIByListType(
195 nsIChannel* aChannel, nsIUrlClassifierFeature::listType aListType,
196 nsIUrlClassifierFeature::URIType* aURIType, nsIURI** aURI) {
197 NS_ENSURE_ARG_POINTER(aChannel);
198 NS_ENSURE_ARG_POINTER(aURIType);
199 NS_ENSURE_ARG_POINTER(aURI);
201 if (aListType == nsIUrlClassifierFeature::blocklist) {
202 *aURIType = nsIUrlClassifierFeature::blocklistURI;
203 return aChannel->GetURI(aURI);
206 MOZ_ASSERT(aListType == nsIUrlClassifierFeature::entitylist);
208 *aURIType = nsIUrlClassifierFeature::pairwiseEntitylistURI;
209 return UrlClassifierCommon::CreatePairwiseEntityListURI(aChannel, aURI);
212 } // namespace net
213 } // namespace mozilla