UrlClassifierFeatureConsentManagerAnnotation.cpp (6626B)
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/. */ 6 7 #include "UrlClassifierFeatureConsentManagerAnnotation.h" 8 9 #include "Classifier.h" 10 #include "mozilla/Logging.h" 11 #include "mozilla/StaticPrefs_privacy.h" 12 #include "mozilla/StaticPtr.h" 13 #include "mozilla/net/UrlClassifierCommon.h" 14 #include "nsIChannel.h" 15 #include "nsIClassifiedChannel.h" 16 #include "nsIWebProgressListener.h" 17 #include "nsContentUtils.h" 18 19 namespace mozilla { 20 namespace net { 21 22 namespace { 23 24 #define CONSENTMANAGER_ANNOTATION_FEATURE_NAME "consentmanager-annotation" 25 26 #define URLCLASSIFIER_CONSENTMANAGER_ANNOTATION_BLOCKLIST \ 27 "urlclassifier.features.consentmanager.annotate.blocklistTables" 28 #define URLCLASSIFIER_CONSENTMANAGER_ANNOTATION_BLOCKLIST_TEST_ENTRIES \ 29 "urlclassifier.features.consentmanager.annotate.blocklistHosts" 30 #define URLCLASSIFIER_CONSENTMANAGER_ANNOTATION_ENTITYLIST \ 31 "urlclassifier.features.consentmanager.annotate.allowlistTables" 32 #define URLCLASSIFIER_CONSENTMANAGER_ANNOTATION_ENTITYLIST_TEST_ENTRIES \ 33 "urlclassifier.features.consentmanager.annotate.allowlistHosts" 34 #define URLCLASSIFIER_CONSENTMANAGER_ANNOTATION_EXCEPTION_URLS \ 35 "urlclassifier.features.consentmanager.annotate.skipURLs" 36 #define TABLE_CONSENTMANAGER_ANNOTATION_BLOCKLIST_PREF \ 37 "consentmanager-annotate-blocklist-pref" 38 #define TABLE_CONSENTMANAGER_ANNOTATION_ENTITYLIST_PREF \ 39 "consentmanager-annotate-allowlist-pref" 40 41 static StaticRefPtr<UrlClassifierFeatureConsentManagerAnnotation> 42 gFeatureConsentManagerAnnotation; 43 44 } // namespace 45 46 UrlClassifierFeatureConsentManagerAnnotation:: 47 UrlClassifierFeatureConsentManagerAnnotation() 48 : UrlClassifierFeatureAntiTrackingBase( 49 nsLiteralCString(CONSENTMANAGER_ANNOTATION_FEATURE_NAME), 50 nsLiteralCString(URLCLASSIFIER_CONSENTMANAGER_ANNOTATION_BLOCKLIST), 51 nsLiteralCString(URLCLASSIFIER_CONSENTMANAGER_ANNOTATION_ENTITYLIST), 52 nsLiteralCString( 53 URLCLASSIFIER_CONSENTMANAGER_ANNOTATION_BLOCKLIST_TEST_ENTRIES), 54 nsLiteralCString( 55 URLCLASSIFIER_CONSENTMANAGER_ANNOTATION_ENTITYLIST_TEST_ENTRIES), 56 nsLiteralCString(TABLE_CONSENTMANAGER_ANNOTATION_BLOCKLIST_PREF), 57 nsLiteralCString(TABLE_CONSENTMANAGER_ANNOTATION_ENTITYLIST_PREF), 58 nsLiteralCString( 59 URLCLASSIFIER_CONSENTMANAGER_ANNOTATION_EXCEPTION_URLS)) {} 60 61 /* static */ const char* UrlClassifierFeatureConsentManagerAnnotation::Name() { 62 return CONSENTMANAGER_ANNOTATION_FEATURE_NAME; 63 } 64 65 /* static */ 66 void UrlClassifierFeatureConsentManagerAnnotation::MaybeInitialize() { 67 MOZ_ASSERT(XRE_IsParentProcess()); 68 UC_LOG_LEAK( 69 ("UrlClassifierFeatureConsentManagerAnnotation::MaybeInitialize")); 70 71 if (!gFeatureConsentManagerAnnotation) { 72 gFeatureConsentManagerAnnotation = 73 new UrlClassifierFeatureConsentManagerAnnotation(); 74 gFeatureConsentManagerAnnotation->InitializePreferences(); 75 } 76 } 77 78 /* static */ 79 void UrlClassifierFeatureConsentManagerAnnotation::MaybeShutdown() { 80 UC_LOG_LEAK(("UrlClassifierFeatureConsentManagerAnnotation::MaybeShutdown")); 81 82 if (gFeatureConsentManagerAnnotation) { 83 gFeatureConsentManagerAnnotation->ShutdownPreferences(); 84 gFeatureConsentManagerAnnotation = nullptr; 85 } 86 } 87 88 /* static */ 89 already_AddRefed<UrlClassifierFeatureConsentManagerAnnotation> 90 UrlClassifierFeatureConsentManagerAnnotation::MaybeCreate( 91 nsIChannel* aChannel) { 92 MOZ_ASSERT(aChannel); 93 94 UC_LOG_LEAK( 95 ("UrlClassifierFeatureConsentManagerAnnotation::MaybeCreate - channel %p", 96 aChannel)); 97 98 if (!StaticPrefs:: 99 privacy_trackingprotection_consentmanager_annotate_channels()) { 100 return nullptr; 101 } 102 103 // We also don't need to annotate the channel if we are not blocking trackers 104 if (!StaticPrefs::privacy_trackingprotection_enabled() && 105 !(NS_UsePrivateBrowsing(aChannel) && 106 StaticPrefs::privacy_trackingprotection_pbmode_enabled())) { 107 return nullptr; 108 } 109 110 MaybeInitialize(); 111 MOZ_ASSERT(gFeatureConsentManagerAnnotation); 112 113 RefPtr<UrlClassifierFeatureConsentManagerAnnotation> self = 114 gFeatureConsentManagerAnnotation; 115 return self.forget(); 116 } 117 118 /* static */ 119 already_AddRefed<nsIUrlClassifierFeature> 120 UrlClassifierFeatureConsentManagerAnnotation::GetIfNameMatches( 121 const nsACString& aName) { 122 if (!aName.EqualsLiteral(CONSENTMANAGER_ANNOTATION_FEATURE_NAME)) { 123 return nullptr; 124 } 125 126 MaybeInitialize(); 127 MOZ_ASSERT(gFeatureConsentManagerAnnotation); 128 129 RefPtr<UrlClassifierFeatureConsentManagerAnnotation> self = 130 gFeatureConsentManagerAnnotation; 131 return self.forget(); 132 } 133 134 NS_IMETHODIMP 135 UrlClassifierFeatureConsentManagerAnnotation::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); 140 141 // This is not a blocking feature. 142 *aShouldContinue = true; 143 144 UC_LOG( 145 ("UrlClassifierFeatureConsentManagerAnnotation::ProcessChannel - " 146 "annotating channel %p", 147 aChannel)); 148 149 static std::vector<UrlClassifierCommon::ClassificationData> 150 sClassificationData = { 151 {"consent-manager-track-"_ns, 152 nsIClassifiedChannel::ClassificationFlags:: 153 CLASSIFIED_CONSENTMANAGER}, 154 }; 155 156 uint32_t flags = UrlClassifierCommon::TablesToClassificationFlags( 157 aList, sClassificationData, 158 nsIClassifiedChannel::ClassificationFlags::CLASSIFIED_CONSENTMANAGER); 159 160 UrlClassifierCommon::SetTrackingInfo(aChannel, aList, aHashes); 161 162 UrlClassifierCommon::AnnotateChannelWithoutNotifying(aChannel, flags); 163 164 return NS_OK; 165 } 166 167 NS_IMETHODIMP 168 UrlClassifierFeatureConsentManagerAnnotation::GetURIByListType( 169 nsIChannel* aChannel, nsIUrlClassifierFeature::listType aListType, 170 nsIUrlClassifierFeature::URIType* aURIType, nsIURI** aURI) { 171 NS_ENSURE_ARG_POINTER(aChannel); 172 NS_ENSURE_ARG_POINTER(aURIType); 173 NS_ENSURE_ARG_POINTER(aURI); 174 175 if (aListType == nsIUrlClassifierFeature::blocklist) { 176 *aURIType = nsIUrlClassifierFeature::blocklistURI; 177 return aChannel->GetURI(aURI); 178 } 179 180 MOZ_ASSERT(aListType == nsIUrlClassifierFeature::entitylist); 181 182 *aURIType = nsIUrlClassifierFeature::pairwiseEntitylistURI; 183 return UrlClassifierCommon::CreatePairwiseEntityListURI(aChannel, aURI); 184 } 185 186 } // namespace net 187 } // namespace mozilla