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/JSStringRefCOM.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;
103 // IUnknown --------------------------------------------------
104 HRESULT STDMETHODCALLTYPE ServerConnection::QueryInterface(REFIID riid, void** ppvObject)
107 if (IsEqualGUID(riid, IID_IUnknown))
109 else if (IsEqualGUID(riid, IID_IWebScriptDebugListener))
110 *ppvObject = static_cast<IWebScriptDebugListener*>(this);
112 return E_NOINTERFACE;
118 ULONG STDMETHODCALLTYPE ServerConnection::AddRef(void)
120 // COM ref-counting isn't useful to us because we're in charge of the lifetime of the WebView.
124 ULONG STDMETHODCALLTYPE ServerConnection::Release(void)
126 // COM ref-counting isn't useful to us because we're in charge of the lifetime of the WebView.
129 // IWebScriptDebugListener -----------------------------------
130 HRESULT STDMETHODCALLTYPE ServerConnection::didLoadMainResourceForDataSource(
131 /* [in] */ IWebView*,
132 /* [in] */ IWebDataSource* dataSource)
134 // Get document source
135 COMPtr<IWebDocumentRepresentation> rep;
136 HRESULT ret = dataSource->representation(&rep);
140 BOOL canProvideDocumentSource = FALSE;
141 ret = rep->canProvideDocumentSource(&canProvideDocumentSource);
145 BSTR documentSource = 0;
146 if (canProvideDocumentSource)
147 ret = rep->documentSource(&documentSource);
149 if (FAILED(ret) || !documentSource)
152 JSRetainPtr<JSStringRef> documentSourceJS(Adopt, JSStringCreateWithBSTR(documentSource));
153 SysFreeString(documentSource);
156 COMPtr<IWebURLResponse> response;
157 ret = dataSource->response(&response);
162 ret = response->URL(&url);
166 JSRetainPtr<JSStringRef> urlJS(Adopt, JSStringCreateWithBSTR(url));
169 DebuggerDocument::updateFileSource(m_globalContext, documentSourceJS.get(), urlJS.get());
174 HRESULT STDMETHODCALLTYPE ServerConnection::didParseSource(
175 /* [in] */ IWebView*,
176 /* [in] */ BSTR sourceCode,
177 /* [in] */ UINT baseLineNumber,
179 /* [in] */ int sourceID,
180 /* [in] */ IWebFrame* webFrame)
183 if (!m_globalContext || !sourceCode)
\r
186 COMPtr<IWebDataSource> dataSource;
\r
187 ret = webFrame->dataSource(&dataSource);
\r
191 COMPtr<IWebURLResponse> response;
\r
192 ret = dataSource->response(&response);
\r
197 ret = response->URL(&responseURL);
\r
201 BSTR documentSource = 0;
\r
202 if (!url || !wcscmp(responseURL, url)) {
\r
203 COMPtr<IWebDocumentRepresentation> rep;
\r
204 ret = dataSource->representation(&rep);
\r
208 BOOL canProvideDocumentSource;
\r
209 rep->canProvideDocumentSource(&canProvideDocumentSource);
\r
213 if (canProvideDocumentSource) {
\r
214 ret = rep->documentSource(&documentSource);
\r
220 ret = response->URL(&url);
\r
225 SysFreeString(responseURL);
\r
227 JSRetainPtr<JSStringRef> sourceJS(Adopt, JSStringCreateWithBSTR(sourceCode));
\r
228 JSRetainPtr<JSStringRef> documentSourceJS(Adopt, JSStringCreateWithBSTR(documentSource));
\r
229 SysFreeString(documentSource);
\r
230 JSRetainPtr<JSStringRef> urlJS(Adopt, JSStringCreateWithBSTR(url));
\r
231 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
\r
232 JSValueRef baseLineJS = JSValueMakeNumber(m_globalContext, baseLineNumber);
\r
234 DebuggerDocument::didParseScript(m_globalContext, sourceJS.get(), documentSourceJS.get(), urlJS.get(), sidJS, baseLineJS);
\r
239 HRESULT STDMETHODCALLTYPE ServerConnection::failedToParseSource(
240 /* [in] */ IWebView*,
245 /* [in] */ IWebFrame*)
250 HRESULT STDMETHODCALLTYPE ServerConnection::didEnterCallFrame(
251 /* [in] */ IWebView*,
252 /* [in] */ IWebScriptCallFrame* frame,
253 /* [in] */ int sourceID,
254 /* [in] */ int lineNumber,
255 /* [in] */ IWebFrame*)
258 if (!m_globalContext)
\r
261 // FIXME: This won't be relevant until IWebScriptCallFrame is implemented on Windows
\r
262 m_currentFrame = frame;
\r
264 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
\r
265 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
\r
267 DebuggerDocument::didEnterCallFrame(m_globalContext, sidJS, linenoJS);
\r
272 HRESULT STDMETHODCALLTYPE ServerConnection::willExecuteStatement(
273 /* [in] */ IWebView*,
274 /* [in] */ IWebScriptCallFrame*,
275 /* [in] */ int sourceID,
276 /* [in] */ int lineNumber,
277 /* [in] */ IWebFrame*)
280 if (!m_globalContext)
\r
283 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
\r
284 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
\r
286 DebuggerDocument::willExecuteStatement(m_globalContext, sidJS, linenoJS);
290 HRESULT STDMETHODCALLTYPE ServerConnection::willLeaveCallFrame(
291 /* [in] */ IWebView*,
292 /* [in] */ IWebScriptCallFrame* frame,
293 /* [in] */ int sourceID,
294 /* [in] */ int lineNumber,
295 /* [in] */ IWebFrame*)
298 if (!m_globalContext)
\r
301 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
\r
302 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
\r
304 DebuggerDocument::willLeaveCallFrame(m_globalContext, sidJS, linenoJS);
\r
306 // FIXME: This won't be relevant until IWebScriptCallFrame is implemented on Windows
\r
307 m_currentFrame = frame;
\r
312 HRESULT STDMETHODCALLTYPE ServerConnection::exceptionWasRaised(
313 /* [in] */ IWebView*,
314 /* [in] */ IWebScriptCallFrame*,
315 /* [in] */ int sourceID,
316 /* [in] */ int lineNumber,
317 /* [in] */ IWebFrame*)
320 if (!m_globalContext)
\r
323 JSValueRef sidJS = JSValueMakeNumber(m_globalContext, sourceID);
\r
324 JSValueRef linenoJS = JSValueMakeNumber(m_globalContext, lineNumber);
\r
326 DebuggerDocument::exceptionWasRaised(m_globalContext, sidJS, linenoJS);