2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2006, 2007 Vladimir Olexa (vladimir.olexa@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 Computer, 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.
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.
31 #include "ServerConnection.h"
33 #include "DebuggerDocument.h"
35 #include <JavaScriptCore/JSContextRef.h>
36 #include <JavaScriptCore/JSRetainPtr.h>
37 #include <JavaScriptCore/JSStringRefBSTR.h>
38 #include <JavaScriptCore/RetainPtr.h>
39 #include <WebKit/IWebScriptCallFrame.h>
40 #include <WebKit/IWebScriptDebugServer.h>
41 #include <WebKit/WebKit.h>
43 ServerConnection::ServerConnection()
46 HRESULT serverCreated = CoCreateInstance(CLSID_WebScriptDebugServer, 0, CLSCTX_LOCAL_SERVER, IID_IWebScriptDebugServer, (void**)&m_server);
47 if (!FAILED(serverCreated))
48 m_server->addListener(this);
51 ServerConnection::~ServerConnection()
54 m_server->removeListener(this);
57 JSGlobalContextRelease(m_globalContext);
60 void ServerConnection::setGlobalContext(JSGlobalContextRef globalContextRef)
62 m_globalContext = JSGlobalContextRetain(globalContextRef);
67 void ServerConnection::pause()
73 void ServerConnection::resume()
79 void ServerConnection::stepInto()
85 // Connection Handling
87 void ServerConnection::applicationTerminating()
90 m_server->removeListener(this);
93 void ServerConnection::serverConnectionDidDie()
96 m_server->removeListener(this);
99 // IUnknown --------------------------------------------------
100 HRESULT STDMETHODCALLTYPE ServerConnection::QueryInterface(REFIID riid, void** ppvObject)
103 if (IsEqualGUID(riid, IID_IUnknown))
105 else if (IsEqualGUID(riid, IID_IWebScriptDebugListener))
106 *ppvObject = static_cast<IWebScriptDebugListener*>(this);
108 return E_NOINTERFACE;
114 ULONG STDMETHODCALLTYPE ServerConnection::AddRef(void)
116 // COM ref-counting isn't useful to us because we're in charge of the lifetime of the WebView.
120 ULONG STDMETHODCALLTYPE ServerConnection::Release(void)
122 // COM ref-counting isn't useful to us because we're in charge of the lifetime of the WebView.
125 // IWebScriptDebugListener -----------------------------------
126 HRESULT STDMETHODCALLTYPE ServerConnection::didLoadMainResourceForDataSource(
127 /* [in] */ IWebView*,
128 /* [in] */ IWebDataSource* dataSource)
130 // Get document source
131 COMPtr<IWebDocumentRepresentation> rep;
132 HRESULT ret = dataSource->representation(&rep);
136 BOOL canProvideDocumentSource = FALSE;
137 ret = rep->canProvideDocumentSource(&canProvideDocumentSource);
141 BSTR documentSource = 0;
142 if (canProvideDocumentSource)
143 ret = rep->documentSource(&documentSource);
145 if (FAILED(ret) || !documentSource)
148 JSRetainPtr<JSStringRef> documentSourceJS(Adopt, JSStringCreateWithBSTR(documentSource));
149 SysFreeString(documentSource);
152 COMPtr<IWebURLResponse> response;
153 ret = dataSource->response(&response);
158 ret = response->URL(&url);
162 JSRetainPtr<JSStringRef> urlJS(Adopt, JSStringCreateWithBSTR(url));
165 DebuggerDocument::updateFileSource(m_globalContext, documentSourceJS.get(), urlJS.get());
170 HRESULT STDMETHODCALLTYPE ServerConnection::didParseSource(
171 /* [in] */ IWebView*,
172 /* [in] */ BSTR sourceCode,
173 /* [in] */ UINT baseLineNumber,
175 /* [in] */ int sourceID,
176 /* [in] */ IWebFrame* webFrame)
179 if (!m_globalContext || !sourceCode)
182 COMPtr<IWebDataSource> dataSource;
183 ret = webFrame->dataSource(&dataSource);
187 COMPtr<IWebURLResponse> response;
188 ret = dataSource->response(&response);
193 ret = response->URL(&responseURL);
197 BSTR documentSource = 0;
198 if (!url || !wcscmp(responseURL, url)) {
199 COMPtr<IWebDocumentRepresentation> rep;
200 ret = dataSource->representation(&rep);
204 BOOL canProvideDocumentSource;
205 rep->canProvideDocumentSource(&canProvideDocumentSource);
209 if (canProvideDocumentSource) {
210 ret = rep->documentSource(&documentSource);
216 ret = response->URL(&url);
221 SysFreeString(responseURL);
223 JSRetainPtr<JSStringRef> sourceJS(Adopt, JSStringCreateWithBSTR(sourceCode));
224 JSRetainPtr<JSStringRef> documentSourceJS(Adopt, JSStringCreateWithBSTR(documentSource));
225 SysFreeString(documentSource);
226 JSRetainPtr<JSStringRef> urlJS(Adopt, JSStringCreateWithBSTR(url));
227 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
228 JSValueRef baseLineJS = JSValueMakeNumber(m_globalContext, baseLineNumber);
230 DebuggerDocument::didParseScript(m_globalContext, sourceJS.get(), documentSourceJS.get(), urlJS.get(), sidJS, baseLineJS);
235 HRESULT STDMETHODCALLTYPE ServerConnection::failedToParseSource(
236 /* [in] */ IWebView*,
241 /* [in] */ IWebFrame*)
246 HRESULT STDMETHODCALLTYPE ServerConnection::didEnterCallFrame(
247 /* [in] */ IWebView*,
248 /* [in] */ IWebScriptCallFrame* frame,
249 /* [in] */ int sourceID,
250 /* [in] */ int lineNumber,
251 /* [in] */ IWebFrame*)
254 if (!m_globalContext)
257 m_currentFrame = frame;
259 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
260 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
262 DebuggerDocument::didEnterCallFrame(m_globalContext, sidJS, linenoJS);
267 HRESULT STDMETHODCALLTYPE ServerConnection::willExecuteStatement(
268 /* [in] */ IWebView*,
269 /* [in] */ IWebScriptCallFrame*,
270 /* [in] */ int sourceID,
271 /* [in] */ int lineNumber,
272 /* [in] */ IWebFrame*)
275 if (!m_globalContext)
278 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
279 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
281 DebuggerDocument::willExecuteStatement(m_globalContext, sidJS, linenoJS);
285 HRESULT STDMETHODCALLTYPE ServerConnection::willLeaveCallFrame(
286 /* [in] */ IWebView*,
287 /* [in] */ IWebScriptCallFrame* frame,
288 /* [in] */ int sourceID,
289 /* [in] */ int lineNumber,
290 /* [in] */ IWebFrame*)
293 if (!m_globalContext)
296 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
297 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
299 DebuggerDocument::willLeaveCallFrame(m_globalContext, sidJS, linenoJS);
301 m_currentFrame = frame;
306 HRESULT STDMETHODCALLTYPE ServerConnection::exceptionWasRaised(
307 /* [in] */ IWebView*,
308 /* [in] */ IWebScriptCallFrame*,
309 /* [in] */ int sourceID,
310 /* [in] */ int lineNumber,
311 /* [in] */ IWebFrame*)
314 if (!m_globalContext)
317 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
318 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
320 DebuggerDocument::exceptionWasRaised(m_globalContext, sidJS, linenoJS);
327 IWebScriptCallFrame* ServerConnection::currentFrame() const
329 return m_currentFrame.get();
332 IWebScriptCallFrame* ServerConnection::getCallerFrame(int callFrame) const
334 COMPtr<IWebScriptCallFrame> cframe = currentFrame();
335 COMPtr<IWebScriptCallFrame> callerFrame;
336 for (int count = 0; count < callFrame; count++) {
337 if (FAILED(cframe->caller(&callerFrame)))
340 cframe = callerFrame;