WebKit/chromium/src/DebuggerAgentImpl.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 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 "DebuggerAgentImpl.h"
       
    33 
       
    34 #include "DebuggerAgentManager.h"
       
    35 #include "Document.h"
       
    36 #include "Frame.h"
       
    37 #include "Page.h"
       
    38 #include "ScriptDebugServer.h"
       
    39 #include "V8Binding.h"
       
    40 #include "WebDevToolsAgentImpl.h"
       
    41 #include "WebViewImpl.h"
       
    42 #include <wtf/HashSet.h>
       
    43 #include <wtf/RefPtr.h>
       
    44 #include <wtf/Vector.h>
       
    45 
       
    46 using WebCore::DOMWindow;
       
    47 using WebCore::Document;
       
    48 using WebCore::Frame;
       
    49 using WebCore::Page;
       
    50 using WebCore::String;
       
    51 
       
    52 namespace WebKit {
       
    53 
       
    54 DebuggerAgentImpl::DebuggerAgentImpl(
       
    55     WebViewImpl* webViewImpl,
       
    56     DebuggerAgentDelegate* delegate,
       
    57     WebDevToolsAgentImpl* webdevtoolsAgent)
       
    58     : m_webViewImpl(webViewImpl)
       
    59     , m_delegate(delegate)
       
    60     , m_webdevtoolsAgent(webdevtoolsAgent)
       
    61     , m_autoContinueOnException(false)
       
    62 {
       
    63     DebuggerAgentManager::debugAttach(this);
       
    64 }
       
    65 
       
    66 DebuggerAgentImpl::~DebuggerAgentImpl()
       
    67 {
       
    68     DebuggerAgentManager::debugDetach(this);
       
    69 }
       
    70 
       
    71 void DebuggerAgentImpl::getContextId()
       
    72 {
       
    73     m_delegate->setContextId(m_webdevtoolsAgent->hostId());
       
    74 }
       
    75 
       
    76 void DebuggerAgentImpl::processDebugCommands()
       
    77 {
       
    78     DebuggerAgentManager::UtilityContextScope utilityScope;
       
    79     v8::Debug::ProcessDebugMessages();
       
    80 }
       
    81 
       
    82 void DebuggerAgentImpl::debuggerOutput(const String& command)
       
    83 {
       
    84     m_delegate->debuggerOutput(command);
       
    85     m_webdevtoolsAgent->forceRepaint();
       
    86 }
       
    87 
       
    88 String DebuggerAgentImpl::executeUtilityFunction(
       
    89     v8::Handle<v8::Context> context,
       
    90     int callId,
       
    91     const char* object,
       
    92     const String &functionName,
       
    93     const String& jsonArgs,
       
    94     bool async,
       
    95     String* exception)
       
    96 {
       
    97     v8::HandleScope scope;
       
    98     ASSERT(!context.IsEmpty());
       
    99     if (context.IsEmpty()) {
       
   100         *exception = "No window context.";
       
   101         return "";
       
   102     }
       
   103     v8::Context::Scope contextScope(context);
       
   104 
       
   105     DebuggerAgentManager::UtilityContextScope utilityScope;
       
   106 
       
   107     v8::Handle<v8::Object> dispatchObject = v8::Handle<v8::Object>::Cast(
       
   108         context->Global()->Get(v8::String::New(object)));
       
   109 
       
   110     v8::Handle<v8::Value> dispatchFunction = dispatchObject->Get(v8::String::New("dispatch"));
       
   111     ASSERT(dispatchFunction->IsFunction());
       
   112     v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(dispatchFunction);
       
   113 
       
   114     v8::Handle<v8::String> functionNameWrapper = v8::Handle<v8::String>(
       
   115         v8::String::New(functionName.utf8().data()));
       
   116     v8::Handle<v8::String> jsonArgsWrapper = v8::Handle<v8::String>(
       
   117         v8::String::New(jsonArgs.utf8().data()));
       
   118     v8::Handle<v8::Number> callIdWrapper = v8::Handle<v8::Number>(
       
   119         v8::Number::New(async ? callId : 0));
       
   120 
       
   121     v8::Handle<v8::Value> args[] = {
       
   122         functionNameWrapper,
       
   123         jsonArgsWrapper,
       
   124         callIdWrapper
       
   125     };
       
   126 
       
   127     v8::TryCatch tryCatch;
       
   128     v8::Handle<v8::Value> resObj = function->Call(context->Global(), 3, args);
       
   129     if (tryCatch.HasCaught()) {
       
   130         v8::Local<v8::Message> message = tryCatch.Message();
       
   131         if (message.IsEmpty())
       
   132             *exception = "Unknown exception";
       
   133         else
       
   134             *exception = WebCore::toWebCoreString(message->Get());
       
   135         return "";
       
   136     }
       
   137     return WebCore::toWebCoreStringWithNullCheck(resObj);
       
   138 }
       
   139 
       
   140 WebCore::Page* DebuggerAgentImpl::page()
       
   141 {
       
   142     return m_webViewImpl->page();
       
   143 }
       
   144 
       
   145 } // namespace WebKit