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#include "PageScriptDebugServer.h"
33
34#if ENABLE(JAVASCRIPT_DEBUGGER)
35
36#include "Frame.h"
37#include "Page.h"
38#include "ScriptDebugListener.h"
39#include "V8Binding.h"
40#include "V8DOMWindow.h"
41#include "V8Proxy.h"
42#include <wtf/OwnPtr.h>
43#include <wtf/PassOwnPtr.h>
44#include <wtf/StdLibExtras.h>
45
46namespace WebCore {
47
48static Frame* retrieveFrame(v8::Handle<v8::Context> context)
49{
50    if (context.IsEmpty())
51        return 0;
52
53    // Test that context has associated global dom window object.
54    v8::Handle<v8::Object> global = context->Global();
55    if (global.IsEmpty())
56        return 0;
57
58    global = V8DOMWrapper::lookupDOMWrapper(V8DOMWindow::GetTemplate(), global);
59    if (global.IsEmpty())
60        return 0;
61
62    return V8Proxy::retrieveFrame(context);
63}
64
65PageScriptDebugServer& PageScriptDebugServer::shared()
66{
67    DEFINE_STATIC_LOCAL(PageScriptDebugServer, server, ());
68    return server;
69}
70
71PageScriptDebugServer::PageScriptDebugServer()
72    : ScriptDebugServer()
73    , m_pausedPage(0)
74    , m_enabled(true)
75{
76}
77
78void PageScriptDebugServer::addListener(ScriptDebugListener* listener, Page* page)
79{
80    if (!m_enabled)
81        return;
82
83    V8Proxy* proxy = V8Proxy::retrieve(page->mainFrame());
84    if (!proxy)
85        return;
86
87    v8::HandleScope scope;
88    v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
89    v8::Context::Scope contextScope(debuggerContext);
90
91    if (!m_listenersMap.size()) {
92        ensureDebuggerScriptCompiled();
93        ASSERT(!m_debuggerScript.get()->IsUndefined());
94        v8::Debug::SetDebugEventListener2(&PageScriptDebugServer::v8DebugEventCallback, v8::External::New(this));
95    }
96    m_listenersMap.set(page, listener);
97
98    V8DOMWindowShell* shell = proxy->windowShell();
99    if (!shell->isContextInitialized())
100        return;
101    v8::Handle<v8::Context> context = shell->context();
102    v8::Handle<v8::Function> getScriptsFunction = v8::Local<v8::Function>::Cast(m_debuggerScript.get()->Get(v8::String::New("getScripts")));
103    v8::Handle<v8::Value> argv[] = { context->GetData() };
104    v8::Handle<v8::Value> value = getScriptsFunction->Call(m_debuggerScript.get(), 1, argv);
105    if (value.IsEmpty())
106        return;
107    ASSERT(!value->IsUndefined() && value->IsArray());
108    v8::Handle<v8::Array> scriptsArray = v8::Handle<v8::Array>::Cast(value);
109    for (unsigned i = 0; i < scriptsArray->Length(); ++i)
110        dispatchDidParseSource(listener, v8::Handle<v8::Object>::Cast(scriptsArray->Get(v8::Integer::New(i))));
111}
112
113void PageScriptDebugServer::removeListener(ScriptDebugListener* listener, Page* page)
114{
115    if (!m_listenersMap.contains(page))
116        return;
117
118    if (m_pausedPage == page)
119        continueProgram();
120
121    m_listenersMap.remove(page);
122
123    if (m_listenersMap.isEmpty())
124        v8::Debug::SetDebugEventListener(0);
125    // FIXME: Remove all breakpoints set by the agent.
126}
127
128void PageScriptDebugServer::setClientMessageLoop(PassOwnPtr<ClientMessageLoop> clientMessageLoop)
129{
130    m_clientMessageLoop = clientMessageLoop;
131}
132
133ScriptDebugListener* PageScriptDebugServer::getDebugListenerForContext(v8::Handle<v8::Context> context)
134{
135    v8::HandleScope scope;
136    Frame* frame = retrieveFrame(context);
137    if (!frame)
138        return 0;
139    return m_listenersMap.get(frame->page());
140}
141
142void PageScriptDebugServer::runMessageLoopOnPause(v8::Handle<v8::Context> context)
143{
144    v8::HandleScope scope;
145    Frame* frame = retrieveFrame(context);
146    m_pausedPage = frame->page();
147
148    // Wait for continue or step command.
149    m_clientMessageLoop->run(m_pausedPage);
150
151    // The listener may have been removed in the nested loop.
152    if (ScriptDebugListener* listener = m_listenersMap.get(m_pausedPage))
153        listener->didContinue();
154
155    m_pausedPage = 0;
156}
157
158void PageScriptDebugServer::quitMessageLoopOnPause()
159{
160    m_clientMessageLoop->quitNow();
161}
162
163} // namespace WebCore
164
165#endif // ENABLE(JAVASCRIPT_DEBUGGER)
166