Home | History | Annotate | Download | only in events
      1 /*
      2  * Copyright (C) 2014 Google Inc. All Rights Reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  *
     25  */
     26 
     27 #ifndef TreeScopeEventContext_h
     28 #define TreeScopeEventContext_h
     29 
     30 #include "core/dom/Node.h"
     31 #include "core/dom/TreeScope.h"
     32 #include "core/events/EventTarget.h"
     33 #include "wtf/PassRefPtr.h"
     34 #include "wtf/RefPtr.h"
     35 #include "wtf/Vector.h"
     36 
     37 namespace WebCore {
     38 
     39 class EventPath;
     40 class EventTarget;
     41 class Node;
     42 class StaticNodeList;
     43 class TouchEventContext;
     44 class TreeScope;
     45 
     46 class TreeScopeEventContext FINAL : public RefCountedWillBeGarbageCollected<TreeScopeEventContext> {
     47     DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(TreeScopeEventContext);
     48 public:
     49     static PassRefPtrWillBeRawPtr<TreeScopeEventContext> create(TreeScope&);
     50     void trace(Visitor*);
     51 
     52     TreeScope& treeScope() const { return *m_treeScope; }
     53 
     54     EventTarget* target() const { return m_target.get(); }
     55     void setTarget(PassRefPtrWillBeRawPtr<EventTarget>);
     56 
     57     EventTarget* relatedTarget() const { return m_relatedTarget.get(); }
     58     void setRelatedTarget(PassRefPtrWillBeRawPtr<EventTarget>);
     59 
     60     TouchEventContext* touchEventContext() const { return m_touchEventContext.get(); }
     61     TouchEventContext* ensureTouchEventContext();
     62 
     63     PassRefPtrWillBeRawPtr<StaticNodeList> ensureEventPath(EventPath&);
     64 
     65     bool isInclusiveAncestorOf(const TreeScopeEventContext&);
     66     void addChild(TreeScopeEventContext& child) { m_children.append(&child); }
     67 
     68     // For ancestor-descendant relationship check in Q(1).
     69     // Preprocessing takes O(N).
     70     int calculatePrePostOrderNumber(int orderNumber);
     71 
     72 private:
     73     TreeScopeEventContext(TreeScope&);
     74 
     75 #ifndef NDEBUG
     76     bool isUnreachableNode(EventTarget&);
     77 #endif
     78 
     79     RawPtrWillBeMember<TreeScope> m_treeScope;
     80     RefPtrWillBeMember<EventTarget> m_target;
     81     RefPtrWillBeMember<EventTarget> m_relatedTarget;
     82     RefPtrWillBeMember<StaticNodeList> m_eventPath;
     83     RefPtrWillBeMember<TouchEventContext> m_touchEventContext;
     84 
     85     WillBeHeapVector<RawPtrWillBeMember<TreeScopeEventContext> > m_children;
     86     int m_preOrder;
     87     int m_postOrder;
     88 };
     89 
     90 #ifndef NDEBUG
     91 inline bool TreeScopeEventContext::isUnreachableNode(EventTarget& target)
     92 {
     93     // FIXME: Checks also for SVG elements.
     94     return target.toNode() && !target.toNode()->isSVGElement() && !target.toNode()->treeScope().isInclusiveOlderSiblingShadowRootOrAncestorTreeScopeOf(treeScope());
     95 }
     96 #endif
     97 
     98 inline void TreeScopeEventContext::setTarget(PassRefPtrWillBeRawPtr<EventTarget> target)
     99 {
    100     ASSERT(target);
    101     ASSERT(!isUnreachableNode(*target));
    102     m_target = target;
    103 }
    104 
    105 inline void TreeScopeEventContext::setRelatedTarget(PassRefPtrWillBeRawPtr<EventTarget> relatedTarget)
    106 {
    107     ASSERT(relatedTarget);
    108     ASSERT(!isUnreachableNode(*relatedTarget));
    109     m_relatedTarget = relatedTarget;
    110 }
    111 
    112 inline bool TreeScopeEventContext::isInclusiveAncestorOf(const TreeScopeEventContext& other)
    113 {
    114     ASSERT(m_preOrder != -1 && m_postOrder != -1 && other.m_preOrder != -1 && other.m_postOrder != -1);
    115     return m_preOrder <= other.m_preOrder && other.m_postOrder <= m_postOrder;
    116 }
    117 
    118 }
    119 
    120 #endif // TreeScopeEventContext_h
    121