Web Inspector: Remove unused InspectorFrontendHost methods
[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 String InspectorFrontendHost::platform()
229 {
230 #if PLATFORM(MAC) || PLATFORM(IOS)
231     return ASCIILiteral("mac");
232 #elif OS(WINDOWS)
233     return ASCIILiteral("windows");
234 #elif OS(LINUX)
235     return ASCIILiteral("linux");
236 #elif OS(FREEBSD)
237     return ASCIILiteral("freebsd");
238 #elif OS(OPENBSD)
239     return ASCIILiteral("openbsd");
240 #elif OS(SOLARIS)
241     return ASCIILiteral("solaris");
242 #else
243     return ASCIILiteral("unknown");
244 #endif
245 }
246
247 String InspectorFrontendHost::port()
248 {
249 #if PLATFORM(GTK)
250     return ASCIILiteral("gtk");
251 #elif PLATFORM(EFL)
252     return ASCIILiteral("efl");
253 #else
254     return ASCIILiteral("unknown");
255 #endif
256 }
257
258 void InspectorFrontendHost::copyText(const String& text)
259 {
260     Pasteboard::createForCopyAndPaste()->writePlainText(text, Pasteboard::CannotSmartReplace);
261 }
262
263 void InspectorFrontendHost::openInNewTab(const String& url)
264 {
265     if (m_client)
266         m_client->openInNewTab(url);
267 }
268
269 bool InspectorFrontendHost::canSave()
270 {
271     if (m_client)
272         return m_client->canSave();
273     return false;
274 }
275
276 void InspectorFrontendHost::save(const String& url, const String& content, bool base64Encoded, bool forceSaveAs)
277 {
278     if (m_client)
279         m_client->save(url, content, base64Encoded, forceSaveAs);
280 }
281
282 void InspectorFrontendHost::append(const String& url, const String& content)
283 {
284     if (m_client)
285         m_client->append(url, content);
286 }
287
288 void InspectorFrontendHost::close(const String&)
289 {
290 }
291
292 void InspectorFrontendHost::sendMessageToBackend(const String& message)
293 {
294     if (m_client)
295         m_client->sendMessageToBackend(message);
296 }
297
298 #if ENABLE(CONTEXT_MENUS)
299 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
300 {
301     if (!event)
302         return;
303
304     ASSERT(m_frontendPage);
305     JSC::ExecState* frontendExecState = execStateFromPage(debuggerWorld(), m_frontendPage);
306     Deprecated::ScriptObject frontendApiObject;
307     if (!ScriptGlobalObject::get(frontendExecState, "InspectorFrontendAPI", frontendApiObject)) {
308         ASSERT_NOT_REACHED();
309         return;
310     }
311     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
312     m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
313     m_menuProvider = menuProvider.get();
314 }
315 #endif
316
317 void InspectorFrontendHost::dispatchEventAsContextMenuEvent(Event* event)
318 {
319 #if ENABLE(CONTEXT_MENUS) && USE(ACCESSIBILITY_CONTEXT_MENUS)
320     if (!is<MouseEvent>(event))
321         return;
322
323     Frame* frame = event->target()->toNode()->document().frame();
324     MouseEvent& mouseEvent = downcast<MouseEvent>(*event);
325     IntPoint mousePoint = IntPoint(mouseEvent.clientX(), mouseEvent.clientY());
326
327     m_frontendPage->contextMenuController().showContextMenuAt(frame, mousePoint);
328 #else
329     UNUSED_PARAM(event);
330 #endif
331 }
332
333 bool InspectorFrontendHost::isUnderTest()
334 {
335     return m_client && m_client->isUnderTest();
336 }
337
338 void InspectorFrontendHost::unbufferedLog(const String& message)
339 {
340     // This is used only for debugging inspector tests.
341     WTFLogAlways("%s", message.utf8().data());
342 }
343
344 void InspectorFrontendHost::beep()
345 {
346     systemBeep();
347 }
348
349 } // namespace WebCore