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