1/*
2 * Copyright (C) 2011-2016 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
37namespace WebKit {
38using namespace WebCore;
39
40static WebMouseEvent::Button mouseButtonForMouseEventData(const Optional<NavigationAction::MouseEventData>& mouseEventData)
41{
42 if (mouseEventData && mouseEventData->buttonDown && mouseEventData->isTrusted)
43 return static_cast<WebMouseEvent::Button>(mouseEventData->button);
44 return WebMouseEvent::NoButton;
45}
46
47static WebMouseEvent::SyntheticClickType syntheticClickTypeForMouseEventData(const Optional<NavigationAction::MouseEventData>& mouseEventData)
48{
49 if (mouseEventData && mouseEventData->buttonDown && mouseEventData->isTrusted)
50 return static_cast<WebMouseEvent::SyntheticClickType>(mouseEventData->syntheticClickType);
51 return WebMouseEvent::NoTap;
52}
53
54static FloatPoint clickLocationInRootViewCoordinatesForMouseEventData(const Optional<NavigationAction::MouseEventData>& mouseEventData)
55{
56 if (mouseEventData && mouseEventData->buttonDown && mouseEventData->isTrusted)
57 return mouseEventData->locationInRootViewCoordinates;
58 return { };
59}
60
61OptionSet<WebEvent::Modifier> InjectedBundleNavigationAction::modifiersForNavigationAction(const NavigationAction& navigationAction)
62{
63 OptionSet<WebEvent::Modifier> modifiers;
64 auto keyStateEventData = navigationAction.keyStateEventData();
65 if (keyStateEventData && keyStateEventData->isTrusted) {
66 if (keyStateEventData->shiftKey)
67 modifiers.add(WebEvent::Modifier::ShiftKey);
68 if (keyStateEventData->ctrlKey)
69 modifiers.add(WebEvent::Modifier::ControlKey);
70 if (keyStateEventData->altKey)
71 modifiers.add(WebEvent::Modifier::AltKey);
72 if (keyStateEventData->metaKey)
73 modifiers.add(WebEvent::Modifier::MetaKey);
74 }
75 return modifiers;
76}
77
78WebMouseEvent::Button InjectedBundleNavigationAction::mouseButtonForNavigationAction(const NavigationAction& navigationAction)
79{
80 return mouseButtonForMouseEventData(navigationAction.mouseEventData());
81}
82
83WebMouseEvent::SyntheticClickType InjectedBundleNavigationAction::syntheticClickTypeForNavigationAction(const NavigationAction& navigationAction)
84{
85 return syntheticClickTypeForMouseEventData(navigationAction.mouseEventData());
86}
87
88FloatPoint InjectedBundleNavigationAction::clickLocationInRootViewCoordinatesForNavigationAction(const NavigationAction& navigationAction)
89{
90 return clickLocationInRootViewCoordinatesForMouseEventData(navigationAction.mouseEventData());
91}
92
93Ref<InjectedBundleNavigationAction> InjectedBundleNavigationAction::create(WebFrame* frame, const NavigationAction& action, RefPtr<FormState>&& formState)
94{
95 return adoptRef(*new InjectedBundleNavigationAction(frame, action, WTFMove(formState)));
96}
97
98InjectedBundleNavigationAction::InjectedBundleNavigationAction(WebFrame* frame, const NavigationAction& navigationAction, RefPtr<FormState>&& formState)
99 : m_navigationType(navigationAction.type())
100 , m_modifiers(modifiersForNavigationAction(navigationAction))
101 , m_mouseButton(WebMouseEvent::NoButton)
102 , m_downloadAttribute(navigationAction.downloadAttribute())
103 , m_shouldOpenExternalURLs(navigationAction.shouldOpenExternalURLsPolicy() == ShouldOpenExternalURLsPolicy::ShouldAllow || navigationAction.shouldOpenExternalURLsPolicy() == ShouldOpenExternalURLsPolicy::ShouldAllowExternalSchemes)
104 , m_shouldTryAppLinks(navigationAction.shouldOpenExternalURLsPolicy() == ShouldOpenExternalURLsPolicy::ShouldAllow)
105{
106 if (auto mouseEventData = navigationAction.mouseEventData()) {
107 m_hitTestResult = InjectedBundleHitTestResult::create(frame->coreFrame()->eventHandler().hitTestResultAtPoint(mouseEventData->absoluteLocation));
108 m_mouseButton = mouseButtonForMouseEventData(mouseEventData);
109 m_syntheticClickType = syntheticClickTypeForNavigationAction(navigationAction);
110 m_clickLocationInRootViewCoordinates = clickLocationInRootViewCoordinatesForNavigationAction(navigationAction);
111 }
112
113 if (formState)
114 m_formElement = InjectedBundleNodeHandle::getOrCreate(formState->form());
115}
116
117} // namespace WebKit
118