gecko-dev/dom/html/HTMLSharedObjectElement.h
Kirk Steuber 7fdb378650 Bug 1359556 - Optimize cloneNode by preinitializing attribute and child arrays r=bz
Currently, attribute and child arrays (implemented in dom/base/nsAttrAndChildArray.h) start out empty. When cloning, the array ends up being resized multiple times in order to add the attributes and children that are being cloned from the original node. This would be quicker if the array was initialized to the correct size in the first place so that resizes are not necessary.

However, preallocating space for children is only necessary when performing a deep clone. Therefore, an additional parameter is being added to the Clone, CopyInnerTo, and CloneDocHelper methods to indicate whether preallocation of children should happen. Attributes are copied either way, so that part of the array is preallocated in both cases.

MozReview-Commit-ID: 3iVezeAKXnI

--HG--
extra : rebase_source : 9c3deec6d7aafd6411044d623d4863637b45fd58
2017-04-20 12:57:48 -07:00

235 lines
7.3 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/. */
#ifndef mozilla_dom_HTMLSharedObjectElement_h
#define mozilla_dom_HTMLSharedObjectElement_h
#include "mozilla/Attributes.h"
#include "nsGenericHTMLElement.h"
#include "nsObjectLoadingContent.h"
#include "nsGkAtoms.h"
#include "nsError.h"
#include "nsIDOMHTMLAppletElement.h"
#include "nsIDOMHTMLEmbedElement.h"
namespace mozilla {
namespace dom {
class HTMLSharedObjectElement final : public nsGenericHTMLElement
, public nsObjectLoadingContent
, public nsIDOMHTMLAppletElement
, public nsIDOMHTMLEmbedElement
{
public:
explicit HTMLSharedObjectElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo,
mozilla::dom::FromParser aFromParser = mozilla::dom::NOT_FROM_PARSER);
// nsISupports
NS_DECL_ISUPPORTS_INHERITED
virtual int32_t TabIndexDefault() override;
#ifdef XP_MACOSX
// nsIDOMEventTarget
NS_IMETHOD PostHandleEvent(EventChainPostVisitor& aVisitor) override;
#endif
// nsIDOMHTMLAppletElement
NS_DECL_NSIDOMHTMLAPPLETELEMENT
// Can't use macro for nsIDOMHTMLEmbedElement because it has conflicts with
// NS_DECL_NSIDOMHTMLAPPLETELEMENT.
// EventTarget
virtual void AsyncEventRunning(AsyncEventDispatcher* aEvent) override;
// nsIDOMHTMLEmbedElement
NS_IMETHOD GetSrc(nsAString &aSrc) override;
NS_IMETHOD SetSrc(const nsAString &aSrc) override;
NS_IMETHOD GetType(nsAString &aType) override;
NS_IMETHOD SetType(const nsAString &aType) override;
virtual nsresult BindToTree(nsIDocument *aDocument, nsIContent *aParent,
nsIContent *aBindingParent,
bool aCompileEventHandlers) override;
virtual void UnbindFromTree(bool aDeep = true,
bool aNullParent = true) override;
virtual nsresult SetAttr(int32_t aNameSpaceID, nsIAtom *aName,
nsIAtom *aPrefix, const nsAString &aValue,
bool aNotify) override;
virtual bool IsHTMLFocusable(bool aWithMouse, bool *aIsFocusable, int32_t *aTabIndex) override;
virtual IMEState GetDesiredIMEState() override;
virtual void DoneAddingChildren(bool aHaveNotified) override;
virtual bool IsDoneAddingChildren() override;
virtual bool ParseAttribute(int32_t aNamespaceID,
nsIAtom *aAttribute,
const nsAString &aValue,
nsAttrValue &aResult) override;
virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const override;
NS_IMETHOD_(bool) IsAttributeMapped(const nsIAtom *aAttribute) const override;
virtual EventStates IntrinsicState() const override;
virtual void DestroyContent() override;
// nsObjectLoadingContent
virtual uint32_t GetCapabilities() const override;
virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
bool aPreallocateChildren) const override;
nsresult CopyInnerTo(Element* aDest, bool aPreallocateChildren);
void StartObjectLoad() { StartObjectLoad(true, false); }
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED_NO_UNLINK(HTMLSharedObjectElement,
nsGenericHTMLElement)
// WebIDL API for <applet>
void GetAlign(DOMString& aValue)
{
GetHTMLAttr(nsGkAtoms::align, aValue);
}
void SetAlign(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::align, aValue, aRv);
}
void GetAlt(DOMString& aValue)
{
GetHTMLAttr(nsGkAtoms::alt, aValue);
}
void SetAlt(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::alt, aValue, aRv);
}
void GetArchive(DOMString& aValue)
{
GetHTMLAttr(nsGkAtoms::archive, aValue);
}
void SetArchive(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::archive, aValue, aRv);
}
void GetCode(DOMString& aValue)
{
GetHTMLAttr(nsGkAtoms::code, aValue);
}
void SetCode(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::code, aValue, aRv);
}
// XPCOM GetCodebase is ok; note that it's a URI attribute
void SetCodeBase(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::codebase, aValue, aRv);
}
void GetHeight(DOMString& aValue)
{
GetHTMLAttr(nsGkAtoms::height, aValue);
}
void SetHeight(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::height, aValue, aRv);
}
uint32_t Hspace()
{
return GetUnsignedIntAttr(nsGkAtoms::hspace, 0);
}
void SetHspace(uint32_t aValue, ErrorResult& aRv)
{
SetUnsignedIntAttr(nsGkAtoms::hspace, aValue, 0, aRv);
}
void GetName(DOMString& aValue)
{
GetHTMLAttr(nsGkAtoms::name, aValue);
}
void SetName(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::name, aValue, aRv);
}
// XPCOM GetObject is ok; note that it's a URI attribute with a weird base URI
void SetObject(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::object, aValue, aRv);
}
uint32_t Vspace()
{
return GetUnsignedIntAttr(nsGkAtoms::vspace, 0);
}
void SetVspace(uint32_t aValue, ErrorResult& aRv)
{
SetUnsignedIntAttr(nsGkAtoms::vspace, aValue, 0, aRv);
}
void GetWidth(DOMString& aValue)
{
GetHTMLAttr(nsGkAtoms::width, aValue);
}
void SetWidth(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::width, aValue, aRv);
}
// WebIDL <embed> api
// XPCOM GetSrc is ok; note that it's a URI attribute
void SetSrc(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::src, aValue, aRv);
}
void GetType(DOMString& aValue)
{
GetHTMLAttr(nsGkAtoms::type, aValue);
}
void SetType(const nsAString& aValue, ErrorResult& aRv)
{
SetHTMLAttr(nsGkAtoms::type, aValue, aRv);
}
// width covered by <applet>
// height covered by <applet>
// align covered by <applet>
// name covered by <applet>
nsIDocument*
GetSVGDocument(nsIPrincipal& aSubjectPrincipal)
{
return GetContentDocument(aSubjectPrincipal);
}
/**
* Calls LoadObject with the correct arguments to start the plugin load.
*/
void StartObjectLoad(bool aNotify, bool aForceLoad);
protected:
// Override for nsImageLoadingContent.
nsIContent* AsContent() override { return this; }
private:
virtual ~HTMLSharedObjectElement();
nsIAtom *URIAttrName() const
{
return mNodeInfo->Equals(nsGkAtoms::applet) ?
nsGkAtoms::code :
nsGkAtoms::src;
}
nsContentPolicyType GetContentPolicyType() const override;
// mIsDoneAddingChildren is only really used for <applet>. This boolean is
// always true for <embed>, per the documentation in nsIContent.h.
bool mIsDoneAddingChildren;
virtual JSObject* WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto) override;
static void MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
GenericSpecifiedValues* aGenericData);
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_HTMLSharedObjectElement_h