1/*
2 * Copyright (C) 2011 Apple 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "InjectedBundleNavigationAction.h"
28
29#include "WebFrame.h"
30#include <WebCore/Frame.h>
31#include <WebCore/HTMLFormElement.h>
32#include <WebCore/MouseEvent.h>
33#include <WebCore/NavigationAction.h>
34#include <WebCore/UIEventWithKeyState.h>
35
36using namespace WebCore;
37
38namespace WebKit {
39
40static const MouseEvent* mouseEventForNavigationAction(const NavigationAction& navigationAction)
41{
42    for (const Event* e = navigationAction.event(); e; e = e->underlyingEvent()) {
43        if (e->isMouseEvent())
44            return static_cast<const MouseEvent*>(e);
45    }
46    return 0;
47}
48
49static WebMouseEvent::Button mouseButtonForMouseEvent(const MouseEvent* mouseEvent)
50{
51    if (!mouseEvent)
52        return WebMouseEvent::NoButton;
53
54    if (!mouseEvent->buttonDown())
55        return WebMouseEvent::NoButton;
56
57    return static_cast<WebMouseEvent::Button>(mouseEvent->button());
58}
59
60WebEvent::Modifiers InjectedBundleNavigationAction::modifiersForNavigationAction(const NavigationAction& navigationAction)
61{
62    uint32_t modifiers = 0;
63    if (const UIEventWithKeyState* keyStateEvent = findEventWithKeyState(const_cast<Event*>(navigationAction.event()))) {
64        if (keyStateEvent->shiftKey())
65            modifiers |= WebEvent::ShiftKey;
66        if (keyStateEvent->ctrlKey())
67            modifiers |= WebEvent::ControlKey;
68        if (keyStateEvent->altKey())
69            modifiers |= WebEvent::AltKey;
70        if (keyStateEvent->metaKey())
71            modifiers |= WebEvent::MetaKey;
72    }
73
74    return static_cast<WebEvent::Modifiers>(modifiers);
75}
76
77WebMouseEvent::Button InjectedBundleNavigationAction::mouseButtonForNavigationAction(const NavigationAction& navigationAction)
78{
79    return mouseButtonForMouseEvent(mouseEventForNavigationAction(navigationAction));
80}
81
82
83PassRefPtr<InjectedBundleNavigationAction> InjectedBundleNavigationAction::create(WebFrame* frame, const NavigationAction& action, PassRefPtr<FormState> formState)
84{
85    return adoptRef(new InjectedBundleNavigationAction(frame, action, formState));
86}
87
88InjectedBundleNavigationAction::InjectedBundleNavigationAction(WebFrame* frame, const NavigationAction& navigationAction, PassRefPtr<FormState> prpFormState)
89{
90    m_navigationType    = navigationAction.type();
91    m_modifiers         = modifiersForNavigationAction(navigationAction);
92
93    if (const MouseEvent* mouseEvent = mouseEventForNavigationAction(navigationAction)) {
94        m_hitTestResult = InjectedBundleHitTestResult::create(frame->coreFrame()->eventHandler()->hitTestResultAtPoint(mouseEvent->absoluteLocation(), false));
95        m_mouseButton   = mouseButtonForMouseEvent(mouseEvent);
96    }
97
98    RefPtr<FormState> formState = prpFormState;
99    if (formState) {
100        ASSERT(formState->form());
101        m_formElement   = InjectedBundleNodeHandle::getOrCreate(formState->form());
102    }
103}
104
105} // namespace WebKit
106