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 #ifndef DebuggerAgentManager_h 32 #define DebuggerAgentManager_h 33 34 #include "WebDevToolsAgent.h" 35 #include <v8-debug.h> 36 #include <wtf/HashMap.h> 37 #include <wtf/Noncopyable.h> 38 39 namespace WebCore { 40 class PageGroupLoadDeferrer; 41 class String; 42 } 43 44 namespace WebKit { 45 46 class DebuggerAgentImpl; 47 class DictionaryValue; 48 class WebFrameImpl; 49 class WebViewImpl; 50 51 // There is single v8 instance per render process. Also there may be several 52 // RenderViews and consequently devtools agents in the process that want to talk 53 // to the v8 debugger. This class coordinates communication between the debug 54 // agents and v8 debugger. It will set debug output handler as long as at least 55 // one debugger agent is attached and remove it when last debugger agent is 56 // detached. When message is received from debugger it will route it to the 57 // right debugger agent if there is one otherwise the message will be ignored. 58 // 59 // v8 may send a message(e.g. exception event) after which it 60 // would expect some actions from the handler. If there is no appropriate 61 // debugger agent to handle such messages the manager will perform the action 62 // itself, otherwise v8 may hang waiting for the action. 63 class DebuggerAgentManager : public Noncopyable { 64 public: 65 static void debugAttach(DebuggerAgentImpl* debuggerAgent); 66 static void debugDetach(DebuggerAgentImpl* debuggerAgent); 67 static void pauseScript(); 68 static void executeDebuggerCommand(const WebCore::String& command, int callerId); 69 static void setMessageLoopDispatchHandler(WebDevToolsAgent::MessageLoopDispatchHandler handler); 70 71 // Sets |hostId| as the frame context data. This id is used to filter scripts 72 // related to the inspected page. 73 static void setHostId(WebFrameImpl* webframe, int hostId); 74 75 static void onWebViewClosed(WebViewImpl* webview); 76 77 static void onNavigate(); 78 79 class UtilityContextScope : public Noncopyable { 80 public: 81 UtilityContextScope() 82 { 83 ASSERT(!s_inUtilityContext); 84 s_inUtilityContext = true; 85 } 86 ~UtilityContextScope() 87 { 88 if (s_debugBreakDelayed) { 89 v8::Debug::DebugBreak(); 90 s_debugBreakDelayed = false; 91 } 92 s_inUtilityContext = false; 93 } 94 }; 95 96 private: 97 DebuggerAgentManager(); 98 ~DebuggerAgentManager(); 99 100 static void debugHostDispatchHandler(); 101 static void onV8DebugMessage(const v8::Debug::Message& message); 102 static void sendCommandToV8(const WebCore::String& cmd, 103 v8::Debug::ClientData* data); 104 static void sendContinueCommandToV8(); 105 106 static DebuggerAgentImpl* findAgentForCurrentV8Context(); 107 static DebuggerAgentImpl* debuggerAgentForHostId(int hostId); 108 109 typedef HashMap<int, DebuggerAgentImpl*> AttachedAgentsMap; 110 static AttachedAgentsMap* s_attachedAgentsMap; 111 112 static WebDevToolsAgent::MessageLoopDispatchHandler s_messageLoopDispatchHandler; 113 static bool s_inHostDispatchHandler; 114 typedef HashMap<WebViewImpl*, WebCore::PageGroupLoadDeferrer*> DeferrersMap; 115 static DeferrersMap s_pageDeferrers; 116 117 static bool s_inUtilityContext; 118 static bool s_debugBreakDelayed; 119 }; 120 121 } // namespace WebKit 122 123 #endif 124