Remove EFL from WebCore
[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 "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(ProcessingUserGesture);
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 String InspectorFrontendHost::userInterfaceLayoutDirection()
200 {
201     if (m_client && m_client->userInterfaceLayoutDirection() == UserInterfaceLayoutDirection::RTL)
202         return ASCIILiteral("rtl");
203
204     return ASCIILiteral("ltr");
205 }
206
207 void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
208 {
209     if (m_client)
210         m_client->changeAttachedWindowHeight(height);
211 }
212
213 void InspectorFrontendHost::setAttachedWindowWidth(unsigned width)
214 {
215     if (m_client)
216         m_client->changeAttachedWindowWidth(width);
217 }
218
219 void InspectorFrontendHost::startWindowDrag()
220 {
221     if (m_client)
222         m_client->startWindowDrag();
223 }
224
225 void InspectorFrontendHost::moveWindowBy(float x, float y) const
226 {
227     if (m_client)
228         m_client->moveWindowBy(x, y);
229 }
230
231 String InspectorFrontendHost::localizedStringsURL()
232 {
233     return m_client ? m_client->localizedStringsURL() : String();
234 }
235
236 String InspectorFrontendHost::backendCommandsURL()
237 {
238     return m_client ? m_client->backendCommandsURL() : String();
239 }
240
241 String InspectorFrontendHost::debuggableType()
242 {
243     return m_client ? m_client->debuggableType() : String();
244 }
245
246 unsigned InspectorFrontendHost::inspectionLevel()
247 {
248     return m_client ? m_client->inspectionLevel() : 1;
249 }
250
251 String InspectorFrontendHost::platform()
252 {
253 #if PLATFORM(MAC) || PLATFORM(IOS)
254     return ASCIILiteral("mac");
255 #elif OS(WINDOWS)
256     return ASCIILiteral("windows");
257 #elif OS(LINUX)
258     return ASCIILiteral("linux");
259 #elif OS(FREEBSD)
260     return ASCIILiteral("freebsd");
261 #elif OS(OPENBSD)
262     return ASCIILiteral("openbsd");
263 #elif OS(SOLARIS)
264     return ASCIILiteral("solaris");
265 #else
266     return ASCIILiteral("unknown");
267 #endif
268 }
269
270 String InspectorFrontendHost::port()
271 {
272 #if PLATFORM(GTK)
273     return ASCIILiteral("gtk");
274 #else
275     return ASCIILiteral("unknown");
276 #endif
277 }
278
279 void InspectorFrontendHost::copyText(const String& text)
280 {
281     Pasteboard::createForCopyAndPaste()->writePlainText(text, Pasteboard::CannotSmartReplace);
282 }
283
284 void InspectorFrontendHost::killText(const String& text, bool shouldPrependToKillRing, bool shouldStartNewSequence)
285 {
286     if (!m_frontendPage)
287         return;
288
289     Editor& editor = m_frontendPage->focusController().focusedOrMainFrame().editor();
290     editor.setStartNewKillRingSequence(shouldStartNewSequence);
291     Editor::KillRingInsertionMode insertionMode = shouldPrependToKillRing ? Editor::KillRingInsertionMode::PrependText : Editor::KillRingInsertionMode::AppendText;
292     editor.addTextToKillRing(text, insertionMode);
293 }
294
295 void InspectorFrontendHost::openInNewTab(const String& url)
296 {
297     if (WebCore::protocolIsJavaScript(url))
298         return;
299
300     if (m_client)
301         m_client->openInNewTab(url);
302 }
303
304 bool InspectorFrontendHost::canSave()
305 {
306     if (m_client)
307         return m_client->canSave();
308     return false;
309 }
310
311 void InspectorFrontendHost::save(const String& url, const String& content, bool base64Encoded, bool forceSaveAs)
312 {
313     if (m_client)
314         m_client->save(url, content, base64Encoded, forceSaveAs);
315 }
316
317 void InspectorFrontendHost::append(const String& url, const String& content)
318 {
319     if (m_client)
320         m_client->append(url, content);
321 }
322
323 void InspectorFrontendHost::close(const String&)
324 {
325 }
326
327 void InspectorFrontendHost::sendMessageToBackend(const String& message)
328 {
329     if (m_client)
330         m_client->sendMessageToBackend(message);
331 }
332
333 #if ENABLE(CONTEXT_MENUS)
334
335 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
336 {
337     if (!event)
338         return;
339
340     ASSERT(m_frontendPage);
341     auto& state = *execStateFromPage(debuggerWorld(), m_frontendPage);
342     JSC::JSObject* frontendApiObject;
343     if (!ScriptGlobalObject::get(state, "InspectorFrontendAPI", frontendApiObject)) {
344         ASSERT_NOT_REACHED();
345         return;
346     }
347     auto menuProvider = FrontendMenuProvider::create(this, { &state, frontendApiObject }, items);
348     m_menuProvider = menuProvider.ptr();
349     m_frontendPage->contextMenuController().showContextMenu(*event, menuProvider);
350 }
351
352 #endif
353
354 void InspectorFrontendHost::dispatchEventAsContextMenuEvent(Event* event)
355 {
356 #if ENABLE(CONTEXT_MENUS) && USE(ACCESSIBILITY_CONTEXT_MENUS)
357     if (!is<MouseEvent>(event))
358         return;
359
360     Frame* frame = event->target()->toNode()->document().frame();
361     MouseEvent& mouseEvent = downcast<MouseEvent>(*event);
362     IntPoint mousePoint = IntPoint(mouseEvent.clientX(), mouseEvent.clientY());
363
364     m_frontendPage->contextMenuController().showContextMenuAt(*frame, mousePoint);
365 #else
366     UNUSED_PARAM(event);
367 #endif
368 }
369
370 bool InspectorFrontendHost::isUnderTest()
371 {
372     return m_client && m_client->isUnderTest();
373 }
374
375 void InspectorFrontendHost::unbufferedLog(const String& message)
376 {
377     // This is used only for debugging inspector tests.
378     WTFLogAlways("%s", message.utf8().data());
379 }
380
381 void InspectorFrontendHost::beep()
382 {
383     systemBeep();
384 }
385
386 } // namespace WebCore