1/* 2 * Copyright (C) 2010 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 EventContext_h 28#define EventContext_h 29 30#include "EventTarget.h" 31#include "Node.h" 32#include "TreeScope.h" 33#include <wtf/RefPtr.h> 34 35namespace WebCore { 36 37class Event; 38#if ENABLE(TOUCH_EVENTS) 39class TouchList; 40#endif 41 42class EventContext { 43public: 44 // FIXME: Use ContainerNode instead of Node. 45 EventContext(PassRefPtr<Node>, PassRefPtr<EventTarget> currentTarget, PassRefPtr<EventTarget> target); 46 virtual ~EventContext(); 47 48 Node* node() const { return m_node.get(); } 49 EventTarget* target() const { return m_target.get(); } 50 bool currentTargetSameAsTarget() const { return m_currentTarget.get() == m_target.get(); } 51 virtual void handleLocalEvents(Event*) const; 52 virtual bool isMouseOrFocusEventContext() const; 53 virtual bool isTouchEventContext() const; 54 55protected: 56#ifndef NDEBUG 57 bool isUnreachableNode(EventTarget*); 58 bool isReachable(Node*) const; 59#endif 60 RefPtr<Node> m_node; 61 RefPtr<EventTarget> m_currentTarget; 62 RefPtr<EventTarget> m_target; 63}; 64 65typedef Vector<OwnPtr<EventContext>, 32> EventPath; 66 67class MouseOrFocusEventContext : public EventContext { 68public: 69 MouseOrFocusEventContext(PassRefPtr<Node>, PassRefPtr<EventTarget> currentTarget, PassRefPtr<EventTarget> target); 70 virtual ~MouseOrFocusEventContext(); 71 EventTarget* relatedTarget() const { return m_relatedTarget.get(); } 72 void setRelatedTarget(PassRefPtr<EventTarget>); 73 virtual void handleLocalEvents(Event*) const OVERRIDE; 74 virtual bool isMouseOrFocusEventContext() const OVERRIDE; 75 76private: 77 RefPtr<EventTarget> m_relatedTarget; 78}; 79 80 81#if ENABLE(TOUCH_EVENTS) 82class TouchEventContext : public EventContext { 83public: 84 TouchEventContext(PassRefPtr<Node>, PassRefPtr<EventTarget> currentTarget, PassRefPtr<EventTarget> target); 85 virtual ~TouchEventContext(); 86 87 virtual void handleLocalEvents(Event*) const OVERRIDE; 88 virtual bool isTouchEventContext() const OVERRIDE; 89 90 TouchList* touches() { return m_touches.get(); } 91 TouchList* targetTouches() { return m_targetTouches.get(); } 92 TouchList* changedTouches() { return m_changedTouches.get(); } 93 94private: 95 RefPtr<TouchList> m_touches; 96 RefPtr<TouchList> m_targetTouches; 97 RefPtr<TouchList> m_changedTouches; 98#ifndef NDEBUG 99 void checkReachability(TouchList*) const; 100#endif 101}; 102 103inline TouchEventContext* toTouchEventContext(EventContext* eventContext) 104{ 105 ASSERT_WITH_SECURITY_IMPLICATION(!eventContext || eventContext->isTouchEventContext()); 106 return static_cast<TouchEventContext*>(eventContext); 107} 108#endif // ENABLE(TOUCH_EVENTS) 109 110#ifndef NDEBUG 111inline bool EventContext::isUnreachableNode(EventTarget* target) 112{ 113 // FIXME: Checks also for SVG elements. 114 return target && target->toNode() && !target->toNode()->isSVGElement() && !isReachable(target->toNode()); 115} 116 117inline bool EventContext::isReachable(Node* target) const 118{ 119 ASSERT(target); 120 TreeScope* targetScope = target->treeScope(); 121 for (TreeScope* scope = m_node->treeScope(); scope; scope = scope->parentTreeScope()) { 122 if (scope == targetScope) 123 return true; 124 } 125 return false; 126} 127#endif 128 129inline void MouseOrFocusEventContext::setRelatedTarget(PassRefPtr<EventTarget> relatedTarget) 130{ 131 ASSERT(!isUnreachableNode(relatedTarget.get())); 132 m_relatedTarget = relatedTarget; 133} 134 135} 136 137#endif // EventContext_h 138