2004-02-24 06:22:35 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2006-04-15 04:37:51 +04:00
|
|
|
* vim: set ts=2 sw=2 et tw=78:
|
2004-02-24 06:22:35 +03:00
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2004-02-24 06:22:35 +03: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 04:37:51 +04:00
|
|
|
#include "xpcexception.h"
|
2004-02-24 06:22:35 +03:00
|
|
|
#include "nsScriptNameSpaceManager.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsJSEventListener.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
2006-06-13 07:07:47 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2004-02-24 06:22:35 +03:00
|
|
|
#include "nsDOMException.h"
|
2004-05-29 21:43:59 +04:00
|
|
|
#include "nsCRT.h"
|
2004-02-24 07:19:15 +03:00
|
|
|
#ifdef MOZ_XUL
|
2007-03-12 08:53:33 +03:00
|
|
|
#include "nsXULPrototypeCache.h"
|
2004-02-24 07:19:15 +03:00
|
|
|
#endif
|
2008-11-06 09:41:52 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2004-02-24 06:22:35 +03:00
|
|
|
|
2006-06-13 07:07:47 +04:00
|
|
|
static NS_DEFINE_CID(kDOMScriptObjectFactoryCID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIExceptionProvider* gExceptionProvider = nullptr;
|
2008-10-01 03:55:42 +04:00
|
|
|
|
2012-04-14 17:03:16 +04:00
|
|
|
nsDOMScriptObjectFactory::nsDOMScriptObjectFactory()
|
2004-02-24 06:22:35 +03: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 20:59:13 +04:00
|
|
|
mozilla::services::GetObserverService();
|
2004-02-24 06:22:35 +03:00
|
|
|
if (observerService) {
|
2011-10-17 18:59:28 +04:00
|
|
|
observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
2004-02-24 06:22:35 +03:00
|
|
|
}
|
|
|
|
|
2012-04-14 17:03:16 +04:00
|
|
|
nsCOMPtr<nsIExceptionProvider> provider = new nsDOMExceptionProvider();
|
|
|
|
nsCOMPtr<nsIExceptionService> xs =
|
|
|
|
do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
|
2008-10-01 03:55:42 +04:00
|
|
|
|
2012-04-14 17:03:16 +04: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);
|
2012-06-03 20:33:52 +04:00
|
|
|
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_INDEXEDDB);
|
|
|
|
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_FILEHANDLE);
|
2004-02-24 06:22:35 +03:00
|
|
|
}
|
2008-10-01 03:55:42 +04:00
|
|
|
|
2012-04-14 17:03:16 +04:00
|
|
|
NS_ASSERTION(!gExceptionProvider, "Registered twice?!");
|
|
|
|
provider.swap(gExceptionProvider);
|
|
|
|
|
2006-06-13 07:07:47 +04:00
|
|
|
// And pre-create the javascript language.
|
2012-04-14 17:03:16 +04:00
|
|
|
NS_CreateJSRuntime(getter_AddRefs(mJSRuntime));
|
2004-02-24 06:22:35 +03: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 12:19:16 +03:00
|
|
|
return NS_GetDOMClassInfoInstance(aID);
|
2004-02-24 06:22:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsISupports *)
|
|
|
|
nsDOMScriptObjectFactory::GetExternalClassInfoInstance(const nsAString& aName)
|
|
|
|
{
|
2007-11-13 13:35:49 +03:00
|
|
|
nsScriptNameSpaceManager *nameSpaceManager = nsJSRuntime::GetNameSpaceManager();
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(nameSpaceManager, nullptr);
|
2004-02-24 06:22:35 +03:00
|
|
|
|
2012-03-29 22:43:13 +04:00
|
|
|
const nsGlobalNameStruct *globalStruct = nameSpaceManager->LookupName(aName);
|
2004-02-24 06:22:35 +03:00
|
|
|
if (globalStruct) {
|
|
|
|
if (globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfoCreator) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIDOMCIExtension> creator(do_CreateInstance(globalStruct->mCID, &rv));
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2004-02-24 06:22:35 +03:00
|
|
|
|
2006-02-03 17:18:39 +03:00
|
|
|
rv = creator->RegisterDOMCI(NS_ConvertUTF16toUTF8(aName).get(), this);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2004-02-24 06:22:35 +03:00
|
|
|
|
2012-03-29 22:43:13 +04:00
|
|
|
globalStruct = nameSpaceManager->LookupName(aName);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(globalStruct, nullptr);
|
2004-02-24 06:22:35 +03: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);
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-02-24 06:22:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMScriptObjectFactory::Observe(nsISupports *aSubject,
|
2005-07-31 00:57:07 +04:00
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *someData)
|
2004-02-24 06:22:35 +03: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 08:53:33 +03:00
|
|
|
nsXULPrototypeCache* cache = nsXULPrototypeCache::GetInstance();
|
2004-02-24 06:22:35 +03:00
|
|
|
|
|
|
|
if (cache)
|
|
|
|
cache->Flush();
|
|
|
|
#endif
|
|
|
|
|
2004-12-10 22:48:22 +03:00
|
|
|
nsGlobalWindow::ShutDown();
|
2004-02-24 06:22:35 +03:00
|
|
|
nsDOMClassInfo::ShutDown();
|
2006-06-13 07:07:47 +04:00
|
|
|
|
2008-10-01 03:55:42 +04:00
|
|
|
if (gExceptionProvider) {
|
|
|
|
nsCOMPtr<nsIExceptionService> xs =
|
|
|
|
do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
|
2004-02-24 06:22:35 +03:00
|
|
|
|
2008-10-01 03:55:42 +04: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);
|
2012-06-03 20:33:52 +04:00
|
|
|
xs->UnregisterExceptionProvider(gExceptionProvider,
|
|
|
|
NS_ERROR_MODULE_DOM_INDEXEDDB);
|
|
|
|
xs->UnregisterExceptionProvider(gExceptionProvider,
|
|
|
|
NS_ERROR_MODULE_DOM_FILEHANDLE);
|
2008-10-01 03:55:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE(gExceptionProvider);
|
2004-02-24 06:22:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMScriptObjectFactory::RegisterDOMClassInfo(const char *aName,
|
|
|
|
nsDOMClassInfoExternalConstructorFnc aConstructorFptr,
|
|
|
|
const nsIID *aProtoChainInterface,
|
|
|
|
const nsIID **aInterfaces,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aScriptableFlags,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aHasClassInterface,
|
2004-02-24 06:22:35 +03:00
|
|
|
const nsCID *aConstructorCID)
|
|
|
|
{
|
2007-11-13 13:35:49 +03: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 06:22:35 +03:00
|
|
|
}
|
2006-06-13 07:07:47 +04:00
|
|
|
|
2012-04-14 17:03:16 +04:00
|
|
|
|
2006-06-13 07:07:47 +04:00
|
|
|
// Factories
|
2012-04-25 02:31:28 +04:00
|
|
|
nsresult
|
|
|
|
NS_GetJSRuntime(nsIScriptRuntime** aLanguage)
|
2012-04-14 17:03:16 +04:00
|
|
|
{
|
|
|
|
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 07:07:47 +04:00
|
|
|
nsresult NS_GetScriptRuntime(const nsAString &aLanguageName,
|
|
|
|
nsIScriptRuntime **aLanguage)
|
|
|
|
{
|
2012-04-14 17:03:16 +04:00
|
|
|
*aLanguage = NULL;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(aLanguageName.EqualsLiteral("application/javascript"),
|
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
|
2012-04-25 02:31:28 +04:00
|
|
|
return NS_GetJSRuntime(aLanguage);
|
2006-06-13 07:07:47 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult NS_GetScriptRuntimeByID(uint32_t aScriptTypeID,
|
2006-06-13 07:07:47 +04:00
|
|
|
nsIScriptRuntime **aLanguage)
|
|
|
|
{
|
2012-04-14 17:03:16 +04:00
|
|
|
*aLanguage = NULL;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(aScriptTypeID == nsIProgrammingLanguage::JAVASCRIPT,
|
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
|
2012-04-25 02:31:28 +04:00
|
|
|
return NS_GetJSRuntime(aLanguage);
|
2006-06-13 07:07:47 +04:00
|
|
|
}
|
2008-10-01 03:55:42 +04:00
|
|
|
|
2012-04-14 17:03:16 +04:00
|
|
|
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsDOMExceptionProvider, nsIExceptionProvider)
|
2008-10-01 03:55:42 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMExceptionProvider::GetException(nsresult result,
|
|
|
|
nsIException *aDefaultException,
|
|
|
|
nsIException **_retval)
|
|
|
|
{
|
2008-11-06 09:41:52 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2008-10-01 03:55:42 +04:00
|
|
|
switch (NS_ERROR_GET_MODULE(result))
|
|
|
|
{
|
2012-06-14 05:12:37 +04:00
|
|
|
case NS_ERROR_MODULE_DOM:
|
2008-10-01 03:55:42 +04:00
|
|
|
case NS_ERROR_MODULE_SVG:
|
|
|
|
case NS_ERROR_MODULE_DOM_XPATH:
|
2012-06-14 05:12:37 +04:00
|
|
|
case NS_ERROR_MODULE_DOM_FILE:
|
|
|
|
case NS_ERROR_MODULE_DOM_INDEXEDDB:
|
|
|
|
case NS_ERROR_MODULE_DOM_FILEHANDLE:
|
2008-10-01 03:55:42 +04:00
|
|
|
return NS_NewDOMException(result, aDefaultException, _retval);
|
2012-06-14 05:12:37 +04:00
|
|
|
default:
|
|
|
|
NS_WARNING("Trying to create an exception for the wrong error module.");
|
|
|
|
return NS_ERROR_FAILURE;
|
2008-10-01 03:55:42 +04:00
|
|
|
}
|
|
|
|
NS_NOTREACHED("Not reached");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|