2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "IWebScriptDebugListener.h"
28 #include "WebKitDLL.h"
29 #include "WebScriptDebugServer.h"
32 #include <wtf/Assertions.h>
33 #include <wtf/Vector.h>
35 static Vector<IWebView*> sViews;
36 static WebScriptDebugServer* sSharedWebScriptDebugServer;
37 static unsigned sListenerCount = 0;
39 unsigned WebScriptDebugServer::listenerCount() { return sListenerCount; };
41 // EnumViews ------------------------------------------------------------------
43 class EnumViews : public IEnumVARIANT
46 EnumViews() : m_refCount(1), m_current(sViews.begin()) { }
48 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
49 virtual ULONG STDMETHODCALLTYPE AddRef();
50 virtual ULONG STDMETHODCALLTYPE Release();
52 virtual HRESULT STDMETHODCALLTYPE Next(ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched);
53 virtual HRESULT STDMETHODCALLTYPE Skip(ULONG celt);
54 virtual HRESULT STDMETHODCALLTYPE Reset(void);
55 virtual HRESULT STDMETHODCALLTYPE Clone(IEnumVARIANT**);
59 Vector<IWebView*>::iterator m_current;
62 HRESULT STDMETHODCALLTYPE EnumViews::QueryInterface(REFIID riid, void** ppvObject)
65 if (IsEqualGUID(riid, IID_IUnknown) || IsEqualGUID(riid, IID_IEnumVARIANT))
74 ULONG STDMETHODCALLTYPE EnumViews::AddRef()
79 ULONG STDMETHODCALLTYPE EnumViews::Release()
81 ULONG newRef = --m_refCount;
87 HRESULT STDMETHODCALLTYPE EnumViews::Next(ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
94 if (!celt || celt > 1)
96 if (m_current == sViews.end())
100 HRESULT hr = (*m_current++)->QueryInterface(IID_IUnknown, (void**)&unknown);
104 V_VT(rgVar) = VT_UNKNOWN;
105 V_UNKNOWN(rgVar) = unknown;
112 HRESULT STDMETHODCALLTYPE EnumViews::Skip(ULONG celt)
115 return (m_current != sViews.end()) ? S_OK : S_FALSE;
118 HRESULT STDMETHODCALLTYPE EnumViews::Reset(void)
120 m_current = sViews.begin();
124 HRESULT STDMETHODCALLTYPE EnumViews::Clone(IEnumVARIANT**)
129 // WebScriptDebugServer ------------------------------------------------------------
131 WebScriptDebugServer::WebScriptDebugServer()
139 WebScriptDebugServer::~WebScriptDebugServer()
144 WebScriptDebugServer* WebScriptDebugServer::createInstance()
146 WebScriptDebugServer* instance = new WebScriptDebugServer;
151 WebScriptDebugServer* WebScriptDebugServer::sharedWebScriptDebugServer()
153 if (!sSharedWebScriptDebugServer)
154 sSharedWebScriptDebugServer = WebScriptDebugServer::createInstance();
156 return sSharedWebScriptDebugServer;
160 // IUnknown -------------------------------------------------------------------
162 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::QueryInterface(REFIID riid, void** ppvObject)
165 if (IsEqualGUID(riid, IID_IUnknown))
166 *ppvObject = static_cast<WebScriptDebugServer*>(this);
167 else if (IsEqualGUID(riid, IID_IWebScriptDebugServer))
168 *ppvObject = static_cast<WebScriptDebugServer*>(this);
170 return E_NOINTERFACE;
176 ULONG STDMETHODCALLTYPE WebScriptDebugServer::AddRef()
181 ULONG STDMETHODCALLTYPE WebScriptDebugServer::Release()
183 ULONG newRef = --m_refCount;
190 void WebScriptDebugServer::viewAdded(IWebView* view)
195 void WebScriptDebugServer::viewRemoved(IWebView* view)
197 Vector<IWebView*>::iterator end = sViews.end();
199 for (Vector<IWebView*>::iterator it = sViews.begin(); it != end; ++it, ++i) {
207 // IWebScriptDebugServer -----------------------------------------------------------
209 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::sharedWebScriptDebugServer(
210 /* [retval][out] */ IWebScriptDebugServer** server)
215 *server = WebScriptDebugServer::sharedWebScriptDebugServer();
221 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::addListener(
222 /* [in] */ IWebScriptDebugListener* listener)
228 m_listeners.add(listener);
233 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::removeListener(
234 /* [in] */ IWebScriptDebugListener* listener)
239 if (!m_listeners.contains(listener))
242 ASSERT(sListenerCount >= 1);
244 m_listeners.remove(listener);
249 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::step()
257 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::pause()
265 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::resume()
273 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::isPaused(
274 /* [out, retval] */ BOOL* isPaused)
279 *isPaused = m_paused;
284 void WebScriptDebugServer::suspendProcessIfPaused()
286 // FIXME: There needs to be some sort of busy wait here.
294 // IWebScriptDebugListener
295 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::didLoadMainResourceForDataSource(
296 /* [in] */ IWebView* webView,
297 /* [in] */ IWebDataSource* dataSource)
299 if (!webView || !dataSource)
302 ListenerSet listenersCopy = m_listeners;
303 for (ListenerSet::iterator it = listenersCopy.begin(); it != listenersCopy.end(); ++it)
304 (**it).didLoadMainResourceForDataSource(webView, dataSource);
309 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::didParseSource(
310 /* [in] */ IWebView* webView,
311 /* [in] */ BSTR sourceCode,
312 /* [in] */ UINT baseLineNumber,
314 /* [in] */ int sourceID,
315 /* [in] */ IWebFrame* webFrame)
317 if (!webView || !sourceCode || !url || !webFrame)
320 ListenerSet listenersCopy = m_listeners;
321 for (ListenerSet::iterator it = listenersCopy.begin(); it != listenersCopy.end(); ++it)
322 (**it).didParseSource(webView, sourceCode, baseLineNumber, url, sourceID, webFrame);
327 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::failedToParseSource(
328 /* [in] */ IWebView* webView,
329 /* [in] */ BSTR sourceCode,
330 /* [in] */ UINT baseLineNumber,
332 /* [in] */ BSTR error,
333 /* [in] */ IWebFrame* webFrame)
335 if (!webView || !sourceCode || !url || !error || !webFrame)
338 ListenerSet listenersCopy = m_listeners;
339 for (ListenerSet::iterator it = listenersCopy.begin(); it != listenersCopy.end(); ++it)
340 (**it).failedToParseSource(webView, sourceCode, baseLineNumber, url, error, webFrame);
345 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::didEnterCallFrame(
346 /* [in] */ IWebView* webView,
347 /* [in] */ IWebScriptCallFrame* frame,
348 /* [in] */ int sourceID,
349 /* [in] */ int lineNumber,
350 /* [in] */ IWebFrame* webFrame)
352 if (!webView || !frame || !webFrame)
355 ListenerSet listenersCopy = m_listeners;
356 for (ListenerSet::iterator it = listenersCopy.begin(); it != listenersCopy.end(); ++it)
357 (**it).didEnterCallFrame(webView, frame, sourceID, lineNumber, webFrame);
362 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::willExecuteStatement(
363 /* [in] */ IWebView* webView,
364 /* [in] */ IWebScriptCallFrame* frame,
365 /* [in] */ int sourceID,
366 /* [in] */ int lineNumber,
367 /* [in] */ IWebFrame* webFrame)
369 if (!webView || !frame || !webFrame)
372 ListenerSet listenersCopy = m_listeners;
373 for (ListenerSet::iterator it = listenersCopy.begin(); it != listenersCopy.end(); ++it)
374 (**it).willExecuteStatement(webView, frame, sourceID, lineNumber, webFrame);
379 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::willLeaveCallFrame(
380 /* [in] */ IWebView* webView,
381 /* [in] */ IWebScriptCallFrame* frame,
382 /* [in] */ int sourceID,
383 /* [in] */ int lineNumber,
384 /* [in] */ IWebFrame* webFrame)
386 if (!webView || !frame || !webFrame)
389 ListenerSet listenersCopy = m_listeners;
390 for (ListenerSet::iterator it = listenersCopy.begin(); it != listenersCopy.end(); ++it)
391 (**it).willLeaveCallFrame(webView, frame, sourceID, lineNumber, webFrame);
396 HRESULT STDMETHODCALLTYPE WebScriptDebugServer::exceptionWasRaised(
397 /* [in] */ IWebView* webView,
398 /* [in] */ IWebScriptCallFrame* frame,
399 /* [in] */ int sourceID,
400 /* [in] */ int lineNumber,
401 /* [in] */ IWebFrame* webFrame)
403 if (!webView || !frame || !webFrame)
406 ListenerSet listenersCopy = m_listeners;
407 for (ListenerSet::iterator it = listenersCopy.begin(); it != listenersCopy.end(); ++it)
408 (**it).exceptionWasRaised(webView, frame, sourceID, lineNumber, webFrame);