Web Inspector: add TestHarness option to tee all commands to system console
[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 "Event.h"
40 #include "HitTestResult.h"
41 #include "InspectorFrontendClient.h"
42 #include "JSMainThreadExecState.h"
43 #include "MainFrame.h"
44 #include "MouseEvent.h"
45 #include "Node.h"
46 #include "Page.h"
47 #include "Pasteboard.h"
48 #include "ScriptGlobalObject.h"
49 #include "ScriptState.h"
50 #include "Sound.h"
51 #include "UserGestureIndicator.h"
52 #include <bindings/ScriptFunctionCall.h>
53 #include <wtf/StdLibExtras.h>
54
55 using namespace Inspector;
56
57 namespace WebCore {
58
59 #if ENABLE(CONTEXT_MENUS)
60 class FrontendMenuProvider : public ContextMenuProvider {
61 public:
62     static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, Deprecated::ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
63     {
64         return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
65     }
66     
67     void disconnect()
68     {
69         m_frontendApiObject = Deprecated::ScriptObject();
70         m_frontendHost = nullptr;
71     }
72     
73 private:
74     FrontendMenuProvider(InspectorFrontendHost* frontendHost, Deprecated::ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
75         : m_frontendHost(frontendHost)
76         , m_frontendApiObject(frontendApiObject)
77         , m_items(items)
78     {
79     }
80
81     virtual ~FrontendMenuProvider()
82     {
83         contextMenuCleared();
84     }
85     
86     virtual void populateContextMenu(ContextMenu* menu) override
87     {
88         for (size_t i = 0; i < m_items.size(); ++i)
89             menu->appendItem(m_items[i]);
90     }
91     
92     virtual void contextMenuItemSelected(ContextMenuItem* item) override
93     {
94         if (m_frontendHost) {
95             UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
96             int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
97
98             Deprecated::ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected", WebCore::functionCallHandlerFromAnyThread);
99             function.appendArgument(itemNumber);
100             function.call();
101         }
102     }
103     
104     virtual void contextMenuCleared() override
105     {
106         if (m_frontendHost) {
107             Deprecated::ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared", WebCore::functionCallHandlerFromAnyThread);
108             function.call();
109
110             m_frontendHost->m_menuProvider = nullptr;
111         }
112         m_items.clear();
113     }
114
115     InspectorFrontendHost* m_frontendHost;
116     Deprecated::ScriptObject m_frontendApiObject;
117     Vector<ContextMenuItem> m_items;
118 };
119 #endif
120
121 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
122     : m_client(client)
123     , m_frontendPage(frontendPage)
124 #if ENABLE(CONTEXT_MENUS)
125     , m_menuProvider(nullptr)
126 #endif
127 {
128 }
129
130 InspectorFrontendHost::~InspectorFrontendHost()
131 {
132     ASSERT(!m_client);
133 }
134
135 void InspectorFrontendHost::disconnectClient()
136 {
137     m_client = nullptr;
138 #if ENABLE(CONTEXT_MENUS)
139     if (m_menuProvider)
140         m_menuProvider->disconnect();
141 #endif
142     m_frontendPage = nullptr;
143 }
144
145 void InspectorFrontendHost::loaded()
146 {
147     if (m_client)
148         m_client->frontendLoaded();
149 }
150
151 void InspectorFrontendHost::requestSetDockSide(const String& side)
152 {
153     if (!m_client)
154         return;
155     if (side == "undocked")
156         m_client->requestSetDockSide(InspectorFrontendClient::DockSide::Undocked);
157     else if (side == "right")
158         m_client->requestSetDockSide(InspectorFrontendClient::DockSide::Right);
159     else if (side == "bottom")
160         m_client->requestSetDockSide(InspectorFrontendClient::DockSide::Bottom);
161 }
162
163 void InspectorFrontendHost::closeWindow()
164 {
165     if (m_client) {
166         m_client->closeWindow();
167         disconnectClient(); // Disconnect from client.
168     }
169 }
170
171 void InspectorFrontendHost::bringToFront()
172 {
173     if (m_client)
174         m_client->bringToFront();
175 }
176
177 void InspectorFrontendHost::setZoomFactor(float zoom)
178 {
179     m_frontendPage->mainFrame().setPageAndTextZoomFactors(zoom, 1);
180 }
181
182 void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
183 {
184     if (m_client)
185         m_client->inspectedURLChanged(newURL);
186 }
187
188 void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
189 {
190     if (m_client)
191         m_client->changeAttachedWindowHeight(height);
192 }
193
194 void InspectorFrontendHost::setAttachedWindowWidth(unsigned width)
195 {
196     if (m_client)
197         m_client->changeAttachedWindowWidth(width);
198 }
199
200 void InspectorFrontendHost::setToolbarHeight(unsigned height)
201 {
202     if (m_client)
203         m_client->setToolbarHeight(height);
204 }
205
206 void InspectorFrontendHost::startWindowDrag()
207 {
208     if (m_client)
209         m_client->startWindowDrag();
210 }
211
212 void InspectorFrontendHost::moveWindowBy(float x, float y) const
213 {
214     if (m_client)
215         m_client->moveWindowBy(x, y);
216 }
217
218 String InspectorFrontendHost::localizedStringsURL()
219 {
220     return m_client ? m_client->localizedStringsURL() : "";
221 }
222
223 String InspectorFrontendHost::debuggableType()
224 {
225     return ASCIILiteral("web");
226 }
227
228 void InspectorFrontendHost::copyText(const String& text)
229 {
230     Pasteboard::createForCopyAndPaste()->writePlainText(text, Pasteboard::CannotSmartReplace);
231 }
232
233 void InspectorFrontendHost::openInNewTab(const String& url)
234 {
235     if (m_client)
236         m_client->openInNewTab(url);
237 }
238
239 bool InspectorFrontendHost::canSave()
240 {
241     if (m_client)
242         return m_client->canSave();
243     return false;
244 }
245
246 void InspectorFrontendHost::save(const String& url, const String& content, bool base64Encoded, bool forceSaveAs)
247 {
248     if (m_client)
249         m_client->save(url, content, base64Encoded, forceSaveAs);
250 }
251
252 void InspectorFrontendHost::append(const String& url, const String& content)
253 {
254     if (m_client)
255         m_client->append(url, content);
256 }
257
258 void InspectorFrontendHost::close(const String&)
259 {
260 }
261
262 void InspectorFrontendHost::sendMessageToBackend(const String& message)
263 {
264     if (m_client)
265         m_client->sendMessageToBackend(message);
266 }
267
268 #if ENABLE(CONTEXT_MENUS)
269 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
270 {
271     if (!event)
272         return;
273
274     ASSERT(m_frontendPage);
275     JSC::ExecState* frontendExecState = execStateFromPage(debuggerWorld(), m_frontendPage);
276     Deprecated::ScriptObject frontendApiObject;
277     if (!ScriptGlobalObject::get(frontendExecState, "InspectorFrontendAPI", frontendApiObject)) {
278         ASSERT_NOT_REACHED();
279         return;
280     }
281     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
282     m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
283     m_menuProvider = menuProvider.get();
284 }
285 #endif
286
287 void InspectorFrontendHost::dispatchEventAsContextMenuEvent(Event* event)
288 {
289 #if ENABLE(CONTEXT_MENUS) && USE(ACCESSIBILITY_CONTEXT_MENUS)
290     if (!is<MouseEvent>(event))
291         return;
292
293     Frame* frame = event->target()->toNode()->document().frame();
294     MouseEvent& mouseEvent = downcast<MouseEvent>(*event);
295     IntPoint mousePoint = IntPoint(mouseEvent.clientX(), mouseEvent.clientY());
296
297     m_frontendPage->contextMenuController().showContextMenuAt(frame, mousePoint);
298 #else
299     UNUSED_PARAM(event);
300 #endif
301 }
302
303 bool InspectorFrontendHost::isUnderTest()
304 {
305     return m_client && m_client->isUnderTest();
306 }
307
308 void InspectorFrontendHost::unbufferedLog(const String& message)
309 {
310     // This is used only for debugging inspector tests.
311     WTFLogAlways("%s", message.utf8().data());
312 }
313
314 void InspectorFrontendHost::beep()
315 {
316     systemBeep();
317 }
318
319 bool InspectorFrontendHost::canSaveAs()
320 {
321     return false;
322 }
323
324 bool InspectorFrontendHost::canInspectWorkers()
325 {
326     return false;
327 }
328
329 } // namespace WebCore