Back to index

lightning-sunbird  0.9+nobinonly
nsJSEnvironment.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.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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #ifndef nsJSEnvironment_h___
00038 #define nsJSEnvironment_h___
00039 
00040 #include "nsIScriptContext.h"
00041 #include "nsCOMPtr.h"
00042 #include "jsapi.h"
00043 #include "nsCOMPtr.h"
00044 #include "nsIObserver.h"
00045 #include "nsIScriptSecurityManager.h"
00046 #include "nsIXPCScriptNotify.h"
00047 #include "nsITimer.h"
00048 #include "prtime.h"
00049 class nsIXPConnectJSObjectHolder;
00050 
00051 class nsJSContext : public nsIScriptContext,
00052                     public nsIXPCScriptNotify,
00053                     public nsITimerCallback
00054 {
00055 public:
00056   nsJSContext(JSRuntime *aRuntime);
00057   virtual ~nsJSContext();
00058 
00059   NS_DECL_ISUPPORTS
00060 
00061   virtual nsresult EvaluateString(const nsAString& aScript,
00062                                   void *aScopeObject,
00063                                   nsIPrincipal *principal,
00064                                   const char *aURL,
00065                                   PRUint32 aLineNo,
00066                                   const char* aVersion,
00067                                   nsAString *aRetValue,
00068                                   PRBool* aIsUndefined);
00069   virtual nsresult EvaluateStringWithValue(const nsAString& aScript,
00070                                      void *aScopeObject,
00071                                      nsIPrincipal *aPrincipal,
00072                                      const char *aURL,
00073                                      PRUint32 aLineNo,
00074                                      const char* aVersion,
00075                                      void* aRetValue,
00076                                      PRBool* aIsUndefined);
00077 
00078   virtual nsresult CompileScript(const PRUnichar* aText,
00079                                  PRInt32 aTextLength,
00080                                  void *aScopeObject,
00081                                  nsIPrincipal *principal,
00082                                  const char *aURL,
00083                                  PRUint32 aLineNo,
00084                                  const char* aVersion,
00085                                  void** aScriptObject);
00086   virtual nsresult ExecuteScript(void* aScriptObject,
00087                                  void *aScopeObject,
00088                                  nsAString* aRetValue,
00089                                  PRBool* aIsUndefined);
00090   virtual nsresult CompileEventHandler(void *aTarget,
00091                                        nsIAtom *aName,
00092                                        const char *aEventName,
00093                                        const nsAString& aBody,
00094                                        const char *aURL,
00095                                        PRUint32 aLineNo,
00096                                        PRBool aShared,
00097                                        void** aHandler);
00098   virtual nsresult CallEventHandler(JSObject *aTarget, JSObject *aHandler, 
00099                                     uintN argc, jsval *argv, jsval* rval);
00100   virtual nsresult BindCompiledEventHandler(void *aTarget,
00101                                             nsIAtom *aName,
00102                                             void *aHandler);
00103   virtual nsresult CompileFunction(void* aTarget,
00104                                    const nsACString& aName,
00105                                    PRUint32 aArgCount,
00106                                    const char** aArgArray,
00107                                    const nsAString& aBody,
00108                                    const char* aURL,
00109                                    PRUint32 aLineNo,
00110                                    PRBool aShared,
00111                                    void** aFunctionObject);
00112 
00113   virtual void SetDefaultLanguageVersion(const char* aVersion);
00114   virtual nsIScriptGlobalObject *GetGlobalObject();
00115   virtual void *GetNativeContext();
00116   virtual nsresult InitContext(nsIScriptGlobalObject *aGlobalObject);
00117   virtual PRBool IsContextInitialized();
00118   virtual void GC();
00119 
00120   virtual void ScriptEvaluated(PRBool aTerminated);
00121   virtual void SetOwner(nsIScriptContextOwner* owner);
00122   virtual nsIScriptContextOwner *GetOwner();
00123   virtual nsresult SetTerminationFunction(nsScriptTerminationFunc aFunc,
00124                                           nsISupports* aRef);
00125   virtual PRBool GetScriptsEnabled();
00126   virtual void SetScriptsEnabled(PRBool aEnabled, PRBool aFireTimeouts);
00127 
00128   virtual PRBool GetProcessingScriptTag();
00129   virtual void SetProcessingScriptTag(PRBool aResult);
00130 
00131   virtual void SetGCOnDestruction(PRBool aGCOnDestruction);
00132 
00133   virtual nsresult InitClasses(JSObject *aGlobalObj);
00134 
00135   virtual void WillInitializeContext();
00136   virtual void DidInitializeContext();
00137 
00138   NS_DECL_NSIXPCSCRIPTNOTIFY
00139 
00140   NS_DECL_NSITIMERCALLBACK
00141 
00142 protected:
00143   nsresult InitializeExternalClasses();
00144   nsresult InitializeLiveConnectClasses(JSObject *aGlobalObj);
00145   // aHolder should be holding our global object
00146   nsresult FindXPCNativeWrapperClass(nsIXPConnectJSObjectHolder *aHolder);
00147 
00148   void FireGCTimer();
00149 
00150 private:
00151   JSContext *mContext;
00152   PRUint32 mNumEvaluations;
00153 
00154   nsIScriptContextOwner* mOwner;  /* NB: weak reference, not ADDREF'd */
00155 
00156 protected:
00157   struct TerminationFuncHolder;
00158   friend struct TerminationFuncHolder;
00159   
00160   struct TerminationFuncClosure
00161   {
00162     TerminationFuncClosure(nsScriptTerminationFunc aFunc,
00163                            nsISupports* aArg,
00164                            TerminationFuncClosure* aNext) :
00165       mTerminationFunc(aFunc),
00166       mTerminationFuncArg(aArg),
00167       mNext(aNext)
00168     {
00169     }
00170     ~TerminationFuncClosure()
00171     {
00172       delete mNext;
00173     }
00174     
00175     nsScriptTerminationFunc mTerminationFunc;
00176     nsCOMPtr<nsISupports> mTerminationFuncArg;
00177     TerminationFuncClosure* mNext;
00178   };
00179 
00180   struct TerminationFuncHolder
00181   {
00182     TerminationFuncHolder(nsJSContext* aContext)
00183       : mContext(aContext),
00184         mTerminations(aContext->mTerminations)
00185     {
00186       aContext->mTerminations = nsnull;
00187     }
00188     ~TerminationFuncHolder()
00189     {
00190       // Have to be careful here.  mContext might have picked up new
00191       // termination funcs while the script was evaluating.  Prepend whatever
00192       // we have to the current termination funcs on the context (since our
00193       // termination funcs were posted first).
00194       if (mTerminations) {
00195         TerminationFuncClosure* cur = mTerminations;
00196         while (cur->mNext) {
00197           cur = cur->mNext;
00198         }
00199         cur->mNext = mContext->mTerminations;
00200         mContext->mTerminations = mTerminations;
00201       }
00202     }
00203 
00204     nsJSContext* mContext;
00205     TerminationFuncClosure* mTerminations;
00206   };
00207   
00208   TerminationFuncClosure* mTerminations;
00209 
00210 private:
00211   PRPackedBool mIsInitialized;
00212   PRPackedBool mScriptsEnabled;
00213   PRPackedBool mGCOnDestruction;
00214   PRPackedBool mProcessingScriptTag;
00215   PRPackedBool mIsTrackingChromeCodeTime;
00216 
00217   PRUint32 mBranchCallbackCount;
00218   PRTime mBranchCallbackTime;
00219   PRUint32 mDefaultJSOptions;
00220 
00221   // mGlobalWrapperRef is used only to hold a strong reference to the
00222   // global object wrapper while the nsJSContext is alive. This cuts
00223   // down on the number of rooting and unrooting calls XPConnect has
00224   // to make when the global object is touched in JS.
00225 
00226   nsCOMPtr<nsISupports> mGlobalWrapperRef;
00227 
00228   static int PR_CALLBACK JSOptionChangedCallback(const char *pref, void *data);
00229 
00230   static JSBool JS_DLL_CALLBACK DOMBranchCallback(JSContext *cx,
00231                                                   JSScript *script);
00232 };
00233 
00234 class nsIJSRuntimeService;
00235 
00236 class nsJSEnvironment
00237 {
00238 private:
00239   static JSRuntime *sRuntime;
00240 
00241 public:
00242   // called from the module Ctor to initialize statics
00243   static void Startup();
00244 
00245   static nsresult Init();
00246 
00247   static nsresult CreateNewContext(nsIScriptContext **aContext);
00248 
00249   static void ShutDown();
00250 };
00251 
00252 /* factory function */
00253 nsresult NS_CreateScriptContext(nsIScriptGlobalObject *aGlobal,
00254                                 nsIScriptContext **aContext);
00255 
00256 /* prototypes */
00257 void JS_DLL_CALLBACK NS_ScriptErrorReporter(JSContext *cx, const char *message, JSErrorReport *report);
00258 
00259 #endif /* nsJSEnvironment_h___ */