2013-01-15 04:26:47 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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/. */
|
1998-12-21 19:51:10 +03:00
|
|
|
|
|
|
|
#include "nsScriptNameSpaceManager.h"
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIComponentManager.h"
|
2004-11-01 21:50:36 +03:00
|
|
|
#include "nsIComponentRegistrar.h"
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
#include "nsICategoryManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2002-09-04 03:36:13 +04:00
|
|
|
#include "nsXPCOM.h"
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsIScriptNameSpaceManager.h"
|
2002-01-17 08:09:55 +03:00
|
|
|
#include "nsIScriptContext.h"
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
#include "nsIInterfaceInfoManager.h"
|
|
|
|
#include "nsIInterfaceInfo.h"
|
|
|
|
#include "xptinfo.h"
|
|
|
|
#include "nsXPIDLString.h"
|
2013-05-02 05:44:44 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2001-03-27 15:17:17 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2005-08-15 22:29:55 +04:00
|
|
|
#include "nsHashKeys.h"
|
2002-03-04 05:25:08 +03:00
|
|
|
#include "nsDOMClassInfo.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2010-11-17 01:09:50 +03:00
|
|
|
#include "nsIObserverService.h"
|
2013-09-23 21:29:27 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2016-05-09 18:08:31 +03:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
#include "mozilla/dom/WebIDLGlobalNameHash.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-05-02 05:44:44 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-12-09 23:35:37 +04:00
|
|
|
#include "mozilla/Services.h"
|
1998-12-21 19:51:10 +03:00
|
|
|
|
2002-03-04 05:25:08 +03:00
|
|
|
#define NS_INTERFACE_PREFIX "nsI"
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
#define NS_DOM_INTERFACE_PREFIX "nsIDOM"
|
1998-12-21 19:51:10 +03:00
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
using namespace mozilla;
|
2016-05-09 18:08:31 +03:00
|
|
|
using namespace mozilla::dom;
|
2012-03-13 02:53:18 +04:00
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashNumber
|
2016-03-16 07:33:44 +03:00
|
|
|
GlobalNameHashHashKey(const void *key)
|
2002-01-17 07:08:14 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
const nsAString *str = static_cast<const nsAString *>(key);
|
2002-01-17 07:08:14 +03:00
|
|
|
return HashString(*str);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2016-03-16 07:33:44 +03:00
|
|
|
GlobalNameHashMatchEntry(const PLDHashEntryHdr *entry, const void *key)
|
2002-01-17 07:08:14 +03:00
|
|
|
{
|
|
|
|
const GlobalNameMapEntry *e =
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<const GlobalNameMapEntry *>(entry);
|
|
|
|
const nsAString *str = static_cast<const nsAString *>(key);
|
2002-01-17 07:08:14 +03:00
|
|
|
|
|
|
|
return str->Equals(e->mKey);
|
|
|
|
}
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void
|
2002-01-17 07:08:14 +03:00
|
|
|
GlobalNameHashClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
|
1998-12-21 19:51:10 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
GlobalNameMapEntry *e = static_cast<GlobalNameMapEntry *>(entry);
|
2002-01-17 07:08:14 +03:00
|
|
|
|
|
|
|
// An entry is being cleared, let the key (nsString) do its own
|
|
|
|
// cleanup.
|
|
|
|
e->mKey.~nsString();
|
|
|
|
|
|
|
|
// This will set e->mGlobalName.mType to
|
|
|
|
// nsGlobalNameStruct::eTypeNotInitialized
|
|
|
|
memset(&e->mGlobalName, 0, sizeof(nsGlobalNameStruct));
|
1998-12-21 19:51:10 +03:00
|
|
|
}
|
|
|
|
|
2015-02-11 20:46:40 +03:00
|
|
|
static void
|
|
|
|
GlobalNameHashInitEntry(PLDHashEntryHdr *entry, const void *key)
|
1998-12-21 19:51:10 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
GlobalNameMapEntry *e = static_cast<GlobalNameMapEntry *>(entry);
|
|
|
|
const nsAString *keyStr = static_cast<const nsAString *>(key);
|
1998-12-21 19:51:10 +03:00
|
|
|
|
2002-03-04 05:25:08 +03:00
|
|
|
// Initialize the key in the entry with placement new
|
2003-03-19 02:23:32 +03:00
|
|
|
new (&e->mKey) nsString(*keyStr);
|
2002-01-17 07:08:14 +03:00
|
|
|
|
|
|
|
// This will set e->mGlobalName.mType to
|
|
|
|
// nsGlobalNameStruct::eTypeNotInitialized
|
|
|
|
memset(&e->mGlobalName, 0, sizeof(nsGlobalNameStruct));
|
|
|
|
}
|
1998-12-21 19:51:10 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(
|
2013-11-07 09:35:30 +04:00
|
|
|
nsScriptNameSpaceManager,
|
|
|
|
nsIObserver,
|
2013-12-08 10:09:10 +04:00
|
|
|
nsISupportsWeakReference,
|
|
|
|
nsIMemoryReporter)
|
2010-11-17 01:09:50 +03:00
|
|
|
|
2015-05-06 02:11:43 +03:00
|
|
|
static const PLDHashTableOps hash_table_ops =
|
|
|
|
{
|
|
|
|
GlobalNameHashHashKey,
|
|
|
|
GlobalNameHashMatchEntry,
|
2015-09-15 00:23:47 +03:00
|
|
|
PLDHashTable::MoveEntryStub,
|
2015-05-06 02:11:43 +03:00
|
|
|
GlobalNameHashClearEntry,
|
|
|
|
GlobalNameHashInitEntry
|
|
|
|
};
|
|
|
|
|
2016-05-09 18:08:31 +03:00
|
|
|
#define GLOBALNAME_HASHTABLE_INITIAL_LENGTH 32
|
2015-05-06 02:11:43 +03:00
|
|
|
|
2002-01-17 07:08:14 +03:00
|
|
|
nsScriptNameSpaceManager::nsScriptNameSpaceManager()
|
2015-05-06 02:11:43 +03:00
|
|
|
: mGlobalNames(&hash_table_ops, sizeof(GlobalNameMapEntry),
|
|
|
|
GLOBALNAME_HASHTABLE_INITIAL_LENGTH)
|
2002-01-17 07:08:14 +03:00
|
|
|
{
|
2008-09-11 01:58:13 +04:00
|
|
|
MOZ_COUNT_CTOR(nsScriptNameSpaceManager);
|
1998-12-21 19:51:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptNameSpaceManager::~nsScriptNameSpaceManager()
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
{
|
2015-05-06 02:11:43 +03:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
2008-09-11 01:58:13 +04:00
|
|
|
MOZ_COUNT_DTOR(nsScriptNameSpaceManager);
|
2002-01-17 07:08:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsGlobalNameStruct *
|
2016-05-09 18:08:31 +03:00
|
|
|
nsScriptNameSpaceManager::AddToHash(const char *aKey,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t **aClassName)
|
2002-01-17 07:08:14 +03:00
|
|
|
{
|
2016-05-09 18:08:31 +03:00
|
|
|
NS_ConvertASCIItoUTF16 key(aKey);
|
|
|
|
auto entry = static_cast<GlobalNameMapEntry*>(mGlobalNames.Add(&key, fallible));
|
2002-01-17 07:08:14 +03:00
|
|
|
if (!entry) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2002-01-17 07:08:14 +03:00
|
|
|
}
|
|
|
|
|
2016-05-09 18:08:31 +03:00
|
|
|
WebIDLGlobalNameHash::Remove(aKey, key.Length());
|
|
|
|
|
2009-06-03 19:30:18 +04:00
|
|
|
if (aClassName) {
|
|
|
|
*aClassName = entry->mKey.get();
|
|
|
|
}
|
|
|
|
|
2002-01-17 07:08:14 +03:00
|
|
|
return &entry->mGlobalName;
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
}
|
|
|
|
|
2013-08-22 17:44:19 +04:00
|
|
|
void
|
2016-02-05 22:20:08 +03:00
|
|
|
nsScriptNameSpaceManager::RemoveFromHash(const nsAString *aKey)
|
2013-08-22 17:44:19 +04:00
|
|
|
{
|
2016-02-05 22:20:08 +03:00
|
|
|
mGlobalNames.Remove(aKey);
|
2013-08-22 17:44:19 +04:00
|
|
|
}
|
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
nsresult
|
|
|
|
nsScriptNameSpaceManager::FillHash(nsICategoryManager *aCategoryManager,
|
2010-11-17 01:09:50 +03:00
|
|
|
const char *aCategory)
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> e;
|
2010-11-17 01:09:50 +03:00
|
|
|
nsresult rv = aCategoryManager->EnumerateCategory(aCategory,
|
|
|
|
getter_AddRefs(e));
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> entry;
|
|
|
|
while (NS_SUCCEEDED(e->GetNext(getter_AddRefs(entry)))) {
|
2010-11-17 01:09:50 +03:00
|
|
|
rv = AddCategoryEntryToHash(aCategoryManager, aCategory, entry);
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-11-17 01:09:50 +03:00
|
|
|
return rv;
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
}
|
1998-12-21 19:51:10 +03:00
|
|
|
}
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
|
2002-08-30 12:48:58 +04:00
|
|
|
return NS_OK;
|
1998-12-21 19:51:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
nsresult
|
|
|
|
nsScriptNameSpaceManager::Init()
|
|
|
|
{
|
2013-11-07 09:35:30 +04:00
|
|
|
RegisterWeakMemoryReporter(this);
|
2013-01-15 04:26:47 +04:00
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICategoryManager> cm =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-11-17 01:09:50 +03:00
|
|
|
rv = FillHash(cm, JAVASCRIPT_GLOBAL_CONSTRUCTOR_CATEGORY);
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-11-17 01:09:50 +03:00
|
|
|
rv = FillHash(cm, JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY);
|
2007-06-26 18:44:20 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-11-17 01:09:50 +03:00
|
|
|
rv = FillHash(cm, JAVASCRIPT_GLOBAL_PRIVILEGED_PROPERTY_CATEGORY);
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-11-17 01:09:50 +03:00
|
|
|
// Initial filling of the has table has been done.
|
|
|
|
// Now, listen for changes.
|
|
|
|
nsCOMPtr<nsIObserverService> serv =
|
2011-12-09 23:35:37 +04:00
|
|
|
mozilla::services::GetObserverService();
|
2010-11-17 01:09:50 +03:00
|
|
|
|
|
|
|
if (serv) {
|
2011-10-17 18:59:28 +04:00
|
|
|
serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID, true);
|
2013-08-22 17:44:19 +04:00
|
|
|
serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID, true);
|
2010-11-17 01:09:50 +03:00
|
|
|
}
|
|
|
|
|
1998-12-21 19:51:10 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-02-05 22:20:08 +03:00
|
|
|
const nsGlobalNameStruct*
|
|
|
|
nsScriptNameSpaceManager::LookupName(const nsAString& aName,
|
|
|
|
const char16_t **aClassName)
|
1998-12-23 01:03:20 +03:00
|
|
|
{
|
2015-05-21 10:34:25 +03:00
|
|
|
auto entry = static_cast<GlobalNameMapEntry*>(mGlobalNames.Search(&aName));
|
2002-01-17 07:08:14 +03:00
|
|
|
|
2015-01-23 08:06:55 +03:00
|
|
|
if (entry) {
|
2003-01-31 15:10:13 +03:00
|
|
|
if (aClassName) {
|
|
|
|
*aClassName = entry->mKey.get();
|
|
|
|
}
|
2012-03-29 22:43:13 +04:00
|
|
|
return &entry->mGlobalName;
|
2002-01-17 07:08:14 +03:00
|
|
|
}
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
|
2012-03-29 22:43:13 +04:00
|
|
|
if (aClassName) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aClassName = nullptr;
|
2012-03-29 22:43:13 +04:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
1998-12-23 01:03:20 +03:00
|
|
|
}
|
|
|
|
|
2001-07-11 13:09:30 +04:00
|
|
|
nsresult
|
|
|
|
nsScriptNameSpaceManager::RegisterClassName(const char *aClassName,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aDOMClassInfoID,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aPrivileged,
|
2013-08-09 18:35:53 +04:00
|
|
|
bool aXBLAllowed,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t **aResult)
|
2001-07-11 13:09:30 +04:00
|
|
|
{
|
2002-03-04 05:25:08 +03:00
|
|
|
if (!nsCRT::IsAscii(aClassName)) {
|
|
|
|
NS_ERROR("Trying to register a non-ASCII class name");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-02-05 22:20:08 +03:00
|
|
|
nsGlobalNameStruct *s = AddToHash(aClassName, aResult);
|
2002-01-17 07:08:14 +03:00
|
|
|
NS_ENSURE_TRUE(s, NS_ERROR_OUT_OF_MEMORY);
|
2001-07-11 13:09:30 +04:00
|
|
|
|
2002-01-18 01:12:28 +03:00
|
|
|
if (s->mType == nsGlobalNameStruct::eTypeClassConstructor) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-07-11 13:09:30 +04:00
|
|
|
|
2002-01-18 01:12:28 +03:00
|
|
|
// If a external constructor is already defined with aClassName we
|
|
|
|
// won't overwrite it.
|
2001-07-11 13:09:30 +04:00
|
|
|
|
2002-01-18 01:12:28 +03:00
|
|
|
if (s->mType == nsGlobalNameStruct::eTypeExternalConstructor) {
|
|
|
|
return NS_OK;
|
2001-07-11 13:09:30 +04:00
|
|
|
}
|
|
|
|
|
2016-05-09 18:08:31 +03:00
|
|
|
NS_ASSERTION(s->mType == nsGlobalNameStruct::eTypeNotInitialized,
|
2002-01-17 07:08:14 +03:00
|
|
|
"Whaaa, JS environment name clash!");
|
2001-07-11 13:09:30 +04:00
|
|
|
|
|
|
|
s->mType = nsGlobalNameStruct::eTypeClassConstructor;
|
|
|
|
s->mDOMClassInfoID = aDOMClassInfoID;
|
2010-04-20 20:44:56 +04:00
|
|
|
s->mChromeOnly = aPrivileged;
|
2013-08-09 18:35:53 +04:00
|
|
|
s->mAllowXBL = aXBLAllowed;
|
2001-07-11 13:09:30 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptNameSpaceManager::RegisterClassProto(const char *aClassName,
|
|
|
|
const nsIID *aConstructorProtoIID,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *aFoundOld)
|
2001-07-11 13:09:30 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aConstructorProtoIID);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*aFoundOld = false;
|
2001-07-11 13:09:30 +04:00
|
|
|
|
2016-02-05 22:20:08 +03:00
|
|
|
nsGlobalNameStruct *s = AddToHash(aClassName);
|
2002-01-17 07:08:14 +03:00
|
|
|
NS_ENSURE_TRUE(s, NS_ERROR_OUT_OF_MEMORY);
|
2001-07-11 13:09:30 +04:00
|
|
|
|
2016-05-09 18:08:31 +03:00
|
|
|
if (s->mType != nsGlobalNameStruct::eTypeNotInitialized) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aFoundOld = true;
|
2001-07-11 13:09:30 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->mType = nsGlobalNameStruct::eTypeClassProto;
|
|
|
|
s->mIID = *aConstructorProtoIID;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-03-04 05:25:08 +03:00
|
|
|
|
2010-11-17 01:09:50 +03:00
|
|
|
nsresult
|
2013-08-22 17:44:19 +04:00
|
|
|
nsScriptNameSpaceManager::OperateCategoryEntryHash(nsICategoryManager* aCategoryManager,
|
|
|
|
const char* aCategory,
|
|
|
|
nsISupports* aEntry,
|
|
|
|
bool aRemove)
|
2010-11-17 01:09:50 +03:00
|
|
|
{
|
2013-08-22 17:44:19 +04:00
|
|
|
MOZ_ASSERT(aCategoryManager);
|
2011-02-03 02:46:12 +03:00
|
|
|
// Get the type from the category name.
|
|
|
|
// NOTE: we could have passed the type in FillHash() and guessed it in
|
|
|
|
// Observe() but this way, we have only one place to update and this is
|
|
|
|
// not performance sensitive.
|
|
|
|
nsGlobalNameStruct::nametype type;
|
|
|
|
if (strcmp(aCategory, JAVASCRIPT_GLOBAL_CONSTRUCTOR_CATEGORY) == 0) {
|
|
|
|
type = nsGlobalNameStruct::eTypeExternalConstructor;
|
|
|
|
} else if (strcmp(aCategory, JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY) == 0 ||
|
|
|
|
strcmp(aCategory, JAVASCRIPT_GLOBAL_PRIVILEGED_PROPERTY_CATEGORY) == 0) {
|
|
|
|
type = nsGlobalNameStruct::eTypeProperty;
|
|
|
|
} else {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-17 01:09:50 +03:00
|
|
|
nsCOMPtr<nsISupportsCString> strWrapper = do_QueryInterface(aEntry);
|
|
|
|
|
|
|
|
if (!strWrapper) {
|
|
|
|
NS_WARNING("Category entry not an nsISupportsCString!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString categoryEntry;
|
2010-11-17 01:09:50 +03:00
|
|
|
nsresult rv = strWrapper->GetData(categoryEntry);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-08-22 17:44:19 +04:00
|
|
|
// We need to handle removal before calling GetCategoryEntry
|
|
|
|
// because the category entry is already removed before we are
|
|
|
|
// notified.
|
|
|
|
if (aRemove) {
|
|
|
|
NS_ConvertASCIItoUTF16 entry(categoryEntry);
|
2016-02-05 22:20:08 +03:00
|
|
|
const nsGlobalNameStruct *s = LookupName(entry);
|
2013-08-22 17:44:19 +04:00
|
|
|
// Verify mType so that this API doesn't remove names
|
|
|
|
// registered by others.
|
|
|
|
if (!s || s->mType != type) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-02-05 22:20:08 +03:00
|
|
|
RemoveFromHash(&entry);
|
2013-08-22 17:44:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-17 01:09:50 +03:00
|
|
|
nsXPIDLCString contractId;
|
|
|
|
rv = aCategoryManager->GetCategoryEntry(aCategory, categoryEntry.get(),
|
|
|
|
getter_Copies(contractId));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-02-05 22:20:08 +03:00
|
|
|
if (type == nsGlobalNameStruct::eTypeExternalConstructor) {
|
|
|
|
nsPrintfCString prefName("dom.global-constructor.disable.%s",
|
2013-05-02 05:44:44 +04:00
|
|
|
categoryEntry.get());
|
|
|
|
if (Preferences::GetType(prefName.get()) == nsIPrefBranch::PREF_BOOL &&
|
|
|
|
Preferences::GetBool(prefName.get(), false)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-17 01:09:50 +03:00
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar;
|
|
|
|
rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCID *cidPtr;
|
|
|
|
rv = registrar->ContractIDToCID(contractId, &cidPtr);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Bad contract id registed with the script namespace manager");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy CID onto the stack, so we can free it right away and avoid having
|
|
|
|
// to add cleanup code at every exit point from this function.
|
|
|
|
nsCID cid = *cidPtr;
|
2015-03-27 03:01:12 +03:00
|
|
|
free(cidPtr);
|
2010-11-17 01:09:50 +03:00
|
|
|
|
2016-02-05 22:20:08 +03:00
|
|
|
nsGlobalNameStruct *s = AddToHash(categoryEntry.get());
|
2010-11-17 01:09:50 +03:00
|
|
|
NS_ENSURE_TRUE(s, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2016-05-09 18:08:31 +03:00
|
|
|
if (s->mType == nsGlobalNameStruct::eTypeNotInitialized) {
|
2010-11-17 01:09:50 +03:00
|
|
|
s->mType = type;
|
|
|
|
s->mCID = cid;
|
|
|
|
s->mChromeOnly =
|
|
|
|
strcmp(aCategory, JAVASCRIPT_GLOBAL_PRIVILEGED_PROPERTY_CATEGORY) == 0;
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Global script name not overwritten!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-22 17:44:19 +04:00
|
|
|
nsresult
|
|
|
|
nsScriptNameSpaceManager::AddCategoryEntryToHash(nsICategoryManager* aCategoryManager,
|
|
|
|
const char* aCategory,
|
|
|
|
nsISupports* aEntry)
|
|
|
|
{
|
|
|
|
return OperateCategoryEntryHash(aCategoryManager, aCategory, aEntry,
|
|
|
|
/* aRemove = */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptNameSpaceManager::RemoveCategoryEntryFromHash(nsICategoryManager* aCategoryManager,
|
|
|
|
const char* aCategory,
|
|
|
|
nsISupports* aEntry)
|
|
|
|
{
|
|
|
|
return OperateCategoryEntryHash(aCategoryManager, aCategory, aEntry,
|
|
|
|
/* aRemove = */ true);
|
|
|
|
}
|
|
|
|
|
2010-11-17 01:09:50 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptNameSpaceManager::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2010-11-17 01:09:50 +03:00
|
|
|
{
|
|
|
|
if (!aData) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-22 17:44:19 +04:00
|
|
|
if (!strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID)) {
|
2010-11-17 01:09:50 +03:00
|
|
|
nsCOMPtr<nsICategoryManager> cm =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
|
|
|
|
if (!cm) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddCategoryEntryToHash(cm, NS_ConvertUTF16toUTF8(aData).get(),
|
|
|
|
aSubject);
|
2013-08-22 17:44:19 +04:00
|
|
|
} else if (!strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID)) {
|
|
|
|
nsCOMPtr<nsICategoryManager> cm =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
|
|
|
|
if (!cm) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RemoveCategoryEntryFromHash(cm, NS_ConvertUTF16toUTF8(aData).get(),
|
|
|
|
aSubject);
|
2010-11-17 01:09:50 +03:00
|
|
|
}
|
|
|
|
|
2013-08-22 17:44:19 +04:00
|
|
|
// TODO: we could observe NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID
|
|
|
|
// but we are safe without it. See bug 600460.
|
2010-11-17 01:09:50 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-08 10:09:10 +04:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(ScriptNameSpaceManagerMallocSizeOf)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptNameSpaceManager::CollectReports(
|
2014-05-21 10:06:54 +04:00
|
|
|
nsIHandleReportCallback* aHandleReport, nsISupports* aData, bool aAnonymize)
|
2013-11-07 09:35:30 +04:00
|
|
|
{
|
2013-12-08 10:09:10 +04:00
|
|
|
return MOZ_COLLECT_REPORT(
|
|
|
|
"explicit/script-namespace-manager", KIND_HEAP, UNITS_BYTES,
|
|
|
|
SizeOfIncludingThis(ScriptNameSpaceManagerMallocSizeOf),
|
|
|
|
"Memory used for the script namespace manager.");
|
2013-11-07 09:35:30 +04:00
|
|
|
}
|
|
|
|
|
2012-11-05 02:18:44 +04:00
|
|
|
size_t
|
2015-07-30 08:28:20 +03:00
|
|
|
nsScriptNameSpaceManager::SizeOfIncludingThis(
|
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-11-05 02:18:44 +04:00
|
|
|
{
|
|
|
|
size_t n = 0;
|
2015-07-30 08:28:20 +03:00
|
|
|
|
|
|
|
n += mGlobalNames.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (auto iter = mGlobalNames.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = static_cast<GlobalNameMapEntry*>(iter.Get());
|
|
|
|
n += entry->SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-11-05 02:18:44 +04:00
|
|
|
return n;
|
|
|
|
}
|