⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 243954 in webkit


Ignore:
Timestamp:
Apr 5, 2019, 5:03:46 PM (7 years ago)
Author:
rniwa@webkit.org
Message:

Make WeakPtr<Element> possible and deploy it in form associated elements code
https://bugs.webkit.org/show_bug.cgi?id=196626

Reviewed by Antti Koivisto.

Source/WebCore:

Make Element inherit from CanMakeWeakPtr and deploy WeakPtr<*Element> in FormAssociatedElement and HTMLFormElement.

No new tests sine there should be no behavioral change.

  • dom/Element.h:
  • html/FormAssociatedElement.cpp:

(WebCore::FormAssociatedElement::FormAssociatedElement):
(WebCore::FormAssociatedElement::insertedIntoAncestor):
(WebCore::FormAssociatedElement::setForm):
(WebCore::FormAssociatedElement::resetFormOwner):
(WebCore::FormAssociatedElement::formAttributeChanged):

  • html/FormAssociatedElement.h:

(WebCore::FormAssociatedElement::form const):

  • html/HTMLElement.cpp:

(WebCore::HTMLElement::asFormNamedItem):
(WebCore::HTMLElement::asFormAssociatedElement):

  • html/HTMLElement.h:

(WebCore::HTMLElement::asFormNamedItem): Deleted.

  • html/HTMLFormControlElement.h:
  • html/HTMLFormControlsCollection.cpp:

(WebCore::HTMLFormControlsCollection::formImageElements const): Inlined into updateNamedElementCache.
(WebCore::HTMLFormControlsCollection::updateNamedElementCache const):

  • html/HTMLFormControlsCollection.h:
  • html/HTMLFormElement.cpp:

(WebCore::HTMLFormElement::registerInvalidAssociatedFormControl):
(WebCore::HTMLFormElement::removeInvalidAssociatedFormControlIfNeeded):
(WebCore::HTMLFormElement::registerImgElement):
(WebCore::HTMLFormElement::defaultButton const):
(WebCore::HTMLFormElement::resetDefaultButton):
(WebCore::HTMLFormElement::matchesValidPseudoClass const):
(WebCore::HTMLFormElement::matchesInvalidPseudoClass const):

  • html/HTMLFormElement.h:
  • html/HTMLImageElement.cpp:

(WebCore::HTMLImageElement::HTMLImageElement):
(WebCore::HTMLImageElement::insertedIntoAncestor):

  • html/HTMLImageElement.h:
  • html/HTMLInputElement.h:
  • html/HTMLMediaElement.h:
  • html/HTMLObjectElement.h:
  • html/HTMLPictureElement.h:
  • html/HTMLSlotElement.h:
  • svg/SVGElement.h:

Source/WTF:

Make it possible to call WeakHashSet::remove and WeakHashSet::contains with
a subclass type U of a type T used to define WeakReference<T>.

Also added computesEmpty, which is slightly more efficient than computeSize
when m_set is either empty or when there are non-released weak references in the set.

  • wtf/WeakHashSet.h:

(WTF::WeakHashSet::remove):
(WTF::WeakHashSet::contains const):
(WTF::WeakHashSet::computesEmpty const): Added.

  • wtf/WeakPtr.h: Added an explicit forward declaration of WeakHashSet to avoid

build failures in GTK+ and WPE ports.

Location:
trunk/Source
Files:
22 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r243944 r243954  
     12019-04-05  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        Make WeakPtr<Element> possible and deploy it in form associated elements code
     4        https://bugs.webkit.org/show_bug.cgi?id=196626
     5
     6        Reviewed by Antti Koivisto.
     7
     8        Make it possible to call WeakHashSet::remove and WeakHashSet::contains with
     9        a subclass type U of a type T used to define WeakReference<T>.
     10
     11        Also added computesEmpty, which is slightly more efficient than computeSize
     12        when m_set is either empty or when there are non-released weak references in the set.
     13
     14        * wtf/WeakHashSet.h:
     15        (WTF::WeakHashSet::remove):
     16        (WTF::WeakHashSet::contains const):
     17        (WTF::WeakHashSet::computesEmpty const): Added.
     18        * wtf/WeakPtr.h: Added an explicit forward declaration of WeakHashSet to avoid
     19        build failures in GTK+ and WPE ports.
     20
    1212019-04-05  Eric Carlson  <eric.carlson@apple.com>
    222
  • trunk/Source/WTF/wtf/WeakHashSet.h

    r243941 r243954  
    9292    }
    9393
    94     void remove(const T& value)
     94    template <typename U>
     95    bool remove(const U& value)
    9596    {
    96         auto* weakReference = value.weakPtrFactory().m_ref.get();
     97        auto* weakReference = weak_reference_downcast<T>(value.weakPtrFactory().m_ref.get());
    9798        if (!weakReference)
    98             return;
    99         m_set.remove(weakReference);
     99            return false;
     100        return m_set.remove(weakReference);
    100101    }
    101102
    102     bool contains(const T& value) const
     103    template <typename U>
     104    bool contains(const U& value) const
    103105    {
    104         auto* weakReference = value.weakPtrFactory().m_ref.get();
     106        auto* weakReference = weak_reference_downcast<T>(value.weakPtrFactory().m_ref.get());
    105107        if (!weakReference)
    106108            return false;
     
    109111
    110112    unsigned capacity() const { return m_set.capacity(); }
     113
     114    bool computesEmpty() const
     115    {
     116        if (m_set.isEmpty())
     117            return true;
     118        for (auto& value : m_set) {
     119            if (value->get())
     120                return false;
     121        }
     122        return true;
     123    }
    111124
    112125    unsigned computeSize() const
  • trunk/Source/WTF/wtf/WeakPtr.h

    r242387 r243954  
    3434namespace WTF {
    3535
     36template<typename U> class WeakHashSet;
    3637template<typename T> class WeakPtr;
    3738template<typename T> class WeakPtrFactory;
  • trunk/Source/WebCore/ChangeLog

    r243943 r243954  
     12019-04-05  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        Make WeakPtr<Element> possible and deploy it in form associated elements code
     4        https://bugs.webkit.org/show_bug.cgi?id=196626
     5
     6        Reviewed by Antti Koivisto.
     7
     8        Make Element inherit from CanMakeWeakPtr and deploy WeakPtr<*Element> in FormAssociatedElement and HTMLFormElement.
     9
     10        No new tests sine there should be no behavioral change.
     11
     12        * dom/Element.h:
     13        * html/FormAssociatedElement.cpp:
     14        (WebCore::FormAssociatedElement::FormAssociatedElement):
     15        (WebCore::FormAssociatedElement::insertedIntoAncestor):
     16        (WebCore::FormAssociatedElement::setForm):
     17        (WebCore::FormAssociatedElement::resetFormOwner):
     18        (WebCore::FormAssociatedElement::formAttributeChanged):
     19        * html/FormAssociatedElement.h:
     20        (WebCore::FormAssociatedElement::form const):
     21        * html/HTMLElement.cpp:
     22        (WebCore::HTMLElement::asFormNamedItem):
     23        (WebCore::HTMLElement::asFormAssociatedElement):
     24        * html/HTMLElement.h:
     25        (WebCore::HTMLElement::asFormNamedItem): Deleted.
     26        * html/HTMLFormControlElement.h:
     27        * html/HTMLFormControlsCollection.cpp:
     28        (WebCore::HTMLFormControlsCollection::formImageElements const): Inlined into updateNamedElementCache.
     29        (WebCore::HTMLFormControlsCollection::updateNamedElementCache const):
     30        * html/HTMLFormControlsCollection.h:
     31        * html/HTMLFormElement.cpp:
     32        (WebCore::HTMLFormElement::registerInvalidAssociatedFormControl):
     33        (WebCore::HTMLFormElement::removeInvalidAssociatedFormControlIfNeeded):
     34        (WebCore::HTMLFormElement::registerImgElement):
     35        (WebCore::HTMLFormElement::defaultButton const):
     36        (WebCore::HTMLFormElement::resetDefaultButton):
     37        (WebCore::HTMLFormElement::matchesValidPseudoClass const):
     38        (WebCore::HTMLFormElement::matchesInvalidPseudoClass const):
     39        * html/HTMLFormElement.h:
     40        * html/HTMLImageElement.cpp:
     41        (WebCore::HTMLImageElement::HTMLImageElement):
     42        (WebCore::HTMLImageElement::insertedIntoAncestor):
     43        * html/HTMLImageElement.h:
     44        * html/HTMLInputElement.h:
     45        * html/HTMLMediaElement.h:
     46        * html/HTMLObjectElement.h:
     47        * html/HTMLPictureElement.h:
     48        * html/HTMLSlotElement.h:
     49        * svg/SVGElement.h:
     50
    1512019-04-05  Caitlin Potter  <caitp@igalia.com>
    252
  • trunk/Source/WebCore/dom/Element.h

    r243941 r243954  
    7878#endif
    7979
    80 class Element : public ContainerNode {
     80class Element : public ContainerNode , public CanMakeWeakPtr<Element> {
    8181    WTF_MAKE_ISO_ALLOCATED(Element);
    8282public:
  • trunk/Source/WebCore/html/FormAssociatedElement.cpp

    r243941 r243954  
    5353FormAssociatedElement::FormAssociatedElement(HTMLFormElement* form)
    5454    : m_form(nullptr)
    55     , m_formSetByParser(form)
     55    , m_formSetByParser(makeWeakPtr(form))
    5656{
    5757}
     
    7575        // The form could have been removed by a script during parsing.
    7676        if (m_formSetByParser->isConnected())
    77             setForm(m_formSetByParser);
     77            setForm(m_formSetByParser.get());
    7878        m_formSetByParser = nullptr;
    7979    }
     
    147147    if (m_form)
    148148        m_form->removeFormElement(this);
    149     m_form = newForm;
    150     if (m_form)
    151         m_form->registerFormElement(this);
     149    m_form = makeWeakPtr(newForm);
     150    if (newForm)
     151        newForm->registerFormElement(this);
    152152    didChangeForm();
    153153}
     
    173173void FormAssociatedElement::resetFormOwner()
    174174{
    175     RefPtr<HTMLFormElement> originalForm = m_form;
    176     setForm(findAssociatedForm(&asHTMLElement(), m_form));
     175    RefPtr<HTMLFormElement> originalForm = m_form.get();
     176    setForm(findAssociatedForm(&asHTMLElement(), originalForm.get()));
    177177    HTMLElement& element = asHTMLElement();
    178     if (m_form && m_form != originalForm && m_form->isConnected())
     178    auto* newForm = m_form.get();
     179    if (newForm && newForm != originalForm && newForm->isConnected())
    179180        element.document().didAssociateFormControl(element);
    180181}
     
    185186    if (!element.hasAttributeWithoutSynchronization(formAttr)) {
    186187        // The form attribute removed. We need to reset form owner here.
    187         RefPtr<HTMLFormElement> originalForm = m_form;
     188        RefPtr<HTMLFormElement> originalForm = m_form.get();
     189        // FIXME: Why does this not pass originalForm to findClosestFormAncestor?
    188190        setForm(HTMLFormElement::findClosestFormAncestor(element));
    189         if (m_form && m_form != originalForm && m_form->isConnected())
     191        auto* newForm = m_form.get();
     192        if (newForm && newForm != originalForm && newForm->isConnected())
    190193            element.document().didAssociateFormControl(element);
    191194        m_formAttributeTargetObserver = nullptr;
  • trunk/Source/WebCore/html/FormAssociatedElement.h

    r243941 r243954  
    2626#include "FormNamedItem.h"
    2727#include "Node.h"
     28#include <wtf/WeakPtr.h>
    2829#include <wtf/text/WTFString.h>
    2930
     
    4849
    4950    static HTMLFormElement* findAssociatedForm(const HTMLElement*, HTMLFormElement*);
    50     HTMLFormElement* form() const { return m_form; }
     51    HTMLFormElement* form() const { return m_form.get(); }
    5152    ValidityState* validity();
    5253
     
    118119
    119120    std::unique_ptr<FormAttributeTargetObserver> m_formAttributeTargetObserver;
    120     HTMLFormElement* m_form;
    121     HTMLFormElement* m_formSetByParser;
     121    WeakPtr<HTMLFormElement> m_form;
     122    WeakPtr<HTMLFormElement> m_formSetByParser;
    122123    String m_customValidationMessage;
    123124};
  • trunk/Source/WebCore/html/HTMLElement.cpp

    r243941 r243954  
    763763}
    764764
     765FormNamedItem* HTMLElement::asFormNamedItem()
     766{
     767    return nullptr;
     768}
     769
     770FormAssociatedElement* HTMLElement::asFormAssociatedElement()
     771{
     772    return nullptr;
     773}
     774
    765775static inline bool elementAffectsDirectionality(const Node& node)
    766776{
  • trunk/Source/WebCore/html/HTMLElement.h

    r243941 r243954  
    3333
    3434class DocumentFragment;
     35class FormAssociatedElement;
    3536class FormNamedItem;
    3637class HTMLCollection;
     
    8990
    9091    virtual bool isLabelable() const { return false; }
    91     virtual FormNamedItem* asFormNamedItem() { return 0; }
     92    virtual FormNamedItem* asFormNamedItem();
     93    virtual FormAssociatedElement* asFormAssociatedElement();
    9294
    9395    bool hasTagName(const HTMLQualifiedName& name) const { return hasLocalName(name.localName()); }
  • trunk/Source/WebCore/html/HTMLFormControlElement.h

    r243941 r243954  
    176176    HTMLElement& asHTMLElement() final { return *this; }
    177177    const HTMLFormControlElement& asHTMLElement() const final { return *this; }
    178     HTMLFormControlElement* asFormNamedItem() final { return this; }
     178    FormNamedItem* asFormNamedItem() final { return this; }
     179    FormAssociatedElement* asFormAssociatedElement() final { return this; }
    179180
    180181    bool needsMouseFocusableQuirk() const;
  • trunk/Source/WebCore/html/HTMLFormControlsCollection.cpp

    r243941 r243954  
    7676}
    7777
    78 const Vector<HTMLImageElement*>& HTMLFormControlsCollection::formImageElements() const
    79 {
    80     return ownerNode().imageElements();
    81 }
    82 
    8378static unsigned findFormAssociatedElement(const Vector<FormAssociatedElement*>& elements, const Element& element)
    8479{
     
    146141    }
    147142
    148     for (auto* elementPtr : formImageElements()) {
     143    for (auto& elementPtr : ownerNode().imageElements()) {
     144        if (!elementPtr)
     145            continue;
    149146        HTMLImageElement& element = *elementPtr;
    150147        const AtomicString& id = element.getIdAttribute();
  • trunk/Source/WebCore/html/HTMLFormControlsCollection.h

    r243941 r243954  
    5757    const Vector<FormAssociatedElement*>& unsafeFormControlElements() const;
    5858    Vector<Ref<FormAssociatedElement>> copyFormControlElementsVector() const;
    59     const Vector<HTMLImageElement*>& formImageElements() const;
    6059
    6160    mutable Element* m_cachedElement;
  • trunk/Source/WebCore/html/HTMLFormElement.cpp

    r243941 r243954  
    567567    ASSERT(static_cast<const Element&>(formControlElement).matchesInvalidPseudoClass());
    568568
    569     if (m_invalidAssociatedFormControls.isEmpty())
     569    if (m_invalidAssociatedFormControls.computesEmpty())
    570570        invalidateStyleForSubtree();
    571     m_invalidAssociatedFormControls.add(&formControlElement);
     571    m_invalidAssociatedFormControls.add(const_cast<HTMLFormControlElement&>(formControlElement));
    572572}
    573573
    574574void HTMLFormElement::removeInvalidAssociatedFormControlIfNeeded(const HTMLFormControlElement& formControlElement)
    575575{
    576     if (m_invalidAssociatedFormControls.remove(&formControlElement)) {
    577         if (m_invalidAssociatedFormControls.isEmpty())
     576    if (m_invalidAssociatedFormControls.remove(formControlElement)) {
     577        if (m_invalidAssociatedFormControls.computesEmpty())
    578578            invalidateStyleForSubtree();
    579579    }
     
    588588{
    589589    ASSERT(m_imageElements.find(e) == notFound);
    590     m_imageElements.append(e);
     590    m_imageElements.append(makeWeakPtr(e));
    591591}
    592592
     
    682682HTMLFormControlElement* HTMLFormElement::defaultButton() const
    683683{
    684     if (!m_defaultButton) {
    685         for (auto& associatedElement : m_associatedElements) {
    686             if (!is<HTMLFormControlElement>(*associatedElement))
    687                 continue;
    688             HTMLFormControlElement& control = downcast<HTMLFormControlElement>(*associatedElement);
    689             if (control.isSuccessfulSubmitButton()) {
    690                 m_defaultButton = &control;
    691                 break;
    692             }
     684    if (m_defaultButton)
     685        return m_defaultButton.get();
     686    for (auto& associatedElement : m_associatedElements) {
     687        if (!is<HTMLFormControlElement>(*associatedElement))
     688            continue;
     689        HTMLFormControlElement& control = downcast<HTMLFormControlElement>(*associatedElement);
     690        if (control.isSuccessfulSubmitButton()) {
     691            m_defaultButton = makeWeakPtr(control);
     692            return &control;
    693693        }
    694694    }
    695     return m_defaultButton;
     695    return nullptr;
    696696}
    697697
     
    707707    ScriptDisallowedScope::InMainThread scriptDisallowedScope;
    708708
    709     auto* oldDefault = m_defaultButton;
    710     m_defaultButton = nullptr;
     709    auto oldDefault = WTFMove(m_defaultButton);
    711710    defaultButton();
    712711    if (m_defaultButton != oldDefault) {
     
    811810bool HTMLFormElement::matchesValidPseudoClass() const
    812811{
    813     return m_invalidAssociatedFormControls.isEmpty();
     812    return m_invalidAssociatedFormControls.computesEmpty();
    814813}
    815814
    816815bool HTMLFormElement::matchesInvalidPseudoClass() const
    817816{
    818     return !m_invalidAssociatedFormControls.isEmpty();
     817    return !matchesValidPseudoClass();
    819818}
    820819
  • trunk/Source/WebCore/html/HTMLFormElement.h

    r243941 r243954  
    3030#include <memory>
    3131#include <wtf/IsoMalloc.h>
     32#include <wtf/WeakHashSet.h>
    3233
    3334#if ENABLE(IOS_AUTOCORRECT_AND_AUTOCAPITALIZE)
     
    121122    WEBCORE_EXPORT const Vector<FormAssociatedElement*>& unsafeAssociatedElements() const;
    122123    Vector<Ref<FormAssociatedElement>> copyAssociatedElementsVector() const;
    123     const Vector<HTMLImageElement*>& imageElements() const { return m_imageElements; }
     124    const Vector<WeakPtr<HTMLImageElement>>& imageElements() const { return m_imageElements; }
    124125
    125126    StringPairVector textFieldValues() const;
     
    174175
    175176    RadioButtonGroups m_radioButtonGroups;
    176     mutable HTMLFormControlElement* m_defaultButton { nullptr };
     177    mutable WeakPtr<HTMLFormControlElement> m_defaultButton;
    177178
    178179    unsigned m_associatedElementsBeforeIndex { 0 };
    179180    unsigned m_associatedElementsAfterIndex { 0 };
    180181    Vector<FormAssociatedElement*> m_associatedElements;
    181     Vector<HTMLImageElement*> m_imageElements;
    182     HashSet<const HTMLFormControlElement*> m_invalidAssociatedFormControls;
     182    Vector<WeakPtr<HTMLImageElement>> m_imageElements;
     183    WeakHashSet<HTMLFormControlElement> m_invalidAssociatedFormControls;
    183184
    184185    bool m_wasUserSubmitted { false };
  • trunk/Source/WebCore/html/HTMLImageElement.cpp

    r243941 r243954  
    7272    , m_imageLoader(*this)
    7373    , m_form(nullptr)
    74     , m_formSetByParser(form)
     74    , m_formSetByParser(makeWeakPtr(form))
    7575    , m_compositeOperator(CompositeSourceOver)
    7676    , m_imageDevicePixelRatio(1.0f)
     
    331331{
    332332    if (m_formSetByParser) {
    333         m_form = m_formSetByParser;
    334         m_formSetByParser = nullptr;
     333        m_form = WTFMove(m_formSetByParser);
    335334        m_form->registerImgElement(this);
    336335    }
     
    342341
    343342    if (!m_form) {
    344         m_form = HTMLFormElement::findClosestFormAncestor(*this);
    345         if (m_form)
    346             m_form->registerImgElement(this);
     343        if (auto* newForm = HTMLFormElement::findClosestFormAncestor(*this)) {
     344            m_form = makeWeakPtr(newForm);
     345            newForm->registerImgElement(this);
     346        }
    347347    }
    348348
  • trunk/Source/WebCore/html/HTMLImageElement.h

    r243941 r243954  
    174174
    175175    HTMLImageLoader m_imageLoader;
    176     HTMLFormElement* m_form;
    177     HTMLFormElement* m_formSetByParser;
     176    WeakPtr<HTMLFormElement> m_form;
     177    WeakPtr<HTMLFormElement> m_formSetByParser;
    178178
    179179    CompositeOperator m_compositeOperator;
  • trunk/Source/WebCore/html/HTMLInputElement.h

    r243941 r243954  
    5555};
    5656
    57 class HTMLInputElement : public HTMLTextFormControlElement, public CanMakeWeakPtr<HTMLInputElement> {
     57class HTMLInputElement : public HTMLTextFormControlElement {
    5858    WTF_MAKE_ISO_ALLOCATED(HTMLInputElement);
    5959public:
  • trunk/Source/WebCore/html/HTMLMediaElement.h

    r243941 r243954  
    127127    , public ActiveDOMObject
    128128    , public MediaControllerInterface
    129     , public CanMakeWeakPtr<HTMLMediaElement>
    130129    , public PlatformMediaSessionClient
    131130    , private MediaCanStartListener
  • trunk/Source/WebCore/html/HTMLObjectElement.h

    r243941 r243954  
    9292
    9393    FormNamedItem* asFormNamedItem() final { return this; }
     94    FormAssociatedElement* asFormAssociatedElement() final { return this; }
    9495    HTMLObjectElement& asHTMLElement() final { return *this; }
    9596    const HTMLObjectElement& asHTMLElement() const final { return *this; }
  • trunk/Source/WebCore/html/HTMLPictureElement.h

    r243941 r243954  
    3131namespace WebCore {
    3232
    33 class HTMLPictureElement final : public HTMLElement, public CanMakeWeakPtr<HTMLPictureElement> {
     33class HTMLPictureElement final : public HTMLElement {
    3434    WTF_MAKE_ISO_ALLOCATED(HTMLPictureElement);
    3535public:
  • trunk/Source/WebCore/html/HTMLSlotElement.h

    r243941 r243954  
    3131namespace WebCore {
    3232
    33 class HTMLSlotElement final : public HTMLElement, public CanMakeWeakPtr<HTMLSlotElement> {
     33class HTMLSlotElement final : public HTMLElement {
    3434    WTF_MAKE_ISO_ALLOCATED(HTMLSlotElement);
    3535public:
  • trunk/Source/WebCore/svg/SVGElement.h

    r243941 r243954  
    4848void mapAttributeToCSSProperty(HashMap<AtomicStringImpl*, CSSPropertyID>* propertyNameToIdMap, const QualifiedName& attrName);
    4949
    50 class SVGElement : public StyledElement, public SVGLangSpace, public SVGPropertyOwner, public CanMakeWeakPtr<SVGElement> {
     50class SVGElement : public StyledElement, public SVGLangSpace, public SVGPropertyOwner {
    5151    WTF_MAKE_ISO_ALLOCATED(SVGElement);
    5252public:
Note: See TracChangeset for help on using the changeset viewer.