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/EventHandler.h>
31#include <WebCore/Frame.h>
32#include <WebCore/HTMLFormElement.h>
33#include <WebCore/MouseEvent.h>
34#include <WebCore/NavigationAction.h>
35#include <WebCore/UIEventWithKeyState.h>
36
37using namespace WebCore;
38
39namespace WebKit {
40
41static const MouseEvent* mouseEventForNavigationAction(const NavigationAction& navigationAction)
42{
43    for (const Event* e = navigationAction.event(); e; e = e->underlyingEvent()) {
44        if (e->isMouseEvent())
45            return static_cast<const MouseEvent*>(e);
46    }
47    return 0;
48}
49
50static WebMouseEvent::Button mouseButtonForMouseEvent(const MouseEvent* mouseEvent)
51{
52    if (!mouseEvent)
53        return WebMouseEvent::NoButton;
54
55    if (!mouseEvent->buttonDown())
56        return WebMouseEvent::NoButton;
57
58    return static_cast<WebMouseEvent::Button>(mouseEvent->button());
59}
60
61WebEvent::Modifiers InjectedBundleNavigationAction::modifiersForNavigationAction(const NavigationAction& navigationAction)
62{
63    uint32_t modifiers = 0;
64    if (const UIEventWithKeyState* keyStateEvent = findEventWithKeyState(const_cast<Event*>(navigationAction.event()))) {
65        if (keyStateEvent->shiftKey())
66            modifiers |= WebEvent::ShiftKey;
67        if (keyStateEvent->ctrlKey())
68            modifiers |= WebEvent::ControlKey;
69        if (keyStateEvent->altKey())
70            modifiers |= WebEvent::AltKey;
71        if (keyStateEvent->metaKey())
72            modifiers |= WebEvent::MetaKey;
73    }
74
75    return static_cast<WebEvent::Modifiers>(modifiers);
76}
77
78WebMouseEvent::Button InjectedBundleNavigationAction::mouseButtonForNavigationAction(const NavigationAction& navigationAction)
79{
80    return mouseButtonForMouseEvent(mouseEventForNavigationAction(navigationAction));
81}
82
83
84PassRefPtr<InjectedBundleNavigationAction> InjectedBundleNavigationAction::create(WebFrame* frame, const NavigationAction& action, PassRefPtr<FormState> formState)
85{
86    return adoptRef(new InjectedBundleNavigationAction(frame, action, formState));
87}
88
89InjectedBundleNavigationAction::InjectedBundleNavigationAction(WebFrame* frame, const NavigationAction& navigationAction, PassRefPtr<FormState> prpFormState)
90    : m_navigationType(navigationAction.type())
91    , m_modifiers(modifiersForNavigationAction(navigationAction))
92    , m_mouseButton(WebMouseEvent::NoButton)
93{
94    if (const MouseEvent* mouseEvent = mouseEventForNavigationAction(navigationAction)) {
95        m_hitTestResult = InjectedBundleHitTestResult::create(frame->coreFrame()->eventHandler().hitTestResultAtPoint(mouseEvent->absoluteLocation()));
96        m_mouseButton   = mouseButtonForMouseEvent(mouseEvent);
97    }
98
99    RefPtr<FormState> formState = prpFormState;
100    if (formState) {
101        ASSERT(formState->form());
102        m_formElement   = InjectedBundleNodeHandle::getOrCreate(formState->form());
103    }
104}
105
106} // namespace WebKit
107