2004-02-24 03:22:35 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2006-04-15 00:37:51 +00:00
|
|
|
* vim: set ts=2 sw=2 et tw=78:
|
2004-02-24 03:22:35 +00:00
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2004-02-24 03:22:35 +00:00
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2004-02-24 03:22:35 +00:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2004-02-24 03:22:35 +00:00
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* ***** END LICENSE BLOCK *****
|
2004-02-24 03:22:35 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This Original Code has been modified by IBM Corporation.
|
|
|
|
* Modifications made by IBM described herein are
|
|
|
|
* Copyright (c) International Business Machines
|
|
|
|
* Corporation, 2000
|
|
|
|
*
|
|
|
|
* Modifications to Mozilla code or documentation
|
|
|
|
* identified per MPL Section 3.3
|
|
|
|
*
|
|
|
|
* Date Modified by Description of modification
|
|
|
|
* 03/27/2000 IBM Corp. Added PR_CALLBACK for Optlink
|
|
|
|
* use in OS2
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsDOMScriptObjectFactory.h"
|
2006-04-15 00:37:51 +00:00
|
|
|
#include "xpcexception.h"
|
2004-02-24 03:22:35 +00:00
|
|
|
#include "nsScriptNameSpaceManager.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsJSEventListener.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsIJSContextStack.h"
|
2006-06-13 03:07:47 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2004-02-24 03:22:35 +00:00
|
|
|
#include "nsDOMException.h"
|
2004-05-29 17:43:59 +00:00
|
|
|
#include "nsCRT.h"
|
2004-02-24 04:19:15 +00:00
|
|
|
#ifdef MOZ_XUL
|
2007-03-12 05:53:33 +00:00
|
|
|
#include "nsXULPrototypeCache.h"
|
2004-02-24 04:19:15 +00:00
|
|
|
#endif
|
2008-11-06 06:41:52 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2004-02-24 03:22:35 +00:00
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
static NS_DEFINE_CID(kDOMScriptObjectFactoryCID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID);
|
|
|
|
|
2008-09-30 23:55:42 +00:00
|
|
|
nsIExceptionProvider* gExceptionProvider = nsnull;
|
|
|
|
|
2012-04-14 13:03:16 +00:00
|
|
|
nsDOMScriptObjectFactory::nsDOMScriptObjectFactory()
|
2004-02-24 03:22:35 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 16:59:13 +00:00
|
|
|
mozilla::services::GetObserverService();
|
2004-02-24 03:22:35 +00:00
|
|
|
if (observerService) {
|
2011-10-17 14:59:28 +00:00
|
|
|
observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
2004-02-24 03:22:35 +00:00
|
|
|
}
|
|
|
|
|
2012-04-14 13:03:16 +00:00
|
|
|
nsCOMPtr<nsIExceptionProvider> provider = new nsDOMExceptionProvider();
|
|
|
|
nsCOMPtr<nsIExceptionService> xs =
|
|
|
|
do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
|
2008-09-30 23:55:42 +00:00
|
|
|
|
2012-04-14 13:03:16 +00:00
|
|
|
if (xs) {
|
|
|
|
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM);
|
|
|
|
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_SVG);
|
|
|
|
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_XPATH);
|
|
|
|
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_INDEXEDDB);
|
|
|
|
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_XPCONNECT);
|
2004-02-24 03:22:35 +00:00
|
|
|
}
|
2008-09-30 23:55:42 +00:00
|
|
|
|
2012-04-14 13:03:16 +00:00
|
|
|
NS_ASSERTION(!gExceptionProvider, "Registered twice?!");
|
|
|
|
provider.swap(gExceptionProvider);
|
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
// And pre-create the javascript language.
|
2012-04-14 13:03:16 +00:00
|
|
|
NS_CreateJSRuntime(getter_AddRefs(mJSRuntime));
|
2004-02-24 03:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDOMScriptObjectFactory)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMScriptObjectFactory)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMScriptObjectFactory)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsDOMScriptObjectFactory)
|
|
|
|
NS_IMPL_RELEASE(nsDOMScriptObjectFactory)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsISupports *)
|
|
|
|
nsDOMScriptObjectFactory::GetClassInfoInstance(nsDOMClassInfoID aID)
|
|
|
|
{
|
2007-03-19 09:19:16 +00:00
|
|
|
return NS_GetDOMClassInfoInstance(aID);
|
2004-02-24 03:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsISupports *)
|
|
|
|
nsDOMScriptObjectFactory::GetExternalClassInfoInstance(const nsAString& aName)
|
|
|
|
{
|
2007-11-13 10:35:49 +00:00
|
|
|
nsScriptNameSpaceManager *nameSpaceManager = nsJSRuntime::GetNameSpaceManager();
|
|
|
|
NS_ENSURE_TRUE(nameSpaceManager, nsnull);
|
2004-02-24 03:22:35 +00:00
|
|
|
|
2012-03-29 18:43:13 +00:00
|
|
|
const nsGlobalNameStruct *globalStruct = nameSpaceManager->LookupName(aName);
|
2004-02-24 03:22:35 +00:00
|
|
|
if (globalStruct) {
|
|
|
|
if (globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfoCreator) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIDOMCIExtension> creator(do_CreateInstance(globalStruct->mCID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
2006-02-03 14:18:39 +00:00
|
|
|
rv = creator->RegisterDOMCI(NS_ConvertUTF16toUTF8(aName).get(), this);
|
2004-02-24 03:22:35 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
2012-03-29 18:43:13 +00:00
|
|
|
globalStruct = nameSpaceManager->LookupName(aName);
|
|
|
|
NS_ENSURE_TRUE(globalStruct, nsnull);
|
2004-02-24 03:22:35 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo,
|
|
|
|
"The classinfo data for this class didn't get registered.");
|
|
|
|
}
|
|
|
|
if (globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
|
|
|
|
return nsDOMClassInfo::GetClassInfoInstance(globalStruct->mData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMScriptObjectFactory::Observe(nsISupports *aSubject,
|
2005-07-30 20:57:07 +00:00
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *someData)
|
2004-02-24 03:22:35 +00:00
|
|
|
{
|
|
|
|
if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
#ifdef MOZ_XUL
|
|
|
|
// Flush the XUL cache since it holds JS roots, and we're about to
|
|
|
|
// start the final GC.
|
2007-03-12 05:53:33 +00:00
|
|
|
nsXULPrototypeCache* cache = nsXULPrototypeCache::GetInstance();
|
2004-02-24 03:22:35 +00:00
|
|
|
|
|
|
|
if (cache)
|
|
|
|
cache->Flush();
|
|
|
|
#endif
|
|
|
|
|
2004-12-10 19:48:22 +00:00
|
|
|
nsGlobalWindow::ShutDown();
|
2004-02-24 03:22:35 +00:00
|
|
|
nsDOMClassInfo::ShutDown();
|
2006-06-13 03:07:47 +00:00
|
|
|
|
2008-09-30 23:55:42 +00:00
|
|
|
if (gExceptionProvider) {
|
|
|
|
nsCOMPtr<nsIExceptionService> xs =
|
|
|
|
do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
|
2004-02-24 03:22:35 +00:00
|
|
|
|
2008-09-30 23:55:42 +00:00
|
|
|
if (xs) {
|
|
|
|
xs->UnregisterExceptionProvider(gExceptionProvider,
|
|
|
|
NS_ERROR_MODULE_DOM);
|
|
|
|
xs->UnregisterExceptionProvider(gExceptionProvider,
|
|
|
|
NS_ERROR_MODULE_SVG);
|
|
|
|
xs->UnregisterExceptionProvider(gExceptionProvider,
|
|
|
|
NS_ERROR_MODULE_DOM_XPATH);
|
|
|
|
xs->UnregisterExceptionProvider(gExceptionProvider,
|
|
|
|
NS_ERROR_MODULE_XPCONNECT);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE(gExceptionProvider);
|
2004-02-24 03:22:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-04-15 00:37:51 +00:00
|
|
|
static nsresult
|
|
|
|
CreateXPConnectException(nsresult aResult, nsIException *aDefaultException,
|
|
|
|
nsIException **_retval)
|
|
|
|
{
|
2007-04-27 04:19:11 +00:00
|
|
|
// See whether we already have a useful XPConnect exception. If we
|
|
|
|
// do, let's not create one with _less_ information!
|
|
|
|
nsCOMPtr<nsIXPCException> exception(do_QueryInterface(aDefaultException));
|
|
|
|
if (!exception) {
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
exception = do_CreateInstance("@mozilla.org/js/xpc/Exception;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = exception->Initialize(nsnull, aResult, nsnull, nsnull, nsnull,
|
|
|
|
nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2006-04-15 00:37:51 +00:00
|
|
|
|
2012-04-14 13:03:16 +00:00
|
|
|
exception.forget(_retval);
|
2006-04-15 00:37:51 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-02-24 03:22:35 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMScriptObjectFactory::RegisterDOMClassInfo(const char *aName,
|
|
|
|
nsDOMClassInfoExternalConstructorFnc aConstructorFptr,
|
|
|
|
const nsIID *aProtoChainInterface,
|
|
|
|
const nsIID **aInterfaces,
|
|
|
|
PRUint32 aScriptableFlags,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aHasClassInterface,
|
2004-02-24 03:22:35 +00:00
|
|
|
const nsCID *aConstructorCID)
|
|
|
|
{
|
2007-11-13 10:35:49 +00:00
|
|
|
nsScriptNameSpaceManager *nameSpaceManager = nsJSRuntime::GetNameSpaceManager();
|
|
|
|
NS_ENSURE_TRUE(nameSpaceManager, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
return nameSpaceManager->RegisterDOMCIData(aName,
|
|
|
|
aConstructorFptr,
|
|
|
|
aProtoChainInterface,
|
|
|
|
aInterfaces,
|
|
|
|
aScriptableFlags,
|
|
|
|
aHasClassInterface,
|
|
|
|
aConstructorCID);
|
2004-02-24 03:22:35 +00:00
|
|
|
}
|
2006-06-13 03:07:47 +00:00
|
|
|
|
2012-04-14 13:03:16 +00:00
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
// Factories
|
2012-04-14 13:03:16 +00:00
|
|
|
static nsresult
|
|
|
|
GetJSRuntime(nsIScriptRuntime** aLanguage)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMScriptObjectFactory> factory =
|
|
|
|
do_GetService(kDOMScriptObjectFactoryCID);
|
|
|
|
NS_ENSURE_TRUE(factory, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aLanguage = factory->GetJSRuntime());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
nsresult NS_GetScriptRuntime(const nsAString &aLanguageName,
|
|
|
|
nsIScriptRuntime **aLanguage)
|
|
|
|
{
|
2012-04-14 13:03:16 +00:00
|
|
|
*aLanguage = NULL;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(aLanguageName.EqualsLiteral("application/javascript"),
|
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return GetJSRuntime(aLanguage);
|
2006-06-13 03:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NS_GetScriptRuntimeByID(PRUint32 aScriptTypeID,
|
|
|
|
nsIScriptRuntime **aLanguage)
|
|
|
|
{
|
2012-04-14 13:03:16 +00:00
|
|
|
*aLanguage = NULL;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(aScriptTypeID == nsIProgrammingLanguage::JAVASCRIPT,
|
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return GetJSRuntime(aLanguage);
|
2006-06-13 03:07:47 +00:00
|
|
|
}
|
2008-09-30 23:55:42 +00:00
|
|
|
|
2012-04-14 13:03:16 +00:00
|
|
|
|
2008-09-30 23:55:42 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDOMExceptionProvider, nsIExceptionProvider)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMExceptionProvider::GetException(nsresult result,
|
|
|
|
nsIException *aDefaultException,
|
|
|
|
nsIException **_retval)
|
|
|
|
{
|
2008-11-06 06:41:52 +00:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2008-09-30 23:55:42 +00:00
|
|
|
switch (NS_ERROR_GET_MODULE(result))
|
|
|
|
{
|
|
|
|
case NS_ERROR_MODULE_SVG:
|
|
|
|
return NS_NewSVGException(result, aDefaultException, _retval);
|
|
|
|
case NS_ERROR_MODULE_DOM_XPATH:
|
|
|
|
return NS_NewXPathException(result, aDefaultException, _retval);
|
|
|
|
case NS_ERROR_MODULE_XPCONNECT:
|
|
|
|
return CreateXPConnectException(result, aDefaultException, _retval);
|
|
|
|
default:
|
2012-04-11 21:55:21 +00:00
|
|
|
MOZ_ASSERT(NS_ERROR_GET_MODULE(result) == NS_ERROR_MODULE_DOM ||
|
|
|
|
NS_ERROR_GET_MODULE(result) == NS_ERROR_MODULE_DOM_FILE ||
|
|
|
|
NS_ERROR_GET_MODULE(result) == NS_ERROR_MODULE_DOM_INDEXEDDB,
|
|
|
|
"Trying to create an exception for the wrong error module.");
|
2008-09-30 23:55:42 +00:00
|
|
|
return NS_NewDOMException(result, aDefaultException, _retval);
|
|
|
|
}
|
|
|
|
NS_NOTREACHED("Not reached");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|