Bug 1887677: Correctly compute inClass predicate when validating unbound private...
[gecko.git] / netwerk / url-classifier / UrlClassifierFeatureBase.cpp
blob6f5924ab50af0753bec63fd7d5d11a5ed48dfc2c
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 "UrlClassifierFeatureBase.h"
8 #include "Classifier.h"
9 #include "mozilla/Preferences.h"
11 namespace mozilla {
13 using namespace safebrowsing;
15 namespace net {
17 namespace {
19 void OnPrefsChange(const char* aPrefName, void* aArray) {
20 auto* array = static_cast<nsTArray<nsCString>*>(aArray);
21 MOZ_ASSERT(array);
23 nsAutoCString value;
24 Preferences::GetCString(aPrefName, value);
25 Classifier::SplitTables(value, *array);
28 } // namespace
30 NS_INTERFACE_MAP_BEGIN(UrlClassifierFeatureBase)
31 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIUrlClassifierFeature)
32 NS_INTERFACE_MAP_ENTRY(nsIUrlClassifierFeature)
33 NS_INTERFACE_MAP_ENTRY(nsIUrlClassifierExceptionListObserver)
34 NS_INTERFACE_MAP_END
36 NS_IMPL_ADDREF(UrlClassifierFeatureBase)
37 NS_IMPL_RELEASE(UrlClassifierFeatureBase)
39 UrlClassifierFeatureBase::UrlClassifierFeatureBase(
40 const nsACString& aName, const nsACString& aPrefBlocklistTables,
41 const nsACString& aPrefEntitylistTables,
42 const nsACString& aPrefBlocklistHosts,
43 const nsACString& aPrefEntitylistHosts,
44 const nsACString& aPrefBlocklistTableName,
45 const nsACString& aPrefEntitylistTableName,
46 const nsACString& aPrefExceptionHosts)
47 : mName(aName), mPrefExceptionHosts(aPrefExceptionHosts) {
48 static_assert(nsIUrlClassifierFeature::blocklist == 0,
49 "nsIUrlClassifierFeature::blocklist must be 0");
50 static_assert(nsIUrlClassifierFeature::entitylist == 1,
51 "nsIUrlClassifierFeature::entitylist must be 1");
53 mPrefTables[nsIUrlClassifierFeature::blocklist] = aPrefBlocklistTables;
54 mPrefTables[nsIUrlClassifierFeature::entitylist] = aPrefEntitylistTables;
56 mPrefHosts[nsIUrlClassifierFeature::blocklist] = aPrefBlocklistHosts;
57 mPrefHosts[nsIUrlClassifierFeature::entitylist] = aPrefEntitylistHosts;
59 mPrefTableNames[nsIUrlClassifierFeature::blocklist] = aPrefBlocklistTableName;
60 mPrefTableNames[nsIUrlClassifierFeature::entitylist] =
61 aPrefEntitylistTableName;
64 UrlClassifierFeatureBase::~UrlClassifierFeatureBase() = default;
66 void UrlClassifierFeatureBase::InitializePreferences() {
67 for (uint32_t i = 0; i < 2; ++i) {
68 if (!mPrefTables[i].IsEmpty()) {
69 Preferences::RegisterCallbackAndCall(OnPrefsChange, mPrefTables[i],
70 &mTables[i]);
73 if (!mPrefHosts[i].IsEmpty()) {
74 Preferences::RegisterCallbackAndCall(OnPrefsChange, mPrefHosts[i],
75 &mHosts[i]);
79 nsCOMPtr<nsIUrlClassifierExceptionListService> exceptionListService =
80 do_GetService("@mozilla.org/url-classifier/exception-list-service;1");
81 if (NS_WARN_IF(!exceptionListService)) {
82 return;
85 exceptionListService->RegisterAndRunExceptionListObserver(
86 mName, mPrefExceptionHosts, this);
89 void UrlClassifierFeatureBase::ShutdownPreferences() {
90 for (uint32_t i = 0; i < 2; ++i) {
91 if (!mPrefTables[i].IsEmpty()) {
92 Preferences::UnregisterCallback(OnPrefsChange, mPrefTables[i],
93 &mTables[i]);
96 if (!mPrefHosts[i].IsEmpty()) {
97 Preferences::UnregisterCallback(OnPrefsChange, mPrefHosts[i], &mHosts[i]);
101 nsCOMPtr<nsIUrlClassifierExceptionListService> exceptionListService =
102 do_GetService("@mozilla.org/url-classifier/exception-list-service;1");
103 if (exceptionListService) {
104 exceptionListService->UnregisterExceptionListObserver(mName, this);
108 NS_IMETHODIMP
109 UrlClassifierFeatureBase::OnExceptionListUpdate(const nsACString& aList) {
110 mExceptionHosts = aList;
111 return NS_OK;
114 NS_IMETHODIMP
115 UrlClassifierFeatureBase::GetName(nsACString& aName) {
116 aName = mName;
117 return NS_OK;
120 NS_IMETHODIMP
121 UrlClassifierFeatureBase::GetTables(nsIUrlClassifierFeature::listType aListType,
122 nsTArray<nsCString>& aTables) {
123 if (aListType != nsIUrlClassifierFeature::blocklist &&
124 aListType != nsIUrlClassifierFeature::entitylist) {
125 return NS_ERROR_INVALID_ARG;
128 aTables = mTables[aListType].Clone();
129 return NS_OK;
132 NS_IMETHODIMP
133 UrlClassifierFeatureBase::HasTable(const nsACString& aTable,
134 nsIUrlClassifierFeature::listType aListType,
135 bool* aResult) {
136 NS_ENSURE_ARG_POINTER(aResult);
138 if (aListType != nsIUrlClassifierFeature::blocklist &&
139 aListType != nsIUrlClassifierFeature::entitylist) {
140 return NS_ERROR_INVALID_ARG;
143 *aResult = mTables[aListType].Contains(aTable);
144 return NS_OK;
147 NS_IMETHODIMP
148 UrlClassifierFeatureBase::HasHostInPreferences(
149 const nsACString& aHost, nsIUrlClassifierFeature::listType aListType,
150 nsACString& aPrefTableName, bool* aResult) {
151 NS_ENSURE_ARG_POINTER(aResult);
153 if (aListType != nsIUrlClassifierFeature::blocklist &&
154 aListType != nsIUrlClassifierFeature::entitylist) {
155 return NS_ERROR_INVALID_ARG;
158 *aResult = mHosts[aListType].Contains(aHost);
159 if (*aResult) {
160 aPrefTableName = mPrefTableNames[aListType];
162 return NS_OK;
165 NS_IMETHODIMP
166 UrlClassifierFeatureBase::GetExceptionHostList(nsACString& aList) {
167 aList = mExceptionHosts;
168 return NS_OK;
171 NS_IMETHODIMP
172 UrlClassifierFeatureAntiTrackingBase::GetExceptionHostList(nsACString& aList) {
173 if (!StaticPrefs::privacy_antitracking_enableWebcompat()) {
174 aList.Truncate();
175 return NS_OK;
178 return UrlClassifierFeatureBase::GetExceptionHostList(aList);
181 } // namespace net
182 } // namespace mozilla