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