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"
13 using namespace safebrowsing
;
19 void OnPrefsChange(const char* aPrefName
, void* aArray
) {
20 auto* array
= static_cast<nsTArray
<nsCString
>*>(aArray
);
24 Preferences::GetCString(aPrefName
, value
);
25 Classifier::SplitTables(value
, *array
);
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
)
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
],
73 if (!mPrefHosts
[i
].IsEmpty()) {
74 Preferences::RegisterCallbackAndCall(OnPrefsChange
, mPrefHosts
[i
],
79 nsCOMPtr
<nsIUrlClassifierExceptionListService
> exceptionListService
=
80 do_GetService("@mozilla.org/url-classifier/exception-list-service;1");
81 if (NS_WARN_IF(!exceptionListService
)) {
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
],
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);
109 UrlClassifierFeatureBase::OnExceptionListUpdate(const nsACString
& aList
) {
110 mExceptionHosts
= aList
;
115 UrlClassifierFeatureBase::GetName(nsACString
& aName
) {
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();
133 UrlClassifierFeatureBase::HasTable(const nsACString
& aTable
,
134 nsIUrlClassifierFeature::listType aListType
,
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
);
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
);
160 aPrefTableName
= mPrefTableNames
[aListType
];
166 UrlClassifierFeatureBase::GetExceptionHostList(nsACString
& aList
) {
167 aList
= mExceptionHosts
;
172 UrlClassifierFeatureAntiTrackingBase::GetExceptionHostList(nsACString
& aList
) {
173 if (!StaticPrefs::privacy_antitracking_enableWebcompat()) {
178 return UrlClassifierFeatureBase::GetExceptionHostList(aList
);
182 } // namespace mozilla