tor-browser

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

UrlClassifierFeaturePhishingProtection.cpp (4086B)


      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 "UrlClassifierFeaturePhishingProtection.h"
      8 #include "mozilla/StaticPrefs_browser.h"
      9 #include "nsCOMPtr.h"
     10 
     11 namespace mozilla {
     12 namespace net {
     13 
     14 struct UrlClassifierFeaturePhishingProtection::PhishingProtectionFeature {
     15  const char* mName;
     16  const char* mBlocklistPrefTables;
     17  bool (*mPref)();
     18 
     19  RefPtr<UrlClassifierFeaturePhishingProtection> mFeature;
     20 };
     21 
     22 namespace {
     23 
     24 MOZ_RUNINIT struct UrlClassifierFeaturePhishingProtection::
     25    PhishingProtectionFeature sPhishingProtectionFeaturesMap[] = {
     26        {"malware", "urlclassifier.malwareTable",
     27         StaticPrefs::browser_safebrowsing_malware_enabled},
     28        {"phishing", "urlclassifier.phishTable",
     29         StaticPrefs::browser_safebrowsing_phishing_enabled},
     30        {"blockedURIs", "urlclassifier.blockedTable",
     31         StaticPrefs::browser_safebrowsing_blockedURIs_enabled},
     32 };
     33 
     34 }  // namespace
     35 
     36 UrlClassifierFeaturePhishingProtection::UrlClassifierFeaturePhishingProtection(
     37    const UrlClassifierFeaturePhishingProtection::PhishingProtectionFeature&
     38        aFeature)
     39    : UrlClassifierFeatureBase(
     40          nsDependentCString(aFeature.mName),
     41          nsDependentCString(aFeature.mBlocklistPrefTables),
     42          ""_ns,    // aPrefEntitylistPrefTbles,
     43          ""_ns,    // aPrefBlocklistHosts
     44          ""_ns,    // aPrefEntitylistHosts
     45          ""_ns,    // aPrefBlocklistTableName
     46          ""_ns,    // aPrefEntitylistTableName
     47          ""_ns) {  // aPrefExceptionHosts
     48 }
     49 
     50 /* static */
     51 void UrlClassifierFeaturePhishingProtection::GetFeatureNames(
     52    nsTArray<nsCString>& aArray) {
     53  for (const PhishingProtectionFeature& feature :
     54       sPhishingProtectionFeaturesMap) {
     55    if (feature.mPref()) {
     56      aArray.AppendElement(nsDependentCString(feature.mName));
     57    }
     58  }
     59 }
     60 
     61 /* static */
     62 void UrlClassifierFeaturePhishingProtection::MaybeInitialize() {
     63  for (PhishingProtectionFeature& feature : sPhishingProtectionFeaturesMap) {
     64    if (!feature.mFeature && feature.mPref()) {
     65      feature.mFeature = new UrlClassifierFeaturePhishingProtection(feature);
     66      feature.mFeature->InitializePreferences();
     67    }
     68  }
     69 }
     70 
     71 /* static */
     72 void UrlClassifierFeaturePhishingProtection::MaybeShutdown() {
     73  for (PhishingProtectionFeature& feature : sPhishingProtectionFeaturesMap) {
     74    if (feature.mFeature) {
     75      feature.mFeature->ShutdownPreferences();
     76      feature.mFeature = nullptr;
     77    }
     78  }
     79 }
     80 
     81 /* static */
     82 void UrlClassifierFeaturePhishingProtection::MaybeCreate(
     83    nsTArray<RefPtr<nsIUrlClassifierFeature>>& aFeatures) {
     84  MaybeInitialize();
     85 
     86  for (const PhishingProtectionFeature& feature :
     87       sPhishingProtectionFeaturesMap) {
     88    if (feature.mPref()) {
     89      MOZ_ASSERT(feature.mFeature);
     90      aFeatures.AppendElement(feature.mFeature);
     91    }
     92  }
     93 }
     94 
     95 /* static */
     96 already_AddRefed<nsIUrlClassifierFeature>
     97 UrlClassifierFeaturePhishingProtection::GetIfNameMatches(
     98    const nsACString& aName) {
     99  MaybeInitialize();
    100 
    101  for (const PhishingProtectionFeature& feature :
    102       sPhishingProtectionFeaturesMap) {
    103    if (feature.mPref() && aName.Equals(feature.mName)) {
    104      MOZ_ASSERT(feature.mFeature);
    105      nsCOMPtr<nsIUrlClassifierFeature> self = feature.mFeature.get();
    106      return self.forget();
    107    }
    108  }
    109 
    110  return nullptr;
    111 }
    112 
    113 NS_IMETHODIMP
    114 UrlClassifierFeaturePhishingProtection::ProcessChannel(
    115    nsIChannel* aChannel, const nsTArray<nsCString>& aList,
    116    const nsTArray<nsCString>& aHashes, bool* aShouldContinue) {
    117  return NS_ERROR_NOT_IMPLEMENTED;
    118 }
    119 
    120 NS_IMETHODIMP
    121 UrlClassifierFeaturePhishingProtection::GetURIByListType(
    122    nsIChannel* aChannel, nsIUrlClassifierFeature::listType aListType,
    123    nsIUrlClassifierFeature::URIType* aURIType, nsIURI** aURI) {
    124  return NS_ERROR_NOT_IMPLEMENTED;
    125 }
    126 
    127 }  // namespace net
    128 }  // namespace mozilla