Remove more uses of Deprecated::ScriptXXX
[WebKit-https.git] / Source / WebCore / inspector / InspectorFrontendHost.cpp
1 /*
2  * Copyright (C) 2007, 2008 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 "InspectorFrontendClient.h"
44 #include "JSMainThreadExecState.h"
45 #include "MainFrame.h"
46 #include "MouseEvent.h"
47 #include "Node.h"
48 #include "Page.h"
49 #include "Pasteboard.h"
50 #include "ScriptGlobalObject.h"
51 #include "ScriptState.h"
52 #include "Sound.h"
53 #include "UserGestureIndicator.h"
54 #include <bindings/ScriptFunctionCall.h>
55 #include <wtf/StdLibExtras.h>
56
57 using namespace Inspector;
58
59 namespace WebCore {
60
61 #if ENABLE(CONTEXT_MENUS)
62 class FrontendMenuProvider : public ContextMenuProvider {
63 public:
64     static Ref<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, Deprecated::ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
65     {
66         return adoptRef(*new FrontendMenuProvider(frontendHost, frontendApiObject, items));
67     }
68     
69     void disconnect()
70     {
71         m_frontendApiObject = { };
72         m_frontendHost = nullptr;
73     }
74     
75 private:
76     FrontendMenuProvider(InspectorFrontendHost* frontendHost, Deprecated::ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
77         : m_frontendHost(frontendHost)
78         , m_frontendApiObject(frontendApiObject)
79         , m_items(items)
80     {
81     }
82
83     virtual ~FrontendMenuProvider()
84     {
85         contextMenuCleared();
86     }
87     
88     void populateContextMenu(ContextMenu* menu) override
89     {
90         for (auto& item : m_items)
91             menu->appendItem(item);
92     }
93     
94     void contextMenuItemSelected(ContextMenuAction action, const String&) override
95     {
96         if (m_frontendHost) {
97             UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
98             int itemNumber = action - ContextMenuItemBaseCustomTag;
99
100             Deprecated::ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected", WebCore::functionCallHandlerFromAnyThread);
101             function.appendArgument(itemNumber);
102             function.call();
103         }
104     }
105     
106     void contextMenuCleared() override
107     {
108         if (m_frontendHost) {
109             Deprecated::ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared", WebCore::functionCallHandlerFromAnyThread);
110             function.call();
111
112             m_frontendHost->m_menuProvider = nullptr;
113         }
114         m_items.clear();
115     }
116
117     InspectorFrontendHost* m_frontendHost;
118     Deprecated::ScriptObject m_frontendApiObject;
119     Vector<ContextMenuItem> m_items;
120 };
121 #endif
122
123 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
124     : m_client(client)
125     , m_frontendPage(frontendPage)
126 #if ENABLE(CONTEXT_MENUS)
127     , m_menuProvider(nullptr)
128 #endif
129 {
130 }
131
132 InspectorFrontendHost::~InspectorFrontendHost()
133 {
134     ASSERT(!m_client);
135 }
136
137 void InspectorFrontendHost::disconnectClient()
138 {
139     m_client = nullptr;
140 #if ENABLE(CONTEXT_MENUS)
141     if (m_menuProvider)
142         m_menuProvider->disconnect();
143 #endif
144     m_frontendPage = nullptr;
145 }
146
147 void InspectorFrontendHost::loaded()
148 {
149     if (m_client)
150         m_client->frontendLoaded();
151 }
152
153 void InspectorFrontendHost::requestSetDockSide(const String& side)
154 {
155     if (!m_client)
156         return;
157     if (side == "undocked")
158         m_client->requestSetDockSide(InspectorFrontendClient::DockSide::Undocked);
159     else if (side == "right")
160         m_client->requestSetDockSide(InspectorFrontendClient::DockSide::Right);
161     else if (side == "bottom")
162         m_client->requestSetDockSide(InspectorFrontendClient::DockSide::Bottom);
163 }
164
165 void InspectorFrontendHost::closeWindow()
166 {
167     if (m_client) {
168         m_client->closeWindow();
169         disconnectClient(); // Disconnect from client.
170     }
171 }
172
173 void InspectorFrontendHost::bringToFront()
174 {
175     if (m_client)
176         m_client->bringToFront();
177 }
178
179 void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
180 {
181     if (m_client)
182         m_client->inspectedURLChanged(newURL);
183 }
184
185 void InspectorFrontendHost::setZoomFactor(float zoom)
186 {
187     if (m_frontendPage)
188         m_frontendPage->mainFrame().setPageAndTextZoomFactors(zoom, 1);
189 }
190
191 float InspectorFrontendHost::zoomFactor()
192 {
193     if (m_frontendPage)
194         return m_frontendPage->mainFrame().pageZoomFactor();
195
196     return 1.0;
197 }
198
199 void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
200 {
201     if (m_client)
202         m_client->changeAttachedWindowHeight(height);
203 }
204
205 void InspectorFrontendHost::setAttachedWindowWidth(unsigned width)
206 {
207     if (m_client)
208         m_client->changeAttachedWindowWidth(width);
209 }
210
211 void InspectorFrontendHost::startWindowDrag()
212 {
213     if (m_client)
214         m_client->startWindowDrag();
215 }
216
217 void InspectorFrontendHost::moveWindowBy(float x, float y) const
218 {
219     if (m_client)
220         m_client->moveWindowBy(x, y);
221 }
222
223 String InspectorFrontendHost::localizedStringsURL()
224 {
225     return m_client ? m_client->localizedStringsURL() : "";
226 }
227
228 String InspectorFrontendHost::debuggableType()
229 {
230     return ASCIILiteral("web");
231 }
232
233 unsigned InspectorFrontendHost::inspectionLevel()
234 {
235     return m_client ? m_client->inspectionLevel() : 1;
236 }
237
238 String InspectorFrontendHost::platform()
239 {
240 #if PLATFORM(MAC) || PLATFORM(IOS)
241     return ASCIILiteral("mac");
242 #elif OS(WINDOWS)
243     return ASCIILiteral("windows");
244 #elif OS(LINUX)
245     return ASCIILiteral("linux");
246 #elif OS(FREEBSD)
247     return ASCIILiteral("freebsd");
248 #elif OS(OPENBSD)
249     return ASCIILiteral("openbsd");
250 #elif OS(SOLARIS)
251     return ASCIILiteral("solaris");
252 #else
253     return ASCIILiteral("unknown");
254 #endif
255 }
256
257 String InspectorFrontendHost::port()
258 {
259 #if PLATFORM(GTK)
260     return ASCIILiteral("gtk");
261 #elif PLATFORM(EFL)
262     return ASCIILiteral("efl");
263 #else
264     return ASCIILiteral("unknown");
265 #endif
266 }
267
268 void InspectorFrontendHost::copyText(const String& text)
269 {
270     Pasteboard::createForCopyAndPaste()->writePlainText(text, Pasteboard::CannotSmartReplace);
271 }
272
273 void InspectorFrontendHost::killText(const String& text, bool shouldPrependToKillRing, bool shouldStartNewSequence)
274 {
275     if (!m_frontendPage)
276         return;
277
278     Editor& editor = m_frontendPage->focusController().focusedOrMainFrame().editor();
279     editor.setStartNewKillRingSequence(shouldStartNewSequence);
280     Editor::KillRingInsertionMode insertionMode = shouldPrependToKillRing ? Editor::KillRingInsertionMode::PrependText : Editor::KillRingInsertionMode::AppendText;
281     editor.addTextToKillRing(text, insertionMode);
282 }
283
284 void InspectorFrontendHost::openInNewTab(const String& url)
285 {
286     if (m_client)
287         m_client->openInNewTab(url);
288 }
289
290 bool InspectorFrontendHost::canSave()
291 {
292     if (m_client)
293         return m_client->canSave();
294     return false;
295 }
296
297 void InspectorFrontendHost::save(const String& url, const String& content, bool base64Encoded, bool forceSaveAs)
298 {
299     if (m_client)
300         m_client->save(url, content, base64Encoded, forceSaveAs);
301 }
302
303 void InspectorFrontendHost::append(const String& url, const String& content)
304 {
305     if (m_client)
306         m_client->append(url, content);
307 }
308
309 void InspectorFrontendHost::close(const String&)
310 {
311 }
312
313 void InspectorFrontendHost::sendMessageToBackend(const String& message)
314 {
315     if (m_client)
316         m_client->sendMessageToBackend(message);
317 }
318
319 #if ENABLE(CONTEXT_MENUS)
320
321 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
322 {
323     if (!event)
324         return;
325
326     ASSERT(m_frontendPage);
327     auto& state = *execStateFromPage(debuggerWorld(), m_frontendPage);
328     JSC::JSObject* frontendApiObject;
329     if (!ScriptGlobalObject::get(state, "InspectorFrontendAPI", frontendApiObject)) {
330         ASSERT_NOT_REACHED();
331         return;
332     }
333     auto menuProvider = FrontendMenuProvider::create(this, { &state, frontendApiObject }, items);
334     m_frontendPage->contextMenuController().showContextMenu(event, menuProvider.ptr());
335     m_menuProvider = menuProvider.ptr();
336 }
337
338 #endif
339
340 void InspectorFrontendHost::dispatchEventAsContextMenuEvent(Event* event)
341 {
342 #if ENABLE(CONTEXT_MENUS) && USE(ACCESSIBILITY_CONTEXT_MENUS)
343     if (!is<MouseEvent>(event))
344         return;
345
346     Frame* frame = event->target()->toNode()->document().frame();
347     MouseEvent& mouseEvent = downcast<MouseEvent>(*event);
348     IntPoint mousePoint = IntPoint(mouseEvent.clientX(), mouseEvent.clientY());
349
350     m_frontendPage->contextMenuController().showContextMenuAt(frame, mousePoint);
351 #else
352     UNUSED_PARAM(event);
353 #endif
354 }
355
356 bool InspectorFrontendHost::isUnderTest()
357 {
358     return m_client && m_client->isUnderTest();
359 }
360
361 void InspectorFrontendHost::unbufferedLog(const String& message)
362 {
363     // This is used only for debugging inspector tests.
364     WTFLogAlways("%s", message.utf8().data());
365 }
366
367 void InspectorFrontendHost::beep()
368 {
369     systemBeep();
370 }
371
372 } // namespace WebCore