michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nscore.h" michael@0: michael@0: #include "nsUniversalDetector.h" michael@0: #include "nsUdetXPCOMWrapper.h" michael@0: #include "nsCharSetProber.h" // for DumpStatus michael@0: michael@0: #include "nsUniversalCharDetDll.h" michael@0: //---- for XPCOM michael@0: #include "nsIFactory.h" michael@0: #include "nsISupports.h" michael@0: #include "nsCOMPtr.h" michael@0: michael@0: static NS_DEFINE_CID(kUniversalDetectorCID, NS_UNIVERSAL_DETECTOR_CID); michael@0: static NS_DEFINE_CID(kUniversalStringDetectorCID, NS_UNIVERSAL_STRING_DETECTOR_CID); michael@0: michael@0: //--------------------------------------------------------------------- michael@0: nsXPCOMDetector:: nsXPCOMDetector(uint32_t aLanguageFilter) michael@0: : nsUniversalDetector(aLanguageFilter) michael@0: { michael@0: } michael@0: //--------------------------------------------------------------------- michael@0: nsXPCOMDetector::~nsXPCOMDetector() michael@0: { michael@0: } michael@0: //--------------------------------------------------------------------- michael@0: michael@0: NS_IMPL_ISUPPORTS(nsXPCOMDetector, nsICharsetDetector) michael@0: michael@0: //--------------------------------------------------------------------- michael@0: NS_IMETHODIMP nsXPCOMDetector::Init( michael@0: nsICharsetDetectionObserver* aObserver) michael@0: { michael@0: NS_ASSERTION(mObserver == nullptr , "Init twice"); michael@0: if(nullptr == aObserver) michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: michael@0: mObserver = aObserver; michael@0: return NS_OK; michael@0: } michael@0: //---------------------------------------------------------- michael@0: NS_IMETHODIMP nsXPCOMDetector::DoIt(const char* aBuf, michael@0: uint32_t aLen, bool* oDontFeedMe) michael@0: { michael@0: NS_ASSERTION(mObserver != nullptr , "have not init yet"); michael@0: michael@0: if((nullptr == aBuf) || (nullptr == oDontFeedMe)) michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: michael@0: this->Reset(); michael@0: nsresult rv = this->HandleData(aBuf, aLen); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: if (mDone) michael@0: { michael@0: if (mDetectedCharset) michael@0: Report(mDetectedCharset); michael@0: michael@0: *oDontFeedMe = true; michael@0: } michael@0: *oDontFeedMe = false; michael@0: return NS_OK; michael@0: } michael@0: //---------------------------------------------------------- michael@0: NS_IMETHODIMP nsXPCOMDetector::Done() michael@0: { michael@0: NS_ASSERTION(mObserver != nullptr , "have not init yet"); michael@0: #ifdef DEBUG_chardet michael@0: for (int32_t i = 0; i < NUM_OF_CHARSET_PROBERS; i++) michael@0: { michael@0: // If no data was received the array might stay filled with nulls michael@0: // the way it was initialized in the constructor. michael@0: if (mCharSetProbers[i]) michael@0: mCharSetProbers[i]->DumpStatus(); michael@0: } michael@0: #endif michael@0: michael@0: this->DataEnd(); michael@0: return NS_OK; michael@0: } michael@0: //---------------------------------------------------------- michael@0: void nsXPCOMDetector::Report(const char* aCharset) michael@0: { michael@0: NS_ASSERTION(mObserver != nullptr , "have not init yet"); michael@0: #ifdef DEBUG_chardet michael@0: printf("Universal Charset Detector report charset %s . \r\n", aCharset); michael@0: #endif michael@0: mObserver->Notify(aCharset, eBestAnswer); michael@0: } michael@0: michael@0: michael@0: //--------------------------------------------------------------------- michael@0: nsXPCOMStringDetector:: nsXPCOMStringDetector(uint32_t aLanguageFilter) michael@0: : nsUniversalDetector(aLanguageFilter) michael@0: { michael@0: } michael@0: //--------------------------------------------------------------------- michael@0: nsXPCOMStringDetector::~nsXPCOMStringDetector() michael@0: { michael@0: } michael@0: //--------------------------------------------------------------------- michael@0: NS_IMPL_ISUPPORTS(nsXPCOMStringDetector, nsIStringCharsetDetector) michael@0: //--------------------------------------------------------------------- michael@0: void nsXPCOMStringDetector::Report(const char *aCharset) michael@0: { michael@0: mResult = aCharset; michael@0: #ifdef DEBUG_chardet michael@0: printf("New Charset Prober report charset %s . \r\n", aCharset); michael@0: #endif michael@0: } michael@0: //--------------------------------------------------------------------- michael@0: NS_IMETHODIMP nsXPCOMStringDetector::DoIt(const char* aBuf, michael@0: uint32_t aLen, const char** oCharset, michael@0: nsDetectionConfident &oConf) michael@0: { michael@0: mResult = nullptr; michael@0: this->Reset(); michael@0: nsresult rv = this->HandleData(aBuf, aLen); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: this->DataEnd(); michael@0: if (mResult) michael@0: { michael@0: *oCharset=mResult; michael@0: oConf = eBestAnswer; michael@0: } michael@0: return NS_OK; michael@0: }