diff options
Diffstat (limited to 'extensions/universalchardet/src/xpcom')
5 files changed, 286 insertions, 0 deletions
diff --git a/extensions/universalchardet/src/xpcom/moz.build b/extensions/universalchardet/src/xpcom/moz.build new file mode 100644 index 000000000..5a24a5771 --- /dev/null +++ b/extensions/universalchardet/src/xpcom/moz.build @@ -0,0 +1,16 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +UNIFIED_SOURCES += [ + 'nsUdetXPCOMWrapper.cpp', + 'nsUniversalCharDetModule.cpp', +] + +FINAL_LIBRARY = 'xul' + +LOCAL_INCLUDES += [ + '../base', +] diff --git a/extensions/universalchardet/src/xpcom/nsUdetXPCOMWrapper.cpp b/extensions/universalchardet/src/xpcom/nsUdetXPCOMWrapper.cpp new file mode 100644 index 000000000..750b1fd07 --- /dev/null +++ b/extensions/universalchardet/src/xpcom/nsUdetXPCOMWrapper.cpp @@ -0,0 +1,130 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nscore.h" + +#include "nsUniversalDetector.h" +#include "nsUdetXPCOMWrapper.h" +#include "nsCharSetProber.h" // for DumpStatus + +#include "nsUniversalCharDetDll.h" +//---- for XPCOM +#include "nsIFactory.h" +#include "nsISupports.h" +#include "nsCOMPtr.h" + +//--------------------------------------------------------------------- +nsXPCOMDetector:: nsXPCOMDetector() + : nsUniversalDetector() +{ +} +//--------------------------------------------------------------------- +nsXPCOMDetector::~nsXPCOMDetector() +{ +} +//--------------------------------------------------------------------- + +NS_IMPL_ISUPPORTS(nsXPCOMDetector, nsICharsetDetector) + +//--------------------------------------------------------------------- +NS_IMETHODIMP nsXPCOMDetector::Init( + nsICharsetDetectionObserver* aObserver) +{ + NS_ASSERTION(mObserver == nullptr , "Init twice"); + if(nullptr == aObserver) + return NS_ERROR_ILLEGAL_VALUE; + + mObserver = aObserver; + return NS_OK; +} +//---------------------------------------------------------- +NS_IMETHODIMP nsXPCOMDetector::DoIt(const char* aBuf, + uint32_t aLen, bool* oDontFeedMe) +{ + NS_ASSERTION(mObserver != nullptr , "have not init yet"); + + if((nullptr == aBuf) || (nullptr == oDontFeedMe)) + return NS_ERROR_ILLEGAL_VALUE; + + this->Reset(); + nsresult rv = this->HandleData(aBuf, aLen); + if (NS_FAILED(rv)) + return rv; + + if (mDone) + { + if (mDetectedCharset) + Report(mDetectedCharset); + + *oDontFeedMe = true; + } + *oDontFeedMe = false; + return NS_OK; +} +//---------------------------------------------------------- +NS_IMETHODIMP nsXPCOMDetector::Done() +{ + NS_ASSERTION(mObserver != nullptr , "have not init yet"); +#ifdef DEBUG_chardet + for (int32_t i = 0; i < NUM_OF_CHARSET_PROBERS; i++) + { + // If no data was received the array might stay filled with nulls + // the way it was initialized in the constructor. + if (mCharSetProbers[i]) + mCharSetProbers[i]->DumpStatus(); + } +#endif + + this->DataEnd(); + return NS_OK; +} +//---------------------------------------------------------- +void nsXPCOMDetector::Report(const char* aCharset) +{ + NS_ASSERTION(mObserver != nullptr , "have not init yet"); +#ifdef DEBUG_chardet + printf("Universal Charset Detector report charset %s . \r\n", aCharset); +#endif + mObserver->Notify(aCharset, eBestAnswer); +} + + +//--------------------------------------------------------------------- +nsXPCOMStringDetector:: nsXPCOMStringDetector() + : nsUniversalDetector() +{ +} +//--------------------------------------------------------------------- +nsXPCOMStringDetector::~nsXPCOMStringDetector() +{ +} +//--------------------------------------------------------------------- +NS_IMPL_ISUPPORTS(nsXPCOMStringDetector, nsIStringCharsetDetector) +//--------------------------------------------------------------------- +void nsXPCOMStringDetector::Report(const char *aCharset) +{ + mResult = aCharset; +#ifdef DEBUG_chardet + printf("New Charset Prober report charset %s . \r\n", aCharset); +#endif +} +//--------------------------------------------------------------------- +NS_IMETHODIMP nsXPCOMStringDetector::DoIt(const char* aBuf, + uint32_t aLen, const char** oCharset, + nsDetectionConfident &oConf) +{ + mResult = nullptr; + this->Reset(); + nsresult rv = this->HandleData(aBuf, aLen); + if (NS_FAILED(rv)) + return rv; + this->DataEnd(); + if (mResult) + { + *oCharset=mResult; + oConf = eBestAnswer; + } + return NS_OK; +} diff --git a/extensions/universalchardet/src/xpcom/nsUdetXPCOMWrapper.h b/extensions/universalchardet/src/xpcom/nsUdetXPCOMWrapper.h new file mode 100644 index 000000000..11a0bd10a --- /dev/null +++ b/extensions/universalchardet/src/xpcom/nsUdetXPCOMWrapper.h @@ -0,0 +1,77 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef _nsUdetXPCOMWrapper_h__ +#define _nsUdetXPCOMWrapper_h__ +#include "nsISupports.h" +#include "nsICharsetDetector.h" +#include "nsIStringCharsetDetector.h" +#include "nsICharsetDetectionObserver.h" +#include "nsCOMPtr.h" + +#include "nsIFactory.h" + +// {12BB8F1B-2389-11d3-B3BF-00805F8A6670} +#define NS_JA_PSMDETECTOR_CID \ +{ 0x12bb8f1b, 0x2389, 0x11d3, { 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } } + +// {12BB8F1C-2389-11d3-B3BF-00805F8A6670} +#define NS_JA_STRING_PSMDETECTOR_CID \ +{ 0x12bb8f1c, 0x2389, 0x11d3, { 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } } + +//===================================================================== +class nsXPCOMDetector : + public nsUniversalDetector, + public nsICharsetDetector +{ + NS_DECL_ISUPPORTS + public: + nsXPCOMDetector(); + NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver) override; + NS_IMETHOD DoIt(const char* aBuf, uint32_t aLen, bool *oDontFeedMe) override; + NS_IMETHOD Done() override; + protected: + virtual ~nsXPCOMDetector(); + virtual void Report(const char* aCharset) override; + private: + nsCOMPtr<nsICharsetDetectionObserver> mObserver; +}; + + +//===================================================================== +class nsXPCOMStringDetector : + public nsUniversalDetector, + public nsIStringCharsetDetector +{ + NS_DECL_ISUPPORTS + public: + nsXPCOMStringDetector(); + NS_IMETHOD DoIt(const char* aBuf, uint32_t aLen, + const char** oCharset, nsDetectionConfident &oConf) override; + protected: + virtual ~nsXPCOMStringDetector(); + virtual void Report(const char* aCharset) override; + private: + nsCOMPtr<nsICharsetDetectionObserver> mObserver; + const char* mResult; +}; + +//===================================================================== + +class nsJAPSMDetector : public nsXPCOMDetector +{ +public: + nsJAPSMDetector() + : nsXPCOMDetector() {} +}; + +class nsJAStringPSMDetector : public nsXPCOMStringDetector +{ +public: + nsJAStringPSMDetector() + : nsXPCOMStringDetector() {} +}; + +#endif //_nsUdetXPCOMWrapper_h__ diff --git a/extensions/universalchardet/src/xpcom/nsUniversalCharDetDll.h b/extensions/universalchardet/src/xpcom/nsUniversalCharDetDll.h new file mode 100644 index 000000000..404cb4518 --- /dev/null +++ b/extensions/universalchardet/src/xpcom/nsUniversalCharDetDll.h @@ -0,0 +1,11 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef nsCharDetDll_h__ +#define nsCharDetDll_h__ + +#include "prtypes.h" + +#endif /* nsCharDetDll_h__ */ diff --git a/extensions/universalchardet/src/xpcom/nsUniversalCharDetModule.cpp b/extensions/universalchardet/src/xpcom/nsUniversalCharDetModule.cpp new file mode 100644 index 000000000..38e2e60bd --- /dev/null +++ b/extensions/universalchardet/src/xpcom/nsUniversalCharDetModule.cpp @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/ModuleUtils.h" + +#include "nsCOMPtr.h" + +#include "nspr.h" +#include "nsString.h" +#include "nsUniversalCharDetDll.h" +#include "nsISupports.h" +#include "nsICategoryManager.h" +#include "nsIComponentManager.h" +#include "nsIServiceManager.h" +#include "nsICharsetDetector.h" +#include "nsIStringCharsetDetector.h" + +#include "nsUniversalDetector.h" +#include "nsUdetXPCOMWrapper.h" + +NS_GENERIC_FACTORY_CONSTRUCTOR(nsJAPSMDetector) +NS_GENERIC_FACTORY_CONSTRUCTOR(nsJAStringPSMDetector) +NS_DEFINE_NAMED_CID(NS_JA_PSMDETECTOR_CID); +NS_DEFINE_NAMED_CID(NS_JA_STRING_PSMDETECTOR_CID); + +static const mozilla::Module::CIDEntry kChardetCIDs[] = { + { &kNS_JA_PSMDETECTOR_CID, false, nullptr, nsJAPSMDetectorConstructor }, + { &kNS_JA_STRING_PSMDETECTOR_CID, false, nullptr, nsJAStringPSMDetectorConstructor }, + { nullptr } +}; + +static const mozilla::Module::ContractIDEntry kChardetContracts[] = { + { NS_CHARSET_DETECTOR_CONTRACTID_BASE "ja_parallel_state_machine", &kNS_JA_PSMDETECTOR_CID }, + { NS_STRCDETECTOR_CONTRACTID_BASE "ja_parallel_state_machine", &kNS_JA_STRING_PSMDETECTOR_CID }, + { nullptr } +}; + +static const mozilla::Module::CategoryEntry kChardetCategories[] = { + { NS_CHARSET_DETECTOR_CATEGORY, "ja_parallel_state_machine", NS_CHARSET_DETECTOR_CONTRACTID_BASE "ja_parallel_state_machine" }, + { nullptr } +}; + +static const mozilla::Module kChardetModule = { + mozilla::Module::kVersion, + kChardetCIDs, + kChardetContracts, + kChardetCategories +}; + +NSMODULE_DEFN(nsUniversalCharDetModule) = &kChardetModule; |