Back to index

lightning-sunbird  0.9+nobinonly
nsDetectionAdaptor.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsString.h"
00040 #include "plstr.h"
00041 #include "pratom.h"
00042 #include "nsCharDetDll.h"
00043 #include "nsIParser.h"
00044 #include "nsIDocument.h"
00045 #include "nsDetectionAdaptor.h"
00046 #include "nsIContentSink.h"
00047 
00048 
00049 //--------------------------------------------------------------
00050 NS_IMETHODIMP nsMyObserver::Notify(
00051     const char* aCharset, nsDetectionConfident aConf)
00052 {
00053     nsresult rv = NS_OK;
00054 
00055     if(mWeakRefParser) {
00056       nsCAutoString existingCharset;
00057       PRInt32 existingSource;
00058       mWeakRefParser->GetDocumentCharset(existingCharset, existingSource);  
00059       if (existingSource >= kCharsetFromAutoDetection) 
00060         return NS_OK;
00061     }
00062      
00063     if(!mCharset.Equals(aCharset)) {
00064       if(mNotifyByReload) {
00065         rv = mWebShellSvc->SetRendering( PR_FALSE);
00066         rv = mWebShellSvc->StopDocumentLoad();
00067         rv = mWebShellSvc->ReloadDocument(aCharset, kCharsetFromAutoDetection);
00068       } else {
00069         nsDependentCString newcharset(aCharset);
00070         if (mWeakRefParser) {
00071           mWeakRefParser->SetDocumentCharset(newcharset, kCharsetFromAutoDetection);
00072           nsCOMPtr<nsIContentSink> contentSink = mWeakRefParser->GetContentSink();
00073           if (contentSink)
00074             contentSink->SetDocumentCharset(newcharset);
00075         }
00076         if(mWeakRefDocument) 
00077           mWeakRefDocument->SetDocumentCharacterSet(newcharset);
00078       }
00079     }
00080     return NS_OK;
00081 }
00082 //--------------------------------------------------------------
00083 NS_IMETHODIMP nsMyObserver::Init( nsIWebShellServices* aWebShellSvc, 
00084                    nsIDocument* aDocument,
00085                    nsIParser* aParser,
00086                    const char* aCharset,
00087                    const char* aCommand)
00088 {
00089     if(aCommand) {
00090         mCommand = aCommand;
00091     }
00092     if(aCharset) {
00093         mCharset = aCharset;
00094     }
00095     if(aDocument) {
00096         mWeakRefDocument = aDocument;
00097     }
00098     if(aParser) {
00099         mWeakRefParser = aParser;
00100     }
00101     if(nsnull != aWebShellSvc)
00102     {
00103         mWebShellSvc = aWebShellSvc;
00104         return NS_OK;
00105     }
00106     return NS_ERROR_ILLEGAL_VALUE;
00107 }
00108 //--------------------------------------------------------------
00109 NS_IMPL_ISUPPORTS1 ( nsMyObserver ,nsICharsetDetectionObserver)
00110 
00111 //--------------------------------------------------------------
00112 nsDetectionAdaptor::nsDetectionAdaptor( void ) 
00113 {
00114      mDontFeedToDetector = PR_TRUE;
00115 }
00116 //--------------------------------------------------------------
00117 nsDetectionAdaptor::~nsDetectionAdaptor()
00118 {
00119 }
00120 
00121 //--------------------------------------------------------------
00122 NS_IMPL_ISUPPORTS2 (nsDetectionAdaptor, nsIParserFilter, nsICharsetDetectionAdaptor)
00123 
00124 //--------------------------------------------------------------
00125 NS_IMETHODIMP nsDetectionAdaptor::Init(
00126     nsIWebShellServices* aWebShellSvc, nsICharsetDetector *aDetector,
00127     nsIDocument* aDocument, nsIParser* aParser, const char* aCharset,
00128     const char* aCommand)
00129 {
00130   if((nsnull != aWebShellSvc) && (nsnull != aDetector) && (nsnull != aCharset))
00131   {
00132     nsresult rv = NS_OK;
00133     mObserver = new nsMyObserver();
00134     if(!mObserver)
00135        return NS_ERROR_OUT_OF_MEMORY;
00136 
00137     rv = mObserver->Init(aWebShellSvc, aDocument, aParser, aCharset, aCommand);
00138     if(NS_SUCCEEDED(rv)) {
00139       rv = aDetector->Init(mObserver.get());
00140       if(NS_SUCCEEDED(rv)) {
00141         mDetector = aDetector;
00142         mDontFeedToDetector = PR_FALSE;
00143         return NS_OK;
00144       }
00145     }
00146   }
00147   return NS_ERROR_ILLEGAL_VALUE;
00148 }
00149 //--------------------------------------------------------------
00150 NS_IMETHODIMP nsDetectionAdaptor::RawBuffer
00151     (const char * buffer, PRUint32 * buffer_length) 
00152 {
00153     if((mDontFeedToDetector) || (!mDetector))
00154        return NS_OK;
00155     nsresult rv = NS_OK;
00156     rv = mDetector->DoIt((const char*)buffer, *buffer_length, &mDontFeedToDetector);
00157     if(mObserver) 
00158        mObserver->SetNotifyByReload(PR_TRUE);
00159 
00160     return NS_OK;
00161 }
00162 //--------------------------------------------------------------
00163 NS_IMETHODIMP nsDetectionAdaptor::Finish()
00164 {
00165     if((mDontFeedToDetector) || (!mDetector))
00166        return NS_OK;
00167     nsresult rv = NS_OK;
00168     rv = mDetector->Done();
00169 
00170     return NS_OK;
00171 }
00172