Remove ENABLE(JAVASCRIPT_DEBUGGER) guards
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / WebInspector.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebInspector.h"
28
29 #if ENABLE(INSPECTOR)
30 #include "WebFrame.h"
31 #include "WebInspectorFrontendClient.h"
32 #include "WebInspectorProxyMessages.h"
33 #include "WebPage.h"
34 #include "WebPageCreationParameters.h"
35 #include "WebProcess.h"
36 #include <WebCore/InspectorController.h>
37 #include <WebCore/InspectorFrontendClient.h>
38 #include <WebCore/MainFrame.h>
39 #include <WebCore/Page.h>
40 #include <WebCore/ScriptController.h>
41 #include <inspector/InspectorAgentBase.h>
42 #include <bindings/ScriptValue.h>
43 #include <wtf/text/StringConcatenate.h>
44
45 using namespace WebCore;
46
47 namespace WebKit {
48
49 PassRefPtr<WebInspector> WebInspector::create(WebPage* page, InspectorFrontendChannel* frontendChannel)
50 {
51     return adoptRef(new WebInspector(page, frontendChannel));
52 }
53
54 WebInspector::WebInspector(WebPage* page, InspectorFrontendChannel* frontendChannel)
55     : m_page(page)
56     , m_inspectorPage(0)
57     , m_frontendClient(0)
58     , m_frontendChannel(frontendChannel)
59 #if PLATFORM(MAC)
60     , m_hasLocalizedStringsURL(false)
61 #endif
62 #if ENABLE(INSPECTOR_SERVER)
63     , m_remoteFrontendConnected(false)
64 #endif
65 {
66 }
67
68 // Called from WebInspectorClient
69 WebPage* WebInspector::createInspectorPage()
70 {
71     if (!m_page)
72         return 0;
73
74     ASSERT(!m_inspectorPage);
75     ASSERT(!m_frontendClient);
76
77     uint64_t inspectorPageID = 0;
78     WebPageCreationParameters parameters;
79
80     if (!WebProcess::shared().parentProcessConnection()->sendSync(Messages::WebInspectorProxy::CreateInspectorPage(),
81             Messages::WebInspectorProxy::CreateInspectorPage::Reply(inspectorPageID, parameters),
82             m_page->pageID(), std::chrono::milliseconds::max())) {
83         return 0;
84     }
85
86     if (!inspectorPageID)
87         return 0;
88
89     WebProcess::shared().createWebPage(inspectorPageID, parameters);
90     m_inspectorPage = WebProcess::shared().webPage(inspectorPageID);
91     ASSERT(m_inspectorPage);
92
93     auto frontendClient = std::make_unique<WebInspectorFrontendClient>(m_page, m_inspectorPage);
94     m_frontendClient = frontendClient.get();
95     m_inspectorPage->corePage()->inspectorController().setInspectorFrontendClient(std::move(frontendClient));
96     return m_inspectorPage;
97 }
98
99 void WebInspector::destroyInspectorPage()
100 {
101     m_inspectorPage = 0;
102     m_frontendClient = 0;
103     m_frontendChannel = 0;
104 }
105
106 // Called from WebInspectorFrontendClient
107 void WebInspector::didClose()
108 {
109     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::DidClose(), m_page->pageID());
110     destroyInspectorPage();
111 }
112
113 void WebInspector::bringToFront()
114 {
115     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::BringToFront(), m_page->pageID());
116 }
117
118 void WebInspector::inspectedURLChanged(const String& urlString)
119 {
120     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::InspectedURLChanged(urlString), m_page->pageID());
121 }
122
123 void WebInspector::save(const String& filename, const String& content, bool base64Encoded, bool forceSaveAs)
124 {
125     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::Save(filename, content, base64Encoded, forceSaveAs), m_page->pageID());
126 }
127
128 void WebInspector::append(const String& filename, const String& content)
129 {
130     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::Append(filename, content), m_page->pageID());
131 }
132
133 void WebInspector::attachBottom()
134 {
135     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::AttachBottom(), m_page->pageID());
136 }
137
138 void WebInspector::attachRight()
139 {
140     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::AttachRight(), m_page->pageID());
141 }
142
143 void WebInspector::detach()
144 {
145     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::Detach(), m_page->pageID());
146 }
147
148 void WebInspector::setAttachedWindowHeight(unsigned height)
149 {
150     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::SetAttachedWindowHeight(height), m_page->pageID());
151 }
152
153 void WebInspector::setAttachedWindowWidth(unsigned width)
154 {
155     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::SetAttachedWindowWidth(width), m_page->pageID());
156 }
157
158 void WebInspector::setToolbarHeight(unsigned height)
159 {
160     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::SetToolbarHeight(height), m_page->pageID());
161 }
162
163 // Called by WebInspector messages
164 void WebInspector::show()
165 {
166     m_page->corePage()->inspectorController().show();
167 }
168
169 void WebInspector::close()
170 {
171     m_page->corePage()->inspectorController().close();
172 }
173
174 void WebInspector::didSave(const String& url)
175 {
176     ASSERT(m_inspectorPage);
177     m_inspectorPage->corePage()->mainFrame().script().executeScript(makeString("InspectorFrontendAPI.savedURL(\"", url, "\")"));
178 }
179
180 void WebInspector::didAppend(const String& url)
181 {
182     ASSERT(m_inspectorPage);
183     m_inspectorPage->corePage()->mainFrame().script().executeScript(makeString("InspectorFrontendAPI.appendedToURL(\"", url, "\")"));
184 }
185
186 void WebInspector::attachedBottom()
187 {
188     if (m_frontendClient)
189         m_frontendClient->setAttachedWindow(InspectorFrontendClient::DOCKED_TO_BOTTOM);
190 }
191
192 void WebInspector::attachedRight()
193 {
194     if (m_frontendClient)
195         m_frontendClient->setAttachedWindow(InspectorFrontendClient::DOCKED_TO_RIGHT);
196 }
197
198 void WebInspector::detached()
199 {
200     if (m_frontendClient)
201         m_frontendClient->setAttachedWindow(InspectorFrontendClient::UNDOCKED);
202 }
203
204 void WebInspector::evaluateScriptForTest(long callID, const String& script)
205 {
206     m_page->corePage()->inspectorController().evaluateForTestInFrontend(callID, script);
207 }
208
209 void WebInspector::showConsole()
210 {
211     m_page->corePage()->inspectorController().show();
212     if (m_frontendClient)
213         m_frontendClient->showConsole();
214 }
215
216 void WebInspector::showResources()
217 {
218     m_page->corePage()->inspectorController().show();
219     if (m_frontendClient)
220         m_frontendClient->showResources();
221 }
222
223 void WebInspector::showMainResourceForFrame(uint64_t frameID)
224 {
225     WebFrame* frame = WebProcess::shared().webFrame(frameID);
226     if (!frame)
227         return;
228
229     m_page->corePage()->inspectorController().show();
230     if (m_frontendClient)
231         m_frontendClient->showMainResourceForFrame(frame->coreFrame());
232 }
233
234 void WebInspector::startJavaScriptDebugging()
235 {
236     m_page->corePage()->inspectorController().show();
237     if (m_frontendClient)
238         m_frontendClient->setDebuggingEnabled(true);
239 }
240
241 void WebInspector::stopJavaScriptDebugging()
242 {
243     m_page->corePage()->inspectorController().show();
244     if (m_frontendClient)
245         m_frontendClient->setDebuggingEnabled(false);
246 }
247
248 void WebInspector::setJavaScriptProfilingEnabled(bool enabled)
249 {
250     m_page->corePage()->inspectorController().show();
251     if (!m_frontendClient)
252         return;
253
254     m_page->corePage()->inspectorController().setProfilerEnabled(enabled);
255 }
256
257 void WebInspector::startJavaScriptProfiling()
258 {
259     m_page->corePage()->inspectorController().show();
260     if (m_frontendClient)
261         m_frontendClient->startProfilingJavaScript();
262 }
263
264 void WebInspector::stopJavaScriptProfiling()
265 {
266     m_page->corePage()->inspectorController().show();
267     if (m_frontendClient)
268         m_frontendClient->stopProfilingJavaScript();
269 }
270
271 void WebInspector::startPageProfiling()
272 {
273     m_page->corePage()->inspectorController().show();
274     if (m_frontendClient)
275         m_frontendClient->setTimelineProfilingEnabled(true);
276 }
277
278 void WebInspector::stopPageProfiling()
279 {
280     m_page->corePage()->inspectorController().show();
281     if (m_frontendClient)
282         m_frontendClient->setTimelineProfilingEnabled(false);
283 }
284
285 void WebInspector::updateDockingAvailability()
286 {
287     if (!m_frontendClient)
288         return;
289
290     bool canAttachWindow = m_frontendClient->canAttachWindow();
291     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::AttachAvailabilityChanged(canAttachWindow), m_page->pageID());
292     m_frontendClient->setDockingUnavailable(!canAttachWindow);
293 }
294
295 #if ENABLE(INSPECTOR_SERVER)
296 void WebInspector::sendMessageToRemoteFrontend(const String& message)
297 {
298     ASSERT(m_remoteFrontendConnected);
299     WebProcess::shared().parentProcessConnection()->send(Messages::WebInspectorProxy::SendMessageToRemoteFrontend(message), m_page->pageID());
300 }
301
302 void WebInspector::dispatchMessageFromRemoteFrontend(const String& message)
303 {
304     m_page->corePage()->inspectorController().dispatchMessageFromFrontend(message);
305 }
306
307 void WebInspector::remoteFrontendConnected()
308 {
309     ASSERT(!m_remoteFrontendConnected);
310     // Switching between in-process and remote inspectors isn't supported yet.
311     ASSERT(!m_inspectorPage);
312     
313     m_page->corePage()->inspectorController().connectFrontend(m_frontendChannel);
314     m_remoteFrontendConnected = true;
315 }
316
317 void WebInspector::remoteFrontendDisconnected()
318 {
319     ASSERT(m_remoteFrontendConnected);
320     m_page->corePage()->inspectorController().disconnectFrontend(Inspector::InspectorDisconnectReason::InspectorDestroyed);
321     m_remoteFrontendConnected = false;
322 }
323 #endif
324
325 } // namespace WebKit
326
327 #endif // ENABLE(INSPECTOR)