summaryrefslogtreecommitdiffstats
path: root/netwerk/url-classifier/UrlClassifierFeatureBase.cpp
blob: 6f5924ab50af0753bec63fd7d5d11a5ed48dfc2c (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
/* -*- 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(nsIUrlClassifierExceptionListObserver)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(UrlClassifierFeatureBase)
NS_IMPL_RELEASE(UrlClassifierFeatureBase)

UrlClassifierFeatureBase::UrlClassifierFeatureBase(
    const nsACString& aName, const nsACString& aPrefBlocklistTables,
    const nsACString& aPrefEntitylistTables,
    const nsACString& aPrefBlocklistHosts,
    const nsACString& aPrefEntitylistHosts,
    const nsACString& aPrefBlocklistTableName,
    const nsACString& aPrefEntitylistTableName,
    const nsACString& aPrefExceptionHosts)
    : mName(aName), mPrefExceptionHosts(aPrefExceptionHosts) {
  static_assert(nsIUrlClassifierFeature::blocklist == 0,
                "nsIUrlClassifierFeature::blocklist must be 0");
  static_assert(nsIUrlClassifierFeature::entitylist == 1,
                "nsIUrlClassifierFeature::entitylist must be 1");

  mPrefTables[nsIUrlClassifierFeature::blocklist] = aPrefBlocklistTables;
  mPrefTables[nsIUrlClassifierFeature::entitylist] = aPrefEntitylistTables;

  mPrefHosts[nsIUrlClassifierFeature::blocklist] = aPrefBlocklistHosts;
  mPrefHosts[nsIUrlClassifierFeature::entitylist] = aPrefEntitylistHosts;

  mPrefTableNames[nsIUrlClassifierFeature::blocklist] = aPrefBlocklistTableName;
  mPrefTableNames[nsIUrlClassifierFeature::entitylist] =
      aPrefEntitylistTableName;
}

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<nsIUrlClassifierExceptionListService> exceptionListService =
      do_GetService("@mozilla.org/url-classifier/exception-list-service;1");
  if (NS_WARN_IF(!exceptionListService)) {
    return;
  }

  exceptionListService->RegisterAndRunExceptionListObserver(
      mName, mPrefExceptionHosts, 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<nsIUrlClassifierExceptionListService> exceptionListService =
      do_GetService("@mozilla.org/url-classifier/exception-list-service;1");
  if (exceptionListService) {
    exceptionListService->UnregisterExceptionListObserver(mName, this);
  }
}

NS_IMETHODIMP
UrlClassifierFeatureBase::OnExceptionListUpdate(const nsACString& aList) {
  mExceptionHosts = 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::blocklist &&
      aListType != nsIUrlClassifierFeature::entitylist) {
    return NS_ERROR_INVALID_ARG;
  }

  aTables = mTables[aListType].Clone();
  return NS_OK;
}

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

  if (aListType != nsIUrlClassifierFeature::blocklist &&
      aListType != nsIUrlClassifierFeature::entitylist) {
    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::blocklist &&
      aListType != nsIUrlClassifierFeature::entitylist) {
    return NS_ERROR_INVALID_ARG;
  }

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

NS_IMETHODIMP
UrlClassifierFeatureBase::GetExceptionHostList(nsACString& aList) {
  aList = mExceptionHosts;
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureAntiTrackingBase::GetExceptionHostList(nsACString& aList) {
  if (!StaticPrefs::privacy_antitracking_enableWebcompat()) {
    aList.Truncate();
    return NS_OK;
  }

  return UrlClassifierFeatureBase::GetExceptionHostList(aList);
}

}  // namespace net
}  // namespace mozilla