Files
palemoon27/dom/base/nsScriptNameSpaceManager.cpp
T
roytam1 8650496cf3 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1267186 - Split lookup of WebIDL DOM class names from lookup of DOMCI DOM class names. r=bz. (1cb4c3698e)
- Bug 1206637: P1. Add MediaPrefs convenience class. r=cpearce (d352b9ca0b)
- Bug 1254378 - Make new 'speech-synth-started' component service category. r=smaug (ece851540b)
- Bug 1206637: P2. Replace all cached preferences with MediaPrefs ones. r=cpearce (1492083f62)
- Bug 1216407 - Use mozilla/Endian.h for NfcService. r=yoshi (b2a508186d)
- Bug 1237493 - [NFC] Separate Gecko and Gonk layers for accessing Androids properties. r=yoshi (062e468bed)
- Bug 1272135, part 1 - Fix leading tab usage in ipc/chromium. r=billm (cf29df6977)
- Bug 1272135, part 2 - Delete two weird modelines in ipc/chromium. r=billm (da4cdafc7c)
- Bug 1272135, part 3 - Fix mode lines in ipc/chromium/. r=billm (040b11738e)
- Bug 1273307 - Remove copy constructor/assignment operator for Message/Pickle (r=froydnj) (b53e6d3470)
- Bug 1262671 - Remove unused TrimWriteData (r=froydnj) (8fbcefde63)
- Bug 1267438 - Group ScrollMetadata's optional clip rect and mask layer index into a LayerClip structure. r=mstange (dcd01e3bce)
- Bug 1267438 - Use IntersectMaybeRects() in Layer::GetCombinedClipRect(). r=mstange (5ec2d68aac)
- Bug 1262937 - part 1 - don't include the protocol name in Clone error messages; r=jld (3e23610fb1)
- Bug 1191452 - Limit IPDL-generated Move()s to Recv methods. r=billm (991cc733b6)
- Bug 1262937 - part 2 - don't include the message name when complaining about handler failure; r=jld (b8abbc5c8b)
- Bug 1262937 - part 3 - move quoting out of checkedRead; r=jld (16d36ae02a)
- Bug 1262937 - part 4 - publically inherit from MessageListener in IProtocol; r=jld (b89aaa7347)
- Bug 1262937 - part 5 - factor out actor reading code to a common base class; r=jld (702388bce7)
- Bug 1262937 - part 7 - factor out union type deserialization errors; r=jld (dcdf428efc)
- Bug 1262937 - part 6 - enable custom error message for ipdl.py's checkedRead; r=jld (5cf11d5d24)
- Bug 1262937 - part 8 - factor out array length deserialization errors; r=jld (d1149cc120)
- Bug 1271601: If a child process fails to duplicate a TransportDescriptor pipe handle then send it anyway and get the target to duplicate. r=gabor (d578cd0c85)
- Bug 1270247 - Crash in ParamTraits<mozilla::net::NetAddr>::Write if the family is unknown. r=hurley (3ed49495c0)
- Bug 1267474 - cache-control: immutable tests 3/3 r=mayhemer (ba0bc6e71a)
- bug 1188100 - fold PSM's test_client_cert.js into necko's test_tls_server.js r=mcmanus (a2b93abbbb)
- Bug 669259 - Expose original header received from a peer. r=mcmanus (cf53cad7ac)
- Bug 386743 - Set default event bubbling/cancelable flags in the WidgetEvent constructor. r=smaug (1c68cfffd2)
- Bug 1259661 part.1 Rename WidgetMouseEvent::reasonType to WidgetMouseEvent::Reason r=smaug (0edd107b56)
- Bug 1259661 part.2 Rename WidgetMouseEvent::context to WidgetMouseEvent::ContextMenuTrigger r=smaug (0049757099)
- Bug 1259661 part.3 Rename WidgetMouseEvent::exitType to WidgetMouseEvent::ExitFrom r=smaug (5ffd3f41b1)
- Bug 1259661 part.4 Rename WidgetMouseEvent::reason to WidgetMouseEvent::mReason r=smaug (5605ce8fd0)
- Bug 1259661 part.5 Rename WidgetMouseEvent::context to WidgetMouseEvent::mContextMenuTrigger r=smaug (4dd92f0066)
- Bug 1259661 part.6 Rename WidgetMouseEvent::exit to WidgetMouseEvent::mExitFrom r=smaug (c46f5fe1bc)
- Bug 1259661 part.7 Get rid of WidgetMouseEvent::acceptActivation because of unused r=smaug (4867aaf1a7)
- Bug 1259661 part.8 Rename WidgetMouseEvent::ignoreRootScrollFrame to WidgetMouseEvent::mIgnoreRootScrollFrame r=smaug (c829c446fd)
- Bug 1259661 part.9 Rename WidgetMouseEvent::clickCount to WidgetMouseEvent::mClickCount r=smaug (2fa015c006)
- Bug 1259661 part.10 Clean up some nits of WidgetMouseEvent definition r=smaug (58d3a0cb2c)
- Bug 1179346 - Add strings and accesskeys for Microsoft Edge. r=mak (b97f5e7ab4)
- Bug 931445 part 1 - Remove unused code from pointerlock_utils.js. r=smaug (e1b9c15569)
- Bug 931445 part 2 - Use util code from fullscreen tests to improve robustness of pointerlock tests. r=smaug (3c7b47751e)
- Bug 931445 part 3 - Reset synth centering pointer when unlocking pointer. r=smaug (e272fec9c7)
- Bug 1263389 NativeKey should initialize WidgetKeyboardEvent::mKeyValue of WM_KEYDOWN of VK_PACKET with following char message r=m_kato (e56a31bd4b)
- Bug 1261880 NativeKey should decide printable KeyboardEvent.key value of keydown and keypress events with following WM_CHAR message of WM_KEYDOWN r=m_kato (7208001852)
- Bug 1254755 part.1 Rename WidgetKeyboardEvent::keyCode to WidgetKeyboardEvent::mKeyCode r=smaug (97ebefa314)
- put back some XP/2003 code, as well as crash/debug (73b3147e58)
- Bug 1259679 - Space key shouldn't work as a space key if it's assigned to a function key. r=masayuki (db422c248b)
- Bug 1101975, handle access keys in content process before menus, r=masayuki (760213b62e)
- Bug 1254755 part.2 Rename WidgetKeyboardEvent::charCode to WidgetKeyboardEvent::mCharCode r=smaug (6cef6114a5)
- Bug 1254755 part.3 Rename WidgetKeyboardEvent::alternativeCharCodes to WidgetKeyboardEvent::mAlternativeCharCodes r=smaug (9d23a113fd)
- Bug 1254755 part.4 Rename WidgetKeyboardEvent::location to WidgetKeyboardEvent::mLocation r=smaug (f44e0212a6)
- Bug 1254755 part.5 Rename WidgetKeyboardEvent::isChar to WidgetKeyboardEvent::mIsChar r=smaug (a713d9903c)
- Bug 1254755 part.6 Reorder the members of WidgetKeyboardEvent for reducing its instance size r=smaug (39a14a7d4f)
- Bug 1262671 - void** -> PickleIterator (r=froydnj) (716a88b499)
2024-10-08 21:41:43 +08:00

445 lines
13 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "nsScriptNameSpaceManager.h"
#include "nsCOMPtr.h"
#include "nsIComponentManager.h"
#include "nsIComponentRegistrar.h"
#include "nsICategoryManager.h"
#include "nsIServiceManager.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsIScriptNameSpaceManager.h"
#include "nsIScriptContext.h"
#include "nsIInterfaceInfoManager.h"
#include "nsIInterfaceInfo.h"
#include "xptinfo.h"
#include "nsXPIDLString.h"
#include "nsPrintfCString.h"
#include "nsReadableUtils.h"
#include "nsHashKeys.h"
#include "nsDOMClassInfo.h"
#include "nsCRT.h"
#include "nsIObserverService.h"
#include "nsISimpleEnumerator.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/WebIDLGlobalNameHash.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#define NS_INTERFACE_PREFIX "nsI"
#define NS_DOM_INTERFACE_PREFIX "nsIDOM"
using namespace mozilla;
using namespace mozilla::dom;
static PLDHashNumber
GlobalNameHashHashKey(const void *key)
{
const nsAString *str = static_cast<const nsAString *>(key);
return HashString(*str);
}
static bool
GlobalNameHashMatchEntry(const PLDHashEntryHdr *entry, const void *key)
{
const GlobalNameMapEntry *e =
static_cast<const GlobalNameMapEntry *>(entry);
const nsAString *str = static_cast<const nsAString *>(key);
return str->Equals(e->mKey);
}
static void
GlobalNameHashClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
{
GlobalNameMapEntry *e = static_cast<GlobalNameMapEntry *>(entry);
// 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));
}
static void
GlobalNameHashInitEntry(PLDHashEntryHdr *entry, const void *key)
{
GlobalNameMapEntry *e = static_cast<GlobalNameMapEntry *>(entry);
const nsAString *keyStr = static_cast<const nsAString *>(key);
// Initialize the key in the entry with placement new
new (&e->mKey) nsString(*keyStr);
// This will set e->mGlobalName.mType to
// nsGlobalNameStruct::eTypeNotInitialized
memset(&e->mGlobalName, 0, sizeof(nsGlobalNameStruct));
}
NS_IMPL_ISUPPORTS(
nsScriptNameSpaceManager,
nsIObserver,
nsISupportsWeakReference,
nsIMemoryReporter)
static const PLDHashTableOps hash_table_ops =
{
GlobalNameHashHashKey,
GlobalNameHashMatchEntry,
PLDHashTable::MoveEntryStub,
GlobalNameHashClearEntry,
GlobalNameHashInitEntry
};
#define GLOBALNAME_HASHTABLE_INITIAL_LENGTH 32
nsScriptNameSpaceManager::nsScriptNameSpaceManager()
: mGlobalNames(&hash_table_ops, sizeof(GlobalNameMapEntry),
GLOBALNAME_HASHTABLE_INITIAL_LENGTH)
{
MOZ_COUNT_CTOR(nsScriptNameSpaceManager);
}
nsScriptNameSpaceManager::~nsScriptNameSpaceManager()
{
UnregisterWeakMemoryReporter(this);
MOZ_COUNT_DTOR(nsScriptNameSpaceManager);
}
nsGlobalNameStruct *
nsScriptNameSpaceManager::AddToHash(const char *aKey,
const char16_t **aClassName)
{
NS_ConvertASCIItoUTF16 key(aKey);
auto entry = static_cast<GlobalNameMapEntry*>(mGlobalNames.Add(&key, fallible));
if (!entry) {
return nullptr;
}
WebIDLGlobalNameHash::Remove(aKey, key.Length());
if (aClassName) {
*aClassName = entry->mKey.get();
}
return &entry->mGlobalName;
}
void
nsScriptNameSpaceManager::RemoveFromHash(const nsAString *aKey)
{
mGlobalNames.Remove(aKey);
}
nsresult
nsScriptNameSpaceManager::FillHash(nsICategoryManager *aCategoryManager,
const char *aCategory)
{
nsCOMPtr<nsISimpleEnumerator> e;
nsresult rv = aCategoryManager->EnumerateCategory(aCategory,
getter_AddRefs(e));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsISupports> entry;
while (NS_SUCCEEDED(e->GetNext(getter_AddRefs(entry)))) {
rv = AddCategoryEntryToHash(aCategoryManager, aCategory, entry);
if (NS_FAILED(rv)) {
return rv;
}
}
return NS_OK;
}
nsresult
nsScriptNameSpaceManager::Init()
{
RegisterWeakMemoryReporter(this);
nsresult rv = NS_OK;
nsCOMPtr<nsICategoryManager> cm =
do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = FillHash(cm, JAVASCRIPT_GLOBAL_CONSTRUCTOR_CATEGORY);
NS_ENSURE_SUCCESS(rv, rv);
rv = FillHash(cm, JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY);
NS_ENSURE_SUCCESS(rv, rv);
rv = FillHash(cm, JAVASCRIPT_GLOBAL_PRIVILEGED_PROPERTY_CATEGORY);
NS_ENSURE_SUCCESS(rv, rv);
// Initial filling of the has table has been done.
// Now, listen for changes.
nsCOMPtr<nsIObserverService> serv =
mozilla::services::GetObserverService();
if (serv) {
serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID, true);
serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID, true);
}
return NS_OK;
}
const nsGlobalNameStruct*
nsScriptNameSpaceManager::LookupName(const nsAString& aName,
const char16_t **aClassName)
{
auto entry = static_cast<GlobalNameMapEntry*>(mGlobalNames.Search(&aName));
if (entry) {
if (aClassName) {
*aClassName = entry->mKey.get();
}
return &entry->mGlobalName;
}
if (aClassName) {
*aClassName = nullptr;
}
return nullptr;
}
nsresult
nsScriptNameSpaceManager::RegisterClassName(const char *aClassName,
int32_t aDOMClassInfoID,
bool aPrivileged,
bool aXBLAllowed,
const char16_t **aResult)
{
if (!nsCRT::IsAscii(aClassName)) {
NS_ERROR("Trying to register a non-ASCII class name");
return NS_OK;
}
nsGlobalNameStruct *s = AddToHash(aClassName, aResult);
NS_ENSURE_TRUE(s, NS_ERROR_OUT_OF_MEMORY);
if (s->mType == nsGlobalNameStruct::eTypeClassConstructor) {
return NS_OK;
}
// If a external constructor is already defined with aClassName we
// won't overwrite it.
if (s->mType == nsGlobalNameStruct::eTypeExternalConstructor) {
return NS_OK;
}
NS_ASSERTION(s->mType == nsGlobalNameStruct::eTypeNotInitialized,
"Whaaa, JS environment name clash!");
s->mType = nsGlobalNameStruct::eTypeClassConstructor;
s->mDOMClassInfoID = aDOMClassInfoID;
s->mChromeOnly = aPrivileged;
s->mAllowXBL = aXBLAllowed;
return NS_OK;
}
nsresult
nsScriptNameSpaceManager::RegisterClassProto(const char *aClassName,
const nsIID *aConstructorProtoIID,
bool *aFoundOld)
{
NS_ENSURE_ARG_POINTER(aConstructorProtoIID);
*aFoundOld = false;
nsGlobalNameStruct *s = AddToHash(aClassName);
NS_ENSURE_TRUE(s, NS_ERROR_OUT_OF_MEMORY);
if (s->mType != nsGlobalNameStruct::eTypeNotInitialized) {
*aFoundOld = true;
return NS_OK;
}
s->mType = nsGlobalNameStruct::eTypeClassProto;
s->mIID = *aConstructorProtoIID;
return NS_OK;
}
nsresult
nsScriptNameSpaceManager::OperateCategoryEntryHash(nsICategoryManager* aCategoryManager,
const char* aCategory,
nsISupports* aEntry,
bool aRemove)
{
MOZ_ASSERT(aCategoryManager);
// 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;
}
nsCOMPtr<nsISupportsCString> strWrapper = do_QueryInterface(aEntry);
if (!strWrapper) {
NS_WARNING("Category entry not an nsISupportsCString!");
return NS_OK;
}
nsAutoCString categoryEntry;
nsresult rv = strWrapper->GetData(categoryEntry);
NS_ENSURE_SUCCESS(rv, rv);
// 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);
const nsGlobalNameStruct *s = LookupName(entry);
// Verify mType so that this API doesn't remove names
// registered by others.
if (!s || s->mType != type) {
return NS_OK;
}
RemoveFromHash(&entry);
return NS_OK;
}
nsXPIDLCString contractId;
rv = aCategoryManager->GetCategoryEntry(aCategory, categoryEntry.get(),
getter_Copies(contractId));
NS_ENSURE_SUCCESS(rv, rv);
if (type == nsGlobalNameStruct::eTypeExternalConstructor) {
nsPrintfCString prefName("dom.global-constructor.disable.%s",
categoryEntry.get());
if (Preferences::GetType(prefName.get()) == nsIPrefBranch::PREF_BOOL &&
Preferences::GetBool(prefName.get(), false)) {
return NS_OK;
}
}
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;
free(cidPtr);
nsGlobalNameStruct *s = AddToHash(categoryEntry.get());
NS_ENSURE_TRUE(s, NS_ERROR_OUT_OF_MEMORY);
if (s->mType == nsGlobalNameStruct::eTypeNotInitialized) {
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;
}
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);
}
NS_IMETHODIMP
nsScriptNameSpaceManager::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData)
{
if (!aData) {
return NS_OK;
}
if (!strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID)) {
nsCOMPtr<nsICategoryManager> cm =
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
if (!cm) {
return NS_OK;
}
return AddCategoryEntryToHash(cm, NS_ConvertUTF16toUTF8(aData).get(),
aSubject);
} 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);
}
// TODO: we could observe NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID
// but we are safe without it. See bug 600460.
return NS_OK;
}
MOZ_DEFINE_MALLOC_SIZE_OF(ScriptNameSpaceManagerMallocSizeOf)
NS_IMETHODIMP
nsScriptNameSpaceManager::CollectReports(
nsIHandleReportCallback* aHandleReport, nsISupports* aData, bool aAnonymize)
{
return MOZ_COLLECT_REPORT(
"explicit/script-namespace-manager", KIND_HEAP, UNITS_BYTES,
SizeOfIncludingThis(ScriptNameSpaceManagerMallocSizeOf),
"Memory used for the script namespace manager.");
}
size_t
nsScriptNameSpaceManager::SizeOfIncludingThis(
mozilla::MallocSizeOf aMallocSizeOf) const
{
size_t n = 0;
n += mGlobalNames.ShallowSizeOfExcludingThis(aMallocSizeOf);
for (auto iter = mGlobalNames.ConstIter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<GlobalNameMapEntry*>(iter.Get());
n += entry->SizeOfExcludingThis(aMallocSizeOf);
}
return n;
}