REGRESSION(r220278): Web Inspector: ContextMenu items are not getting triggered
[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 using namespace Inspector;
61
62 namespace WebCore {
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 #elif OS(SOLARIS)
282     return ASCIILiteral("solaris");
283 #else
284     return ASCIILiteral("unknown");
285 #endif
286 }
287
288 String InspectorFrontendHost::port()
289 {
290 #if PLATFORM(GTK)
291     return ASCIILiteral("gtk");
292 #else
293     return ASCIILiteral("unknown");
294 #endif
295 }
296
297 void InspectorFrontendHost::copyText(const String& text)
298 {
299     Pasteboard::createForCopyAndPaste()->writePlainText(text, Pasteboard::CannotSmartReplace);
300 }
301
302 void InspectorFrontendHost::killText(const String& text, bool shouldPrependToKillRing, bool shouldStartNewSequence)
303 {
304     if (!m_frontendPage)
305         return;
306
307     Editor& editor = m_frontendPage->focusController().focusedOrMainFrame().editor();
308     editor.setStartNewKillRingSequence(shouldStartNewSequence);
309     Editor::KillRingInsertionMode insertionMode = shouldPrependToKillRing ? Editor::KillRingInsertionMode::PrependText : Editor::KillRingInsertionMode::AppendText;
310     editor.addTextToKillRing(text, insertionMode);
311 }
312
313 void InspectorFrontendHost::openInNewTab(const String& url)
314 {
315     if (WebCore::protocolIsJavaScript(url))
316         return;
317
318     if (m_client)
319         m_client->openInNewTab(url);
320 }
321
322 bool InspectorFrontendHost::canSave()
323 {
324     if (m_client)
325         return m_client->canSave();
326     return false;
327 }
328
329 void InspectorFrontendHost::save(const String& url, const String& content, bool base64Encoded, bool forceSaveAs)
330 {
331     if (m_client)
332         m_client->save(url, content, base64Encoded, forceSaveAs);
333 }
334
335 void InspectorFrontendHost::append(const String& url, const String& content)
336 {
337     if (m_client)
338         m_client->append(url, content);
339 }
340
341 void InspectorFrontendHost::close(const String&)
342 {
343 }
344
345 void InspectorFrontendHost::sendMessageToBackend(const String& message)
346 {
347     if (m_client)
348         m_client->sendMessageToBackend(message);
349 }
350
351 #if ENABLE(CONTEXT_MENUS)
352
353 static void populateContextMenu(Vector<InspectorFrontendHost::ContextMenuItem>&& items, ContextMenu& menu)
354 {
355     for (auto& item : items) {
356         if (item.type == "separator") {
357             menu.appendItem({ SeparatorType, ContextMenuItemTagNoAction, { } });
358             continue;
359         }
360
361         if (item.type == "subMenu" && item.subItems) {
362             ContextMenu subMenu;
363             populateContextMenu(WTFMove(*item.subItems), subMenu);
364
365             menu.appendItem({ SubmenuType, ContextMenuItemTagNoAction, item.label, &subMenu });
366             continue;
367         }
368
369         auto type = item.type == "checkbox" ? CheckableActionType : ActionType;
370         auto action = static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + item.id.value_or(0));
371         ContextMenuItem menuItem = { type, action, item.label };
372         if (item.enabled)
373             menuItem.setEnabled(*item.enabled);
374         if (item.checked)
375             menuItem.setChecked(*item.checked);
376         menu.appendItem(menuItem);
377     }
378 }
379 #endif
380
381 void InspectorFrontendHost::showContextMenu(Event& event, Vector<ContextMenuItem>&& items)
382 {
383 #if ENABLE(CONTEXT_MENUS)
384     ASSERT(m_frontendPage);
385
386     auto& state = *execStateFromPage(debuggerWorld(), m_frontendPage);
387     auto value = state.lexicalGlobalObject()->get(&state, JSC::Identifier::fromString(&state.vm(), "InspectorFrontendAPI"));
388     ASSERT(value);
389     ASSERT(value.isObject());
390     auto* frontendAPIObject = asObject(value);
391     
392     ContextMenu menu;
393     populateContextMenu(WTFMove(items), menu);
394
395     auto menuProvider = FrontendMenuProvider::create(this, { &state, frontendAPIObject }, menu.items());
396     m_menuProvider = menuProvider.ptr();
397     m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
398 #else
399     UNUSED_PARAM(event);
400     UNUSED_PARAM(items);
401 #endif
402 }
403
404 void InspectorFrontendHost::dispatchEventAsContextMenuEvent(Event& event)
405 {
406 #if ENABLE(CONTEXT_MENUS) && USE(ACCESSIBILITY_CONTEXT_MENUS)
407     if (!is<MouseEvent>(event))
408         return;
409
410     auto& mouseEvent = downcast<MouseEvent>(event);
411     IntPoint mousePoint { mouseEvent.clientX(), mouseEvent.clientY() };
412     auto& frame = *mouseEvent.target()->toNode()->document().frame();
413
414     m_frontendPage->contextMenuController().showContextMenuAt(frame, mousePoint);
415 #else
416     UNUSED_PARAM(event);
417 #endif
418 }
419
420 bool InspectorFrontendHost::isUnderTest()
421 {
422     return m_client && m_client->isUnderTest();
423 }
424
425 void InspectorFrontendHost::unbufferedLog(const String& message)
426 {
427     // This is used only for debugging inspector tests.
428     WTFLogAlways("%s", message.utf8().data());
429 }
430
431 void InspectorFrontendHost::beep()
432 {
433     PAL::systemBeep();
434 }
435
436 void InspectorFrontendHost::inspectInspector()
437 {
438     if (m_frontendPage)
439         m_frontendPage->inspectorController().show();
440 }
441
442 } // namespace WebCore