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