nsIDNService.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/. */
  5. #ifndef nsIDNService_h__
  6. #define nsIDNService_h__
  7. #include "nsIIDNService.h"
  8. #include "nsCOMPtr.h"
  9. #include "nsIObserver.h"
  10. #include "nsUnicodeScriptCodes.h"
  11. #include "nsWeakReference.h"
  12. #include "unicode/uidna.h"
  13. #include "nsString.h"
  14. class nsIPrefBranch;
  15. //-----------------------------------------------------------------------------
  16. // nsIDNService
  17. //-----------------------------------------------------------------------------
  18. class nsIDNService final : public nsIIDNService,
  19. public nsIObserver,
  20. public nsSupportsWeakReference
  21. {
  22. public:
  23. NS_DECL_THREADSAFE_ISUPPORTS
  24. NS_DECL_NSIIDNSERVICE
  25. NS_DECL_NSIOBSERVER
  26. nsIDNService();
  27. nsresult Init();
  28. protected:
  29. virtual ~nsIDNService();
  30. private:
  31. enum stringPrepFlag {
  32. eStringPrepForDNS,
  33. eStringPrepForUI,
  34. eStringPrepIgnoreErrors
  35. };
  36. /**
  37. * Convert the following characters that must be recognized as label
  38. * separators per RFC 3490 to ASCII full stop characters
  39. *
  40. * U+3002 (ideographic full stop)
  41. * U+FF0E (fullwidth full stop)
  42. * U+FF61 (halfwidth ideographic full stop)
  43. */
  44. void normalizeFullStops(nsAString& s);
  45. /**
  46. * Convert and encode a DNS label in ACE/punycode.
  47. * @param flag
  48. * if eStringPrepIgnoreErrors, all non-ASCII labels are
  49. * converted to punycode.
  50. * if eStringPrepForUI, only labels that are considered safe
  51. * for display are converted.
  52. * @see isLabelSafe
  53. * if eStringPrepForDNS and stringPrep finds an illegal
  54. * character, returns NS_FAILURE and out is empty
  55. */
  56. nsresult stringPrepAndACE(const nsAString& in, nsACString& out,
  57. stringPrepFlag flag);
  58. /**
  59. * Convert a DNS label using the stringprep profile defined in RFC 3454
  60. */
  61. nsresult stringPrep(const nsAString& in, nsAString& out, stringPrepFlag flag);
  62. /**
  63. * Decode an ACE-encoded DNS label to UTF-8
  64. *
  65. * @param flag
  66. * if eStringPrepForUI and the label is not considered safe to
  67. * display, the output is the same as the input
  68. * @see isLabelSafe
  69. */
  70. nsresult decodeACE(const nsACString& in, nsACString& out,
  71. stringPrepFlag flag);
  72. /**
  73. * Convert complete domain names between UTF8 and ACE and vice versa
  74. *
  75. * @param flag is passed to decodeACE or stringPrepAndACE for each
  76. * label individually, so the output may contain some labels in
  77. * punycode and some in UTF-8
  78. */
  79. nsresult UTF8toACE(const nsACString& input, nsACString& ace,
  80. stringPrepFlag flag);
  81. nsresult ACEtoUTF8(const nsACString& input, nsACString& _retval,
  82. stringPrepFlag flag);
  83. bool isInWhitelist(const nsACString &host);
  84. void prefsChanged(nsIPrefBranch *prefBranch, const char16_t *pref);
  85. /**
  86. * Determine whether a label is considered safe to display to the user
  87. * according to the algorithm defined in UTR 39 and the profile
  88. * selected in mRestrictionProfile.
  89. *
  90. * For the ASCII-only profile, returns false for all labels containing
  91. * non-ASCII characters.
  92. *
  93. * For the other profiles, returns false for labels containing any of
  94. * the following:
  95. *
  96. * Characters in scripts other than the "recommended scripts" and
  97. * "aspirational scripts" defined in
  98. * http://www.unicode.org/reports/tr31/#Table_Recommended_Scripts
  99. * and http://www.unicode.org/reports/tr31/#Aspirational_Use_Scripts
  100. * This includes codepoints that are not defined as Unicode
  101. * characters
  102. *
  103. * Illegal combinations of scripts (@see illegalScriptCombo)
  104. *
  105. * Numbers from more than one different numbering system
  106. *
  107. * Sequences of the same non-spacing mark
  108. *
  109. * Both simplified-only and traditional-only Chinese characters
  110. * XXX this test was disabled by bug 857481
  111. */
  112. bool isLabelSafe(const nsAString &label);
  113. /**
  114. * Determine whether a combination of scripts in a single label is
  115. * permitted according to the algorithm defined in UTR 39 and the
  116. * profile selected in mRestrictionProfile.
  117. *
  118. * For the "Highly restrictive" profile, all characters in each
  119. * identifier must be from a single script, or from the combinations:
  120. * Latin + Han + Hiragana + Katakana;
  121. * Latin + Han + Bopomofo; or
  122. * Latin + Han + Hangul
  123. *
  124. * For the "Moderately restrictive" profile, Latin is also allowed
  125. * with other scripts except Cyrillic and Greek
  126. */
  127. bool illegalScriptCombo(mozilla::unicode::Script script,
  128. int32_t& savedScript);
  129. /**
  130. * Convert a DNS label from ASCII to Unicode using IDNA2008
  131. */
  132. nsresult IDNA2008ToUnicode(const nsACString& input, nsAString& output);
  133. /**
  134. * Convert a DNS label to a normalized form conforming to IDNA2008
  135. */
  136. nsresult IDNA2008StringPrep(const nsAString& input, nsAString& output,
  137. stringPrepFlag flag);
  138. UIDNA* mIDNA;
  139. nsXPIDLString mIDNBlacklist;
  140. /**
  141. * Flag set by the pref network.IDN_show_punycode. When it is true,
  142. * IDNs containing non-ASCII characters are always displayed to the
  143. * user in punycode
  144. */
  145. bool mShowPunycode;
  146. /**
  147. * Restriction-level Detection profiles defined in UTR 39
  148. * http://www.unicode.org/reports/tr39/#Restriction_Level_Detection,
  149. * and selected by the pref network.IDN.restriction_profile
  150. */
  151. enum restrictionProfile {
  152. eASCIIOnlyProfile,
  153. eHighlyRestrictiveProfile,
  154. eModeratelyRestrictiveProfile
  155. };
  156. restrictionProfile mRestrictionProfile;
  157. nsCOMPtr<nsIPrefBranch> mIDNWhitelistPrefBranch;
  158. bool mIDNUseWhitelist;
  159. };
  160. #endif // nsIDNService_h__