[Win] Add UserAgentWin.cpp
[WebKit-https.git] / Source / WebKit / WebProcess / WebPage / win / WebPageWin.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2017 Sony Interactive Entertainment Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "WebPage.h"
29
30 #include "EditorState.h"
31 #include "WebEvent.h"
32 #include "WebFrame.h"
33 #include "WebPageProxyMessages.h"
34 #include "WebProcess.h"
35 #include <WebCore/BackForwardController.h>
36 #include <WebCore/EventHandler.h>
37 #include <WebCore/EventNames.h>
38 #include <WebCore/FocusController.h>
39 #include <WebCore/Frame.h>
40 #include <WebCore/FrameView.h>
41 #include <WebCore/KeyboardEvent.h>
42 #include <WebCore/NotImplemented.h>
43 #include <WebCore/Page.h>
44 #include <WebCore/PlatformKeyboardEvent.h>
45 #include <WebCore/Settings.h>
46 #include <WebCore/SharedBuffer.h>
47 #include <WebCore/UserAgent.h>
48 #include <WebCore/WindowsKeyboardCodes.h>
49
50 using namespace WebCore;
51
52 namespace WebKit {
53
54 void WebPage::platformInitialize()
55 {
56 }
57
58 void WebPage::platformDetach()
59 {
60 }
61
62 void WebPage::platformEditorState(Frame& frame, EditorState& result, IncludePostLayoutDataHint shouldIncludePostLayoutData) const
63 {
64 }
65
66 bool WebPage::performDefaultBehaviorForKeyEvent(const WebKeyboardEvent& keyboardEvent)
67 {
68     if (keyboardEvent.type() != WebEvent::KeyDown && keyboardEvent.type() != WebEvent::RawKeyDown)
69         return false;
70
71     switch (keyboardEvent.windowsVirtualKeyCode()) {
72     case VK_SPACE:
73         scroll(m_page.get(), keyboardEvent.shiftKey() ? ScrollUp : ScrollDown, ScrollByPage);
74         break;
75     case VK_LEFT:
76         scroll(m_page.get(), ScrollLeft, ScrollByLine);
77         break;
78     case VK_RIGHT:
79         scroll(m_page.get(), ScrollRight, ScrollByLine);
80         break;
81     case VK_UP:
82         scroll(m_page.get(), ScrollUp, ScrollByLine);
83         break;
84     case VK_DOWN:
85         scroll(m_page.get(), ScrollDown, ScrollByLine);
86         break;
87     case VK_HOME:
88         scroll(m_page.get(), ScrollUp, ScrollByDocument);
89         break;
90     case VK_END:
91         scroll(m_page.get(), ScrollDown, ScrollByDocument);
92         break;
93     case VK_PRIOR:
94         scroll(m_page.get(), ScrollUp, ScrollByPage);
95         break;
96     case VK_NEXT:
97         scroll(m_page.get(), ScrollDown, ScrollByPage);
98         break;
99     default:
100         return false;
101     }
102
103     return true;
104 }
105
106 bool WebPage::platformHasLocalDataForURL(const URL&)
107 {
108     notImplemented();
109     return false;
110 }
111
112 String WebPage::cachedResponseMIMETypeForURL(const URL&)
113 {
114     notImplemented();
115     return String();
116 }
117
118 bool WebPage::platformCanHandleRequest(const ResourceRequest&)
119 {
120     notImplemented();
121     return false;
122 }
123
124 String WebPage::cachedSuggestedFilenameForURL(const URL&)
125 {
126     notImplemented();
127     return String();
128 }
129
130 RefPtr<SharedBuffer> WebPage::cachedResponseDataForURL(const URL&)
131 {
132     notImplemented();
133     return 0;
134 }
135
136 String WebPage::platformUserAgent(const URL& url) const
137 {
138     if (url.isNull() || !m_page->settings().needsSiteSpecificQuirks())
139         return String();
140
141     return WebCore::standardUserAgentForURL(url);
142 }
143
144 static const unsigned CtrlKey = 1 << 0;
145 static const unsigned AltKey = 1 << 1;
146 static const unsigned ShiftKey = 1 << 2;
147
148 struct KeyDownEntry {
149     unsigned virtualKey;
150     unsigned modifiers;
151     const char* name;
152 };
153
154 struct KeyPressEntry {
155     unsigned charCode;
156     unsigned modifiers;
157     const char* name;
158 };
159
160 static const KeyDownEntry keyDownEntries[] = {
161     { VK_LEFT,   0,                  "MoveLeft" },
162     { VK_LEFT,   ShiftKey,           "MoveLeftAndModifySelection" },
163     { VK_LEFT,   CtrlKey,            "MoveWordLeft" },
164     { VK_LEFT,   CtrlKey | ShiftKey, "MoveWordLeftAndModifySelection" },
165     { VK_RIGHT,  0,                  "MoveRight" },
166     { VK_RIGHT,  ShiftKey,           "MoveRightAndModifySelection" },
167     { VK_RIGHT,  CtrlKey,            "MoveWordRight" },
168     { VK_RIGHT,  CtrlKey | ShiftKey, "MoveWordRightAndModifySelection" },
169     { VK_UP,     0,                  "MoveUp" },
170     { VK_UP,     ShiftKey,           "MoveUpAndModifySelection" },
171     { VK_PRIOR,  ShiftKey,           "MovePageUpAndModifySelection" },
172     { VK_DOWN,   0,                  "MoveDown" },
173     { VK_DOWN,   ShiftKey,           "MoveDownAndModifySelection" },
174     { VK_NEXT,   ShiftKey,           "MovePageDownAndModifySelection" },
175     { VK_PRIOR,  0,                  "MovePageUp" },
176     { VK_NEXT,   0,                  "MovePageDown" },
177     { VK_HOME,   0,                  "MoveToBeginningOfLine" },
178     { VK_HOME,   ShiftKey,           "MoveToBeginningOfLineAndModifySelection" },
179     { VK_HOME,   CtrlKey,            "MoveToBeginningOfDocument" },
180     { VK_HOME,   CtrlKey | ShiftKey, "MoveToBeginningOfDocumentAndModifySelection" },
181
182     { VK_END,    0,                  "MoveToEndOfLine" },
183     { VK_END,    ShiftKey,           "MoveToEndOfLineAndModifySelection" },
184     { VK_END,    CtrlKey,            "MoveToEndOfDocument" },
185     { VK_END,    CtrlKey | ShiftKey, "MoveToEndOfDocumentAndModifySelection" },
186
187     { VK_BACK,   0,                  "DeleteBackward" },
188     { VK_BACK,   ShiftKey,           "DeleteBackward" },
189     { VK_DELETE, 0,                  "DeleteForward" },
190     { VK_BACK,   CtrlKey,            "DeleteWordBackward" },
191     { VK_DELETE, CtrlKey,            "DeleteWordForward" },
192
193     { 'B',       CtrlKey,            "ToggleBold" },
194     { 'I',       CtrlKey,            "ToggleItalic" },
195
196     { VK_ESCAPE, 0,                  "Cancel" },
197     { VK_OEM_PERIOD, CtrlKey,        "Cancel" },
198     { VK_TAB,    0,                  "InsertTab" },
199     { VK_TAB,    ShiftKey,           "InsertBacktab" },
200     { VK_RETURN, 0,                  "InsertNewline" },
201     { VK_RETURN, CtrlKey,            "InsertNewline" },
202     { VK_RETURN, AltKey,             "InsertNewline" },
203     { VK_RETURN, ShiftKey,           "InsertNewline" },
204     { VK_RETURN, AltKey | ShiftKey,  "InsertNewline" },
205
206     // It's not quite clear whether clipboard shortcuts and Undo/Redo should be handled
207     // in the application or in WebKit. We chose WebKit.
208     { 'C',       CtrlKey,            "Copy" },
209     { 'V',       CtrlKey,            "Paste" },
210     { 'X',       CtrlKey,            "Cut" },
211     { 'A',       CtrlKey,            "SelectAll" },
212     { VK_INSERT, CtrlKey,            "Copy" },
213     { VK_DELETE, ShiftKey,           "Cut" },
214     { VK_INSERT, ShiftKey,           "Paste" },
215     { 'Z',       CtrlKey,            "Undo" },
216     { 'Z',       CtrlKey | ShiftKey, "Redo" },
217 };
218
219 static const KeyPressEntry keyPressEntries[] = {
220     { '\t',   0,                  "InsertTab" },
221     { '\t',   ShiftKey,           "InsertBacktab" },
222     { '\r',   0,                  "InsertNewline" },
223     { '\r',   CtrlKey,            "InsertNewline" },
224     { '\r',   AltKey,             "InsertNewline" },
225     { '\r',   ShiftKey,           "InsertNewline" },
226     { '\r',   AltKey | ShiftKey,  "InsertNewline" },
227 };
228
229 const char* WebPage::interpretKeyEvent(const WebCore::KeyboardEvent* evt)
230 {
231     ASSERT(evt->type() == eventNames().keydownEvent || evt->type() == eventNames().keypressEvent);
232
233     static HashMap<int, const char*>* keyDownCommandsMap = 0;
234     static HashMap<int, const char*>* keyPressCommandsMap = 0;
235
236     if (!keyDownCommandsMap) {
237         keyDownCommandsMap = new HashMap<int, const char*>;
238         keyPressCommandsMap = new HashMap<int, const char*>;
239
240         for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyDownEntries); ++i)
241             keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, keyDownEntries[i].name);
242
243         for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyPressEntries); ++i)
244             keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, keyPressEntries[i].name);
245     }
246
247     unsigned modifiers = 0;
248     if (evt->shiftKey())
249         modifiers |= ShiftKey;
250     if (evt->altKey())
251         modifiers |= AltKey;
252     if (evt->ctrlKey())
253         modifiers |= CtrlKey;
254
255     if (evt->type() == eventNames().keydownEvent) {
256         int mapKey = modifiers << 16 | evt->keyCode();
257         return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
258     }
259
260     int mapKey = modifiers << 16 | evt->charCode();
261     return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
262 }
263 } // namespace WebKit