1/*
2 * Copyright (C) 2012 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitPrivate.h"
22
23#include "APIError.h"
24#include "WebEvent.h"
25#include "WebKitError.h"
26
27#if PLATFORM(GTK)
28#include <gdk/gdk.h>
29#elif PLATFORM(WPE)
30#include <wpe/wpe.h>
31#endif
32
33#if PLATFORM(GTK)
34unsigned toPlatformModifiers(OptionSet<WebKit::WebEvent::Modifier> wkModifiers)
35{
36 unsigned modifiers = 0;
37 if (wkModifiers.contains(WebKit::WebEvent::Modifier::ShiftKey))
38 modifiers |= GDK_SHIFT_MASK;
39 if (wkModifiers.contains(WebKit::WebEvent::Modifier::ControlKey))
40 modifiers |= GDK_CONTROL_MASK;
41 if (wkModifiers.contains(WebKit::WebEvent::Modifier::AltKey))
42 modifiers |= GDK_MOD1_MASK;
43 if (wkModifiers.contains(WebKit::WebEvent::Modifier::MetaKey))
44 modifiers |= GDK_META_MASK;
45 if (wkModifiers.contains(WebKit::WebEvent::Modifier::CapsLockKey))
46 modifiers |= GDK_LOCK_MASK;
47 return modifiers;
48}
49#elif PLATFORM(WPE)
50unsigned toPlatformModifiers(OptionSet<WebKit::WebEvent::Modifier> wkModifiers)
51{
52 unsigned modifiers = 0;
53 if (wkModifiers.contains(WebKit::WebEvent::Modifier::ShiftKey))
54 modifiers |= wpe_input_keyboard_modifier_shift;
55 if (wkModifiers.contains(WebKit::WebEvent::Modifier::ControlKey))
56 modifiers |= wpe_input_keyboard_modifier_control;
57 if (wkModifiers.contains(WebKit::WebEvent::Modifier::AltKey))
58 modifiers |= wpe_input_keyboard_modifier_alt;
59 if (wkModifiers.contains(WebKit::WebEvent::Modifier::MetaKey))
60 modifiers |= wpe_input_keyboard_modifier_meta;
61 return modifiers;
62}
63#endif
64
65WebKitNavigationType toWebKitNavigationType(WebCore::NavigationType type)
66{
67 switch (type) {
68 case WebCore::NavigationType::LinkClicked:
69 return WEBKIT_NAVIGATION_TYPE_LINK_CLICKED;
70 case WebCore::NavigationType::FormSubmitted:
71 return WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED;
72 case WebCore::NavigationType::BackForward:
73 return WEBKIT_NAVIGATION_TYPE_BACK_FORWARD;
74 case WebCore::NavigationType::Reload:
75 return WEBKIT_NAVIGATION_TYPE_RELOAD;
76 case WebCore::NavigationType::FormResubmitted:
77 return WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED;
78 case WebCore::NavigationType::Other:
79 return WEBKIT_NAVIGATION_TYPE_OTHER;
80 default:
81 ASSERT_NOT_REACHED();
82 return WEBKIT_NAVIGATION_TYPE_OTHER;
83 }
84}
85
86unsigned toWebKitMouseButton(WebKit::WebMouseEvent::Button button)
87{
88 switch (button) {
89 case WebKit::WebMouseEvent::Button::NoButton:
90 return 0;
91 case WebKit::WebMouseEvent::Button::LeftButton:
92 return 1;
93 case WebKit::WebMouseEvent::Button::MiddleButton:
94 return 2;
95 case WebKit::WebMouseEvent::Button::RightButton:
96 return 3;
97 }
98 ASSERT_NOT_REACHED();
99 return 0;
100}
101
102unsigned toWebKitError(unsigned webCoreError)
103{
104 switch (webCoreError) {
105 case API::Error::Network::Cancelled:
106 return WEBKIT_NETWORK_ERROR_CANCELLED;
107 case API::Error::Network::FileDoesNotExist:
108 return WEBKIT_NETWORK_ERROR_FILE_DOES_NOT_EXIST;
109 case API::Error::Policy::CannotShowMIMEType:
110 return WEBKIT_POLICY_ERROR_CANNOT_SHOW_MIME_TYPE;
111 case API::Error::Policy::CannotShowURL:
112 return WEBKIT_POLICY_ERROR_CANNOT_SHOW_URI;
113 case API::Error::Policy::FrameLoadInterruptedByPolicyChange:
114 return WEBKIT_POLICY_ERROR_FRAME_LOAD_INTERRUPTED_BY_POLICY_CHANGE;
115 case API::Error::Policy::CannotUseRestrictedPort:
116 return WEBKIT_POLICY_ERROR_CANNOT_USE_RESTRICTED_PORT;
117 case API::Error::Plugin::CannotFindPlugIn:
118 return WEBKIT_PLUGIN_ERROR_CANNOT_FIND_PLUGIN;
119 case API::Error::Plugin::CannotLoadPlugIn:
120 return WEBKIT_PLUGIN_ERROR_CANNOT_LOAD_PLUGIN;
121 case API::Error::Plugin::JavaUnavailable:
122 return WEBKIT_PLUGIN_ERROR_JAVA_UNAVAILABLE;
123 case API::Error::Plugin::PlugInCancelledConnection:
124 return WEBKIT_PLUGIN_ERROR_CONNECTION_CANCELLED;
125 case API::Error::Plugin::PlugInWillHandleLoad:
126 return WEBKIT_PLUGIN_ERROR_WILL_HANDLE_LOAD;
127 case API::Error::Download::Transport:
128 return WEBKIT_DOWNLOAD_ERROR_NETWORK;
129 case API::Error::Download::CancelledByUser:
130 return WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER;
131 case API::Error::Download::Destination:
132 return WEBKIT_DOWNLOAD_ERROR_DESTINATION;
133#if PLATFORM(GTK)
134 case API::Error::Print::Generic:
135 return WEBKIT_PRINT_ERROR_GENERAL;
136 case API::Error::Print::PrinterNotFound:
137 return WEBKIT_PRINT_ERROR_PRINTER_NOT_FOUND;
138 case API::Error::Print::InvalidPageRange:
139 return WEBKIT_PRINT_ERROR_INVALID_PAGE_RANGE;
140#endif
141 default:
142 // This may be a user app defined error, which needs to be passed as-is.
143 return webCoreError;
144 }
145}
146
147unsigned toWebCoreError(unsigned webKitError)
148{
149 switch (webKitError) {
150 case WEBKIT_NETWORK_ERROR_CANCELLED:
151 return API::Error::Network::Cancelled;
152 case WEBKIT_NETWORK_ERROR_FILE_DOES_NOT_EXIST:
153 return API::Error::Network::FileDoesNotExist;
154 case WEBKIT_POLICY_ERROR_CANNOT_SHOW_MIME_TYPE:
155 return API::Error::Policy::CannotShowMIMEType;
156 case WEBKIT_POLICY_ERROR_CANNOT_SHOW_URI:
157 return API::Error::Policy::CannotShowURL;
158 case WEBKIT_POLICY_ERROR_FRAME_LOAD_INTERRUPTED_BY_POLICY_CHANGE:
159 return API::Error::Policy::FrameLoadInterruptedByPolicyChange;
160 case WEBKIT_POLICY_ERROR_CANNOT_USE_RESTRICTED_PORT:
161 return API::Error::Policy::CannotUseRestrictedPort;
162 case WEBKIT_PLUGIN_ERROR_CANNOT_FIND_PLUGIN:
163 return API::Error::Plugin::CannotFindPlugIn;
164 case WEBKIT_PLUGIN_ERROR_CANNOT_LOAD_PLUGIN:
165 return API::Error::Plugin::CannotLoadPlugIn;
166 case WEBKIT_PLUGIN_ERROR_JAVA_UNAVAILABLE:
167 return API::Error::Plugin::JavaUnavailable;
168 case WEBKIT_PLUGIN_ERROR_CONNECTION_CANCELLED:
169 return API::Error::Plugin::PlugInCancelledConnection;
170 case WEBKIT_PLUGIN_ERROR_WILL_HANDLE_LOAD:
171 return API::Error::Plugin::PlugInWillHandleLoad;
172 case WEBKIT_DOWNLOAD_ERROR_NETWORK:
173 return API::Error::Download::Transport;
174 case WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER:
175 return API::Error::Download::CancelledByUser;
176 case WEBKIT_DOWNLOAD_ERROR_DESTINATION:
177 return API::Error::Download::Destination;
178#if PLATFORM(GTK)
179 case WEBKIT_PRINT_ERROR_GENERAL:
180 return API::Error::Print::Generic;
181 case WEBKIT_PRINT_ERROR_PRINTER_NOT_FOUND:
182 return API::Error::Print::PrinterNotFound;
183 case WEBKIT_PRINT_ERROR_INVALID_PAGE_RANGE:
184 return API::Error::Print::InvalidPageRange;
185#endif
186 default:
187 // This may be a user app defined error, which needs to be passed as-is.
188 return webKitError;
189 }
190}
191