Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / intl / chardet / src / nsDetectionAdaptor.cpp
blob993044d283495d6d374554b371c40bc1c280732e
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
39 #include "nsString.h"
40 #include "plstr.h"
41 #include "pratom.h"
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)
53 nsresult rv = NS_OK;
55 if(mWeakRefParser) {
56 nsCAutoString existingCharset;
57 PRInt32 existingSource;
58 mWeakRefParser->GetDocumentCharset(existingCharset, existingSource);
59 if (existingSource >= kCharsetFromAutoDetection)
60 return NS_OK;
63 if(!mCharset.Equals(aCharset)) {
64 if(mNotifyByReload) {
65 rv = mWebShellSvc->SetRendering( PR_FALSE);
66 rv = mWebShellSvc->StopDocumentLoad();
67 rv = mWebShellSvc->ReloadDocument(aCharset, kCharsetFromAutoDetection);
68 } else {
69 nsDependentCString newcharset(aCharset);
70 if (mWeakRefParser) {
71 mWeakRefParser->SetDocumentCharset(newcharset, kCharsetFromAutoDetection);
72 nsCOMPtr<nsIContentSink> contentSink = mWeakRefParser->GetContentSink();
73 if (contentSink)
74 contentSink->SetDocumentCharset(newcharset);
76 if(mWeakRefDocument)
77 mWeakRefDocument->SetDocumentCharacterSet(newcharset);
80 return NS_OK;
82 //--------------------------------------------------------------
83 NS_IMETHODIMP nsMyObserver::Init( nsIWebShellServices* aWebShellSvc,
84 nsIDocument* aDocument,
85 nsIParser* aParser,
86 const char* aCharset,
87 const char* aCommand)
89 if(aCommand) {
90 mCommand = aCommand;
92 if(aCharset) {
93 mCharset = aCharset;
95 if(aDocument) {
96 mWeakRefDocument = aDocument;
98 if(aParser) {
99 mWeakRefParser = aParser;
101 if(nsnull != aWebShellSvc)
103 mWebShellSvc = aWebShellSvc;
104 return NS_OK;
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))
132 nsresult rv = NS_OK;
133 mObserver = new nsMyObserver();
134 if(!mObserver)
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;
143 return NS_OK;
147 return NS_ERROR_ILLEGAL_VALUE;
149 //--------------------------------------------------------------
150 NS_IMETHODIMP nsDetectionAdaptor::RawBuffer
151 (const char * buffer, PRUint32 * buffer_length)
153 if((mDontFeedToDetector) || (!mDetector))
154 return NS_OK;
155 nsresult rv = NS_OK;
156 rv = mDetector->DoIt((const char*)buffer, *buffer_length, &mDontFeedToDetector);
157 if(mObserver)
158 mObserver->SetNotifyByReload(PR_TRUE);
160 return NS_OK;
162 //--------------------------------------------------------------
163 NS_IMETHODIMP nsDetectionAdaptor::Finish()
165 if((mDontFeedToDetector) || (!mDetector))
166 return NS_OK;
167 nsresult rv = NS_OK;
168 rv = mDetector->Done();
170 return NS_OK;