fix 22203. r=cata/jbetak do not call the reload but set the parser charset directly if auto detect happen in the first block.

This commit is contained in:
ftang%netscape.com 2000-02-15 09:15:18 +00:00
parent 64fce3f2d8
commit 189f468495
5 changed files with 89 additions and 18 deletions

View File

@ -685,7 +685,8 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand,
webShell->QueryInterface(
NS_GET_IID(nsIWebShellServices),(void**) &wss)))
{
rv_detect = adp->Init(wss, cdet, aCommand);
rv_detect = adp->Init(wss, cdet, (nsIDocument*)this,
mParser, charset.GetUnicode(),aCommand);
}
}
}

View File

@ -25,6 +25,8 @@
class nsICharsetDetector;
class nsIWebShellServices;
class nsIDocument;
class nsIParser;
// {12BB8F13-2389-11d3-B3BF-00805F8A6670}
#define NS_ICHARSETDETECTIONADAPTOR_IID \
@ -45,6 +47,8 @@ public:
Initialize it by setup the nsICharsetDetector and the
nsIWebShell
*/
NS_IMETHOD Init(nsIWebShellServices* aWebShell, nsICharsetDetector *aDetector, const char* aCommand=nsnull) = 0;
NS_IMETHOD Init(nsIWebShellServices* aWebShell, nsICharsetDetector *aDetector,
nsIDocument* aDocument, nsIParser* aParser,
const PRUnichar* aCharset, const char* aCommand=nsnull) = 0;
};
#endif /* nsICDETAdaptor_h__ */

View File

@ -83,6 +83,7 @@ public:
mCharsets = aCharsets;
for(PRUint8 i=0;i<mItems;i++)
mProb[i] = mLastCls[i] =0;
mDone = PR_FALSE;
};
virtual ~nsCyrillicDetector() {};
virtual void HandleData(const char* aBuf, PRUint32 aLen);
@ -96,6 +97,7 @@ private:
const char** mCharsets;
PRUint32 mProb[NUM_CYR_CHARSET];
PRUint8 mLastCls[NUM_CYR_CHARSET];
PRBool mDone;
};
//---------------------------------------------------------------------
@ -104,6 +106,8 @@ void nsCyrillicDetector::HandleData(const char* aBuf, PRUint32 aLen)
PRUint8 cls;
const char* b;
PRUint32 i;
if(mDone)
return;
for(i=0, b=aBuf;i<aLen;i++,b++)
{
for(PRUint8 j=0;j<mItems;j++)
@ -117,13 +121,18 @@ void nsCyrillicDetector::HandleData(const char* aBuf, PRUint32 aLen)
mLastCls[j] = cls;
}
}
// We now only based on the first block we receive
DataEnd();
}
//---------------------------------------------------------------------
#define THRESHOLD_RATIO 1.5f
void nsCyrillicDetector::DataEnd()
{
PRUint32 max=0;
PRUint8 maxIdx=0;
PRUint8 j;
if(mDone)
return;
for(j=1;j<mItems;j++) {
if(mProb[j] > max)
{
@ -136,6 +145,7 @@ void nsCyrillicDetector::DataEnd()
printf("Charset %s->\t%d\n", mCharsets[j], mProb[j]);
#endif
this->Report(mCharsets[maxIdx]);
mDone = PR_TRUE;
}
//=====================================================================
class nsCyrXPCOMDetector :

View File

@ -23,6 +23,7 @@
#define IMPL_NS_IPARSERFILTER
#include "nsString.h"
#include "nsICharsetDetectionObserver.h"
#include "nsICharsetDetectionAdaptor.h"
#include "nsDetectionAdaptor.h"
@ -36,6 +37,8 @@
static NS_DEFINE_IID(kIParserFilterIID, NS_IPARSERFILTER_IID);
#endif /* IMPL_NS_IPARSERFILTER */
#include "nsIParser.h"
#include "nsIDocument.h"
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
@ -53,41 +56,80 @@ class nsMyObserver : public nsICharsetDetectionObserver
NS_INIT_REFCNT();
PR_AtomicIncrement(& g_InstanceCount);
mWebShellSvc = nsnull;
mCommand[0] = '\0';
mNotifyByReload = PR_FALSE;
mWeakRefDocument = nsnull;
mWeakRefParser = nsnull;
mCommand ="";
mCharset ="";
}
virtual ~nsMyObserver( void )
{
NS_IF_RELEASE(mWebShellSvc);
PR_AtomicDecrement(& g_InstanceCount);
// do not release nor delete mWeakRefDocument
// do not release nor delete mWeakRefParser
}
// Methods to support nsICharsetDetectionAdaptor
NS_IMETHOD Init(nsIWebShellServices* aWebShellSvc, const char* aCommand);
NS_IMETHOD Init(nsIWebShellServices* aWebShellSvc,
nsIDocument* aDocument,
nsIParser* aParser,
const PRUnichar* aCharset,
const char* aCommand);
// Methods to support nsICharsetDetectionObserver
NS_IMETHOD Notify(const char* aCharset, nsDetectionConfident aConf);
void SetNotifyByReload(PRBool aByReload) { mNotifyByReload = aByReload; };
private:
nsIWebShellServices* mWebShellSvc;
char mCommand[32];
PRBool mNotifyByReload;
nsIDocument* mWeakRefDocument;
nsIParser* mWeakRefParser;
nsAutoString mCharset;
nsCAutoString mCommand;
};
//--------------------------------------------------------------
NS_IMETHODIMP nsMyObserver::Notify(
const char* aCharset, nsDetectionConfident aConf)
{
nsresult rv = NS_OK;
rv = mWebShellSvc->SetRendering( PR_FALSE);
rv = mWebShellSvc->StopDocumentLoad();
rv = mWebShellSvc->ReloadDocument( aCharset, kCharsetFromAutoDetection,
mCommand[0] ? mCommand : nsnull);
if(mCharset != aCharset) {
if(mNotifyByReload) {
rv = mWebShellSvc->SetRendering( PR_FALSE);
rv = mWebShellSvc->StopDocumentLoad();
rv = mWebShellSvc->ReloadDocument( aCharset, kCharsetFromAutoDetection,
mCommand.Length()>0 ? mCommand.GetBuffer() : nsnull);
} else {
nsAutoString newcharset(aCharset);
if(mWeakRefDocument) {
mWeakRefDocument->SetDocumentCharacterSet(newcharset);
}
if(mWeakRefParser) {
mWeakRefParser->SetDocumentCharset(newcharset, kCharsetFromAutoDetection);
}
}
}
return NS_OK;
}
//--------------------------------------------------------------
NS_IMETHODIMP nsMyObserver::Init( nsIWebShellServices* aWebShellSvc,
const char* aCommand)
nsIDocument* aDocument,
nsIParser* aParser,
const PRUnichar* aCharset,
const char* aCommand)
{
if(aCommand) {
PL_strncpy(mCommand, aCommand, 31);
mCommand = aCommand;
}
if(aCharset) {
mCharset = aCharset;
}
if(aDocument) {
mWeakRefDocument = aDocument;
}
if(aParser) {
mWeakRefParser = aParser;
}
if(nsnull != aWebShellSvc)
{
@ -116,6 +158,9 @@ class nsDetectionAdaptor :
// Methods to support nsICharsetDetectionAdaptor
NS_IMETHOD Init(nsIWebShellServices* aWebShellSvc, nsICharsetDetector *aDetector,
nsIDocument* aDocument,
nsIParser* aParser,
const PRUnichar* aCharset,
const char* aCommand=nsnull);
// Methode to suppor nsIParserFilter
@ -130,6 +175,8 @@ class nsDetectionAdaptor :
private:
nsICharsetDetector* mDetector;
PRBool mDontFeedToDetector;
nsMyObserver* mObserver;
};
//--------------------------------------------------------------
nsDetectionAdaptor::nsDetectionAdaptor( void )
@ -138,11 +185,13 @@ nsDetectionAdaptor::nsDetectionAdaptor( void )
PR_AtomicIncrement(& g_InstanceCount);
mDetector = nsnull;
mDontFeedToDetector = PR_TRUE;
mObserver = nsnull;
}
//--------------------------------------------------------------
nsDetectionAdaptor::~nsDetectionAdaptor()
{
NS_IF_RELEASE(mDetector);
NS_IF_RELEASE(mObserver);
PR_AtomicDecrement(& g_InstanceCount);
}
//--------------------------------------------------------------
@ -181,21 +230,24 @@ NS_IMETHODIMP nsDetectionAdaptor::QueryInterface(REFNSIID aIID, void**aInstanceP
//--------------------------------------------------------------
NS_IMETHODIMP nsDetectionAdaptor::Init(
nsIWebShellServices* aWebShellSvc, nsICharsetDetector *aDetector,
nsIDocument* aDocument, nsIParser* aParser, const PRUnichar* aCharset,
const char* aCommand)
{
if((nsnull != aWebShellSvc) && (nsnull != aDetector))
if((nsnull != aWebShellSvc) && (nsnull != aDetector) && (nsnull != aCharset))
{
nsICharsetDetectionObserver* aObserver = nsnull;
nsresult rv = NS_OK;
nsMyObserver* mobs = new nsMyObserver();
if(nsnull == mobs)
mObserver = new nsMyObserver(); // weak ref to it, release by charset detector
if(nsnull == mObserver)
return NS_ERROR_OUT_OF_MEMORY;
NS_IF_ADDREF(mObserver);
rv = mobs->QueryInterface(NS_GET_IID(nsICharsetDetectionObserver),
rv = mObserver->QueryInterface(NS_GET_IID(nsICharsetDetectionObserver),
(void**) &aObserver);
if(NS_SUCCEEDED(rv)) {
rv = mobs->Init(aWebShellSvc, aCommand);
rv = mObserver->Init(aWebShellSvc, aDocument,
aParser, aCharset, aCommand);
if(NS_SUCCEEDED(rv)) {
rv = aDetector->Init(aObserver);
}
@ -211,7 +263,7 @@ NS_IMETHODIMP nsDetectionAdaptor::Init(
mDontFeedToDetector = PR_FALSE;
return NS_OK;
} else {
delete mobs;
NS_IF_RELEASE(mObserver);
}
}
return NS_ERROR_ILLEGAL_VALUE;
@ -224,6 +276,9 @@ NS_IMETHODIMP nsDetectionAdaptor::RawBuffer
return NS_OK;
nsresult rv = NS_OK;
rv = mDetector->DoIt((const char*)buffer, *buffer_length, &mDontFeedToDetector);
if(mObserver)
mObserver->SetNotifyByReload(PR_TRUE);
return NS_OK;
}
//--------------------------------------------------------------

View File

@ -685,7 +685,8 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand,
webShell->QueryInterface(
NS_GET_IID(nsIWebShellServices),(void**) &wss)))
{
rv_detect = adp->Init(wss, cdet, aCommand);
rv_detect = adp->Init(wss, cdet, (nsIDocument*)this,
mParser, charset.GetUnicode(),aCommand);
}
}
}