File: UrlClassifierExceptionListEntry.cpp

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (203 lines) | stat: -rw-r--r-- 6,913 bytes parent folder | download | duplicates (3)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* -*- 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 "UrlClassifierExceptionListEntry.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/StaticPrefs_privacy.h"

namespace mozilla::net {

NS_IMPL_ISUPPORTS(UrlClassifierExceptionListEntry,
                  nsIUrlClassifierExceptionListEntry)

NS_IMETHODIMP
UrlClassifierExceptionListEntry::Init(
    nsIUrlClassifierExceptionListEntry::Category aCategory,
    const nsACString& aUrlPattern, const nsACString& aTopLevelUrlPattern,
    bool aIsPrivateBrowsingOnly,
    const nsTArray<nsCString>& aFilterContentBlockingCategories,
    const nsTArray<nsCString>& aClassifierFeatures) {
  // Validate category.
  NS_ENSURE_TRUE(
      aCategory == nsIUrlClassifierExceptionListEntry::Category::
                       CATEGORY_INTERNAL_PREF ||
          aCategory ==
              nsIUrlClassifierExceptionListEntry::Category::CATEGORY_BASELINE ||
          aCategory == nsIUrlClassifierExceptionListEntry::Category::
                           CATEGORY_CONVENIENCE,
      NS_ERROR_INVALID_ARG);
  mCategory = aCategory;
  mUrlPattern = aUrlPattern;
  mTopLevelUrlPattern = aTopLevelUrlPattern;
  mIsPrivateBrowsingOnly = aIsPrivateBrowsingOnly;
  mFilterContentBlockingCategories = aFilterContentBlockingCategories.Clone();
  mClassifierFeatures = aClassifierFeatures.Clone();

  // Create pattern from urlPattern and topLevelUrlPattern strings.
  ErrorResult error;
  mMatcher = new extensions::MatchPatternCore(
      NS_ConvertUTF8toUTF16(mUrlPattern), false, false, error);
  RETURN_NSRESULT_ON_FAILURE(error);

  if (!mTopLevelUrlPattern.IsEmpty()) {
    mTopLevelMatcher = new extensions::MatchPatternCore(
        NS_ConvertUTF8toUTF16(mTopLevelUrlPattern), false, false, error);
    RETURN_NSRESULT_ON_FAILURE(error);
  }
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierExceptionListEntry::Matches(nsIURI* aURI, nsIURI* aTopLevelURI,
                                         bool aIsPrivateBrowsing,
                                         bool* aResult) {
  NS_ENSURE_ARG_POINTER(aURI);
  NS_ENSURE_ARG_POINTER(aResult);
  *aResult = false;

  MOZ_ASSERT(
      mCategory == nsIUrlClassifierExceptionListEntry::Category::
                       CATEGORY_INTERNAL_PREF ||
      mCategory ==
          nsIUrlClassifierExceptionListEntry::Category::CATEGORY_BASELINE ||
      mCategory ==
          nsIUrlClassifierExceptionListEntry::Category::CATEGORY_CONVENIENCE);

  // If the entry category is not internal pref, we need to check if the
  // baseline and convenience exceptions are enabled.
  if (mCategory !=
      nsIUrlClassifierExceptionListEntry::Category::CATEGORY_INTERNAL_PREF) {
    bool baselineEnabled =
        StaticPrefs::privacy_trackingprotection_allow_list_baseline_enabled();
    bool convenienceEnabled = StaticPrefs::
        privacy_trackingprotection_allow_list_convenience_enabled();

    // If baseline is disabled, we should not allow convenience exceptions
    // either.
    if (!baselineEnabled) {
      convenienceEnabled = false;
    }

    // Check if the entry category is enabled. CATEGORY_INTERNAL_PREF always
    // applies.
    if ((mCategory ==
             nsIUrlClassifierExceptionListEntry::Category::CATEGORY_BASELINE &&
         !baselineEnabled) ||
        (mCategory == nsIUrlClassifierExceptionListEntry::Category::
                          CATEGORY_CONVENIENCE &&
         !convenienceEnabled)) {
      return NS_OK;
    }
  }

  // Entry is scoped to private browsing only and we're not in private browsing.
  if (!aIsPrivateBrowsing && mIsPrivateBrowsingOnly) {
    return NS_OK;
  }

  // Next, check if the current content blocking category pref matches the
  // allowed content blocking categories for this exception entry.
  if (!mFilterContentBlockingCategories.IsEmpty()) {
    nsCString prefValue;
    nsresult rv =
        Preferences::GetCString("browser.contentblocking.category", prefValue);

    // If the pref is not set this check is skipped.
    if (NS_SUCCEEDED(rv) && !prefValue.IsEmpty()) {
      if (!mFilterContentBlockingCategories.Contains(prefValue)) {
        return NS_OK;
      }
    }
  }

  // Check if the load URI matches the urlPattern.
  if (!mMatcher->Matches(aURI)) {
    return NS_OK;
  }

  // If this entry filters for top level site, check if the top level URI
  // matches the topLevelUrlPattern. If the entry filters for top level site,
  // but the caller does not provide one, we will not match.
  if (mTopLevelMatcher &&
      (!aTopLevelURI || !mTopLevelMatcher->Matches(aTopLevelURI))) {
    return NS_OK;
  }

  *aResult = true;
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierExceptionListEntry::GetCategory(
    nsIUrlClassifierExceptionListEntry::Category* aCategory) {
  *aCategory = mCategory;
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierExceptionListEntry::GetUrlPattern(nsACString& aUrlPattern) {
  aUrlPattern = mUrlPattern;
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierExceptionListEntry::GetTopLevelUrlPattern(
    nsACString& aTopLevelUrlPattern) {
  aTopLevelUrlPattern = mTopLevelUrlPattern;
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierExceptionListEntry::GetIsPrivateBrowsingOnly(
    bool* aIsPrivateBrowsingOnly) {
  *aIsPrivateBrowsingOnly = mIsPrivateBrowsingOnly;
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierExceptionListEntry::GetFilterContentBlockingCategories(
    nsTArray<nsCString>& aFilterContentBlockingCategories) {
  aFilterContentBlockingCategories = mFilterContentBlockingCategories.Clone();
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierExceptionListEntry::GetClassifierFeatures(
    nsTArray<nsCString>& aClassifierFeatures) {
  aClassifierFeatures = mClassifierFeatures.Clone();
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierExceptionListEntry::Describe(nsACString& aDescription) {
  nsAutoCString categories;
  for (const auto& category : mFilterContentBlockingCategories) {
    if (!categories.IsEmpty()) {
      categories.AppendLiteral(", ");
    }
    categories.Append(category);
  }

  nsAutoCString classifierFeatures;
  for (const auto& feature : mClassifierFeatures) {
    if (!classifierFeatures.IsEmpty()) {
      classifierFeatures.AppendLiteral(", ");
    }
    classifierFeatures.Append(feature);
  }

  aDescription.AppendPrintf(
      "UrlClassifierExceptionListEntry(urlPattern='%s', "
      "topLevelUrlPattern='%s', isPrivateBrowsingOnly=%s, "
      "filterContentBlockingCategories=[%s], classifierFeatures=[%s])",
      mUrlPattern.get(), mTopLevelUrlPattern.get(),
      mIsPrivateBrowsingOnly ? "true" : "false", categories.get(),
      classifierFeatures.get());

  return NS_OK;
}

}  // namespace mozilla::net