DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (ed839e4a3021)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "UrlClassifierFeatureBase.h"
#include "Classifier.h"
#include "mozilla/Preferences.h"

namespace mozilla {

using namespace safebrowsing;

namespace net {

namespace {

void OnPrefsChange(const char* aPrefName, void* aArray) {
  auto array = static_cast<nsTArray<nsCString>*>(aArray);
  MOZ_ASSERT(array);

  nsAutoCString value;
  Preferences::GetCString(aPrefName, value);
  Classifier::SplitTables(value, *array);
}

}  // namespace

NS_INTERFACE_MAP_BEGIN(UrlClassifierFeatureBase)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIUrlClassifierFeature)
  NS_INTERFACE_MAP_ENTRY(nsIUrlClassifierFeature)
  NS_INTERFACE_MAP_ENTRY(nsIUrlClassifierSkipListObserver)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(UrlClassifierFeatureBase)
NS_IMPL_RELEASE(UrlClassifierFeatureBase)

UrlClassifierFeatureBase::UrlClassifierFeatureBase(
    const nsACString& aName, const nsACString& aPrefBlacklistTables,
    const nsACString& aPrefWhitelistTables,
    const nsACString& aPrefBlacklistHosts,
    const nsACString& aPrefWhitelistHosts,
    const nsACString& aPrefBlacklistTableName,
    const nsACString& aPrefWhitelistTableName, const nsACString& aPrefSkipHosts)
    : mName(aName), mPrefSkipHosts(aPrefSkipHosts) {
  static_assert(nsIUrlClassifierFeature::blacklist == 0,
                "nsIUrlClassifierFeature::blacklist must be 0");
  static_assert(nsIUrlClassifierFeature::whitelist == 1,
                "nsIUrlClassifierFeature::whitelist must be 1");

  mPrefTables[nsIUrlClassifierFeature::blacklist] = aPrefBlacklistTables;
  mPrefTables[nsIUrlClassifierFeature::whitelist] = aPrefWhitelistTables;

  mPrefHosts[nsIUrlClassifierFeature::blacklist] = aPrefBlacklistHosts;
  mPrefHosts[nsIUrlClassifierFeature::whitelist] = aPrefWhitelistHosts;

  mPrefTableNames[nsIUrlClassifierFeature::blacklist] = aPrefBlacklistTableName;
  mPrefTableNames[nsIUrlClassifierFeature::whitelist] = aPrefWhitelistTableName;
}

UrlClassifierFeatureBase::~UrlClassifierFeatureBase() = default;

void UrlClassifierFeatureBase::InitializePreferences() {
  for (uint32_t i = 0; i < 2; ++i) {
    if (!mPrefTables[i].IsEmpty()) {
      Preferences::RegisterCallbackAndCall(OnPrefsChange, mPrefTables[i],
                                           &mTables[i]);
    }

    if (!mPrefHosts[i].IsEmpty()) {
      Preferences::RegisterCallbackAndCall(OnPrefsChange, mPrefHosts[i],
                                           &mHosts[i]);
    }
  }

  nsCOMPtr<nsIUrlClassifierSkipListService> skipListService =
      do_GetService("@mozilla.org/url-classifier/skip-list-service;1");
  if (NS_WARN_IF(!skipListService)) {
    return;
  }

  skipListService->RegisterAndRunSkipListObserver(mName, mPrefSkipHosts, this);
}

void UrlClassifierFeatureBase::ShutdownPreferences() {
  for (uint32_t i = 0; i < 2; ++i) {
    if (!mPrefTables[i].IsEmpty()) {
      Preferences::UnregisterCallback(OnPrefsChange, mPrefTables[i],
                                      &mTables[i]);
    }

    if (!mPrefHosts[i].IsEmpty()) {
      Preferences::UnregisterCallback(OnPrefsChange, mPrefHosts[i], &mHosts[i]);
    }
  }

  nsCOMPtr<nsIUrlClassifierSkipListService> skipListService =
      do_GetService("@mozilla.org/url-classifier/skip-list-service;1");
  if (skipListService) {
    skipListService->UnregisterSkipListObserver(mName, this);
  }
}

NS_IMETHODIMP
UrlClassifierFeatureBase::OnSkipListUpdate(const nsACString& aList) {
  mSkipHosts = aList;
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureBase::GetName(nsACString& aName) {
  aName = mName;
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureBase::GetTables(nsIUrlClassifierFeature::listType aListType,
                                    nsTArray<nsCString>& aTables) {
  if (aListType != nsIUrlClassifierFeature::blacklist &&
      aListType != nsIUrlClassifierFeature::whitelist) {
    return NS_ERROR_INVALID_ARG;
  }

  aTables = mTables[aListType];
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureBase::HasTable(const nsACString& aTable,
                                   nsIUrlClassifierFeature::listType aListType,
                                   bool* aResult) {
  NS_ENSURE_ARG_POINTER(aResult);

  if (aListType != nsIUrlClassifierFeature::blacklist &&
      aListType != nsIUrlClassifierFeature::whitelist) {
    return NS_ERROR_INVALID_ARG;
  }

  *aResult = mTables[aListType].Contains(aTable);
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureBase::HasHostInPreferences(
    const nsACString& aHost, nsIUrlClassifierFeature::listType aListType,
    nsACString& aPrefTableName, bool* aResult) {
  NS_ENSURE_ARG_POINTER(aResult);

  if (aListType != nsIUrlClassifierFeature::blacklist &&
      aListType != nsIUrlClassifierFeature::whitelist) {
    return NS_ERROR_INVALID_ARG;
  }

  *aResult = mHosts[aListType].Contains(aHost);
  if (*aResult) {
    aPrefTableName = mPrefTableNames[aListType];
  }
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureBase::GetSkipHostList(nsACString& aList) {
  aList = mSkipHosts;
  return NS_OK;
}

}  // namespace net
}  // namespace mozilla