1/*
2 * Copyright (C) 2007 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 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "WebInspector.h"
31
32#include "WebKitDLL.h"
33#include "WebView.h"
34#include <WebCore/InspectorController.h>
35#include <WebCore/Page.h>
36#include <wtf/Assertions.h>
37
38using namespace WebCore;
39
40WebInspector* WebInspector::createInstance(WebView* webView)
41{
42    WebInspector* inspector = new WebInspector(webView);
43    inspector->AddRef();
44    return inspector;
45}
46
47WebInspector::WebInspector(WebView* webView)
48    : m_refCount(0)
49    , m_webView(webView)
50{
51    ASSERT_ARG(webView, webView);
52
53    gClassCount++;
54    gClassNameCount.add("WebInspector");
55}
56
57WebInspector::~WebInspector()
58{
59    gClassCount--;
60    gClassNameCount.remove("WebInspector");
61}
62
63void WebInspector::webViewClosed()
64{
65    m_webView = 0;
66}
67
68HRESULT STDMETHODCALLTYPE WebInspector::QueryInterface(REFIID riid, void** ppvObject)
69{
70    *ppvObject = 0;
71    if (IsEqualGUID(riid, IID_IWebInspector))
72        *ppvObject = static_cast<IWebInspector*>(this);
73    else if (IsEqualGUID(riid, IID_IWebInspectorPrivate))
74        *ppvObject = static_cast<IWebInspectorPrivate*>(this);
75    else if (IsEqualGUID(riid, IID_IUnknown))
76        *ppvObject = static_cast<IWebInspector*>(this);
77    else
78        return E_NOINTERFACE;
79
80    AddRef();
81    return S_OK;
82}
83
84ULONG STDMETHODCALLTYPE WebInspector::AddRef(void)
85{
86    return ++m_refCount;
87}
88
89ULONG STDMETHODCALLTYPE WebInspector::Release(void)
90{
91    ULONG newRef = --m_refCount;
92    if (!newRef)
93        delete this;
94
95    return newRef;
96}
97
98HRESULT STDMETHODCALLTYPE WebInspector::show()
99{
100    if (m_webView)
101        if (Page* page = m_webView->page())
102            page->inspectorController()->show();
103
104    return S_OK;
105}
106
107HRESULT STDMETHODCALLTYPE WebInspector::showConsole()
108{
109    if (m_webView)
110        if (Page* page = m_webView->page())
111            page->inspectorController()->showConsole();
112
113    return S_OK;
114}
115
116HRESULT STDMETHODCALLTYPE WebInspector::unused1()
117{
118    return S_OK;
119}
120
121HRESULT STDMETHODCALLTYPE WebInspector::close()
122{
123    if (m_webView)
124        if (Page* page = m_webView->page())
125            page->inspectorController()->close();
126
127    return S_OK;
128}
129
130HRESULT STDMETHODCALLTYPE WebInspector::attach()
131{
132    return S_OK;
133}
134
135HRESULT STDMETHODCALLTYPE WebInspector::detach()
136{
137    return S_OK;
138}
139
140HRESULT STDMETHODCALLTYPE WebInspector::isDebuggingJavaScript(BOOL* isDebugging)
141{
142    if (!isDebugging)
143        return E_POINTER;
144
145    *isDebugging = FALSE;
146
147    if (!m_webView)
148        return S_OK;
149
150    Page* page = m_webView->page();
151    if (!page)
152        return S_OK;
153
154    *isDebugging = page->inspectorController()->debuggerEnabled();
155    return S_OK;
156}
157
158HRESULT STDMETHODCALLTYPE WebInspector::toggleDebuggingJavaScript()
159{
160    if (!m_webView)
161        return S_OK;
162
163    Page* page = m_webView->page();
164    if (!page)
165        return S_OK;
166
167    InspectorController* inspector = page->inspectorController();
168
169    if (inspector->debuggerEnabled())
170        inspector->disableDebugger();
171    else
172        inspector->showAndEnableDebugger();
173
174    return S_OK;
175}
176
177HRESULT STDMETHODCALLTYPE WebInspector::isProfilingJavaScript(BOOL* isProfiling)
178{
179    if (!isProfiling)
180        return E_POINTER;
181
182    *isProfiling = FALSE;
183
184    if (!m_webView)
185        return S_OK;
186
187    Page* page = m_webView->page();
188    if (!page)
189        return S_OK;
190
191    *isProfiling = page->inspectorController()->isRecordingUserInitiatedProfile();
192    return S_OK;
193}
194
195HRESULT STDMETHODCALLTYPE WebInspector::toggleProfilingJavaScript()
196{
197    if (!m_webView)
198        return S_OK;
199
200    Page* page = m_webView->page();
201    if (!page)
202        return S_OK;
203
204    InspectorController* inspector = page->inspectorController();
205
206    if (inspector->isRecordingUserInitiatedProfile())
207        inspector->stopUserInitiatedProfiling();
208    else
209        inspector->startUserInitiatedProfiling();
210
211    return S_OK;
212}
213
214HRESULT STDMETHODCALLTYPE WebInspector::isJavaScriptProfilingEnabled(BOOL* isProfilingEnabled)
215{
216    if (!isProfilingEnabled)
217        return E_POINTER;
218
219    *isProfilingEnabled = FALSE;
220
221    if (!m_webView)
222        return S_OK;
223
224    Page* page = m_webView->page();
225    if (!page)
226        return S_OK;
227
228    *isProfilingEnabled = page->inspectorController()->profilerEnabled();
229    return S_OK;
230}
231
232HRESULT STDMETHODCALLTYPE WebInspector::setJavaScriptProfilingEnabled(BOOL enabled)
233{
234    if (!m_webView)
235        return S_OK;
236
237    Page* page = m_webView->page();
238    if (!page)
239        return S_OK;
240
241    if (enabled)
242        page->inspectorController()->enableProfiler();
243    else
244        page->inspectorController()->disableProfiler();
245
246    return S_OK;
247}
248
249HRESULT STDMETHODCALLTYPE  WebInspector::evaluateInFrontend(ULONG callId, BSTR bScript)
250{
251    if (!m_webView)
252        return S_OK;
253
254    Page* page = m_webView->page();
255    if (!page)
256        return S_OK;
257
258    String script(bScript, SysStringLen(bScript));
259    page->inspectorController()->evaluateForTestInFrontend(callId, script);
260    return S_OK;
261}
262
263HRESULT STDMETHODCALLTYPE WebInspector::isTimelineProfilingEnabled(BOOL* isEnabled)
264{
265    if (!isEnabled)
266        return E_POINTER;
267
268    *isEnabled = FALSE;
269
270    if (!m_webView)
271        return S_OK;
272
273    Page* page = m_webView->page();
274    if (!page)
275        return S_OK;
276
277    *isEnabled = page->inspectorController()->timelineProfilerEnabled();
278    return S_OK;
279}
280
281HRESULT STDMETHODCALLTYPE WebInspector::setTimelineProfilingEnabled(BOOL enabled)
282{
283    if (!m_webView)
284        return S_OK;
285
286    Page* page = m_webView->page();
287    if (!page)
288        return S_OK;
289
290    if (enabled)
291        page->inspectorController()->startTimelineProfiler();
292    else
293        page->inspectorController()->stopTimelineProfiler();
294
295    return S_OK;
296}
297