Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 #include "nscore.h"
8 #include "nsUniversalDetector.h"
9 #include "nsUdetXPCOMWrapper.h"
10 #include "nsCharSetProber.h" // for DumpStatus
12 #include "nsUniversalCharDetDll.h"
13 //---- for XPCOM
14 #include "nsIFactory.h"
15 #include "nsISupports.h"
16 #include "nsCOMPtr.h"
18 static NS_DEFINE_CID(kUniversalDetectorCID, NS_UNIVERSAL_DETECTOR_CID);
19 static NS_DEFINE_CID(kUniversalStringDetectorCID, NS_UNIVERSAL_STRING_DETECTOR_CID);
21 //---------------------------------------------------------------------
22 nsXPCOMDetector:: nsXPCOMDetector(uint32_t aLanguageFilter)
23 : nsUniversalDetector(aLanguageFilter)
24 {
25 }
26 //---------------------------------------------------------------------
27 nsXPCOMDetector::~nsXPCOMDetector()
28 {
29 }
30 //---------------------------------------------------------------------
32 NS_IMPL_ISUPPORTS(nsXPCOMDetector, nsICharsetDetector)
34 //---------------------------------------------------------------------
35 NS_IMETHODIMP nsXPCOMDetector::Init(
36 nsICharsetDetectionObserver* aObserver)
37 {
38 NS_ASSERTION(mObserver == nullptr , "Init twice");
39 if(nullptr == aObserver)
40 return NS_ERROR_ILLEGAL_VALUE;
42 mObserver = aObserver;
43 return NS_OK;
44 }
45 //----------------------------------------------------------
46 NS_IMETHODIMP nsXPCOMDetector::DoIt(const char* aBuf,
47 uint32_t aLen, bool* oDontFeedMe)
48 {
49 NS_ASSERTION(mObserver != nullptr , "have not init yet");
51 if((nullptr == aBuf) || (nullptr == oDontFeedMe))
52 return NS_ERROR_ILLEGAL_VALUE;
54 this->Reset();
55 nsresult rv = this->HandleData(aBuf, aLen);
56 if (NS_FAILED(rv))
57 return rv;
59 if (mDone)
60 {
61 if (mDetectedCharset)
62 Report(mDetectedCharset);
64 *oDontFeedMe = true;
65 }
66 *oDontFeedMe = false;
67 return NS_OK;
68 }
69 //----------------------------------------------------------
70 NS_IMETHODIMP nsXPCOMDetector::Done()
71 {
72 NS_ASSERTION(mObserver != nullptr , "have not init yet");
73 #ifdef DEBUG_chardet
74 for (int32_t i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
75 {
76 // If no data was received the array might stay filled with nulls
77 // the way it was initialized in the constructor.
78 if (mCharSetProbers[i])
79 mCharSetProbers[i]->DumpStatus();
80 }
81 #endif
83 this->DataEnd();
84 return NS_OK;
85 }
86 //----------------------------------------------------------
87 void nsXPCOMDetector::Report(const char* aCharset)
88 {
89 NS_ASSERTION(mObserver != nullptr , "have not init yet");
90 #ifdef DEBUG_chardet
91 printf("Universal Charset Detector report charset %s . \r\n", aCharset);
92 #endif
93 mObserver->Notify(aCharset, eBestAnswer);
94 }
97 //---------------------------------------------------------------------
98 nsXPCOMStringDetector:: nsXPCOMStringDetector(uint32_t aLanguageFilter)
99 : nsUniversalDetector(aLanguageFilter)
100 {
101 }
102 //---------------------------------------------------------------------
103 nsXPCOMStringDetector::~nsXPCOMStringDetector()
104 {
105 }
106 //---------------------------------------------------------------------
107 NS_IMPL_ISUPPORTS(nsXPCOMStringDetector, nsIStringCharsetDetector)
108 //---------------------------------------------------------------------
109 void nsXPCOMStringDetector::Report(const char *aCharset)
110 {
111 mResult = aCharset;
112 #ifdef DEBUG_chardet
113 printf("New Charset Prober report charset %s . \r\n", aCharset);
114 #endif
115 }
116 //---------------------------------------------------------------------
117 NS_IMETHODIMP nsXPCOMStringDetector::DoIt(const char* aBuf,
118 uint32_t aLen, const char** oCharset,
119 nsDetectionConfident &oConf)
120 {
121 mResult = nullptr;
122 this->Reset();
123 nsresult rv = this->HandleData(aBuf, aLen);
124 if (NS_FAILED(rv))
125 return rv;
126 this->DataEnd();
127 if (mResult)
128 {
129 *oCharset=mResult;
130 oConf = eBestAnswer;
131 }
132 return NS_OK;
133 }