Back to index

lightning-sunbird  0.9+nobinonly
nsScriptLoader.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Vidur Apparao <vidur@netscape.com> (original author)
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 #ifndef __nsScriptLoader_h__
00040 #define __nsScriptLoader_h__
00041 
00042 #include "nsCOMPtr.h"
00043 #include "nsIScriptLoader.h"
00044 #include "nsIScriptElement.h"
00045 #include "nsIScriptLoaderObserver.h"
00046 #include "nsIURI.h"
00047 #include "nsCOMArray.h"
00048 #include "nsIDocument.h"
00049 #include "nsIStreamLoader.h"
00050 
00051 class nsScriptLoadRequest;
00052 
00054 // Script loader implementation
00056 
00057 class nsScriptLoader : public nsIScriptLoader,
00058                        public nsIStreamLoaderObserver
00059 {
00060 public:
00061   nsScriptLoader();
00062   virtual ~nsScriptLoader();
00063 
00064   NS_DECL_ISUPPORTS
00065   NS_DECL_NSISCRIPTLOADER
00066   NS_DECL_NSISTREAMLOADEROBSERVER
00067 
00079   static nsresult ConvertToUTF16(nsIChannel* aChannel, const PRUint8* aData,
00080                                  PRUint32 aLength,
00081                                  const nsString& aHintCharset,
00082                                  nsIDocument* aDocument, nsString& aString);
00083 
00091   void AddExecuteBlocker()
00092   {
00093     if (!mBlockerCount++) {
00094       mHadPendingScripts = mPendingRequests.Count() != 0;
00095     }
00096   }
00097   void RemoveExecuteBlocker()
00098   {
00099     if (!--mBlockerCount) {
00100       // If there were pending scripts then the newly added scripts will
00101       // execute once whatever event triggers the pending scripts fires.
00102       // However, due to synchronous loads and pushed event queues it's
00103       // possible that the requests that were there have already been processed
00104       // if so we need to process any new requests asynchronously.
00105       // Ideally that should be fixed such that it can't happen.
00106       if (mHadPendingScripts) {
00107         ProcessPendingRequestsAsync();
00108       }
00109       else {
00110         ProcessPendingReqests();
00111       }
00112     }
00113   }
00114 
00115   void ProcessPendingReqests();
00116 
00121   static PRBool ShouldExecuteScript(nsIDocument* aDocument,
00122                                     nsIChannel* aChannel);
00123 
00124 protected:
00125   PRBool InNonScriptingContainer(nsIScriptElement* aScriptElement);
00126   PRBool IsScriptEventHandler(nsIScriptElement* aScriptElement);
00127   void FireErrorNotification(nsresult aResult,
00128                              nsIScriptElement* aElement,
00129                              nsIScriptLoaderObserver* aObserver);
00130   nsresult ProcessRequest(nsScriptLoadRequest* aRequest);
00131   void FireScriptAvailable(nsresult aResult,
00132                            nsScriptLoadRequest* aRequest,
00133                            const nsAFlatString& aScript);
00134   void FireScriptEvaluated(nsresult aResult,
00135                            nsScriptLoadRequest* aRequest);
00136   nsresult EvaluateScript(nsScriptLoadRequest* aRequest,
00137                           const nsAFlatString& aScript);
00138 
00139   virtual void ProcessPendingRequestsAsync();
00140 
00141   // The guts of ProcessScriptElement.  If aFireErrorNotification is
00142   // true, FireErrorNotification should be called with the return
00143   // value of this method.
00144   nsresult DoProcessScriptElement(nsIScriptElement *aElement,
00145                                   nsIScriptLoaderObserver *aObserver,
00146                                   PRBool* aFireErrorNotification);
00147 
00148   nsIDocument* mDocument;                   // [WEAK]
00149   nsCOMArray<nsIScriptLoaderObserver> mObservers;
00150   nsCOMArray<nsScriptLoadRequest> mPendingRequests;
00151   nsCOMPtr<nsIScriptElement> mCurrentScript;
00152   PRBool mEnabled;
00153   PRBool mHadPendingScripts;
00154   PRUint32 mBlockerCount;
00155 };
00156 
00157 #endif //__nsScriptLoader_h__