More is<> and downcast<>, less static_cast<>
[WebKit-https.git] / Source / WebCore / inspector / InspectorFrontendHost.cpp
1 /*
2  * Copyright (C) 2007-2017 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
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  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "InspectorFrontendHost.h"
32
33 #include "ContextMenu.h"
34 #include "ContextMenuController.h"
35 #include "ContextMenuItem.h"
36 #include "ContextMenuProvider.h"
37 #include "DOMWrapperWorld.h"
38 #include "Document.h"
39 #include "Editor.h"
40 #include "Event.h"
41 #include "FocusController.h"
42 #include "HitTestResult.h"
43 #include "InspectorController.h"
44 #include "InspectorFrontendClient.h"
45 #include "JSDOMConvertInterface.h"
46 #include "JSDOMExceptionHandling.h"
47 #include "JSInspectorFrontendHost.h"
48 #include "JSMainThreadExecState.h"
49 #include "MainFrame.h"
50 #include "MouseEvent.h"
51 #include "Node.h"
52 #include "Page.h"
53 #include "Pasteboard.h"
54 #include "ScriptState.h"
55 #include "UserGestureIndicator.h"
56 #include <bindings/ScriptFunctionCall.h>
57 #include <pal/system/Sound.h>
58 #include <wtf/StdLibExtras.h>
59
60
61 namespace WebCore {
62 using namespace Inspector;
63
64 #if ENABLE(CONTEXT_MENUS)
65 class FrontendMenuProvider : public ContextMenuProvider {
66 public:
67     static Ref<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, Deprecated::ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
68     {
69         return adoptRef(*new FrontendMenuProvider(frontendHost, frontendApiObject, items));
70     }
71     
72     void disconnect()
73     {
74         m_frontendApiObject = { };
75         m_frontendHost = nullptr;
76     }
77     
78 private:
79     FrontendMenuProvider(InspectorFrontendHost* frontendHost, Deprecated::ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
80         : m_frontendHost(frontendHost)
81         , m_frontendApiObject(frontendApiObject)
82         , m_items(items)
83     {
84     }
85
86     virtual ~FrontendMenuProvider()
87     {
88         contextMenuCleared();
89     }
90     
91     void populateContextMenu(ContextMenu* menu) override
92     {
93         for (auto& item : m_items)
94             menu->appendItem(item);
95     }
96     
97     void contextMenuItemSelected(ContextMenuAction action, const String&) override
98     {
99         if (m_frontendHost) {
100             UserGestureIndicator gestureIndicator(ProcessingUserGesture);
101             int itemNumber = action - ContextMenuItemBaseCustomTag;
102
103             Deprecated::ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected", WebCore::functionCallHandlerFromAnyThread);
104             function.appendArgument(itemNumber);
105             function.call();
106         }
107     }
108     
109     void contextMenuCleared() override
110     {
111         if (m_frontendHost) {
112             Deprecated::ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared", WebCore::functionCallHandlerFromAnyThread);
113             function.call();
114
115             m_frontendHost->m_menuProvider = nullptr;
116         }
117         m_items.clear();
118     }
119
120     InspectorFrontendHost* m_frontendHost;
121     Deprecated::ScriptObject m_frontendApiObject;
122     Vector<ContextMenuItem> m_items;
123 };
124 #endif
125
126 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
127     : m_client(client)
128     , m_frontendPage(frontendPage)
129 #if ENABLE(CONTEXT_MENUS)
130     , m_menuProvider(nullptr)
131 #endif
132 {
133 }
134
135 InspectorFrontendHost::~InspectorFrontendHost()
136 {
137     ASSERT(!m_client);
138 }
139
140 void InspectorFrontendHost::disconnectClient()
141 {
142     m_client = nullptr;
143 #if ENABLE(CONTEXT_MENUS)
144     if (m_menuProvider)
145         m_menuProvider->disconnect();
146 #endif
147     m_frontendPage = nullptr;
148 }
149
150 void InspectorFrontendHost::addSelfToGlobalObjectInWorld(DOMWrapperWorld& world)
151 {
152     auto& state = *execStateFromPage(world, m_frontendPage);
153     auto& vm = state.vm();
154     JSC::JSLockHolder lock(vm);
155     auto scope = DECLARE_CATCH_SCOPE(vm);
156
157     auto& globalObject = *JSC::jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject());
158     globalObject.putDirect(vm, JSC::Identifier::fromString(&vm, "InspectorFrontendHost"), toJS<IDLInterface<InspectorFrontendHost>>(state, globalObject, *this));
159     if (UNLIKELY(scope.exception()))
160         reportException(&state, scope.exception());
161 }
162
163 void InspectorFrontendHost::loaded()
164 {
165     if (m_client)
166         m_client->frontendLoaded();
167 }
168
169 void InspectorFrontendHost::requestSetDockSide(const String& side)
170 {
171     if (!m_client)
172         return;
173     if (side == "undocked")
174         m_client->requestSetDockSide(InspectorFrontendClient::DockSide::Undocked);
175     else if (side == "right")
176         m_client->requestSetDockSide(InspectorFrontendClient::DockSide::Right);
177     else if (side == "left")
178         m_client->requestSetDockSide(InspectorFrontendClient::DockSide::Left);
179     else if (side == "bottom")
180         m_client->requestSetDockSide(InspectorFrontendClient::DockSide::Bottom);
181 }
182
183 void InspectorFrontendHost::closeWindow()
184 {
185     if (m_client) {
186         m_client->closeWindow();
187         disconnectClient(); // Disconnect from client.
188     }
189 }
190
191 void InspectorFrontendHost::bringToFront()
192 {
193     if (m_client)
194         m_client->bringToFront();
195 }
196
197 void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
198 {
199     if (m_client)
200         m_client->inspectedURLChanged(newURL);
201 }
202
203 void InspectorFrontendHost::setZoomFactor(float zoom)
204 {
205     if (m_frontendPage)
206         m_frontendPage->mainFrame().setPageAndTextZoomFactors(zoom, 1);
207 }
208
209 float InspectorFrontendHost::zoomFactor()
210 {
211     if (m_frontendPage)
212         return m_frontendPage->mainFrame().pageZoomFactor();
213
214     return 1.0;
215 }
216
217 String InspectorFrontendHost::userInterfaceLayoutDirection()
218 {
219     if (m_client && m_client->userInterfaceLayoutDirection() == UserInterfaceLayoutDirection::RTL)
220         return ASCIILiteral("rtl");
221
222     return ASCIILiteral("ltr");
223 }
224
225 void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
226 {
227     if (m_client)
228         m_client->changeAttachedWindowHeight(height);
229 }
230
231 void InspectorFrontendHost::setAttachedWindowWidth(unsigned width)
232 {
233     if (m_client)
234         m_client->changeAttachedWindowWidth(width);
235 }
236
237 void InspectorFrontendHost::startWindowDrag()
238 {
239     if (m_client)
240         m_client->startWindowDrag();
241 }
242
243 void InspectorFrontendHost::moveWindowBy(float x, float y) const
244 {
245     if (m_client)
246         m_client->moveWindowBy(x, y);
247 }
248
249 String InspectorFrontendHost::localizedStringsURL()
250 {
251     return m_client ? m_client->localizedStringsURL() : String();
252 }
253
254 String InspectorFrontendHost::backendCommandsURL()
255 {
256     return m_client ? m_client->backendCommandsURL() : String();
257 }
258
259 String InspectorFrontendHost::debuggableType()
260 {
261     return m_client ? m_client->debuggableType() : String();
262 }
263
264 unsigned InspectorFrontendHost::inspectionLevel()
265 {
266     return m_client ? m_client->inspectionLevel() : 1;
267 }
268
269 String InspectorFrontendHost::platform()
270 {
271 #if PLATFORM(MAC) || PLATFORM(IOS)
272     return ASCIILiteral("mac");
273 #elif OS(WINDOWS)
274     return ASCIILiteral("windows");
275 #elif OS(LINUX)
276     return ASCIILiteral("linux");
277 #elif OS(FREEBSD)
278     return ASCIILiteral("freebsd");
279 #elif OS(OPENBSD)
280     return ASCIILiteral("openbsd");
281 #else
282     return ASCIILiteral("unknown");
283 #endif
284 }
285
286 String InspectorFrontendHost::port()
287 {
288 #if PLATFORM(GTK)
289     return ASCIILiteral("gtk");
290 #else
291     return ASCIILiteral("unknown");
292 #endif
293 }
294
295 void InspectorFrontendHost::copyText(const String& text)
296 {
297     Pasteboard::createForCopyAndPaste()->writePlainText(text, Pasteboard::CannotSmartReplace);
298 }
299
300 void InspectorFrontendHost::killText(const String& text, bool shouldPrependToKillRing, bool shouldStartNewSequence)
301 {
302     if (!m_frontendPage)
303         return;
304
305     Editor& editor = m_frontendPage->focusController().focusedOrMainFrame().editor();
306     editor.setStartNewKillRingSequence(shouldStartNewSequence);
307     Editor::KillRingInsertionMode insertionMode = shouldPrependToKillRing ? Editor::KillRingInsertionMode::PrependText : Editor::KillRingInsertionMode::AppendText;
308     editor.addTextToKillRing(text, insertionMode);
309 }
310
311 void InspectorFrontendHost::openInNewTab(const String& url)
312 {
313     if (WebCore::protocolIsJavaScript(url))
314         return;
315
316     if (m_client)
317         m_client->openInNewTab(url);
318 }
319
320 bool InspectorFrontendHost::canSave()
321 {
322     if (m_client)
323         return m_client->canSave();
324     return false;
325 }
326
327 void InspectorFrontendHost::save(const String& url, const String& content, bool base64Encoded, bool forceSaveAs)
328 {
329     if (m_client)
330         m_client->save(url, content, base64Encoded, forceSaveAs);
331 }
332
333 void InspectorFrontendHost::append(const String& url, const String& content)
334 {
335     if (m_client)
336         m_client->append(url, content);
337 }
338
339 void InspectorFrontendHost::close(const String&)
340 {
341 }
342
343 void InspectorFrontendHost::sendMessageToBackend(const String& message)
344 {
345     if (m_client)
346         m_client->sendMessageToBackend(message);
347 }
348
349 #if ENABLE(CONTEXT_MENUS)
350
351 static void populateContextMenu(Vector<InspectorFrontendHost::ContextMenuItem>&& items, ContextMenu& menu)
352 {
353     for (auto& item : items) {
354         if (item.type == "separator") {
355             menu.appendItem({ SeparatorType, ContextMenuItemTagNoAction, { } });
356             continue;
357         }
358
359         if (item.type == "subMenu" && item.subItems) {
360             ContextMenu subMenu;
361             populateContextMenu(WTFMove(*item.subItems), subMenu);
362
363             menu.appendItem({ SubmenuType, ContextMenuItemTagNoAction, item.label, &subMenu });
364             continue;
365         }
366
367         auto type = item.type == "checkbox" ? CheckableActionType : ActionType;
368         auto action = static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + item.id.value_or(0));
369         ContextMenuItem menuItem = { type, action, item.label };
370         if (item.enabled)
371             menuItem.setEnabled(*item.enabled);
372         if (item.checked)
373             menuItem.setChecked(*item.checked);
374         menu.appendItem(menuItem);
375     }
376 }
377 #endif
378
379 void InspectorFrontendHost::showContextMenu(Event& event, Vector<ContextMenuItem>&& items)
380 {
381 #if ENABLE(CONTEXT_MENUS)
382     ASSERT(m_frontendPage);
383
384     auto& state = *execStateFromPage(debuggerWorld(), m_frontendPage);
385     auto value = state.lexicalGlobalObject()->get(&state, JSC::Identifier::fromString(&state.vm(), "InspectorFrontendAPI"));
386     ASSERT(value);
387     ASSERT(value.isObject());
388     auto* frontendAPIObject = asObject(value);
389     
390     ContextMenu menu;
391     populateContextMenu(WTFMove(items), menu);
392
393     auto menuProvider = FrontendMenuProvider::create(this, { &state, frontendAPIObject }, menu.items());
394     m_menuProvider = menuProvider.ptr();
395     m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
396 #else
397     UNUSED_PARAM(event);
398     UNUSED_PARAM(items);
399 #endif
400 }
401
402 void InspectorFrontendHost::dispatchEventAsContextMenuEvent(Event& event)
403 {
404 #if ENABLE(CONTEXT_MENUS) && USE(ACCESSIBILITY_CONTEXT_MENUS)
405     if (!is<MouseEvent>(event))
406         return;
407
408     auto& mouseEvent = downcast<MouseEvent>(event);
409     IntPoint mousePoint { mouseEvent.clientX(), mouseEvent.clientY() };
410     auto& frame = *downcast<Node>(mouseEvent.target())->document().frame();
411
412     m_frontendPage->contextMenuController().showContextMenuAt(frame, mousePoint);
413 #else
414     UNUSED_PARAM(event);
415 #endif
416 }
417
418 bool InspectorFrontendHost::isUnderTest()
419 {
420     return m_client && m_client->isUnderTest();
421 }
422
423 void InspectorFrontendHost::unbufferedLog(const String& message)
424 {
425     // This is used only for debugging inspector tests.
426     WTFLogAlways("%s", message.utf8().data());
427 }
428
429 void InspectorFrontendHost::beep()
430 {
431     PAL::systemBeep();
432 }
433
434 void InspectorFrontendHost::inspectInspector()
435 {
436     if (m_frontendPage)
437         m_frontendPage->inspectorController().show();
438 }
439
440 } // namespace WebCore