Bug 1794427 - Origin Controls reflect implicit access through content scripts r=willd...
[gecko.git] / netwerk / dns / IDNBlocklistUtils.h
blob6e1c6c0a77b755bb8b05b1f2fd9f2a0153f750fa
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef IDNBlocklistUtils_h__
7 #define IDNBlocklistUtils_h__
9 #include <utility>
10 #include "nsTArray.h"
12 namespace mozilla {
13 namespace net {
15 // A blocklist range is defined as all of the characters between:
16 // { firstCharacterInRange, lastCharacterInRange }
17 using BlocklistRange = std::pair<char16_t, char16_t>;
19 // Used to perform a binary search of the needle in the sorted array of pairs
20 class BlocklistPairToCharComparator {
21 public:
22 bool Equals(const BlocklistRange& pair, char16_t needle) const {
23 // If the needle is between pair.first and pair.second it
24 // is part of the range.
25 return pair.first <= needle && needle <= pair.second;
28 bool LessThan(const BlocklistRange& pair, char16_t needle) const {
29 // The needle has to be larger than the second value,
30 // otherwise it may be equal.
31 return pair.second < needle;
35 // Used to sort the array of pairs
36 class BlocklistEntryComparator {
37 public:
38 bool Equals(const BlocklistRange& a, const BlocklistRange& b) const {
39 return a.first == b.first && a.second == b.second;
42 bool LessThan(const BlocklistRange& a, const BlocklistRange& b) const {
43 return a.first < b.first;
47 // Returns true if the char can be found in the blocklist
48 inline bool CharInBlocklist(char16_t aChar,
49 const nsTArray<BlocklistRange>& aBlocklist) {
50 return aBlocklist.ContainsSorted(aChar, BlocklistPairToCharComparator());
53 // Initializes the blocklist based on the statically defined list and the
54 // values of the following preferences:
55 // - network.IDN.extra_allowed_chars
56 // - network.IDN.extra_blocked_chars
57 void InitializeBlocklist(nsTArray<BlocklistRange>& aBlocklist);
59 void RemoveCharFromBlocklist(char16_t aChar,
60 nsTArray<BlocklistRange>& aBlocklist);
62 } // namespace net
63 } // namespace mozilla
65 #endif // IDNBlocklistUtils_h__