Bug 1874684 - Part 13: Pass DateDuration to AddDate/CalendarDateAdd. r=sfink
[gecko.git] / netwerk / url-classifier / UrlClassifierFeatureTrackingProtection.cpp
blob8c50b33d4d1db68c3ced5d0e1c06f59d0a110c03
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 "UrlClassifierFeatureTrackingProtection.h"
9 #include "mozilla/AntiTrackingUtils.h"
10 #include "mozilla/net/UrlClassifierCommon.h"
11 #include "ChannelClassifierService.h"
12 #include "nsIChannel.h"
13 #include "nsIHttpChannelInternal.h"
14 #include "nsILoadContext.h"
15 #include "nsNetUtil.h"
16 #include "mozilla/StaticPtr.h"
17 #include "nsXULAppAPI.h"
18 #include "nsIWebProgressListener.h"
20 namespace mozilla {
21 namespace net {
23 namespace {
25 #define TRACKING_PROTECTION_FEATURE_NAME "tracking-protection"
27 #define URLCLASSIFIER_TRACKING_BLOCKLIST "urlclassifier.trackingTable"
28 #define URLCLASSIFIER_TRACKING_BLOCKLIST_TEST_ENTRIES \
29 "urlclassifier.trackingTable.testEntries"
30 #define URLCLASSIFIER_TRACKING_ENTITYLIST "urlclassifier.trackingWhitelistTable"
31 #define URLCLASSIFIER_TRACKING_ENTITYLIST_TEST_ENTRIES \
32 "urlclassifier.trackingWhitelistTable.testEntries"
33 #define URLCLASSIFIER_TRACKING_PROTECTION_EXCEPTION_URLS \
34 "urlclassifier.trackingSkipURLs"
35 #define TABLE_TRACKING_BLOCKLIST_PREF "tracking-blocklist-pref"
36 #define TABLE_TRACKING_ENTITYLIST_PREF "tracking-entitylist-pref"
38 StaticRefPtr<UrlClassifierFeatureTrackingProtection> gFeatureTrackingProtection;
40 } // namespace
42 UrlClassifierFeatureTrackingProtection::UrlClassifierFeatureTrackingProtection()
43 : UrlClassifierFeatureAntiTrackingBase(
44 nsLiteralCString(TRACKING_PROTECTION_FEATURE_NAME),
45 nsLiteralCString(URLCLASSIFIER_TRACKING_BLOCKLIST),
46 nsLiteralCString(URLCLASSIFIER_TRACKING_ENTITYLIST),
47 nsLiteralCString(URLCLASSIFIER_TRACKING_BLOCKLIST_TEST_ENTRIES),
48 nsLiteralCString(URLCLASSIFIER_TRACKING_ENTITYLIST_TEST_ENTRIES),
49 nsLiteralCString(TABLE_TRACKING_BLOCKLIST_PREF),
50 nsLiteralCString(TABLE_TRACKING_ENTITYLIST_PREF),
51 nsLiteralCString(URLCLASSIFIER_TRACKING_PROTECTION_EXCEPTION_URLS)) {}
53 /* static */ const char* UrlClassifierFeatureTrackingProtection::Name() {
54 return TRACKING_PROTECTION_FEATURE_NAME;
57 /* static */
58 void UrlClassifierFeatureTrackingProtection::MaybeInitialize() {
59 MOZ_ASSERT(XRE_IsParentProcess());
60 UC_LOG_LEAK(("UrlClassifierFeatureTrackingProtection::MaybeInitialize"));
62 if (!gFeatureTrackingProtection) {
63 gFeatureTrackingProtection = new UrlClassifierFeatureTrackingProtection();
64 gFeatureTrackingProtection->InitializePreferences();
68 /* static */
69 void UrlClassifierFeatureTrackingProtection::MaybeShutdown() {
70 UC_LOG_LEAK(("UrlClassifierFeatureTrackingProtection::MaybeShutdown"));
72 if (gFeatureTrackingProtection) {
73 gFeatureTrackingProtection->ShutdownPreferences();
74 gFeatureTrackingProtection = nullptr;
78 /* static */
79 already_AddRefed<UrlClassifierFeatureTrackingProtection>
80 UrlClassifierFeatureTrackingProtection::MaybeCreate(nsIChannel* aChannel) {
81 MOZ_ASSERT(aChannel);
83 UC_LOG_LEAK(
84 ("UrlClassifierFeatureTrackingProtection::MaybeCreate - channel %p",
85 aChannel));
87 nsCOMPtr<nsILoadContext> loadContext;
88 NS_QueryNotificationCallbacks(aChannel, loadContext);
89 if (!loadContext) {
90 // Some channels don't have a loadcontext, check the global tracking
91 // protection preference.
92 if (!StaticPrefs::privacy_trackingprotection_enabled() &&
93 !(NS_UsePrivateBrowsing(aChannel) &&
94 StaticPrefs::privacy_trackingprotection_pbmode_enabled())) {
95 return nullptr;
97 } else if (!loadContext->UseTrackingProtection()) {
98 return nullptr;
101 bool isThirdParty = AntiTrackingUtils::IsThirdPartyChannel(aChannel);
102 if (!isThirdParty) {
103 UC_LOG(
104 ("UrlClassifierFeatureTrackingProtection::MaybeCreate - "
105 "skipping first party or top-level load for channel %p",
106 aChannel));
107 return nullptr;
110 if (!UrlClassifierCommon::ShouldEnableProtectionForChannel(aChannel)) {
111 return nullptr;
114 MaybeInitialize();
115 MOZ_ASSERT(gFeatureTrackingProtection);
117 RefPtr<UrlClassifierFeatureTrackingProtection> self =
118 gFeatureTrackingProtection;
119 return self.forget();
122 /* static */
123 already_AddRefed<nsIUrlClassifierFeature>
124 UrlClassifierFeatureTrackingProtection::GetIfNameMatches(
125 const nsACString& aName) {
126 if (!aName.EqualsLiteral(TRACKING_PROTECTION_FEATURE_NAME)) {
127 return nullptr;
130 MaybeInitialize();
131 MOZ_ASSERT(gFeatureTrackingProtection);
133 RefPtr<UrlClassifierFeatureTrackingProtection> self =
134 gFeatureTrackingProtection;
135 return self.forget();
138 NS_IMETHODIMP
139 UrlClassifierFeatureTrackingProtection::ProcessChannel(
140 nsIChannel* aChannel, const nsTArray<nsCString>& aList,
141 const nsTArray<nsCString>& aHashes, bool* aShouldContinue) {
142 NS_ENSURE_ARG_POINTER(aChannel);
143 NS_ENSURE_ARG_POINTER(aShouldContinue);
145 bool isAllowListed = UrlClassifierCommon::IsAllowListed(aChannel);
147 // This is a blocking feature.
148 *aShouldContinue = isAllowListed;
150 if (isAllowListed) {
151 return NS_OK;
154 nsAutoCString list;
155 UrlClassifierCommon::TablesToString(aList, list);
157 ChannelBlockDecision decision =
158 ChannelClassifierService::OnBeforeBlockChannel(aChannel, mName, list);
159 if (decision != ChannelBlockDecision::Blocked) {
160 uint32_t event =
161 decision == ChannelBlockDecision::Replaced
162 ? nsIWebProgressListener::STATE_REPLACED_TRACKING_CONTENT
163 : nsIWebProgressListener::STATE_ALLOWED_TRACKING_CONTENT;
165 // Need to set aBlocked to True if we replace the Tracker with a shim,
166 // since the shim is treated as a blocked event
167 // Note: If we need to account for which kind of tracker was replaced,
168 // we need to create a new event type in nsIWebProgressListener
169 if (event == nsIWebProgressListener::STATE_REPLACED_TRACKING_CONTENT) {
170 ContentBlockingNotifier::OnEvent(aChannel, event, true);
171 } else {
172 ContentBlockingNotifier::OnEvent(aChannel, event, false);
175 *aShouldContinue = true;
176 return NS_OK;
179 UrlClassifierCommon::SetBlockedContent(aChannel, NS_ERROR_TRACKING_URI, list,
180 ""_ns, ""_ns);
182 UC_LOG(
183 ("UrlClassifierFeatureTrackingProtection::ProcessChannel - "
184 "cancelling channel %p",
185 aChannel));
187 nsCOMPtr<nsIHttpChannelInternal> httpChannel = do_QueryInterface(aChannel);
188 if (httpChannel) {
189 Unused << httpChannel->CancelByURLClassifier(NS_ERROR_TRACKING_URI);
190 } else {
191 Unused << aChannel->Cancel(NS_ERROR_TRACKING_URI);
194 return NS_OK;
197 NS_IMETHODIMP
198 UrlClassifierFeatureTrackingProtection::GetURIByListType(
199 nsIChannel* aChannel, nsIUrlClassifierFeature::listType aListType,
200 nsIUrlClassifierFeature::URIType* aURIType, nsIURI** aURI) {
201 NS_ENSURE_ARG_POINTER(aChannel);
202 NS_ENSURE_ARG_POINTER(aURIType);
203 NS_ENSURE_ARG_POINTER(aURI);
205 if (aListType == nsIUrlClassifierFeature::blocklist) {
206 *aURIType = nsIUrlClassifierFeature::blocklistURI;
207 return aChannel->GetURI(aURI);
210 MOZ_ASSERT(aListType == nsIUrlClassifierFeature::entitylist);
212 *aURIType = nsIUrlClassifierFeature::pairwiseEntitylistURI;
213 return UrlClassifierCommon::CreatePairwiseEntityListURI(aChannel, aURI);
216 } // namespace net
217 } // namespace mozilla