1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/universalchardet/src/xpcom/nsUdetXPCOMWrapper.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,133 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nscore.h" 1.10 + 1.11 +#include "nsUniversalDetector.h" 1.12 +#include "nsUdetXPCOMWrapper.h" 1.13 +#include "nsCharSetProber.h" // for DumpStatus 1.14 + 1.15 +#include "nsUniversalCharDetDll.h" 1.16 +//---- for XPCOM 1.17 +#include "nsIFactory.h" 1.18 +#include "nsISupports.h" 1.19 +#include "nsCOMPtr.h" 1.20 + 1.21 +static NS_DEFINE_CID(kUniversalDetectorCID, NS_UNIVERSAL_DETECTOR_CID); 1.22 +static NS_DEFINE_CID(kUniversalStringDetectorCID, NS_UNIVERSAL_STRING_DETECTOR_CID); 1.23 + 1.24 +//--------------------------------------------------------------------- 1.25 +nsXPCOMDetector:: nsXPCOMDetector(uint32_t aLanguageFilter) 1.26 + : nsUniversalDetector(aLanguageFilter) 1.27 +{ 1.28 +} 1.29 +//--------------------------------------------------------------------- 1.30 +nsXPCOMDetector::~nsXPCOMDetector() 1.31 +{ 1.32 +} 1.33 +//--------------------------------------------------------------------- 1.34 + 1.35 +NS_IMPL_ISUPPORTS(nsXPCOMDetector, nsICharsetDetector) 1.36 + 1.37 +//--------------------------------------------------------------------- 1.38 +NS_IMETHODIMP nsXPCOMDetector::Init( 1.39 + nsICharsetDetectionObserver* aObserver) 1.40 +{ 1.41 + NS_ASSERTION(mObserver == nullptr , "Init twice"); 1.42 + if(nullptr == aObserver) 1.43 + return NS_ERROR_ILLEGAL_VALUE; 1.44 + 1.45 + mObserver = aObserver; 1.46 + return NS_OK; 1.47 +} 1.48 +//---------------------------------------------------------- 1.49 +NS_IMETHODIMP nsXPCOMDetector::DoIt(const char* aBuf, 1.50 + uint32_t aLen, bool* oDontFeedMe) 1.51 +{ 1.52 + NS_ASSERTION(mObserver != nullptr , "have not init yet"); 1.53 + 1.54 + if((nullptr == aBuf) || (nullptr == oDontFeedMe)) 1.55 + return NS_ERROR_ILLEGAL_VALUE; 1.56 + 1.57 + this->Reset(); 1.58 + nsresult rv = this->HandleData(aBuf, aLen); 1.59 + if (NS_FAILED(rv)) 1.60 + return rv; 1.61 + 1.62 + if (mDone) 1.63 + { 1.64 + if (mDetectedCharset) 1.65 + Report(mDetectedCharset); 1.66 + 1.67 + *oDontFeedMe = true; 1.68 + } 1.69 + *oDontFeedMe = false; 1.70 + return NS_OK; 1.71 +} 1.72 +//---------------------------------------------------------- 1.73 +NS_IMETHODIMP nsXPCOMDetector::Done() 1.74 +{ 1.75 + NS_ASSERTION(mObserver != nullptr , "have not init yet"); 1.76 +#ifdef DEBUG_chardet 1.77 + for (int32_t i = 0; i < NUM_OF_CHARSET_PROBERS; i++) 1.78 + { 1.79 + // If no data was received the array might stay filled with nulls 1.80 + // the way it was initialized in the constructor. 1.81 + if (mCharSetProbers[i]) 1.82 + mCharSetProbers[i]->DumpStatus(); 1.83 + } 1.84 +#endif 1.85 + 1.86 + this->DataEnd(); 1.87 + return NS_OK; 1.88 +} 1.89 +//---------------------------------------------------------- 1.90 +void nsXPCOMDetector::Report(const char* aCharset) 1.91 +{ 1.92 + NS_ASSERTION(mObserver != nullptr , "have not init yet"); 1.93 +#ifdef DEBUG_chardet 1.94 + printf("Universal Charset Detector report charset %s . \r\n", aCharset); 1.95 +#endif 1.96 + mObserver->Notify(aCharset, eBestAnswer); 1.97 +} 1.98 + 1.99 + 1.100 +//--------------------------------------------------------------------- 1.101 +nsXPCOMStringDetector:: nsXPCOMStringDetector(uint32_t aLanguageFilter) 1.102 + : nsUniversalDetector(aLanguageFilter) 1.103 +{ 1.104 +} 1.105 +//--------------------------------------------------------------------- 1.106 +nsXPCOMStringDetector::~nsXPCOMStringDetector() 1.107 +{ 1.108 +} 1.109 +//--------------------------------------------------------------------- 1.110 +NS_IMPL_ISUPPORTS(nsXPCOMStringDetector, nsIStringCharsetDetector) 1.111 +//--------------------------------------------------------------------- 1.112 +void nsXPCOMStringDetector::Report(const char *aCharset) 1.113 +{ 1.114 + mResult = aCharset; 1.115 +#ifdef DEBUG_chardet 1.116 + printf("New Charset Prober report charset %s . \r\n", aCharset); 1.117 +#endif 1.118 +} 1.119 +//--------------------------------------------------------------------- 1.120 +NS_IMETHODIMP nsXPCOMStringDetector::DoIt(const char* aBuf, 1.121 + uint32_t aLen, const char** oCharset, 1.122 + nsDetectionConfident &oConf) 1.123 +{ 1.124 + mResult = nullptr; 1.125 + this->Reset(); 1.126 + nsresult rv = this->HandleData(aBuf, aLen); 1.127 + if (NS_FAILED(rv)) 1.128 + return rv; 1.129 + this->DataEnd(); 1.130 + if (mResult) 1.131 + { 1.132 + *oCharset=mResult; 1.133 + oConf = eBestAnswer; 1.134 + } 1.135 + return NS_OK; 1.136 +}