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
37namespace blink {
38
39class EventPath;
40class EventTarget;
41class Node;
42template <typename NodeType> class StaticNodeTypeList;
43typedef StaticNodeTypeList<Node> StaticNodeList;
44class TouchEventContext;
45class TreeScope;
46
47class TreeScopeEventContext FINAL : public RefCountedWillBeGarbageCollected<TreeScopeEventContext> {
48    DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(TreeScopeEventContext);
49public:
50    static PassRefPtrWillBeRawPtr<TreeScopeEventContext> create(TreeScope&);
51    void trace(Visitor*);
52
53    TreeScope& treeScope() const { return *m_treeScope; }
54
55    EventTarget* target() const { return m_target.get(); }
56    void setTarget(PassRefPtrWillBeRawPtr<EventTarget>);
57
58    EventTarget* relatedTarget() const { return m_relatedTarget.get(); }
59    void setRelatedTarget(PassRefPtrWillBeRawPtr<EventTarget>);
60
61    TouchEventContext* touchEventContext() const { return m_touchEventContext.get(); }
62    TouchEventContext* ensureTouchEventContext();
63
64    PassRefPtrWillBeRawPtr<StaticNodeList> ensureEventPath(EventPath&);
65
66    bool isInclusiveAncestorOf(const TreeScopeEventContext&);
67    void addChild(TreeScopeEventContext& child) { m_children.append(&child); }
68
69    // For ancestor-descendant relationship check in Q(1).
70    // Preprocessing takes O(N).
71    int calculatePrePostOrderNumber(int orderNumber);
72
73private:
74    TreeScopeEventContext(TreeScope&);
75
76#if ENABLE(ASSERT)
77    bool isUnreachableNode(EventTarget&);
78#endif
79
80    RawPtrWillBeMember<TreeScope> m_treeScope;
81    RefPtrWillBeMember<EventTarget> m_target;
82    RefPtrWillBeMember<EventTarget> m_relatedTarget;
83    RefPtrWillBeMember<StaticNodeList> m_eventPath;
84    RefPtrWillBeMember<TouchEventContext> m_touchEventContext;
85
86    WillBeHeapVector<RawPtrWillBeMember<TreeScopeEventContext> > m_children;
87    int m_preOrder;
88    int m_postOrder;
89};
90
91#if ENABLE(ASSERT)
92inline bool TreeScopeEventContext::isUnreachableNode(EventTarget& target)
93{
94    // FIXME: Checks also for SVG elements.
95    return target.toNode() && !target.toNode()->isSVGElement() && !target.toNode()->treeScope().isInclusiveOlderSiblingShadowRootOrAncestorTreeScopeOf(treeScope());
96}
97#endif
98
99inline void TreeScopeEventContext::setTarget(PassRefPtrWillBeRawPtr<EventTarget> target)
100{
101    ASSERT(target);
102    ASSERT(!isUnreachableNode(*target));
103    m_target = target;
104}
105
106inline void TreeScopeEventContext::setRelatedTarget(PassRefPtrWillBeRawPtr<EventTarget> relatedTarget)
107{
108    ASSERT(relatedTarget);
109    ASSERT(!isUnreachableNode(*relatedTarget));
110    m_relatedTarget = relatedTarget;
111}
112
113inline bool TreeScopeEventContext::isInclusiveAncestorOf(const TreeScopeEventContext& other)
114{
115    ASSERT(m_preOrder != -1 && m_postOrder != -1 && other.m_preOrder != -1 && other.m_postOrder != -1);
116    return m_preOrder <= other.m_preOrder && other.m_postOrder <= m_postOrder;
117}
118
119}
120
121#endif // TreeScopeEventContext_h
122