1/*
2 * Copyright (C) 2014 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#import "config.h"
27#import "WKNavigationActionInternal.h"
28
29#if WK_API_ENABLED
30
31#import "NavigationActionData.h"
32#import <wtf/RetainPtr.h>
33
34@implementation WKNavigationAction {
35    RetainPtr<WKFrameInfo> _sourceFrame;
36    RetainPtr<WKFrameInfo> _targetFrame;
37    RetainPtr<NSURLRequest> _request;
38    RetainPtr<NSURL> _originalURL;
39    BOOL _userInitiated;
40    BOOL _canHandleRequest;
41}
42
43static WKNavigationType toWKNavigationType(WebCore::NavigationType navigationType)
44{
45    switch (navigationType) {
46    case WebCore::NavigationTypeLinkClicked:
47        return WKNavigationTypeLinkActivated;
48    case WebCore::NavigationTypeFormSubmitted:
49        return WKNavigationTypeFormSubmitted;
50    case WebCore::NavigationTypeBackForward:
51        return WKNavigationTypeBackForward;
52    case WebCore::NavigationTypeReload:
53        return WKNavigationTypeReload;
54    case WebCore::NavigationTypeFormResubmitted:
55        return WKNavigationTypeFormResubmitted;
56    case WebCore::NavigationTypeOther:
57        return WKNavigationTypeOther;
58    }
59
60    ASSERT_NOT_REACHED();
61    return WKNavigationTypeOther;
62}
63
64#if PLATFORM(MAC)
65
66// FIXME: This really belongs in WebEventFactory.
67static NSEventModifierFlags toNSEventModifierFlags(WebKit::WebEvent::Modifiers modifiers)
68{
69    NSEventModifierFlags modifierFlags = 0;
70
71    if (modifiers & WebKit::WebEvent::CapsLockKey)
72        modifierFlags |= NSAlphaShiftKeyMask;
73    if (modifiers & WebKit::WebEvent::ShiftKey)
74        modifierFlags |= NSShiftKeyMask;
75    if (modifiers & WebKit::WebEvent::ControlKey)
76        modifierFlags |= NSControlKeyMask;
77    if (modifiers & WebKit::WebEvent::AltKey)
78        modifierFlags |= NSAlternateKeyMask;
79    if (modifiers & WebKit::WebEvent::MetaKey)
80        modifierFlags |= NSCommandKeyMask;
81
82    return modifierFlags;
83}
84
85static NSInteger toNSButtonNumber(WebKit::WebMouseEvent::Button mouseButton)
86{
87    switch (mouseButton) {
88    case WebKit::WebMouseEvent::NoButton:
89        return 0;
90
91    case WebKit::WebMouseEvent::LeftButton:
92        return 1 << 0;
93
94    case WebKit::WebMouseEvent::RightButton:
95        return 1 << 1;
96
97    case WebKit::WebMouseEvent::MiddleButton:
98        return 1 << 2;
99
100    default:
101        return 0;
102    }
103}
104#endif
105
106- (instancetype)_initWithNavigationActionData:(const WebKit::NavigationActionData&)navigationActionData
107{
108    if (!(self = [super init]))
109        return nil;
110
111    _navigationType = toWKNavigationType(navigationActionData.navigationType);
112
113#if PLATFORM(MAC)
114    _modifierFlags = toNSEventModifierFlags(navigationActionData.modifiers);
115    _buttonNumber = toNSButtonNumber(navigationActionData.mouseButton);
116#endif
117
118    _userInitiated = navigationActionData.isProcessingUserGesture;
119    _canHandleRequest = navigationActionData.canHandleRequest;
120
121    return self;
122}
123
124- (NSString *)description
125{
126    return [NSString stringWithFormat:@"<%@: %p; navigationType = %ld; request = %@; sourceFrame = %@; targetFrame = %@>", NSStringFromClass(self.class), self,
127        (long)_navigationType, _request.get(), _sourceFrame.get(), _targetFrame.get()];
128}
129
130- (WKFrameInfo *)sourceFrame
131{
132    return _sourceFrame.get();
133}
134
135- (void)setSourceFrame:(WKFrameInfo *)sourceFrame
136{
137    _sourceFrame = adoptNS([sourceFrame copy]);
138}
139
140- (WKFrameInfo *)targetFrame
141{
142    return _targetFrame.get();
143}
144
145- (void)setTargetFrame:(WKFrameInfo *)targetFrame
146{
147    _targetFrame = adoptNS([targetFrame copy]);
148}
149
150- (NSURLRequest *)request
151{
152    return _request.get();
153}
154
155- (void)setRequest:(NSURLRequest *)request
156{
157    _request = adoptNS([request copy]);
158}
159
160- (void)_setOriginalURL:(NSURL *)originalURL
161{
162    _originalURL = adoptNS([originalURL copy]);
163}
164
165- (NSURL *)_originalURL
166{
167    return _originalURL.get();
168}
169
170@end
171
172@implementation WKNavigationAction (WKPrivate)
173
174- (BOOL)_isUserInitiated
175{
176    return _userInitiated;
177}
178
179- (BOOL)_canHandleRequest
180{
181    return _canHandleRequest;
182}
183
184@end
185
186#endif
187