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
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nscore.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsUniversalDetector.h" |
michael@0 | 9 | #include "nsUdetXPCOMWrapper.h" |
michael@0 | 10 | #include "nsCharSetProber.h" // for DumpStatus |
michael@0 | 11 | |
michael@0 | 12 | #include "nsUniversalCharDetDll.h" |
michael@0 | 13 | //---- for XPCOM |
michael@0 | 14 | #include "nsIFactory.h" |
michael@0 | 15 | #include "nsISupports.h" |
michael@0 | 16 | #include "nsCOMPtr.h" |
michael@0 | 17 | |
michael@0 | 18 | static NS_DEFINE_CID(kUniversalDetectorCID, NS_UNIVERSAL_DETECTOR_CID); |
michael@0 | 19 | static NS_DEFINE_CID(kUniversalStringDetectorCID, NS_UNIVERSAL_STRING_DETECTOR_CID); |
michael@0 | 20 | |
michael@0 | 21 | //--------------------------------------------------------------------- |
michael@0 | 22 | nsXPCOMDetector:: nsXPCOMDetector(uint32_t aLanguageFilter) |
michael@0 | 23 | : nsUniversalDetector(aLanguageFilter) |
michael@0 | 24 | { |
michael@0 | 25 | } |
michael@0 | 26 | //--------------------------------------------------------------------- |
michael@0 | 27 | nsXPCOMDetector::~nsXPCOMDetector() |
michael@0 | 28 | { |
michael@0 | 29 | } |
michael@0 | 30 | //--------------------------------------------------------------------- |
michael@0 | 31 | |
michael@0 | 32 | NS_IMPL_ISUPPORTS(nsXPCOMDetector, nsICharsetDetector) |
michael@0 | 33 | |
michael@0 | 34 | //--------------------------------------------------------------------- |
michael@0 | 35 | NS_IMETHODIMP nsXPCOMDetector::Init( |
michael@0 | 36 | nsICharsetDetectionObserver* aObserver) |
michael@0 | 37 | { |
michael@0 | 38 | NS_ASSERTION(mObserver == nullptr , "Init twice"); |
michael@0 | 39 | if(nullptr == aObserver) |
michael@0 | 40 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 41 | |
michael@0 | 42 | mObserver = aObserver; |
michael@0 | 43 | return NS_OK; |
michael@0 | 44 | } |
michael@0 | 45 | //---------------------------------------------------------- |
michael@0 | 46 | NS_IMETHODIMP nsXPCOMDetector::DoIt(const char* aBuf, |
michael@0 | 47 | uint32_t aLen, bool* oDontFeedMe) |
michael@0 | 48 | { |
michael@0 | 49 | NS_ASSERTION(mObserver != nullptr , "have not init yet"); |
michael@0 | 50 | |
michael@0 | 51 | if((nullptr == aBuf) || (nullptr == oDontFeedMe)) |
michael@0 | 52 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 53 | |
michael@0 | 54 | this->Reset(); |
michael@0 | 55 | nsresult rv = this->HandleData(aBuf, aLen); |
michael@0 | 56 | if (NS_FAILED(rv)) |
michael@0 | 57 | return rv; |
michael@0 | 58 | |
michael@0 | 59 | if (mDone) |
michael@0 | 60 | { |
michael@0 | 61 | if (mDetectedCharset) |
michael@0 | 62 | Report(mDetectedCharset); |
michael@0 | 63 | |
michael@0 | 64 | *oDontFeedMe = true; |
michael@0 | 65 | } |
michael@0 | 66 | *oDontFeedMe = false; |
michael@0 | 67 | return NS_OK; |
michael@0 | 68 | } |
michael@0 | 69 | //---------------------------------------------------------- |
michael@0 | 70 | NS_IMETHODIMP nsXPCOMDetector::Done() |
michael@0 | 71 | { |
michael@0 | 72 | NS_ASSERTION(mObserver != nullptr , "have not init yet"); |
michael@0 | 73 | #ifdef DEBUG_chardet |
michael@0 | 74 | for (int32_t i = 0; i < NUM_OF_CHARSET_PROBERS; i++) |
michael@0 | 75 | { |
michael@0 | 76 | // If no data was received the array might stay filled with nulls |
michael@0 | 77 | // the way it was initialized in the constructor. |
michael@0 | 78 | if (mCharSetProbers[i]) |
michael@0 | 79 | mCharSetProbers[i]->DumpStatus(); |
michael@0 | 80 | } |
michael@0 | 81 | #endif |
michael@0 | 82 | |
michael@0 | 83 | this->DataEnd(); |
michael@0 | 84 | return NS_OK; |
michael@0 | 85 | } |
michael@0 | 86 | //---------------------------------------------------------- |
michael@0 | 87 | void nsXPCOMDetector::Report(const char* aCharset) |
michael@0 | 88 | { |
michael@0 | 89 | NS_ASSERTION(mObserver != nullptr , "have not init yet"); |
michael@0 | 90 | #ifdef DEBUG_chardet |
michael@0 | 91 | printf("Universal Charset Detector report charset %s . \r\n", aCharset); |
michael@0 | 92 | #endif |
michael@0 | 93 | mObserver->Notify(aCharset, eBestAnswer); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | |
michael@0 | 97 | //--------------------------------------------------------------------- |
michael@0 | 98 | nsXPCOMStringDetector:: nsXPCOMStringDetector(uint32_t aLanguageFilter) |
michael@0 | 99 | : nsUniversalDetector(aLanguageFilter) |
michael@0 | 100 | { |
michael@0 | 101 | } |
michael@0 | 102 | //--------------------------------------------------------------------- |
michael@0 | 103 | nsXPCOMStringDetector::~nsXPCOMStringDetector() |
michael@0 | 104 | { |
michael@0 | 105 | } |
michael@0 | 106 | //--------------------------------------------------------------------- |
michael@0 | 107 | NS_IMPL_ISUPPORTS(nsXPCOMStringDetector, nsIStringCharsetDetector) |
michael@0 | 108 | //--------------------------------------------------------------------- |
michael@0 | 109 | void nsXPCOMStringDetector::Report(const char *aCharset) |
michael@0 | 110 | { |
michael@0 | 111 | mResult = aCharset; |
michael@0 | 112 | #ifdef DEBUG_chardet |
michael@0 | 113 | printf("New Charset Prober report charset %s . \r\n", aCharset); |
michael@0 | 114 | #endif |
michael@0 | 115 | } |
michael@0 | 116 | //--------------------------------------------------------------------- |
michael@0 | 117 | NS_IMETHODIMP nsXPCOMStringDetector::DoIt(const char* aBuf, |
michael@0 | 118 | uint32_t aLen, const char** oCharset, |
michael@0 | 119 | nsDetectionConfident &oConf) |
michael@0 | 120 | { |
michael@0 | 121 | mResult = nullptr; |
michael@0 | 122 | this->Reset(); |
michael@0 | 123 | nsresult rv = this->HandleData(aBuf, aLen); |
michael@0 | 124 | if (NS_FAILED(rv)) |
michael@0 | 125 | return rv; |
michael@0 | 126 | this->DataEnd(); |
michael@0 | 127 | if (mResult) |
michael@0 | 128 | { |
michael@0 | 129 | *oCharset=mResult; |
michael@0 | 130 | oConf = eBestAnswer; |
michael@0 | 131 | } |
michael@0 | 132 | return NS_OK; |
michael@0 | 133 | } |