1/*
2 * Copyright (c) 2011 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32
33#if ENABLE(JAVASCRIPT_DEBUGGER)
34
35#include "PageScriptDebugServer.h"
36
37#include "EventLoop.h"
38#include "Frame.h"
39#include "FrameView.h"
40#include "JSDOMWindowCustom.h"
41#include "Page.h"
42#include "PageGroup.h"
43#include "PluginView.h"
44#include "ScriptController.h"
45#include "ScriptDebugListener.h"
46#include "Widget.h"
47#include <runtime/JSLock.h>
48#include <wtf/MainThread.h>
49#include <wtf/OwnPtr.h>
50#include <wtf/PassOwnPtr.h>
51#include <wtf/StdLibExtras.h>
52
53using namespace JSC;
54
55namespace WebCore {
56
57static Page* toPage(JSGlobalObject* globalObject)
58{
59    ASSERT_ARG(globalObject, globalObject);
60
61    JSDOMWindow* window = asJSDOMWindow(globalObject);
62    Frame* frame = window->impl()->frame();
63    return frame ? frame->page() : 0;
64}
65
66PageScriptDebugServer& PageScriptDebugServer::shared()
67{
68    DEFINE_STATIC_LOCAL(PageScriptDebugServer, server, ());
69    return server;
70}
71
72PageScriptDebugServer::PageScriptDebugServer()
73    : ScriptDebugServer()
74    , m_pausedPage(0)
75{
76}
77
78PageScriptDebugServer::~PageScriptDebugServer()
79{
80}
81
82void PageScriptDebugServer::addListener(ScriptDebugListener* listener, Page* page)
83{
84    ASSERT_ARG(listener, listener);
85    ASSERT_ARG(page, page);
86
87    OwnPtr<ListenerSet>& listeners = m_pageListenersMap.add(page, nullptr).iterator->value;
88    if (!listeners)
89        listeners = adoptPtr(new ListenerSet);
90    listeners->add(listener);
91
92    recompileAllJSFunctionsSoon();
93    page->setDebugger(this);
94}
95
96void PageScriptDebugServer::removeListener(ScriptDebugListener* listener, Page* page)
97{
98    ASSERT_ARG(listener, listener);
99    ASSERT_ARG(page, page);
100
101    PageListenersMap::iterator it = m_pageListenersMap.find(page);
102    if (it == m_pageListenersMap.end())
103        return;
104
105    ListenerSet* listeners = it->value.get();
106    listeners->remove(listener);
107    if (listeners->isEmpty()) {
108        m_pageListenersMap.remove(it);
109        didRemoveLastListener(page);
110    }
111}
112
113void PageScriptDebugServer::recompileAllJSFunctions(Timer<ScriptDebugServer>*)
114{
115    JSLockHolder lock(JSDOMWindow::commonVM());
116    // If JavaScript stack is not empty postpone recompilation.
117    if (JSDOMWindow::commonVM()->dynamicGlobalObject)
118        recompileAllJSFunctionsSoon();
119    else
120        Debugger::recompileAllJSFunctions(JSDOMWindow::commonVM());
121}
122
123ScriptDebugServer::ListenerSet* PageScriptDebugServer::getListenersForGlobalObject(JSGlobalObject* globalObject)
124{
125    Page* page = toPage(globalObject);
126    if (!page)
127        return 0;
128    return m_pageListenersMap.get(page);
129}
130
131void PageScriptDebugServer::didPause(JSC::JSGlobalObject* globalObject)
132{
133    ASSERT(!m_pausedPage);
134
135    Page* page = toPage(globalObject);
136    ASSERT(page);
137    if (!page)
138        return;
139
140    m_pausedPage = page;
141
142    setJavaScriptPaused(page->group(), true);
143}
144
145void PageScriptDebugServer::didContinue(JSC::JSGlobalObject* globalObject)
146{
147    // Page can be null if we are continuing because the Page closed.
148    Page* page = toPage(globalObject);
149    ASSERT(!page || page == m_pausedPage);
150
151    m_pausedPage = 0;
152
153    if (page)
154        setJavaScriptPaused(page->group(), false);
155}
156
157void PageScriptDebugServer::didRemoveLastListener(Page* page)
158{
159    ASSERT(page);
160
161    if (m_pausedPage == page)
162        m_doneProcessingDebuggerEvents = true;
163
164    recompileAllJSFunctionsSoon();
165    page->setDebugger(0);
166}
167
168void PageScriptDebugServer::runEventLoopWhilePaused()
169{
170    EventLoop loop;
171    while (!m_doneProcessingDebuggerEvents && !loop.ended())
172        loop.cycle();
173}
174
175void PageScriptDebugServer::setJavaScriptPaused(const PageGroup& pageGroup, bool paused)
176{
177    setMainThreadCallbacksPaused(paused);
178
179    const HashSet<Page*>& pages = pageGroup.pages();
180
181    HashSet<Page*>::const_iterator end = pages.end();
182    for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it)
183        setJavaScriptPaused(*it, paused);
184}
185
186void PageScriptDebugServer::setJavaScriptPaused(Page* page, bool paused)
187{
188    ASSERT_ARG(page, page);
189
190    page->setDefersLoading(paused);
191
192    for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext())
193        setJavaScriptPaused(frame, paused);
194}
195
196void PageScriptDebugServer::setJavaScriptPaused(Frame* frame, bool paused)
197{
198    ASSERT_ARG(frame, frame);
199
200    if (!frame->script()->canExecuteScripts(NotAboutToExecuteScript))
201        return;
202
203    frame->script()->setPaused(paused);
204
205    Document* document = frame->document();
206    if (paused) {
207        document->suspendScriptedAnimationControllerCallbacks();
208        document->suspendActiveDOMObjects(ActiveDOMObject::JavaScriptDebuggerPaused);
209    } else {
210        document->resumeActiveDOMObjects(ActiveDOMObject::JavaScriptDebuggerPaused);
211        document->resumeScriptedAnimationControllerCallbacks();
212    }
213
214    setJavaScriptPaused(frame->view(), paused);
215}
216
217void PageScriptDebugServer::setJavaScriptPaused(FrameView* view, bool paused)
218{
219    if (!view)
220        return;
221
222    const HashSet<RefPtr<Widget> >* children = view->children();
223    ASSERT(children);
224
225    HashSet<RefPtr<Widget> >::const_iterator end = children->end();
226    for (HashSet<RefPtr<Widget> >::const_iterator it = children->begin(); it != end; ++it) {
227        Widget* widget = (*it).get();
228        if (!widget->isPluginView())
229            continue;
230        toPluginView(widget)->setJavaScriptPaused(paused);
231    }
232}
233
234} // namespace WebCore
235
236#endif // ENABLE(JAVASCRIPT_DEBUGGER)
237