tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

UrlClassifierFeatureBase.cpp (5991B)


      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 "UrlClassifierFeatureBase.h"
      8 #include "Classifier.h"
      9 #include "mozilla/Preferences.h"
     10 #include "nsIUrlClassifierExceptionList.h"
     11 #include "nsIUrlClassifierExceptionListEntry.h"
     12 
     13 namespace mozilla {
     14 
     15 using namespace safebrowsing;
     16 
     17 namespace net {
     18 
     19 namespace {
     20 
     21 void OnPrefsChange(const char* aPrefName, void* aArray) {
     22  auto* array = static_cast<nsTArray<nsCString>*>(aArray);
     23  MOZ_ASSERT(array);
     24 
     25  nsAutoCString value;
     26  Preferences::GetCString(aPrefName, value);
     27  Classifier::SplitTables(value, *array);
     28 }
     29 
     30 }  // namespace
     31 
     32 NS_INTERFACE_MAP_BEGIN(UrlClassifierFeatureBase)
     33  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIUrlClassifierFeature)
     34  NS_INTERFACE_MAP_ENTRY(nsIUrlClassifierFeature)
     35  NS_INTERFACE_MAP_ENTRY(nsIUrlClassifierExceptionListObserver)
     36 NS_INTERFACE_MAP_END
     37 
     38 NS_IMPL_ADDREF(UrlClassifierFeatureBase)
     39 NS_IMPL_RELEASE(UrlClassifierFeatureBase)
     40 
     41 UrlClassifierFeatureBase::UrlClassifierFeatureBase(
     42    const nsACString& aName, const nsACString& aPrefBlocklistTables,
     43    const nsACString& aPrefEntitylistTables,
     44    const nsACString& aPrefBlocklistHosts,
     45    const nsACString& aPrefEntitylistHosts,
     46    const nsACString& aPrefBlocklistTableName,
     47    const nsACString& aPrefEntitylistTableName,
     48    const nsACString& aPrefExceptionHosts)
     49    : mName(aName), mPrefExceptionHosts(aPrefExceptionHosts) {
     50  static_assert(nsIUrlClassifierFeature::blocklist == 0,
     51                "nsIUrlClassifierFeature::blocklist must be 0");
     52  static_assert(nsIUrlClassifierFeature::entitylist == 1,
     53                "nsIUrlClassifierFeature::entitylist must be 1");
     54 
     55  mPrefTables[nsIUrlClassifierFeature::blocklist] = aPrefBlocklistTables;
     56  mPrefTables[nsIUrlClassifierFeature::entitylist] = aPrefEntitylistTables;
     57 
     58  mPrefHosts[nsIUrlClassifierFeature::blocklist] = aPrefBlocklistHosts;
     59  mPrefHosts[nsIUrlClassifierFeature::entitylist] = aPrefEntitylistHosts;
     60 
     61  mPrefTableNames[nsIUrlClassifierFeature::blocklist] = aPrefBlocklistTableName;
     62  mPrefTableNames[nsIUrlClassifierFeature::entitylist] =
     63      aPrefEntitylistTableName;
     64 }
     65 
     66 UrlClassifierFeatureBase::~UrlClassifierFeatureBase() = default;
     67 
     68 void UrlClassifierFeatureBase::InitializePreferences() {
     69  for (uint32_t i = 0; i < 2; ++i) {
     70    if (!mPrefTables[i].IsEmpty()) {
     71      Preferences::RegisterCallbackAndCall(OnPrefsChange, mPrefTables[i],
     72                                           &mTables[i]);
     73    }
     74 
     75    if (!mPrefHosts[i].IsEmpty()) {
     76      Preferences::RegisterCallbackAndCall(OnPrefsChange, mPrefHosts[i],
     77                                           &mHosts[i]);
     78    }
     79  }
     80 
     81  nsCOMPtr<nsIUrlClassifierExceptionListService> exceptionListService =
     82      do_GetService("@mozilla.org/url-classifier/exception-list-service;1");
     83  if (NS_WARN_IF(!exceptionListService)) {
     84    return;
     85  }
     86 
     87  exceptionListService->RegisterAndRunExceptionListObserver(
     88      mName, mPrefExceptionHosts, this);
     89 }
     90 
     91 void UrlClassifierFeatureBase::ShutdownPreferences() {
     92  for (uint32_t i = 0; i < 2; ++i) {
     93    if (!mPrefTables[i].IsEmpty()) {
     94      Preferences::UnregisterCallback(OnPrefsChange, mPrefTables[i],
     95                                      &mTables[i]);
     96    }
     97 
     98    if (!mPrefHosts[i].IsEmpty()) {
     99      Preferences::UnregisterCallback(OnPrefsChange, mPrefHosts[i], &mHosts[i]);
    100    }
    101  }
    102 
    103  nsCOMPtr<nsIUrlClassifierExceptionListService> exceptionListService =
    104      do_GetService("@mozilla.org/url-classifier/exception-list-service;1");
    105  if (exceptionListService) {
    106    exceptionListService->UnregisterExceptionListObserver(mName, this);
    107  }
    108 }
    109 
    110 NS_IMETHODIMP
    111 UrlClassifierFeatureBase::OnExceptionListUpdate(
    112    nsIUrlClassifierExceptionList* aList) {
    113  mExceptionList = aList;
    114 
    115  return NS_OK;
    116 }
    117 
    118 NS_IMETHODIMP
    119 UrlClassifierFeatureBase::GetName(nsACString& aName) {
    120  aName = mName;
    121  return NS_OK;
    122 }
    123 
    124 NS_IMETHODIMP
    125 UrlClassifierFeatureBase::GetTables(nsIUrlClassifierFeature::listType aListType,
    126                                    nsTArray<nsCString>& aTables) {
    127  if (aListType != nsIUrlClassifierFeature::blocklist &&
    128      aListType != nsIUrlClassifierFeature::entitylist) {
    129    return NS_ERROR_INVALID_ARG;
    130  }
    131 
    132  aTables = mTables[aListType].Clone();
    133  return NS_OK;
    134 }
    135 
    136 NS_IMETHODIMP
    137 UrlClassifierFeatureBase::HasTable(const nsACString& aTable,
    138                                   nsIUrlClassifierFeature::listType aListType,
    139                                   bool* aResult) {
    140  NS_ENSURE_ARG_POINTER(aResult);
    141 
    142  if (aListType != nsIUrlClassifierFeature::blocklist &&
    143      aListType != nsIUrlClassifierFeature::entitylist) {
    144    return NS_ERROR_INVALID_ARG;
    145  }
    146 
    147  *aResult = mTables[aListType].Contains(aTable);
    148  return NS_OK;
    149 }
    150 
    151 NS_IMETHODIMP
    152 UrlClassifierFeatureBase::HasHostInPreferences(
    153    const nsACString& aHost, nsIUrlClassifierFeature::listType aListType,
    154    nsACString& aPrefTableName, bool* aResult) {
    155  NS_ENSURE_ARG_POINTER(aResult);
    156 
    157  if (aListType != nsIUrlClassifierFeature::blocklist &&
    158      aListType != nsIUrlClassifierFeature::entitylist) {
    159    return NS_ERROR_INVALID_ARG;
    160  }
    161 
    162  *aResult = mHosts[aListType].Contains(aHost);
    163  if (*aResult) {
    164    aPrefTableName = mPrefTableNames[aListType];
    165  }
    166  return NS_OK;
    167 }
    168 
    169 NS_IMETHODIMP
    170 UrlClassifierFeatureBase::GetExceptionList(
    171    nsIUrlClassifierExceptionList** aList) {
    172  NS_ENSURE_ARG_POINTER(aList);
    173 
    174  *aList = mExceptionList;
    175  NS_IF_ADDREF(*aList);
    176 
    177  return NS_OK;
    178 }
    179 
    180 NS_IMETHODIMP
    181 UrlClassifierFeatureAntiTrackingBase::GetExceptionList(
    182    nsIUrlClassifierExceptionList** aList) {
    183  if (!StaticPrefs::privacy_antitracking_enableWebcompat()) {
    184    *aList = nullptr;
    185    return NS_OK;
    186  }
    187 
    188  return UrlClassifierFeatureBase::GetExceptionList(aList);
    189 }
    190 
    191 }  // namespace net
    192 }  // namespace mozilla