Bug 1489949 - Port bug 1488659: Replace XPCOM use of nsICharsetDetector, part 1. r=Ehsan
authorJorg K <jorgk@jorgk.com>
Mon, 10 Sep 2018 06:11:00 +0200
changeset 33117 cacb2b5c2373484ecbf434c126e55750c8b2d993
parent 33116 5b512cb296c7a8b087ab94a24801c991e0d2fbad
child 33118 2275b3b53ada2a4835d57caf207c656b4011bdbb
push id387
push userclokep@gmail.com
push dateMon, 10 Dec 2018 21:30:47 +0000
reviewersEhsan
bugs1489949, 1488659
Bug 1489949 - Port bug 1488659: Replace XPCOM use of nsICharsetDetector, part 1. r=Ehsan
mailnews/base/util/moz.build
mailnews/base/util/nsMsgUtils.cpp
--- a/mailnews/base/util/moz.build
+++ b/mailnews/base/util/moz.build
@@ -65,16 +65,19 @@ EXTRA_JS_MODULES += [
     'OAuth2.jsm',
     'OAuth2Providers.jsm',
     'StringBundle.js',
     'templateUtils.js',
     'traceHelper.js',
 ]
 
 LOCAL_INCLUDES += [
+  '/%s/extensions/universalchardet/src/base' % CONFIG['mozreltopsrcdir'],
+  '/%s/extensions/universalchardet/src/xpcom' % CONFIG['mozreltopsrcdir'],
+  '/%s/intl/chardet' % CONFIG['mozreltopsrcdir'],
   '/%s/netwerk/base' % CONFIG['mozreltopsrcdir']
 ]
 
 FINAL_LIBRARY = 'mail'
 
 Library('msgbsutl_s')
 
 DEFINES['_IMPL_NS_MSG_BASE'] = True
--- a/mailnews/base/util/nsMsgUtils.cpp
+++ b/mailnews/base/util/nsMsgUtils.cpp
@@ -63,29 +63,34 @@
 #include "nsIPrompt.h"
 #include "nsIArray.h"
 #include "nsIMsgSearchTerm.h"
 #include "nsTextFormatter.h"
 #include "nsIStreamListener.h"
 #include "nsReadLine.h"
 #include "nsICharsetDetectionObserver.h"
 #include "nsICharsetDetector.h"
+#include "nsIStringCharsetDetector.h"
+#include "nsCyrillicDetector.h"
+#include "nsUniversalDetector.h"
+#include "nsUdetXPCOMWrapper.h"
 #include "nsILineInputStream.h"
 #include "nsIParserUtils.h"
 #include "nsICharsetConverterManager.h"
 #include "nsIDocumentEncoder.h"
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/Services.h"
 #include "locale.h"
 #include "nsStringStream.h"
 #include "nsIInputStreamPump.h"
 #include "nsIInputStream.h"
 #include "nsIChannel.h"
 #include "nsIURIMutator.h"
 #include "mozilla/Unused.h"
+#include "mozilla/Preferences.h"
 
 /* for logging to Error Console */
 #include "nsIScriptError.h"
 #include "nsIConsoleService.h"
 
 // Log an error string to the error console
 // (adapted from nsContentUtils::LogSimpleConsoleError).
 // Flag can indicate error, warning or info.
@@ -1886,30 +1891,27 @@ private:
   nsCString mCharset;
 };
 
 NS_IMPL_ISUPPORTS(CharsetDetectionObserver, nsICharsetDetectionObserver)
 
 NS_MSG_BASE nsresult
 MsgDetectCharsetFromFile(nsIFile *aFile, nsACString &aCharset)
 {
-  // First try the universal charset detector
-  nsCOMPtr<nsICharsetDetector> detector
-    = do_CreateInstance(NS_CHARSET_DETECTOR_CONTRACTID_BASE
-                        "universal_charset_detector");
-  if (!detector) {
-    // No universal charset detector, try the default charset detector
-    nsString detectorName;
-    NS_GetLocalizedUnicharPreferenceWithDefault(nullptr, "intl.charset.detector",
-                                                EmptyString(), detectorName);
-    if (!detectorName.IsEmpty()) {
-      nsAutoCString detectorContractID;
-      detectorContractID.AssignLiteral(NS_CHARSET_DETECTOR_CONTRACTID_BASE);
-      AppendUTF16toUTF8(detectorName, detectorContractID);
-      detector = do_CreateInstance(detectorContractID.get());
+  nsCOMPtr<nsICharsetDetector> detector;
+  nsAutoCString detectorName;
+  Preferences::GetLocalizedCString("intl.charset.detector", detectorName);
+  if (!detectorName.IsEmpty()) {
+    // We recognize one of the three magic strings for the following languages.
+    if (detectorName.EqualsLiteral("ruprob")) {
+      detector = new nsRUProbDetector();
+    } else if (detectorName.EqualsLiteral("ukprob")) {
+      detector = new nsUKProbDetector();
+    } else if (detectorName.EqualsLiteral("ja_parallel_state_machine")) {
+      detector = new nsJAPSMDetector();
     }
   }
 
   nsresult rv;
   nsCOMPtr<nsIInputStream> inputStream;
   rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), aFile);
   NS_ENSURE_SUCCESS(rv, rv);