WebKit/chromium/src/WebDevToolsFrontendImpl.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 "WebDevToolsFrontendImpl.h"
       
    33 
       
    34 #include "BoundObject.h"
       
    35 #include "ContextMenuController.h"
       
    36 #include "ContextMenuItem.h"
       
    37 #include "DOMWindow.h"
       
    38 #include "DebuggerAgent.h"
       
    39 #include "DevToolsRPCJS.h"
       
    40 #include "Document.h"
       
    41 #include "Event.h"
       
    42 #include "Frame.h"
       
    43 #include "InspectorBackend.h"
       
    44 #include "InspectorController.h"
       
    45 #include "InspectorFrontendClientImpl.h"
       
    46 #include "InspectorFrontendHost.h"
       
    47 #include "Node.h"
       
    48 #include "Page.h"
       
    49 #include "Pasteboard.h"
       
    50 #include "PlatformString.h"
       
    51 #include "ProfilerAgent.h"
       
    52 #include "SecurityOrigin.h"
       
    53 #include "Settings.h"
       
    54 #include "ToolsAgent.h"
       
    55 #include "V8Binding.h"
       
    56 #include "V8DOMWrapper.h"
       
    57 #include "V8InspectorFrontendHost.h"
       
    58 #include "V8MouseEvent.h"
       
    59 #include "V8Node.h"
       
    60 #include "V8Proxy.h"
       
    61 #include "V8Utilities.h"
       
    62 #include "WebDevToolsFrontendClient.h"
       
    63 #include "WebFrameImpl.h"
       
    64 #include "WebScriptSource.h"
       
    65 #include "WebViewImpl.h"
       
    66 #include <wtf/OwnPtr.h>
       
    67 #include <wtf/Vector.h>
       
    68 
       
    69 using namespace WebCore;
       
    70 
       
    71 namespace WebKit {
       
    72 
       
    73 static v8::Local<v8::String> ToV8String(const String& s)
       
    74 {
       
    75     if (s.isNull())
       
    76         return v8::Local<v8::String>();
       
    77 
       
    78     return v8::String::New(reinterpret_cast<const uint16_t*>(s.characters()), s.length());
       
    79 }
       
    80 
       
    81 DEFINE_RPC_JS_BOUND_OBJ(DebuggerAgent, DEBUGGER_AGENT_STRUCT, DebuggerAgentDelegate, DEBUGGER_AGENT_DELEGATE_STRUCT)
       
    82 DEFINE_RPC_JS_BOUND_OBJ(ProfilerAgent, PROFILER_AGENT_STRUCT, ProfilerAgentDelegate, PROFILER_AGENT_DELEGATE_STRUCT)
       
    83 DEFINE_RPC_JS_BOUND_OBJ(ToolsAgent, TOOLS_AGENT_STRUCT, ToolsAgentDelegate, TOOLS_AGENT_DELEGATE_STRUCT)
       
    84 
       
    85 WebDevToolsFrontend* WebDevToolsFrontend::create(
       
    86     WebView* view,
       
    87     WebDevToolsFrontendClient* client,
       
    88     const WebString& applicationLocale)
       
    89 {
       
    90     return new WebDevToolsFrontendImpl(
       
    91       static_cast<WebViewImpl*>(view),
       
    92       client,
       
    93       applicationLocale);
       
    94 }
       
    95 
       
    96 WebDevToolsFrontendImpl::WebDevToolsFrontendImpl(
       
    97     WebViewImpl* webViewImpl,
       
    98     WebDevToolsFrontendClient* client,
       
    99     const String& applicationLocale)
       
   100     : m_webViewImpl(webViewImpl)
       
   101     , m_client(client)
       
   102     , m_applicationLocale(applicationLocale)
       
   103     , m_loaded(false)
       
   104 {
       
   105     InspectorController* ic = m_webViewImpl->page()->inspectorController();
       
   106     ic->setInspectorFrontendClient(new InspectorFrontendClientImpl(m_webViewImpl->page(), m_client, this));
       
   107 
       
   108     // Put each DevTools frontend Page into its own (single page) group so that it's not
       
   109     // deferred along with the inspected page.
       
   110     m_webViewImpl->page()->setGroupName(String());
       
   111 
       
   112     WebFrameImpl* frame = m_webViewImpl->mainFrameImpl();
       
   113     v8::HandleScope scope;
       
   114     v8::Handle<v8::Context> frameContext = V8Proxy::context(frame->frame());
       
   115 
       
   116     m_debuggerAgentObj.set(new JSDebuggerAgentBoundObj(this, frameContext, "RemoteDebuggerAgent"));
       
   117     m_profilerAgentObj.set(new JSProfilerAgentBoundObj(this, frameContext, "RemoteProfilerAgent"));
       
   118     m_toolsAgentObj.set(new JSToolsAgentBoundObj(this, frameContext, "RemoteToolsAgent"));
       
   119 
       
   120     // Debugger commands should be sent using special method.
       
   121     BoundObject debuggerCommandExecutorObj(frameContext, this, "RemoteDebuggerCommandExecutor");
       
   122     debuggerCommandExecutorObj.addProtoFunction(
       
   123         "DebuggerCommand",
       
   124         WebDevToolsFrontendImpl::jsDebuggerCommand);
       
   125     debuggerCommandExecutorObj.addProtoFunction(
       
   126         "DebuggerPauseScript",
       
   127         WebDevToolsFrontendImpl::jsDebuggerPauseScript);
       
   128     debuggerCommandExecutorObj.build();
       
   129 }
       
   130 
       
   131 WebDevToolsFrontendImpl::~WebDevToolsFrontendImpl()
       
   132 {
       
   133 }
       
   134 
       
   135 void WebDevToolsFrontendImpl::dispatchMessageFromAgent(const WebDevToolsMessageData& data)
       
   136 {
       
   137     Vector<String> v;
       
   138     v.append(data.className);
       
   139     v.append(data.methodName);
       
   140     for (size_t i = 0; i < data.arguments.size(); i++)
       
   141         v.append(data.arguments[i]);
       
   142     if (!m_loaded) {
       
   143         m_pendingIncomingMessages.append(v);
       
   144         return;
       
   145     }
       
   146     executeScript(v);
       
   147 }
       
   148 
       
   149 void WebDevToolsFrontendImpl::frontendLoaded()
       
   150 {
       
   151     m_loaded = true;
       
   152 
       
   153     // Grant the devtools page the ability to have source view iframes.
       
   154     SecurityOrigin* origin = m_webViewImpl->page()->mainFrame()->domWindow()->securityOrigin();
       
   155     origin->grantUniversalAccess();
       
   156 
       
   157     for (Vector<Vector<String> >::iterator it = m_pendingIncomingMessages.begin();
       
   158          it != m_pendingIncomingMessages.end();
       
   159          ++it) {
       
   160         executeScript(*it);
       
   161     }
       
   162     m_pendingIncomingMessages.clear();
       
   163 }
       
   164 
       
   165 void WebDevToolsFrontendImpl::executeScript(const Vector<String>& v)
       
   166 {
       
   167     WebFrameImpl* frame = m_webViewImpl->mainFrameImpl();
       
   168     v8::HandleScope scope;
       
   169     v8::Handle<v8::Context> frameContext = V8Proxy::context(frame->frame());
       
   170     v8::Context::Scope contextScope(frameContext);
       
   171     v8::Handle<v8::Value> dispatchFunction = frameContext->Global()->Get(v8::String::New("devtools$$dispatch"));
       
   172     ASSERT(dispatchFunction->IsFunction());
       
   173     v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(dispatchFunction);
       
   174     Vector< v8::Handle<v8::Value> > args;
       
   175     for (size_t i = 0; i < v.size(); i++)
       
   176         args.append(ToV8String(v.at(i)));
       
   177     v8::TryCatch tryCatch;
       
   178     tryCatch.SetVerbose(true);
       
   179     function->Call(frameContext->Global(), args.size(), args.data());
       
   180 }
       
   181 
       
   182 void WebDevToolsFrontendImpl::sendRpcMessage(const WebDevToolsMessageData& data)
       
   183 {
       
   184     m_client->sendMessageToAgent(data);
       
   185 }
       
   186 
       
   187 v8::Handle<v8::Value> WebDevToolsFrontendImpl::jsDebuggerCommand(const v8::Arguments& args)
       
   188 {
       
   189     WebDevToolsFrontendImpl* frontend = static_cast<WebDevToolsFrontendImpl*>(v8::External::Cast(*args.Data())->Value());
       
   190     WebString command = WebCore::toWebCoreStringWithNullCheck(args[0]);
       
   191     frontend->m_client->sendDebuggerCommandToAgent(command);
       
   192     return v8::Undefined();
       
   193 }
       
   194 
       
   195 v8::Handle<v8::Value> WebDevToolsFrontendImpl::jsDebuggerPauseScript(const v8::Arguments& args)
       
   196 {
       
   197     WebDevToolsFrontendImpl* frontend = static_cast<WebDevToolsFrontendImpl*>(v8::External::Cast(*args.Data())->Value());
       
   198     frontend->m_client->sendDebuggerPauseScript();
       
   199     return v8::Undefined();
       
   200 }
       
   201 
       
   202 } // namespace WebKit