1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 #include "nsCharDetDll.h"
43 #include "nsIParser.h"
44 #include "nsIDocument.h"
45 #include "nsDetectionAdaptor.h"
46 #include "nsIContentSink.h"
49 //--------------------------------------------------------------
50 NS_IMETHODIMP
nsMyObserver::Notify(
51 const char* aCharset
, nsDetectionConfident aConf
)
56 nsCAutoString existingCharset
;
57 PRInt32 existingSource
;
58 mWeakRefParser
->GetDocumentCharset(existingCharset
, existingSource
);
59 if (existingSource
>= kCharsetFromAutoDetection
)
63 if(!mCharset
.Equals(aCharset
)) {
65 rv
= mWebShellSvc
->SetRendering( PR_FALSE
);
66 rv
= mWebShellSvc
->StopDocumentLoad();
67 rv
= mWebShellSvc
->ReloadDocument(aCharset
, kCharsetFromAutoDetection
);
69 nsDependentCString
newcharset(aCharset
);
71 mWeakRefParser
->SetDocumentCharset(newcharset
, kCharsetFromAutoDetection
);
72 nsCOMPtr
<nsIContentSink
> contentSink
= mWeakRefParser
->GetContentSink();
74 contentSink
->SetDocumentCharset(newcharset
);
77 mWeakRefDocument
->SetDocumentCharacterSet(newcharset
);
82 //--------------------------------------------------------------
83 NS_IMETHODIMP
nsMyObserver::Init( nsIWebShellServices
* aWebShellSvc
,
84 nsIDocument
* aDocument
,
96 mWeakRefDocument
= aDocument
;
99 mWeakRefParser
= aParser
;
101 if(nsnull
!= aWebShellSvc
)
103 mWebShellSvc
= aWebShellSvc
;
106 return NS_ERROR_ILLEGAL_VALUE
;
108 //--------------------------------------------------------------
109 NS_IMPL_ISUPPORTS1 ( nsMyObserver
,nsICharsetDetectionObserver
)
111 //--------------------------------------------------------------
112 nsDetectionAdaptor::nsDetectionAdaptor( void )
114 mDontFeedToDetector
= PR_TRUE
;
116 //--------------------------------------------------------------
117 nsDetectionAdaptor::~nsDetectionAdaptor()
121 //--------------------------------------------------------------
122 NS_IMPL_ISUPPORTS2 (nsDetectionAdaptor
, nsIParserFilter
, nsICharsetDetectionAdaptor
)
124 //--------------------------------------------------------------
125 NS_IMETHODIMP
nsDetectionAdaptor::Init(
126 nsIWebShellServices
* aWebShellSvc
, nsICharsetDetector
*aDetector
,
127 nsIDocument
* aDocument
, nsIParser
* aParser
, const char* aCharset
,
128 const char* aCommand
)
130 if((nsnull
!= aWebShellSvc
) && (nsnull
!= aDetector
) && (nsnull
!= aCharset
))
133 mObserver
= new nsMyObserver();
135 return NS_ERROR_OUT_OF_MEMORY
;
137 rv
= mObserver
->Init(aWebShellSvc
, aDocument
, aParser
, aCharset
, aCommand
);
138 if(NS_SUCCEEDED(rv
)) {
139 rv
= aDetector
->Init(mObserver
.get());
140 if(NS_SUCCEEDED(rv
)) {
141 mDetector
= aDetector
;
142 mDontFeedToDetector
= PR_FALSE
;
147 return NS_ERROR_ILLEGAL_VALUE
;
149 //--------------------------------------------------------------
150 NS_IMETHODIMP
nsDetectionAdaptor::RawBuffer
151 (const char * buffer
, PRUint32
* buffer_length
)
153 if((mDontFeedToDetector
) || (!mDetector
))
156 rv
= mDetector
->DoIt((const char*)buffer
, *buffer_length
, &mDontFeedToDetector
);
158 mObserver
->SetNotifyByReload(PR_TRUE
);
162 //--------------------------------------------------------------
163 NS_IMETHODIMP
nsDetectionAdaptor::Finish()
165 if((mDontFeedToDetector
) || (!mDetector
))
168 rv
= mDetector
->Done();