Bug 1517880 - Clean up the XUL Prototype a bit. r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D34827

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Zibi Braniecki 2019-07-26 20:16:22 +00:00
parent a943d33842
commit ad66c0ca23
10 changed files with 34 additions and 47 deletions

View File

@ -2407,6 +2407,11 @@ class Document : public nsINode,
* Returns true if this document was created from a nsXULPrototypeDocument.
*/
bool LoadedFromPrototype() const { return mPrototypeDocument; }
/**
* Returns the prototype the document was created from, or null if it was not
* created from a prototype.
*/
nsXULPrototypeDocument* GetPrototype() const { return mPrototypeDocument; }
bool IsTopLevelContentDocument() const { return mIsTopLevelContentDocument; }
void SetIsTopLevelContentDocument(bool aIsTopLevelContentDocument) {

View File

@ -1063,7 +1063,7 @@ nsresult PrototypeDocumentContentSink::AddAttributes(
nsXULPrototypeElement* aPrototype, Element* aElement) {
nsresult rv;
for (uint32_t i = 0; i < aPrototype->mNumAttributes; ++i) {
for (size_t i = 0; i < aPrototype->mAttributes.Length(); ++i) {
nsXULPrototypeAttribute* protoattr = &(aPrototype->mAttributes[i]);
nsAutoString valueStr;
protoattr->mValue.ToString(valueStr);

View File

@ -776,12 +776,9 @@ nsresult nsXBLContentSink::AddAttributesToXULPrototype(
// Create storage for the attributes
nsXULPrototypeAttribute* attrs = nullptr;
if (aAttsCount > 0) {
attrs = new nsXULPrototypeAttribute[aAttsCount];
attrs = aElement->mAttributes.AppendElements(aAttsCount);
}
aElement->mAttributes = attrs;
aElement->mNumAttributes = aAttsCount;
// Copy the attributes into the prototype
RefPtr<nsAtom> prefix, localName;

View File

@ -1018,12 +1018,9 @@ nsresult nsXBLPrototypeBinding::ReadContentNode(nsIObjectInputStream* aStream,
nsXULPrototypeAttribute* attrs = nullptr;
if (attrCount > 0) {
attrs = new nsXULPrototypeAttribute[attrCount];
attrs = prototype->mAttributes.AppendElements(attrCount);
}
prototype->mAttributes = attrs;
prototype->mNumAttributes = attrCount;
for (uint32_t i = 0; i < attrCount; i++) {
rv = ReadNamespace(aStream, namespaceID);
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -17,6 +17,7 @@ if CONFIG['MOZ_XUL']:
EXPORTS += [
'nsXULCommandDispatcher.h',
'nsXULElement.h',
'nsXULPrototypeDocument.h',
'nsXULSortService.h',
]

View File

@ -805,12 +805,9 @@ nsresult XULContentSinkImpl::AddAttributes(const char16_t** aAttributes,
// Create storage for the attributes
nsXULPrototypeAttribute* attrs = nullptr;
if (aAttrLen > 0) {
attrs = new nsXULPrototypeAttribute[aAttrLen];
attrs = aElement->mAttributes.AppendElements(aAttrLen);
}
aElement->mAttributes = attrs;
aElement->mNumAttributes = aAttrLen;
// Copy the attributes into the prototype
uint32_t i;
for (i = 0; i < aAttrLen; ++i) {

View File

@ -198,13 +198,13 @@ already_AddRefed<nsXULElement> nsXULElement::CreateFromPrototype(
// Check each attribute on the prototype to see if we need to do
// any additional processing and hookup that would otherwise be
// done 'automagically' by SetAttr().
for (uint32_t i = 0; i < aPrototype->mNumAttributes; ++i) {
for (size_t i = 0; i < aPrototype->mAttributes.Length(); ++i) {
element->AddListenerFor(aPrototype->mAttributes[i].mName);
}
}
if (aIsRoot && aPrototype->mNodeInfo->Equals(nsGkAtoms::window)) {
for (uint32_t i = 0; i < aPrototype->mNumAttributes; ++i) {
for (size_t i = 0; i < aPrototype->mAttributes.Length(); ++i) {
if (aPrototype->mAttributes[i].mName.Equals(nsGkAtoms::windowtype)) {
element->MaybeUpdatePrivateLifetime();
}
@ -1240,9 +1240,9 @@ nsresult nsXULElement::MakeHeavyweight(nsXULPrototypeElement* aPrototype) {
return NS_OK;
}
uint32_t i;
size_t i;
nsresult rv;
for (i = 0; i < aPrototype->mNumAttributes; ++i) {
for (i = 0; i < aPrototype->mAttributes.Length(); ++i) {
nsXULPrototypeAttribute* protoattr = &aPrototype->mAttributes[i];
nsAttrValue attrValue;
@ -1473,8 +1473,8 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXULPrototypeNode)
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mNodeInfo");
cb.NoteNativeChild(elem->mNodeInfo,
NS_CYCLE_COLLECTION_PARTICIPANT(NodeInfo));
uint32_t i;
for (i = 0; i < elem->mNumAttributes; ++i) {
size_t i;
for (i = 0; i < elem->mAttributes.Length(); ++i) {
const nsAttrName& name = elem->mAttributes[i].mName;
if (!name.IsAtom()) {
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb,
@ -1527,14 +1527,14 @@ nsresult nsXULPrototypeElement::Serialize(
}
// Write Attributes
tmp = aStream->Write32(mNumAttributes);
tmp = aStream->Write32(mAttributes.Length());
if (NS_FAILED(tmp)) {
rv = tmp;
}
nsAutoString attributeValue;
uint32_t i;
for (i = 0; i < mNumAttributes; ++i) {
size_t i;
for (i = 0; i < mAttributes.Length(); ++i) {
RefPtr<mozilla::dom::NodeInfo> ni;
if (mAttributes[i].mName.IsAtom()) {
ni = mNodeInfo->NodeInfoManager()->GetNodeInfo(
@ -1636,16 +1636,13 @@ nsresult nsXULPrototypeElement::Deserialize(
// Read Attributes
rv = aStream->Read32(&number);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
mNumAttributes = int32_t(number);
int32_t attributes = int32_t(number);
if (mNumAttributes > 0) {
mAttributes = new (fallible) nsXULPrototypeAttribute[mNumAttributes];
if (!mAttributes) {
return NS_ERROR_OUT_OF_MEMORY;
}
if (attributes > 0) {
mAttributes.AppendElements(attributes);
nsAutoString attributeValue;
for (uint32_t i = 0; i < mNumAttributes; ++i) {
for (size_t i = 0; i < mAttributes.Length(); ++i) {
rv = aStream->Read32(&number);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
mozilla::dom::NodeInfo* ni = aNodeInfos->SafeElementAt(number, nullptr);
@ -1744,7 +1741,7 @@ nsresult nsXULPrototypeElement::Deserialize(
nsresult nsXULPrototypeElement::SetAttrAt(uint32_t aPos,
const nsAString& aValue,
nsIURI* aDocumentURI) {
MOZ_ASSERT(aPos < mNumAttributes, "out-of-bounds");
MOZ_ASSERT(aPos < mAttributes.Length(), "out-of-bounds");
// WARNING!!
// This code is largely duplicated in nsXULElement::SetAttr.
@ -1821,9 +1818,7 @@ nsresult nsXULPrototypeElement::SetAttrAt(uint32_t aPos,
}
void nsXULPrototypeElement::Unlink() {
mNumAttributes = 0;
delete[] mAttributes;
mAttributes = nullptr;
mAttributes.Clear();
mChildren.Clear();
}

View File

@ -136,11 +136,9 @@ class nsXULPrototypeElement : public nsXULPrototypeNode {
explicit nsXULPrototypeElement(mozilla::dom::NodeInfo* aNodeInfo = nullptr)
: nsXULPrototypeNode(eType_Element),
mNodeInfo(aNodeInfo),
mNumAttributes(0),
mHasIdAttribute(false),
mHasClassAttribute(false),
mHasStyleAttribute(false),
mAttributes(nullptr),
mIsAtom(nullptr) {}
private:
@ -175,20 +173,13 @@ class nsXULPrototypeElement : public nsXULPrototypeNode {
RefPtr<mozilla::dom::NodeInfo> mNodeInfo;
uint32_t mNumAttributes : 29;
uint32_t mHasIdAttribute : 1;
uint32_t mHasClassAttribute : 1;
uint32_t mHasStyleAttribute : 1;
nsXULPrototypeAttribute* mAttributes; // [OWNER]
nsTArray<nsXULPrototypeAttribute> mAttributes; // [OWNER]
RefPtr<nsAtom> mIsAtom;
};
namespace mozilla {
namespace dom {
class XULDocument;
} // namespace dom
} // namespace mozilla
class nsXULPrototypeScript : public nsXULPrototypeNode {
public:
explicit nsXULPrototypeScript(uint32_t aLineNo);

View File

@ -121,7 +121,9 @@ nsXULPrototypeDocument* nsXULPrototypeCache::GetPrototype(nsIURI* aURI) {
NS_GetURIWithoutRef(aURI, getter_AddRefs(uriWithoutRef));
nsXULPrototypeDocument* protoDoc = mPrototypeTable.GetWeak(uriWithoutRef);
if (protoDoc) return protoDoc;
if (protoDoc) {
return protoDoc;
}
nsresult rv = BeginCaching(aURI);
if (NS_FAILED(rv)) return nullptr;
@ -129,7 +131,9 @@ nsXULPrototypeDocument* nsXULPrototypeCache::GetPrototype(nsIURI* aURI) {
// No prototype in XUL memory cache. Spin up the cache Service.
nsCOMPtr<nsIObjectInputStream> ois;
rv = GetInputStream(aURI, getter_AddRefs(ois));
if (NS_FAILED(rv)) return nullptr;
if (NS_FAILED(rv)) {
return nullptr;
}
RefPtr<nsXULPrototypeDocument> newProto;
rv = NS_NewXULPrototypeDocument(getter_AddRefs(newProto));

View File

@ -203,8 +203,8 @@ static nsresult GetNodeInfos(nsXULPrototypeElement* aPrototype,
}
// Search attributes
uint32_t i;
for (i = 0; i < aPrototype->mNumAttributes; ++i) {
size_t i;
for (i = 0; i < aPrototype->mAttributes.Length(); ++i) {
RefPtr<mozilla::dom::NodeInfo> ni;
nsAttrName* name = &aPrototype->mAttributes[i].mName;
if (name->IsAtom()) {