Back to index

lightning-sunbird  0.9+nobinonly
nsDOMScriptObjectFactory.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  * vim: set ts=2 sw=2 et tw=78:
00003  *
00004  * ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is mozilla.org code.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK *****
00039  *
00040  *
00041  * This Original Code has been modified by IBM Corporation.
00042  * Modifications made by IBM described herein are
00043  * Copyright (c) International Business Machines
00044  * Corporation, 2000
00045  *
00046  * Modifications to Mozilla code or documentation
00047  * identified per MPL Section 3.3
00048  *
00049  * Date         Modified by     Description of modification
00050  * 03/27/2000   IBM Corp.       Added PR_CALLBACK for Optlink
00051  *                               use in OS2
00052  */
00053 
00054 #include "nsDOMScriptObjectFactory.h"
00055 #include "xpcexception.h"
00056 #include "nsScriptNameSpaceManager.h"
00057 #include "nsIObserverService.h"
00058 #include "nsJSEnvironment.h"
00059 #include "nsJSEventListener.h"
00060 #include "nsGlobalWindow.h"
00061 #include "nsIJSContextStack.h"
00062 #include "nsDOMException.h"
00063 #include "nsCRT.h"
00064 #ifdef MOZ_XUL
00065 #include "nsIXULPrototypeCache.h"
00066 #endif
00067 
00068 nsDOMScriptObjectFactory::nsDOMScriptObjectFactory()
00069 {
00070   nsCOMPtr<nsIObserverService> observerService =
00071     do_GetService("@mozilla.org/observer-service;1");
00072 
00073   if (observerService) {
00074     observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
00075   }
00076 
00077   nsCOMPtr<nsIExceptionService> xs =
00078     do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
00079 
00080   if (xs) {
00081     xs->RegisterExceptionProvider(this, NS_ERROR_MODULE_DOM);
00082     xs->RegisterExceptionProvider(this, NS_ERROR_MODULE_DOM_RANGE);
00083 #ifdef MOZ_SVG
00084     xs->RegisterExceptionProvider(this, NS_ERROR_MODULE_SVG);
00085 #endif
00086     xs->RegisterExceptionProvider(this, NS_ERROR_MODULE_XPCONNECT);
00087   }
00088 }
00089 
00090 NS_INTERFACE_MAP_BEGIN(nsDOMScriptObjectFactory)
00091   NS_INTERFACE_MAP_ENTRY(nsIDOMScriptObjectFactory)
00092   NS_INTERFACE_MAP_ENTRY(nsIObserver)
00093   NS_INTERFACE_MAP_ENTRY(nsIExceptionProvider)
00094   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMScriptObjectFactory)
00095 NS_INTERFACE_MAP_END
00096 
00097 
00098 NS_IMPL_ADDREF(nsDOMScriptObjectFactory)
00099 NS_IMPL_RELEASE(nsDOMScriptObjectFactory)
00100 
00101 
00102 NS_IMETHODIMP
00103 nsDOMScriptObjectFactory::NewScriptContext(nsIScriptGlobalObject *aGlobal,
00104                                            nsIScriptContext **aContext)
00105 {
00106   return NS_CreateScriptContext(aGlobal, aContext);
00107 }
00108 
00109 NS_IMETHODIMP
00110 nsDOMScriptObjectFactory::NewScriptGlobalObject(PRBool aIsChrome,
00111                                                 nsIScriptGlobalObject **aGlobal)
00112 {
00113   return NS_NewScriptGlobalObject(aIsChrome, aGlobal);
00114 }
00115 
00116 NS_IMETHODIMP_(nsISupports *)
00117 nsDOMScriptObjectFactory::GetClassInfoInstance(nsDOMClassInfoID aID)
00118 {
00119   return nsDOMClassInfo::GetClassInfoInstance(aID);
00120 }
00121 
00122 NS_IMETHODIMP_(nsISupports *)
00123 nsDOMScriptObjectFactory::GetExternalClassInfoInstance(const nsAString& aName)
00124 {
00125   extern nsScriptNameSpaceManager *gNameSpaceManager;
00126 
00127   NS_ENSURE_TRUE(gNameSpaceManager, nsnull);
00128 
00129   const nsGlobalNameStruct *globalStruct;
00130   gNameSpaceManager->LookupName(aName, &globalStruct);
00131   if (globalStruct) {
00132     if (globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfoCreator) {
00133       nsresult rv;
00134       nsCOMPtr<nsIDOMCIExtension> creator(do_CreateInstance(globalStruct->mCID, &rv));
00135       NS_ENSURE_SUCCESS(rv, nsnull);
00136 
00137       rv = creator->RegisterDOMCI(NS_ConvertUCS2toUTF8(aName).get(), this);
00138       NS_ENSURE_SUCCESS(rv, nsnull);
00139 
00140       rv = gNameSpaceManager->LookupName(aName, &globalStruct);
00141       NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && globalStruct, nsnull);
00142 
00143       NS_ASSERTION(globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo,
00144                    "The classinfo data for this class didn't get registered.");
00145     }
00146     if (globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
00147       return nsDOMClassInfo::GetClassInfoInstance(globalStruct->mData);
00148     }
00149   }
00150   return nsnull;
00151 }
00152 
00153 NS_IMETHODIMP
00154 nsDOMScriptObjectFactory::Observe(nsISupports *aSubject,
00155                                   const char *aTopic,
00156                                   const PRUnichar *someData)
00157 {
00158   if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
00159 #ifdef MOZ_XUL
00160     // Flush the XUL cache since it holds JS roots, and we're about to
00161     // start the final GC.
00162     nsCOMPtr<nsIXULPrototypeCache> cache =
00163       do_GetService("@mozilla.org/xul/xul-prototype-cache;1");
00164 
00165     if (cache)
00166       cache->Flush();
00167 #endif
00168 
00169     nsCOMPtr<nsIThreadJSContextStack> stack =
00170       do_GetService("@mozilla.org/js/xpc/ContextStack;1");
00171 
00172     if (stack) {
00173       JSContext *cx = nsnull;
00174 
00175       stack->GetSafeJSContext(&cx);
00176 
00177       if (cx) {
00178         // Do one final GC to clean things up before shutdown.
00179 
00180         ::JS_GC(cx);
00181       }
00182     }
00183 
00184     nsGlobalWindow::ShutDown();
00185     nsDOMClassInfo::ShutDown();
00186     nsJSEnvironment::ShutDown();
00187 
00188     nsCOMPtr<nsIExceptionService> xs =
00189       do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
00190 
00191     if (xs) {
00192       xs->UnregisterExceptionProvider(this, NS_ERROR_MODULE_DOM);
00193       xs->UnregisterExceptionProvider(this, NS_ERROR_MODULE_DOM_RANGE);
00194 #ifdef MOZ_SVG
00195       xs->UnregisterExceptionProvider(this, NS_ERROR_MODULE_SVG);
00196 #endif
00197       xs->UnregisterExceptionProvider(this, NS_ERROR_MODULE_XPCONNECT);
00198     }
00199   }
00200 
00201   return NS_OK;
00202 }
00203 
00204 static nsresult
00205 CreateXPConnectException(nsresult aResult, nsIException *aDefaultException,
00206                          nsIException **_retval)
00207 {
00208   nsresult rv = NS_OK;
00209   nsCOMPtr<nsIXPCException> exception(
00210       do_CreateInstance("@mozilla.org/js/xpc/Exception;1", &rv));
00211   NS_ENSURE_SUCCESS(rv, rv);
00212 
00213   rv = exception->Initialize(nsnull, aResult, nsnull, nsnull, nsnull, nsnull);
00214   NS_ENSURE_SUCCESS(rv, rv);
00215 
00216   NS_ADDREF(*_retval = exception);
00217   return NS_OK;
00218 }
00219 
00220 NS_IMETHODIMP
00221 nsDOMScriptObjectFactory::GetException(nsresult result,
00222                                    nsIException *aDefaultException,
00223                                    nsIException **_retval)
00224 {
00225   switch (NS_ERROR_GET_MODULE(result))
00226   {
00227     case NS_ERROR_MODULE_DOM_RANGE:
00228       return NS_NewRangeException(result, aDefaultException, _retval);
00229 #ifdef MOZ_SVG
00230     case NS_ERROR_MODULE_SVG:
00231       return NS_NewSVGException(result, aDefaultException, _retval);
00232 #endif
00233     case NS_ERROR_MODULE_XPCONNECT:
00234       return CreateXPConnectException(result, aDefaultException, _retval);
00235     default:
00236       return NS_NewDOMException(result, aDefaultException, _retval);
00237   }
00238 }
00239 
00240 NS_IMETHODIMP
00241 nsDOMScriptObjectFactory::RegisterDOMClassInfo(const char *aName,
00242                                           nsDOMClassInfoExternalConstructorFnc aConstructorFptr,
00243                                           const nsIID *aProtoChainInterface,
00244                                           const nsIID **aInterfaces,
00245                                           PRUint32 aScriptableFlags,
00246                                           PRBool aHasClassInterface,
00247                                           const nsCID *aConstructorCID)
00248 {
00249   extern nsScriptNameSpaceManager *gNameSpaceManager;
00250 
00251   NS_ENSURE_TRUE(gNameSpaceManager, NS_ERROR_NOT_INITIALIZED);
00252 
00253   return gNameSpaceManager->RegisterDOMCIData(aName,
00254                                               aConstructorFptr,
00255                                               aProtoChainInterface,
00256                                               aInterfaces,
00257                                               aScriptableFlags,
00258                                               aHasClassInterface,
00259                                               aConstructorCID);
00260 }