Bug 1267186 - Split lookup of WebIDL DOM class names from lookup of DOMCI DOM class names. r=bz.

--HG--
extra : rebase_source : 5578057666186f58e434767af84fbfeac0cbdc3c
This commit is contained in:
Peter Van der Beken 2016-03-09 15:48:12 +01:00
Родитель 3c9262edb7
Коммит 827ee394ba
12 изменённых файлов: 465 добавлений и 221 удалений

Просмотреть файл

@ -1766,98 +1766,6 @@ nsWindowSH::GlobalResolve(nsGlobalWindow *aWin, JSContext *cx,
nsresult rv = NS_OK;
if (name_struct->mType == nsGlobalNameStruct::eTypeNewDOMBinding ||
name_struct->mType == nsGlobalNameStruct::eTypeClassProto ||
name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor) {
// Lookup new DOM bindings.
DefineInterface getOrCreateInterfaceObject =
name_struct->mDefineDOMInterface;
if (getOrCreateInterfaceObject) {
if (name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor &&
!OldBindingConstructorEnabled(name_struct, aWin, cx)) {
return NS_OK;
}
ConstructorEnabled* checkEnabledForScope = name_struct->mConstructorEnabled;
// We do the enabled check on the current compartment of cx, but for the
// actual object we pass in the underlying object in the Xray case. That
// way the callee can decide whether to allow access based on the caller
// or the window being touched.
JS::Rooted<JSObject*> global(cx,
js::CheckedUnwrap(obj, /* stopAtWindowProxy = */ false));
if (!global) {
return NS_ERROR_DOM_SECURITY_ERR;
}
if (checkEnabledForScope && !checkEnabledForScope(cx, global)) {
return NS_OK;
}
// The DOM constructor resolve machinery interacts with Xrays in tricky
// ways, and there are some asymmetries that are important to understand.
//
// In the regular (non-Xray) case, we only want to resolve constructors
// once (so that if they're deleted, they don't reappear). We do this by
// stashing the constructor in a slot on the global, such that we can see
// during resolve whether we've created it already. This is rather
// memory-intensive, so we don't try to maintain these semantics when
// manipulating a global over Xray (so the properties just re-resolve if
// they've been deleted).
//
// Unfortunately, there's a bit of an impedance-mismatch between the Xray
// and non-Xray machinery. The Xray machinery wants an API that returns a
// JS::PropertyDescriptor, so that the resolve hook doesn't have to get
// snared up with trying to define a property on the Xray holder. At the
// same time, the DefineInterface callbacks are set up to define things
// directly on the global. And re-jiggering them to return property
// descriptors is tricky, because some DefineInterface callbacks define
// multiple things (like the Image() alias for HTMLImageElement).
//
// So the setup is as-follows:
//
// * The resolve function takes a JS::PropertyDescriptor, but in the
// non-Xray case, callees may define things directly on the global, and
// set the value on the property descriptor to |undefined| to indicate
// that there's nothing more for the caller to do. We assert against
// this behavior in the Xray case.
//
// * We make sure that we do a non-Xray resolve first, so that all the
// slots are set up. In the Xray case, this means unwrapping and doing
// a non-Xray resolve before doing the Xray resolve.
//
// This all could use some grand refactoring, but for now we just limp
// along.
if (xpc::WrapperFactory::IsXrayWrapper(obj)) {
JS::Rooted<JSObject*> interfaceObject(cx);
{
JSAutoCompartment ac(cx, global);
interfaceObject = getOrCreateInterfaceObject(cx, global, id, false);
}
if (NS_WARN_IF(!interfaceObject)) {
return NS_ERROR_FAILURE;
}
if (!JS_WrapObject(cx, &interfaceObject)) {
return NS_ERROR_FAILURE;
}
FillPropertyDescriptor(desc, obj, 0, JS::ObjectValue(*interfaceObject));
} else {
JS::Rooted<JSObject*> interfaceObject(cx,
getOrCreateInterfaceObject(cx, obj, id, true));
if (NS_WARN_IF(!interfaceObject)) {
return NS_ERROR_FAILURE;
}
// We've already defined the property. We indicate this to the caller
// by filling a property descriptor with JS::UndefinedValue() as the
// value. We still have to fill in a property descriptor, though, so
// that the caller knows the property is in fact on this object. It
// doesn't matter what we pass for the "readonly" argument here.
FillPropertyDescriptor(desc, obj, JS::UndefinedValue(), false);
}
return NS_OK;
}
}
if (name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor) {
if (!OldBindingConstructorEnabled(name_struct, aWin, cx)) {
return NS_OK;

Просмотреть файл

@ -229,6 +229,7 @@
#include "mozilla/dom/ImageBitmap.h"
#include "mozilla/dom/ServiceWorkerRegistration.h"
#include "mozilla/dom/U2F.h"
#include "mozilla/dom/WebIDLGlobalNameHash.h"
#ifdef HAVE_SIDEBAR
#include "mozilla/dom/ExternalBinding.h"
#endif
@ -4499,6 +4500,15 @@ nsGlobalWindow::DoResolve(JSContext* aCx, JS::Handle<JSObject*> aObj,
return true;
}
bool found;
if (!WebIDLGlobalNameHash::DefineIfEnabled(aCx, aObj, aId, aDesc, &found)) {
return false;
}
if (found) {
return true;
}
nsresult rv = nsWindowSH::GlobalResolve(this, aCx, aObj, aId, aDesc);
if (NS_FAILED(rv)) {
return Throw(aCx, rv);
@ -4527,6 +4537,10 @@ nsGlobalWindow::MayResolve(jsid aId)
return true;
}
if (WebIDLGlobalNameHash::MayResolve(aId)) {
return true;
}
nsScriptNameSpaceManager *nameSpaceManager = PeekNameSpaceManager();
if (!nameSpaceManager) {
// Really shouldn't happen. Fail safe.
@ -4550,12 +4564,13 @@ nsGlobalWindow::GetOwnPropertyNames(JSContext* aCx, nsTArray<nsString>& aNames,
nsScriptNameSpaceManager* nameSpaceManager = GetNameSpaceManager();
if (nameSpaceManager) {
JS::Rooted<JSObject*> wrapper(aCx, GetWrapper());
WebIDLGlobalNameHash::GetNames(aCx, wrapper, aNames);
for (auto i = nameSpaceManager->GlobalNameIter(); !i.Done(); i.Next()) {
const GlobalNameMapEntry* entry = i.Get();
if (nsWindowSH::NameStructEnabled(aCx, this, entry->mKey,
entry->mGlobalName) &&
(!entry->mGlobalName.mConstructorEnabled ||
entry->mGlobalName.mConstructorEnabled(aCx, wrapper))) {
entry->mGlobalName)) {
aNames.AppendElement(entry->mKey);
}
}

Просмотреть файл

@ -25,7 +25,8 @@
#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"
@ -34,6 +35,7 @@
#define NS_DOM_INTERFACE_PREFIX "nsIDOM"
using namespace mozilla;
using namespace mozilla::dom;
static PLDHashNumber
GlobalNameHashHashKey(const void *key)
@ -95,7 +97,7 @@ static const PLDHashTableOps hash_table_ops =
GlobalNameHashInitEntry
};
#define GLOBALNAME_HASHTABLE_INITIAL_LENGTH 512
#define GLOBALNAME_HASHTABLE_INITIAL_LENGTH 32
nsScriptNameSpaceManager::nsScriptNameSpaceManager()
: mGlobalNames(&hash_table_ops, sizeof(GlobalNameMapEntry),
@ -111,14 +113,17 @@ nsScriptNameSpaceManager::~nsScriptNameSpaceManager()
}
nsGlobalNameStruct *
nsScriptNameSpaceManager::AddToHash(const nsAString *aKey,
nsScriptNameSpaceManager::AddToHash(const char *aKey,
const char16_t **aClassName)
{
auto entry = static_cast<GlobalNameMapEntry*>(mGlobalNames.Add(aKey, fallible));
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();
}
@ -230,8 +235,7 @@ nsScriptNameSpaceManager::RegisterClassName(const char *aClassName,
return NS_OK;
}
NS_ASSERTION(s->mType == nsGlobalNameStruct::eTypeNotInitialized ||
s->mType == nsGlobalNameStruct::eTypeNewDOMBinding,
NS_ASSERTION(s->mType == nsGlobalNameStruct::eTypeNotInitialized,
"Whaaa, JS environment name clash!");
s->mType = nsGlobalNameStruct::eTypeClassConstructor;
@ -254,8 +258,7 @@ nsScriptNameSpaceManager::RegisterClassProto(const char *aClassName,
nsGlobalNameStruct *s = AddToHash(aClassName);
NS_ENSURE_TRUE(s, NS_ERROR_OUT_OF_MEMORY);
if (s->mType != nsGlobalNameStruct::eTypeNotInitialized &&
s->mType != nsGlobalNameStruct::eTypeNewDOMBinding) {
if (s->mType != nsGlobalNameStruct::eTypeNotInitialized) {
*aFoundOld = true;
return NS_OK;
@ -349,8 +352,7 @@ nsScriptNameSpaceManager::OperateCategoryEntryHash(nsICategoryManager* aCategory
nsGlobalNameStruct *s = AddToHash(categoryEntry.get());
NS_ENSURE_TRUE(s, NS_ERROR_OUT_OF_MEMORY);
if (s->mType == nsGlobalNameStruct::eTypeNotInitialized ||
s->mType == nsGlobalNameStruct::eTypeNewDOMBinding) {
if (s->mType == nsGlobalNameStruct::eTypeNotInitialized) {
s->mType = type;
s->mCID = cid;
s->mChromeOnly =
@ -414,21 +416,6 @@ nsScriptNameSpaceManager::Observe(nsISupports* aSubject, const char* aTopic,
return NS_OK;
}
void
nsScriptNameSpaceManager::RegisterDefineDOMInterface(const nsAFlatString& aName,
mozilla::dom::DefineInterface aDefineDOMInterface,
mozilla::dom::ConstructorEnabled* aConstructorEnabled)
{
nsGlobalNameStruct *s = AddToHash(&aName);
if (s) {
if (s->mType == nsGlobalNameStruct::eTypeNotInitialized) {
s->mType = nsGlobalNameStruct::eTypeNewDOMBinding;
}
s->mDefineDOMInterface = aDefineDOMInterface;
s->mConstructorEnabled = aConstructorEnabled;
}
}
MOZ_DEFINE_MALLOC_SIZE_OF(ScriptNameSpaceManagerMallocSizeOf)
NS_IMETHODIMP

Просмотреть файл

@ -33,35 +33,24 @@
#include "nsWeakReference.h"
#include "xpcpublic.h"
struct nsGlobalNameStruct
{
enum nametype {
eTypeNotInitialized,
eTypeNewDOMBinding,
eTypeProperty,
eTypeExternalConstructor,
eTypeClassConstructor,
eTypeClassProto,
} mType;
// mChromeOnly is only used for structs that define non-WebIDL things
// (possibly in addition to WebIDL ones). In particular, it's not even
// initialized for eTypeNewDOMBinding structs.
bool mChromeOnly : 1;
bool mAllowXBL : 1;
union {
int32_t mDOMClassInfoID; // eTypeClassConstructor
nsIID mIID; // eTypeClassProto
nsCID mCID; // All other types except eTypeNewDOMBinding
nsCID mCID; // All other types
};
// For new style DOM bindings.
mozilla::dom::DefineInterface mDefineDOMInterface;
// May be null if enabled unconditionally
mozilla::dom::ConstructorEnabled* mConstructorEnabled;
};
class GlobalNameMapEntry : public PLDHashEntryHdr
@ -112,19 +101,6 @@ public:
const nsIID *aConstructorProtoIID,
bool *aFoundOld);
void RegisterDefineDOMInterface(const nsAFlatString& aName,
mozilla::dom::DefineInterface aDefineDOMInterface,
mozilla::dom::ConstructorEnabled* aConstructorEnabled);
template<size_t N>
void RegisterDefineDOMInterface(const char16_t (&aKey)[N],
mozilla::dom::DefineInterface aDefineDOMInterface,
mozilla::dom::ConstructorEnabled* aConstructorEnabled)
{
nsLiteralString key(aKey);
return RegisterDefineDOMInterface(key, aDefineDOMInterface,
aConstructorEnabled);
}
class NameIterator : public PLDHashTable::Iterator
{
public:
@ -155,22 +131,14 @@ private:
// that aKey will be mapped to. If mType in the returned
// nsGlobalNameStruct is != eTypeNotInitialized, an entry for aKey
// already existed.
nsGlobalNameStruct *AddToHash(const nsAString *aKey,
const char16_t **aClassName = nullptr);
nsGlobalNameStruct *AddToHash(const char *aKey,
const char16_t **aClassName = nullptr)
{
NS_ConvertASCIItoUTF16 key(aKey);
return AddToHash(&key, aClassName);
}
const char16_t **aClassName = nullptr);
// Removes an existing entry from the hash.
void RemoveFromHash(const nsAString *aKey);
nsresult FillHash(nsICategoryManager *aCategoryManager,
const char *aCategory);
nsresult RegisterInterface(const char* aIfName,
const nsIID *aIfIID,
bool* aFoundOld);
/**
* Add a new category entry into the hash table.

Просмотреть файл

@ -45,6 +45,7 @@
#include "mozilla/dom/HTMLAppletElementBinding.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/ResolveSystemBinding.h"
#include "mozilla/dom/WebIDLGlobalNameHash.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerScope.h"
#include "mozilla/jsipc/CrossProcessObjectWrappers.h"
@ -2936,20 +2937,15 @@ RegisterDOMNames()
return NS_OK;
}
// Register new DOM bindings
WebIDLGlobalNameHash::Init();
nsresult rv = nsDOMClassInfo::Init();
if (NS_FAILED(rv)) {
NS_ERROR("Could not initialize nsDOMClassInfo");
return rv;
}
// Register new DOM bindings
nsScriptNameSpaceManager* nameSpaceManager = GetNameSpaceManager();
if (!nameSpaceManager) {
NS_ERROR("Could not initialize nsScriptNameSpaceManager");
return NS_ERROR_FAILURE;
}
mozilla::dom::Register(nameSpaceManager);
sRegisteredDOMNames = true;
return NS_OK;

Просмотреть файл

@ -12057,14 +12057,14 @@ class CGDescriptor(CGThing):
if descriptor.interface.hasInterfacePrototypeObject():
cgThings.append(CGPrototypeJSClass(descriptor, properties))
if descriptor.interface.hasInterfaceObject():
cgThings.append(CGDefineDOMInterfaceMethod(descriptor))
if ((descriptor.interface.hasInterfaceObject() or descriptor.interface.isNavigatorProperty()) and
not descriptor.interface.isExternal() and
descriptor.isExposedConditionally()):
cgThings.append(CGConstructorEnabled(descriptor))
if descriptor.registersGlobalNamesOnWindow:
cgThings.append(CGDefineDOMInterfaceMethod(descriptor))
if (descriptor.interface.hasMembersInSlots() and
descriptor.interface.hasChildInterfaces()):
raise TypeError("We don't support members in slots on "
@ -13057,48 +13057,53 @@ class CGResolveSystemBinding(CGAbstractMethod):
"\n").define()
class CGRegisterProtos(CGAbstractMethod):
def getGlobalNames(config):
names = []
for desc in config.getDescriptors(registersGlobalNamesOnWindow=True):
names.append((desc.name, desc))
names.extend((n.identifier.name, desc) for n in desc.interface.namedConstructors)
return names
class CGGlobalNamesString(CGGeneric):
def __init__(self, config):
CGAbstractMethod.__init__(self, None, 'Register', 'void',
[Argument('nsScriptNameSpaceManager*', 'aNameSpaceManager')])
globalNames = getGlobalNames(config)
currentOffset = 0
strings = []
for (name, _) in globalNames:
strings.append('/* %i */ "%s\\0"' % (currentOffset, name))
currentOffset += len(name) + 1 # Add trailing null.
define = fill("""
const uint32_t WebIDLGlobalNameHash::sCount = ${count};
const char WebIDLGlobalNameHash::sNames[] =
$*{strings}
""",
count=len(globalNames),
strings="\n".join(strings) + ";\n")
CGGeneric.__init__(self, define=define)
class CGRegisterGlobalNames(CGAbstractMethod):
def __init__(self, config):
CGAbstractMethod.__init__(self, None, 'RegisterWebIDLGlobalNames',
'void', [])
self.config = config
def _defineMacro(self):
return dedent("""
#define REGISTER_PROTO(_dom_class, _ctor_check) \\
aNameSpaceManager->RegisterDefineDOMInterface(MOZ_UTF16(#_dom_class), _dom_class##Binding::DefineDOMInterface, _ctor_check);
#define REGISTER_CONSTRUCTOR(_dom_constructor, _dom_class, _ctor_check) \\
aNameSpaceManager->RegisterDefineDOMInterface(MOZ_UTF16(#_dom_constructor), _dom_class##Binding::DefineDOMInterface, _ctor_check);
""")
def _undefineMacro(self):
return dedent("""
#undef REGISTER_CONSTRUCTOR
#undef REGISTER_PROTO
""")
def _registerProtos(self):
def definition_body(self):
def getCheck(desc):
if not desc.isExposedConditionally():
return "nullptr"
return "%sBinding::ConstructorEnabled" % desc.name
lines = []
for desc in self.config.getDescriptors(hasInterfaceObject=True,
isExternal=False,
workers=False,
isExposedInWindow=True,
register=True):
lines.append("REGISTER_PROTO(%s, %s);\n" % (desc.name, getCheck(desc)))
lines.extend("REGISTER_CONSTRUCTOR(%s, %s, %s);\n" % (n.identifier.name, desc.name, getCheck(desc))
for n in desc.interface.namedConstructors)
return ''.join(lines)
def indent_body(self, body):
# Don't indent the body of this method, as it's all preprocessor gunk.
return body
def definition_body(self):
return "\n" + self._defineMacro() + "\n" + self._registerProtos() + "\n" + self._undefineMacro()
define = ""
currentOffset = 0
for (name, desc) in getGlobalNames(self.config):
length = len(name)
define += "WebIDLGlobalNameHash::Register(%i, %i, %sBinding::DefineDOMInterface, %s);\n" % (currentOffset, length, desc.name, getCheck(desc))
currentOffset += length + 1 # Add trailing null.
return define
def dependencySortObjects(objects, dependencyGetter, nameGetter):
@ -16208,7 +16213,7 @@ class GlobalGenRoots():
@staticmethod
def RegisterBindings(config):
curr = CGRegisterProtos(config)
curr = CGList([CGGlobalNamesString(config), CGRegisterGlobalNames(config)])
# Wrap all of that in our namespaces.
curr = CGNamespace.build(['mozilla', 'dom'],
@ -16221,7 +16226,7 @@ class GlobalGenRoots():
workers=False,
isExposedInWindow=True,
register=True)]
defineIncludes.append('nsScriptNameSpaceManager.h')
defineIncludes.append('mozilla/dom/WebIDLGlobalNameHash.h')
defineIncludes.extend([CGHeaders.getDeclarationFilename(desc.interface)
for desc in config.getDescriptors(isNavigatorProperty=True,
workers=False,

Просмотреть файл

@ -810,6 +810,14 @@ class Descriptor(DescriptorProvider):
return (self.interface.getExtendedAttribute("Global") or
self.interface.getExtendedAttribute("PrimaryGlobal"))
@property
def registersGlobalNamesOnWindow(self):
return (not self.interface.isExternal() and
self.interface.hasInterfaceObject() and
not self.workers and
self.interface.isExposedInWindow() and
self.register)
# Some utility methods
def getTypesFromDescriptor(descriptor):

Просмотреть файл

@ -0,0 +1,302 @@
/* -*- 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 "WebIDLGlobalNameHash.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/dom/DOMJSProxyHandler.h"
#include "mozilla/dom/RegisterBindings.h"
#include "nsIMemoryReporter.h"
#include "nsTHashtable.h"
namespace mozilla {
namespace dom {
struct MOZ_STACK_CLASS WebIDLNameTableKey
{
explicit WebIDLNameTableKey(JSFlatString* aJSString)
: mLength(js::GetFlatStringLength(aJSString))
{
JSLinearString* jsString = js::FlatStringToLinearString(aJSString);
if (js::LinearStringHasLatin1Chars(jsString)) {
mLatin1String = reinterpret_cast<const char*>(
js::GetLatin1LinearStringChars(mNogc, jsString));
mTwoBytesString = nullptr;
mHash = mLatin1String ? HashString(mLatin1String, mLength) : 0;
} else {
mLatin1String = nullptr;
mTwoBytesString = js::GetTwoByteLinearStringChars(mNogc, jsString);
mHash = mTwoBytesString ? HashString(mTwoBytesString, mLength) : 0;
}
}
explicit WebIDLNameTableKey(const char* aString, size_t aLength)
: mLatin1String(aString),
mTwoBytesString(nullptr),
mLength(aLength),
mHash(HashString(aString, aLength))
{
MOZ_ASSERT(aString[aLength] == '\0');
}
JS::AutoCheckCannotGC mNogc;
const char* mLatin1String;
const char16_t* mTwoBytesString;
size_t mLength;
uint32_t mHash;
};
struct WebIDLNameTableEntry : public PLDHashEntryHdr
{
typedef const WebIDLNameTableKey& KeyType;
typedef const WebIDLNameTableKey* KeyTypePointer;
explicit WebIDLNameTableEntry(KeyTypePointer aKey)
{}
explicit WebIDLNameTableEntry(WebIDLNameTableEntry&& aEntry)
: mNameOffset(aEntry.mNameOffset),
mNameLength(aEntry.mNameLength),
mDefine(aEntry.mDefine),
mEnabled(aEntry.mEnabled)
{}
~WebIDLNameTableEntry()
{}
bool KeyEquals(KeyTypePointer aKey) const
{
if (mNameLength != aKey->mLength) {
return false;
}
const char* name = WebIDLGlobalNameHash::sNames + mNameOffset;
if (aKey->mLatin1String) {
return PodEqual(aKey->mLatin1String, name, aKey->mLength);
}
return nsCharTraits<char16_t>::compareASCII(aKey->mTwoBytesString, name,
aKey->mLength) == 0;
}
static KeyTypePointer KeyToPointer(KeyType aKey)
{
return &aKey;
}
static PLDHashNumber HashKey(KeyTypePointer aKey)
{
return aKey->mHash;
}
enum { ALLOW_MEMMOVE = true };
uint16_t mNameOffset;
uint16_t mNameLength;
WebIDLGlobalNameHash::DefineGlobalName mDefine;
// May be null if enabled unconditionally
WebIDLGlobalNameHash::ConstructorEnabled* mEnabled;
};
static nsTHashtable<WebIDLNameTableEntry>* sWebIDLGlobalNames;
class WebIDLGlobalNamesHashReporter final : public nsIMemoryReporter
{
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
~WebIDLGlobalNamesHashReporter() {}
public:
NS_DECL_ISUPPORTS
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize) override
{
int64_t amount =
sWebIDLGlobalNames ?
sWebIDLGlobalNames->ShallowSizeOfIncludingThis(MallocSizeOf) : 0;
return MOZ_COLLECT_REPORT("explicit/dom/webidl-globalnames", KIND_HEAP,
UNITS_BYTES, amount,
"Memory used by the hash table for WebIDL's "
"global names.");
}
};
NS_IMPL_ISUPPORTS(WebIDLGlobalNamesHashReporter, nsIMemoryReporter)
/* static */
void
WebIDLGlobalNameHash::Init()
{
sWebIDLGlobalNames = new nsTHashtable<WebIDLNameTableEntry>(sCount);
RegisterWebIDLGlobalNames();
RegisterStrongMemoryReporter(new WebIDLGlobalNamesHashReporter());
}
/* static */
void
WebIDLGlobalNameHash::Shutdown()
{
delete sWebIDLGlobalNames;
}
/* static */
void
WebIDLGlobalNameHash::Register(uint16_t aNameOffset, uint16_t aNameLength,
DefineGlobalName aDefine,
ConstructorEnabled* aEnabled)
{
const char* name = sNames + aNameOffset;
WebIDLNameTableKey key(name, aNameLength);
WebIDLNameTableEntry* entry = sWebIDLGlobalNames->PutEntry(key);
entry->mNameOffset = aNameOffset;
entry->mNameLength = aNameLength;
entry->mDefine = aDefine;
entry->mEnabled = aEnabled;
}
/* static */
void
WebIDLGlobalNameHash::Remove(const char* aName, uint32_t aLength)
{
WebIDLNameTableKey key(aName, aLength);
sWebIDLGlobalNames->RemoveEntry(key);
}
/* static */
bool
WebIDLGlobalNameHash::DefineIfEnabled(JSContext* aCx,
JS::Handle<JSObject*> aObj,
JS::Handle<jsid> aId,
JS::MutableHandle<JS::PropertyDescriptor> aDesc,
bool* aFound)
{
MOZ_ASSERT(JSID_IS_STRING(aId), "Check for string id before calling this!");
const WebIDLNameTableEntry* entry;
{
WebIDLNameTableKey key(JSID_TO_FLAT_STRING(aId));
entry = sWebIDLGlobalNames->GetEntry(key);
}
if (!entry) {
*aFound = false;
return true;
}
*aFound = true;
ConstructorEnabled* checkEnabledForScope = entry->mEnabled;
// We do the enabled check on the current compartment of aCx, but for the
// actual object we pass in the underlying object in the Xray case. That
// way the callee can decide whether to allow access based on the caller
// or the window being touched.
JS::Rooted<JSObject*> global(aCx,
js::CheckedUnwrap(aObj, /* stopAtWindowProxy = */ false));
if (!global) {
return Throw(aCx, NS_ERROR_DOM_SECURITY_ERR);
}
{
DebugOnly<nsGlobalWindow*> win;
MOZ_ASSERT(NS_SUCCEEDED(UNWRAP_OBJECT(Window, global, win)));
}
if (checkEnabledForScope && !checkEnabledForScope(aCx, global)) {
return true;
}
// The DOM constructor resolve machinery interacts with Xrays in tricky
// ways, and there are some asymmetries that are important to understand.
//
// In the regular (non-Xray) case, we only want to resolve constructors
// once (so that if they're deleted, they don't reappear). We do this by
// stashing the constructor in a slot on the global, such that we can see
// during resolve whether we've created it already. This is rather
// memory-intensive, so we don't try to maintain these semantics when
// manipulating a global over Xray (so the properties just re-resolve if
// they've been deleted).
//
// Unfortunately, there's a bit of an impedance-mismatch between the Xray
// and non-Xray machinery. The Xray machinery wants an API that returns a
// JS::PropertyDescriptor, so that the resolve hook doesn't have to get
// snared up with trying to define a property on the Xray holder. At the
// same time, the DefineInterface callbacks are set up to define things
// directly on the global. And re-jiggering them to return property
// descriptors is tricky, because some DefineInterface callbacks define
// multiple things (like the Image() alias for HTMLImageElement).
//
// So the setup is as-follows:
//
// * The resolve function takes a JS::PropertyDescriptor, but in the
// non-Xray case, callees may define things directly on the global, and
// set the value on the property descriptor to |undefined| to indicate
// that there's nothing more for the caller to do. We assert against
// this behavior in the Xray case.
//
// * We make sure that we do a non-Xray resolve first, so that all the
// slots are set up. In the Xray case, this means unwrapping and doing
// a non-Xray resolve before doing the Xray resolve.
//
// This all could use some grand refactoring, but for now we just limp
// along.
if (xpc::WrapperFactory::IsXrayWrapper(aObj)) {
JS::Rooted<JSObject*> interfaceObject(aCx);
{
JSAutoCompartment ac(aCx, global);
interfaceObject = entry->mDefine(aCx, global, aId, false);
}
if (NS_WARN_IF(!interfaceObject)) {
return Throw(aCx, NS_ERROR_FAILURE);
}
if (!JS_WrapObject(aCx, &interfaceObject)) {
return Throw(aCx, NS_ERROR_FAILURE);
}
FillPropertyDescriptor(aDesc, aObj, 0, JS::ObjectValue(*interfaceObject));
return true;
}
JS::Rooted<JSObject*> interfaceObject(aCx,
entry->mDefine(aCx, aObj, aId, true));
if (NS_WARN_IF(!interfaceObject)) {
return Throw(aCx, NS_ERROR_FAILURE);
}
// We've already defined the property. We indicate this to the caller
// by filling a property descriptor with JS::UndefinedValue() as the
// value. We still have to fill in a property descriptor, though, so
// that the caller knows the property is in fact on this object. It
// doesn't matter what we pass for the "readonly" argument here.
FillPropertyDescriptor(aDesc, aObj, JS::UndefinedValue(), false);
return true;
}
/* static */
bool
WebIDLGlobalNameHash::MayResolve(jsid aId)
{
WebIDLNameTableKey key(JSID_TO_FLAT_STRING(aId));
return sWebIDLGlobalNames->Contains(key);
}
/* static */
void
WebIDLGlobalNameHash::GetNames(JSContext* aCx, JS::Handle<JSObject*> aObj,
nsTArray<nsString>& aNames)
{
for (auto iter = sWebIDLGlobalNames->Iter(); !iter.Done(); iter.Next()) {
const WebIDLNameTableEntry* entry = iter.Get();
if (!entry->mEnabled || entry->mEnabled(aCx, aObj)) {
AppendASCIItoUTF16(nsDependentCString(sNames + entry->mNameOffset,
entry->mNameLength),
*aNames.AppendElement());
}
}
}
} // namespace dom
} // namespace mozilla

Просмотреть файл

@ -0,0 +1,70 @@
/* -*- 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/. */
#ifndef mozilla_dom_WebIDLGlobalNameHash_h__
#define mozilla_dom_WebIDLGlobalNameHash_h__
#include "js/RootingAPI.h"
#include "nsTArray.h"
namespace mozilla {
namespace dom {
struct WebIDLNameTableEntry;
class WebIDLGlobalNameHash
{
public:
static void Init();
static void Shutdown();
typedef JSObject*
(*DefineGlobalName)(JSContext* cx, JS::Handle<JSObject*> global,
JS::Handle<jsid> id, bool defineOnGlobal);
// Check whether a constructor should be enabled for the given object.
// Note that the object should NOT be an Xray, since Xrays will end up
// defining constructors on the underlying object.
// This is a typedef for the function type itself, not the function
// pointer, so it's more obvious that pointers to a ConstructorEnabled
// can be null.
typedef bool
(ConstructorEnabled)(JSContext* cx, JS::Handle<JSObject*> obj);
static void Register(uint16_t aNameOffset, uint16_t aNameLength,
DefineGlobalName aDefine, ConstructorEnabled* aEnabled);
static void Remove(const char* aName, uint32_t aLength);
// Returns false if something failed. aFound is set to true if the name is in
// the hash, whether it's enabled or not.
static bool DefineIfEnabled(JSContext* aCx, JS::Handle<JSObject*> aObj,
JS::Handle<jsid> aId,
JS::MutableHandle<JS::PropertyDescriptor> aDesc,
bool* aFound);
static bool MayResolve(jsid aId);
static void GetNames(JSContext* aCx, JS::Handle<JSObject*> aObj,
nsTArray<nsString>& aNames);
private:
friend struct WebIDLNameTableEntry;
// The total number of names that we will add to the hash.
// The value of sCount is generated by Codegen.py in RegisterBindings.cpp.
static const uint32_t sCount;
// The names that will be registered in the hash, concatenated as one big
// string with \0 as a separator between names.
// The value of sNames is generated by Codegen.py in RegisterBindings.cpp.
static const char sNames[];
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_WebIDLGlobalNameHash_h__

Просмотреть файл

@ -39,6 +39,7 @@ EXPORTS.mozilla.dom += [
'ToJSValue.h',
'TypedArray.h',
'UnionMember.h',
'WebIDLGlobalNameHash.h',
]
# Generated bindings reference *Binding.h, not mozilla/dom/*Binding.h. And,
@ -89,6 +90,7 @@ UNIFIED_SOURCES += [
'IterableIterator.cpp',
'SimpleGlobalObject.cpp',
'ToJSValue.cpp',
'WebIDLGlobalNameHash.cpp',
]
SOURCES += [

Просмотреть файл

@ -578,25 +578,6 @@ void RemoveGCCallback(xpcGCCallback cb);
namespace mozilla {
namespace dom {
typedef JSObject*
(*DefineInterface)(JSContext* cx, JS::Handle<JSObject*> global,
JS::Handle<jsid> id, bool defineOnGlobal);
typedef JSObject*
(*ConstructNavigatorProperty)(JSContext* cx, JS::Handle<JSObject*> naviObj);
// Check whether a constructor should be enabled for the given object.
// Note that the object should NOT be an Xray, since Xrays will end up
// defining constructors on the underlying object.
// This is a typedef for the function type itself, not the function
// pointer, so it's more obvious that pointers to a ConstructorEnabled
// can be null.
typedef bool
(ConstructorEnabled)(JSContext* cx, JS::Handle<JSObject*> obj);
void
Register(nsScriptNameSpaceManager* aNameSpaceManager);
/**
* A test for whether WebIDL methods that should only be visible to
* chrome or XBL scopes should be exposed.

Просмотреть файл

@ -131,6 +131,7 @@ using namespace mozilla::system;
#include "mozilla/dom/devicestorage/DeviceStorageStatics.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/StaticPresData.h"
#include "mozilla/dom/WebIDLGlobalNameHash.h"
#ifdef MOZ_B2G_BT
#include "mozilla/dom/BluetoothUUID.h"
@ -384,6 +385,7 @@ nsLayoutStatics::Shutdown()
ShutdownJSEnvironment();
nsGlobalWindow::ShutDown();
nsDOMClassInfo::ShutDown();
WebIDLGlobalNameHash::Shutdown();
nsListControlFrame::Shutdown();
nsXBLService::Shutdown();
nsAutoCopyListener::Shutdown();