Home | History | Annotate | Download | only in v8
      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 "bindings/v8/PageScriptDebugServer.h"
     33 
     34 
     35 #include "V8Window.h"
     36 #include "bindings/v8/ScriptController.h"
     37 #include "bindings/v8/V8Binding.h"
     38 #include "bindings/v8/V8ScriptRunner.h"
     39 #include "bindings/v8/V8WindowShell.h"
     40 #include "core/inspector/InspectorInstrumentation.h"
     41 #include "core/inspector/ScriptDebugListener.h"
     42 #include "core/page/Frame.h"
     43 #include "core/page/Page.h"
     44 #include "wtf/OwnPtr.h"
     45 #include "wtf/PassOwnPtr.h"
     46 #include "wtf/StdLibExtras.h"
     47 
     48 namespace WebCore {
     49 
     50 static Frame* retrieveFrameWithGlobalObjectCheck(v8::Handle<v8::Context> context)
     51 {
     52     if (context.IsEmpty())
     53         return 0;
     54 
     55     // Test that context has associated global dom window object.
     56     v8::Handle<v8::Object> global = context->Global();
     57     if (global.IsEmpty())
     58         return 0;
     59 
     60     global = global->FindInstanceInPrototypeChain(V8Window::GetTemplate(context->GetIsolate(), worldTypeInMainThread(context->GetIsolate())));
     61     if (global.IsEmpty())
     62         return 0;
     63 
     64     return toFrameIfNotDetached(context);
     65 }
     66 
     67 PageScriptDebugServer& PageScriptDebugServer::shared()
     68 {
     69     DEFINE_STATIC_LOCAL(PageScriptDebugServer, server, ());
     70     return server;
     71 }
     72 
     73 PageScriptDebugServer::PageScriptDebugServer()
     74     : ScriptDebugServer(v8::Isolate::GetCurrent())
     75     , m_pausedPage(0)
     76 {
     77 }
     78 
     79 void PageScriptDebugServer::addListener(ScriptDebugListener* listener, Page* page)
     80 {
     81     ScriptController* scriptController = page->mainFrame()->script();
     82     if (!scriptController->canExecuteScripts(NotAboutToExecuteScript))
     83         return;
     84 
     85     v8::HandleScope scope(m_isolate);
     86     v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
     87     v8::Context::Scope contextScope(debuggerContext);
     88 
     89     v8::Local<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isolate);
     90     if (!m_listenersMap.size()) {
     91         ensureDebuggerScriptCompiled();
     92         ASSERT(!debuggerScript->IsUndefined());
     93         v8::Debug::SetDebugEventListener2(&PageScriptDebugServer::v8DebugEventCallback, v8::External::New(this));
     94     }
     95     m_listenersMap.set(page, listener);
     96 
     97     V8WindowShell* shell = scriptController->existingWindowShell(mainThreadNormalWorld());
     98     if (!shell || !shell->isContextInitialized())
     99         return;
    100     v8::Local<v8::Context> context = shell->context();
    101     v8::Handle<v8::Function> getScriptsFunction = v8::Local<v8::Function>::Cast(debuggerScript->Get(v8::String::NewSymbol("getScripts")));
    102     v8::Handle<v8::Value> argv[] = { context->GetEmbedderData(0) };
    103     v8::Handle<v8::Value> value = V8ScriptRunner::callInternalFunction(getScriptsFunction, debuggerScript, WTF_ARRAY_LENGTH(argv), argv, m_isolate);
    104     if (value.IsEmpty())
    105         return;
    106     ASSERT(!value->IsUndefined() && value->IsArray());
    107     v8::Handle<v8::Array> scriptsArray = v8::Handle<v8::Array>::Cast(value);
    108     for (unsigned i = 0; i < scriptsArray->Length(); ++i)
    109         dispatchDidParseSource(listener, v8::Handle<v8::Object>::Cast(scriptsArray->Get(v8::Integer::New(i, m_isolate))));
    110 }
    111 
    112 void PageScriptDebugServer::removeListener(ScriptDebugListener* listener, Page* page)
    113 {
    114     if (!m_listenersMap.contains(page))
    115         return;
    116 
    117     if (m_pausedPage == page)
    118         continueProgram();
    119 
    120     m_listenersMap.remove(page);
    121 
    122     if (m_listenersMap.isEmpty())
    123         v8::Debug::SetDebugEventListener2(0);
    124     // FIXME: Remove all breakpoints set by the agent.
    125 }
    126 
    127 void PageScriptDebugServer::setClientMessageLoop(PassOwnPtr<ClientMessageLoop> clientMessageLoop)
    128 {
    129     m_clientMessageLoop = clientMessageLoop;
    130 }
    131 
    132 void PageScriptDebugServer::compileScript(ScriptState* state, const String& expression, const String& sourceURL, String* scriptId, String* exceptionMessage)
    133 {
    134     ScriptExecutionContext* scriptExecutionContext = state->scriptExecutionContext();
    135     RefPtr<Frame> protect = toDocument(scriptExecutionContext)->frame();
    136     ScriptDebugServer::compileScript(state, expression, sourceURL, scriptId, exceptionMessage);
    137     if (!scriptId->isNull())
    138         m_compiledScriptURLs.set(*scriptId, sourceURL);
    139 }
    140 
    141 void PageScriptDebugServer::clearCompiledScripts()
    142 {
    143     ScriptDebugServer::clearCompiledScripts();
    144     m_compiledScriptURLs.clear();
    145 }
    146 
    147 void PageScriptDebugServer::runScript(ScriptState* state, const String& scriptId, ScriptValue* result, bool* wasThrown, String* exceptionMessage)
    148 {
    149     String sourceURL = m_compiledScriptURLs.take(scriptId);
    150 
    151     ScriptExecutionContext* scriptExecutionContext = state->scriptExecutionContext();
    152     Frame* frame = toDocument(scriptExecutionContext)->frame();
    153     InspectorInstrumentationCookie cookie;
    154     if (frame)
    155         cookie = InspectorInstrumentation::willEvaluateScript(frame, sourceURL, TextPosition::minimumPosition().m_line.oneBasedInt());
    156 
    157     RefPtr<Frame> protect = frame;
    158     ScriptDebugServer::runScript(state, scriptId, result, wasThrown, exceptionMessage);
    159 
    160     if (frame)
    161         InspectorInstrumentation::didEvaluateScript(cookie);
    162 }
    163 
    164 ScriptDebugListener* PageScriptDebugServer::getDebugListenerForContext(v8::Handle<v8::Context> context)
    165 {
    166     v8::HandleScope scope(m_isolate);
    167     Frame* frame = retrieveFrameWithGlobalObjectCheck(context);
    168     if (!frame)
    169         return 0;
    170     return m_listenersMap.get(frame->page());
    171 }
    172 
    173 void PageScriptDebugServer::runMessageLoopOnPause(v8::Handle<v8::Context> context)
    174 {
    175     v8::HandleScope scope(m_isolate);
    176     Frame* frame = retrieveFrameWithGlobalObjectCheck(context);
    177     m_pausedPage = frame->page();
    178 
    179     // Wait for continue or step command.
    180     m_clientMessageLoop->run(m_pausedPage);
    181 
    182     // The listener may have been removed in the nested loop.
    183     if (ScriptDebugListener* listener = m_listenersMap.get(m_pausedPage))
    184         listener->didContinue();
    185 
    186     m_pausedPage = 0;
    187 }
    188 
    189 void PageScriptDebugServer::quitMessageLoopOnPause()
    190 {
    191     m_clientMessageLoop->quitNow();
    192 }
    193 
    194 } // namespace WebCore
    195