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#include "config.h"
28#include "EventContext.h"
29
30#include "DOMWindow.h"
31#include "Document.h"
32#include "Event.h"
33#include "FocusEvent.h"
34#include "MouseEvent.h"
35#include "Node.h"
36#include "TouchEvent.h"
37#include "TouchList.h"
38
39namespace WebCore {
40
41EventContext::EventContext(PassRefPtr<Node> node, PassRefPtr<EventTarget> currentTarget, PassRefPtr<EventTarget> target)
42    : m_node(node)
43    , m_currentTarget(currentTarget)
44    , m_target(target)
45{
46    ASSERT(m_node);
47    ASSERT(!isUnreachableNode(m_target.get()));
48}
49
50EventContext::~EventContext()
51{
52}
53
54void EventContext::handleLocalEvents(Event* event) const
55{
56    event->setTarget(m_target.get());
57    event->setCurrentTarget(m_currentTarget.get());
58    m_node->handleLocalEvents(event);
59}
60
61bool EventContext::isMouseOrFocusEventContext() const
62{
63    return false;
64}
65
66bool EventContext::isTouchEventContext() const
67{
68    return false;
69}
70
71MouseOrFocusEventContext::MouseOrFocusEventContext(PassRefPtr<Node> node, PassRefPtr<EventTarget> currentTarget, PassRefPtr<EventTarget> target)
72    : EventContext(node, currentTarget, target)
73    , m_relatedTarget(0)
74{
75}
76
77MouseOrFocusEventContext::~MouseOrFocusEventContext()
78{
79}
80
81void MouseOrFocusEventContext::handleLocalEvents(Event* event) const
82{
83    ASSERT(event->isMouseEvent() || event->isFocusEvent());
84    if (m_relatedTarget.get() && event->isMouseEvent())
85        toMouseEvent(event)->setRelatedTarget(m_relatedTarget.get());
86    else if (m_relatedTarget.get() && event->isFocusEvent())
87        toFocusEvent(event)->setRelatedTarget(m_relatedTarget.get());
88    EventContext::handleLocalEvents(event);
89}
90
91bool MouseOrFocusEventContext::isMouseOrFocusEventContext() const
92{
93    return true;
94}
95
96#if ENABLE(TOUCH_EVENTS)
97TouchEventContext::TouchEventContext(PassRefPtr<Node> node, PassRefPtr<EventTarget> currentTarget, PassRefPtr<EventTarget> target)
98    : EventContext(node, currentTarget, target)
99    , m_touches(TouchList::create())
100    , m_targetTouches(TouchList::create())
101    , m_changedTouches(TouchList::create())
102{
103}
104
105TouchEventContext::~TouchEventContext()
106{
107}
108
109void TouchEventContext::handleLocalEvents(Event* event) const
110{
111#ifndef NDEBUG
112    checkReachability(m_touches.get());
113    checkReachability(m_targetTouches.get());
114    checkReachability(m_changedTouches.get());
115#endif
116    ASSERT(event->isTouchEvent());
117    TouchEvent* touchEvent = toTouchEvent(event);
118    touchEvent->setTouches(m_touches);
119    touchEvent->setTargetTouches(m_targetTouches);
120    touchEvent->setChangedTouches(m_changedTouches);
121    EventContext::handleLocalEvents(event);
122}
123
124bool TouchEventContext::isTouchEventContext() const
125{
126    return true;
127}
128
129#ifndef NDEBUG
130void TouchEventContext::checkReachability(TouchList* touchList) const
131{
132    for (size_t i = 0; i < touchList->length(); ++i)
133        ASSERT(isReachable(touchList->item(i)->target()->toNode()));
134}
135#endif
136
137#endif // ENABLE(TOUCH_EVENTS)
138
139}
140