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/WebKit.h>
41 ServerConnection::ServerConnection()
44 HRESULT serverCreated = CoCreateInstance(CLSID_WebScriptDebugServer, 0, CLSCTX_LOCAL_SERVER, IID_IWebScriptDebugServer, (void**)&m_server);
45 if (!FAILED(serverCreated))
46 m_server->addListener(this);
49 ServerConnection::~ServerConnection()
52 m_server->removeListener(this);
55 JSGlobalContextRelease(m_globalContext);
58 void ServerConnection::setGlobalContext(JSGlobalContextRef globalContextRef)
60 m_globalContext = JSGlobalContextRetain(globalContextRef);
63 void ServerConnection::pause()
69 void ServerConnection::resume()
75 void ServerConnection::stepInto()
81 // Connection Handling
83 void ServerConnection::applicationTerminating()
86 m_server->removeListener(this);
89 void ServerConnection::serverConnectionDidDie()
92 m_server->removeListener(this);
97 IWebScriptCallFrame* ServerConnection::currentFrame() const
99 return m_currentFrame;
102 IWebScriptCallFrame* ServerConnection::getCallerFrame(int callFrame) const
104 COMPtr<IWebScriptCallFrame> cframe = currentFrame();
105 COMPtr<IWebScriptCallFrame> callerFrame;
106 for (int count = 0; count < callFrame; count++) {
107 if (FAILED(cframe->caller(&callerFrame)))
110 cframe = callerFrame;
117 // IUnknown --------------------------------------------------
118 HRESULT STDMETHODCALLTYPE ServerConnection::QueryInterface(REFIID riid, void** ppvObject)
121 if (IsEqualGUID(riid, IID_IUnknown))
123 else if (IsEqualGUID(riid, IID_IWebScriptDebugListener))
124 *ppvObject = static_cast<IWebScriptDebugListener*>(this);
126 return E_NOINTERFACE;
132 ULONG STDMETHODCALLTYPE ServerConnection::AddRef(void)
134 // COM ref-counting isn't useful to us because we're in charge of the lifetime of the WebView.
138 ULONG STDMETHODCALLTYPE ServerConnection::Release(void)
140 // COM ref-counting isn't useful to us because we're in charge of the lifetime of the WebView.
143 // IWebScriptDebugListener -----------------------------------
144 HRESULT STDMETHODCALLTYPE ServerConnection::didLoadMainResourceForDataSource(
145 /* [in] */ IWebView*,
146 /* [in] */ IWebDataSource* dataSource)
148 // Get document source
149 COMPtr<IWebDocumentRepresentation> rep;
150 HRESULT ret = dataSource->representation(&rep);
154 BOOL canProvideDocumentSource = FALSE;
155 ret = rep->canProvideDocumentSource(&canProvideDocumentSource);
159 BSTR documentSource = 0;
160 if (canProvideDocumentSource)
161 ret = rep->documentSource(&documentSource);
163 if (FAILED(ret) || !documentSource)
166 JSRetainPtr<JSStringRef> documentSourceJS(Adopt, JSStringCreateWithBSTR(documentSource));
167 SysFreeString(documentSource);
170 COMPtr<IWebURLResponse> response;
171 ret = dataSource->response(&response);
176 ret = response->URL(&url);
180 JSRetainPtr<JSStringRef> urlJS(Adopt, JSStringCreateWithBSTR(url));
183 DebuggerDocument::updateFileSource(m_globalContext, documentSourceJS.get(), urlJS.get());
188 HRESULT STDMETHODCALLTYPE ServerConnection::didParseSource(
189 /* [in] */ IWebView*,
190 /* [in] */ BSTR sourceCode,
191 /* [in] */ UINT baseLineNumber,
193 /* [in] */ int sourceID,
194 /* [in] */ IWebFrame* webFrame)
197 if (!m_globalContext || !sourceCode)
\r
200 COMPtr<IWebDataSource> dataSource;
\r
201 ret = webFrame->dataSource(&dataSource);
\r
205 COMPtr<IWebURLResponse> response;
\r
206 ret = dataSource->response(&response);
\r
211 ret = response->URL(&responseURL);
\r
215 BSTR documentSource = 0;
\r
216 if (!url || !wcscmp(responseURL, url)) {
\r
217 COMPtr<IWebDocumentRepresentation> rep;
\r
218 ret = dataSource->representation(&rep);
\r
222 BOOL canProvideDocumentSource;
\r
223 rep->canProvideDocumentSource(&canProvideDocumentSource);
\r
227 if (canProvideDocumentSource) {
\r
228 ret = rep->documentSource(&documentSource);
\r
234 ret = response->URL(&url);
\r
239 SysFreeString(responseURL);
\r
241 JSRetainPtr<JSStringRef> sourceJS(Adopt, JSStringCreateWithBSTR(sourceCode));
\r
242 JSRetainPtr<JSStringRef> documentSourceJS(Adopt, JSStringCreateWithBSTR(documentSource));
\r
243 SysFreeString(documentSource);
\r
244 JSRetainPtr<JSStringRef> urlJS(Adopt, JSStringCreateWithBSTR(url));
\r
245 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
\r
246 JSValueRef baseLineJS = JSValueMakeNumber(m_globalContext, baseLineNumber);
\r
248 DebuggerDocument::didParseScript(m_globalContext, sourceJS.get(), documentSourceJS.get(), urlJS.get(), sidJS, baseLineJS);
\r
253 HRESULT STDMETHODCALLTYPE ServerConnection::failedToParseSource(
254 /* [in] */ IWebView*,
259 /* [in] */ IWebFrame*)
264 HRESULT STDMETHODCALLTYPE ServerConnection::didEnterCallFrame(
265 /* [in] */ IWebView*,
266 /* [in] */ IWebScriptCallFrame* frame,
267 /* [in] */ int sourceID,
268 /* [in] */ int lineNumber,
269 /* [in] */ IWebFrame*)
272 if (!m_globalContext)
\r
275 // FIXME: This won't be relevant until IWebScriptCallFrame is implemented on Windows
\r
276 m_currentFrame = frame;
\r
278 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
\r
279 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
\r
281 DebuggerDocument::didEnterCallFrame(m_globalContext, sidJS, linenoJS);
\r
286 HRESULT STDMETHODCALLTYPE ServerConnection::willExecuteStatement(
287 /* [in] */ IWebView*,
288 /* [in] */ IWebScriptCallFrame*,
289 /* [in] */ int sourceID,
290 /* [in] */ int lineNumber,
291 /* [in] */ IWebFrame*)
294 if (!m_globalContext)
\r
297 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
\r
298 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
\r
300 DebuggerDocument::willExecuteStatement(m_globalContext, sidJS, linenoJS);
304 HRESULT STDMETHODCALLTYPE ServerConnection::willLeaveCallFrame(
305 /* [in] */ IWebView*,
306 /* [in] */ IWebScriptCallFrame* frame,
307 /* [in] */ int sourceID,
308 /* [in] */ int lineNumber,
309 /* [in] */ IWebFrame*)
312 if (!m_globalContext)
\r
315 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
\r
316 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
\r
318 DebuggerDocument::willLeaveCallFrame(m_globalContext, sidJS, linenoJS);
\r
320 // FIXME: This won't be relevant until IWebScriptCallFrame is implemented on Windows
\r
321 m_currentFrame = frame;
\r
326 HRESULT STDMETHODCALLTYPE ServerConnection::exceptionWasRaised(
327 /* [in] */ IWebView*,
328 /* [in] */ IWebScriptCallFrame*,
329 /* [in] */ int sourceID,
330 /* [in] */ int lineNumber,
331 /* [in] */ IWebFrame*)
334 if (!m_globalContext)
\r
337 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
\r
338 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
\r
340 DebuggerDocument::exceptionWasRaised(m_globalContext, sidJS, linenoJS);