nsUrlClassifierUtils.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef nsUrlClassifierUtils_h_
  5. #define nsUrlClassifierUtils_h_
  6. #include "nsAutoPtr.h"
  7. #include "nsIUrlClassifierUtils.h"
  8. #include "nsClassHashtable.h"
  9. #include "nsIObserver.h"
  10. class nsUrlClassifierUtils final : public nsIUrlClassifierUtils,
  11. public nsIObserver
  12. {
  13. public:
  14. typedef nsClassHashtable<nsCStringHashKey, nsCString> ProviderDictType;
  15. private:
  16. /**
  17. * A fast, bit-vector map for ascii characters.
  18. *
  19. * Internally stores 256 bits in an array of 8 ints.
  20. * Does quick bit-flicking to lookup needed characters.
  21. */
  22. class Charmap
  23. {
  24. public:
  25. Charmap(uint32_t b0, uint32_t b1, uint32_t b2, uint32_t b3,
  26. uint32_t b4, uint32_t b5, uint32_t b6, uint32_t b7)
  27. {
  28. mMap[0] = b0; mMap[1] = b1; mMap[2] = b2; mMap[3] = b3;
  29. mMap[4] = b4; mMap[5] = b5; mMap[6] = b6; mMap[7] = b7;
  30. }
  31. /**
  32. * Do a quick lookup to see if the letter is in the map.
  33. */
  34. bool Contains(unsigned char c) const
  35. {
  36. return mMap[c >> 5] & (1 << (c & 31));
  37. }
  38. private:
  39. // Store the 256 bits in an 8 byte array.
  40. uint32_t mMap[8];
  41. };
  42. public:
  43. nsUrlClassifierUtils();
  44. NS_DECL_THREADSAFE_ISUPPORTS
  45. NS_DECL_NSIURLCLASSIFIERUTILS
  46. NS_DECL_NSIOBSERVER
  47. nsresult Init();
  48. nsresult CanonicalizeHostname(const nsACString & hostname,
  49. nsACString & _retval);
  50. nsresult CanonicalizePath(const nsACString & url, nsACString & _retval);
  51. // This function will encode all "special" characters in typical url encoding,
  52. // that is %hh where h is a valid hex digit. The characters which are encoded
  53. // by this function are any ascii characters under 32(control characters and
  54. // space), 37(%), and anything 127 or above (special characters). Url is the
  55. // string to encode, ret is the encoded string. Function returns true if
  56. // ret != url.
  57. bool SpecialEncode(const nsACString & url,
  58. bool foldSlashes,
  59. nsACString & _retval);
  60. void ParseIPAddress(const nsACString & host, nsACString & _retval);
  61. void CanonicalNum(const nsACString & num,
  62. uint32_t bytes,
  63. bool allowOctal,
  64. nsACString & _retval);
  65. private:
  66. ~nsUrlClassifierUtils() {}
  67. // Disallow copy constructor
  68. nsUrlClassifierUtils(const nsUrlClassifierUtils&);
  69. // Function to tell if we should encode a character.
  70. bool ShouldURLEscape(const unsigned char c) const;
  71. void CleanupHostname(const nsACString & host, nsACString & _retval);
  72. nsresult ReadProvidersFromPrefs(ProviderDictType& aDict);
  73. nsAutoPtr<Charmap> mEscapeCharmap;
  74. // The provider lookup table and its mutex.
  75. ProviderDictType mProviderDict;
  76. mozilla::Mutex mProviderDictLock;
  77. };
  78. #endif // nsUrlClassifierUtils_h_