2 * Copyright (C) 2010-2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "WebDevToolsAgentImpl.h"
34 #include "ExceptionCode.h"
35 #include "GraphicsContext.h"
36 #include "InjectedScriptHost.h"
37 #include "InspectorBackendDispatcher.h"
38 #include "InspectorController.h"
39 #include "InspectorFrontend.h"
40 #include "InspectorInstrumentation.h"
41 #include "InspectorProtocolVersion.h"
42 #include "MemoryCache.h"
44 #include "PageGroup.h"
45 #include "PageOverlay.h"
46 #include "PageScriptDebugServer.h"
47 #include "PlatformString.h"
48 #include "ResourceError.h"
49 #include "ResourceRequest.h"
50 #include "ResourceResponse.h"
51 #include "V8Binding.h"
53 #include "V8Utilities.h"
54 #include "WebDataSource.h"
55 #include "WebDevToolsAgentClient.h"
56 #include "WebFrameImpl.h"
58 #include "WebString.h"
60 #include "WebURLError.h"
61 #include "WebURLRequest.h"
62 #include "WebURLResponse.h"
63 #include "WebViewClient.h"
64 #include "WebViewImpl.h"
65 #include <wtf/CurrentTime.h>
66 #include <wtf/Noncopyable.h>
67 #include <wtf/OwnPtr.h>
69 using namespace WebCore;
73 class ClientMessageLoopAdapter : public PageScriptDebugServer::ClientMessageLoop {
75 static void ensureClientMessageLoopCreated(WebDevToolsAgentClient* client)
79 OwnPtr<ClientMessageLoopAdapter> instance = adoptPtr(new ClientMessageLoopAdapter(adoptPtr(client->createClientMessageLoop())));
80 s_instance = instance.get();
81 PageScriptDebugServer::shared().setClientMessageLoop(instance.release());
84 static void inspectedViewClosed(WebViewImpl* view)
87 s_instance->m_frozenViews.remove(view);
90 static void didNavigate()
92 // Release render thread if necessary.
93 if (s_instance && s_instance->m_running)
94 PageScriptDebugServer::shared().continueProgram();
98 ClientMessageLoopAdapter(PassOwnPtr<WebKit::WebDevToolsAgentClient::WebKitClientMessageLoop> messageLoop)
100 , m_messageLoop(messageLoop) { }
103 virtual void run(Page* page)
109 Vector<WebViewImpl*> views;
111 // 1. Disable input events.
112 HashSet<Page*>::const_iterator end = page->group().pages().end();
113 for (HashSet<Page*>::const_iterator it = page->group().pages().begin(); it != end; ++it) {
114 WebViewImpl* view = WebViewImpl::fromPage(*it);
115 m_frozenViews.add(view);
117 view->setIgnoreInputEvents(true);
120 // 2. Disable active objects
121 WebView::willEnterModalLoop();
123 // 3. Process messages until quitNow is called.
124 m_messageLoop->run();
126 // 4. Resume active objects
127 WebView::didExitModalLoop();
129 // 5. Resume input events.
130 for (Vector<WebViewImpl*>::iterator it = views.begin(); it != views.end(); ++it) {
131 if (m_frozenViews.contains(*it)) {
132 // The view was not closed during the dispatch.
133 (*it)->setIgnoreInputEvents(false);
137 // 6. All views have been resumed, clear the set.
138 m_frozenViews.clear();
143 virtual void quitNow()
145 m_messageLoop->quitNow();
149 OwnPtr<WebKit::WebDevToolsAgentClient::WebKitClientMessageLoop> m_messageLoop;
150 typedef HashSet<WebViewImpl*> FrozenViewsSet;
151 FrozenViewsSet m_frozenViews;
152 // FIXME: The ownership model for s_instance is somewhat complicated. Can we make this simpler?
153 static ClientMessageLoopAdapter* s_instance;
156 ClientMessageLoopAdapter* ClientMessageLoopAdapter::s_instance = 0;
158 class DebuggerTask : public PageScriptDebugServer::Task {
160 DebuggerTask(PassOwnPtr<WebDevToolsAgent::MessageDescriptor> descriptor)
161 : m_descriptor(descriptor)
165 virtual ~DebuggerTask() { }
168 if (WebDevToolsAgent* webagent = m_descriptor->agent())
169 webagent->dispatchOnInspectorBackend(m_descriptor->message());
173 OwnPtr<WebDevToolsAgent::MessageDescriptor> m_descriptor;
176 WebDevToolsAgentImpl::WebDevToolsAgentImpl(
177 WebViewImpl* webViewImpl,
178 WebDevToolsAgentClient* client)
179 : m_hostId(client->hostIdentifier())
181 , m_webViewImpl(webViewImpl)
184 ASSERT(m_hostId > 0);
187 WebDevToolsAgentImpl::~WebDevToolsAgentImpl()
189 ClientMessageLoopAdapter::inspectedViewClosed(m_webViewImpl);
192 void WebDevToolsAgentImpl::attach()
197 ClientMessageLoopAdapter::ensureClientMessageLoopCreated(m_client);
198 inspectorController()->connectFrontend();
202 void WebDevToolsAgentImpl::reattach(const WebString& savedState)
207 ClientMessageLoopAdapter::ensureClientMessageLoopCreated(m_client);
208 inspectorController()->restoreInspectorStateFromCookie(savedState);
212 void WebDevToolsAgentImpl::detach()
214 // Prevent controller from sending messages to the frontend.
215 InspectorController* ic = inspectorController();
216 ic->disconnectFrontend();
222 void WebDevToolsAgentImpl::didNavigate()
224 ClientMessageLoopAdapter::didNavigate();
227 void WebDevToolsAgentImpl::didClearWindowObject(WebFrameImpl* webframe)
229 WebCore::V8Proxy* proxy = WebCore::V8Proxy::retrieve(webframe->frame());
231 proxy->setContextDebugId(m_hostId);
234 void WebDevToolsAgentImpl::dispatchOnInspectorBackend(const WebString& message)
236 inspectorController()->dispatchMessageFromFrontend(message);
239 void WebDevToolsAgentImpl::inspectElementAt(const WebPoint& point)
241 m_webViewImpl->inspectElementAt(point);
244 InspectorController* WebDevToolsAgentImpl::inspectorController()
246 if (Page* page = m_webViewImpl->page())
247 return page->inspectorController();
251 Frame* WebDevToolsAgentImpl::mainFrame()
253 if (Page* page = m_webViewImpl->page())
254 return page->mainFrame();
258 void WebDevToolsAgentImpl::inspectorDestroyed()
260 // Our lifetime is bound to the WebViewImpl.
263 void WebDevToolsAgentImpl::openInspectorFrontend(InspectorController*)
267 void WebDevToolsAgentImpl::closeInspectorFrontend()
271 void WebDevToolsAgentImpl::bringFrontendToFront()
276 void WebDevToolsAgentImpl::paintPageOverlay(GraphicsContext& gc)
278 InspectorController* ic = inspectorController();
280 ic->drawHighlight(gc);
283 void WebDevToolsAgentImpl::highlight()
285 m_webViewImpl->setPageOverlayClient(this);
288 void WebDevToolsAgentImpl::hideHighlight()
290 m_webViewImpl->setPageOverlayClient(0);
293 bool WebDevToolsAgentImpl::sendMessageToFrontend(const String& message)
295 WebDevToolsAgentImpl* devToolsAgent = static_cast<WebDevToolsAgentImpl*>(m_webViewImpl->devToolsAgent());
299 m_client->sendMessageToInspectorFrontend(message);
303 void WebDevToolsAgentImpl::updateInspectorStateCookie(const String& state)
305 m_client->saveAgentRuntimeState(state);
308 void WebDevToolsAgentImpl::clearBrowserCache()
310 m_client->clearBrowserCache();
313 void WebDevToolsAgentImpl::clearBrowserCookies()
315 m_client->clearBrowserCookies();
318 void WebDevToolsAgentImpl::setProcessId(long processId)
320 inspectorController()->setProcessId(processId);
323 void WebDevToolsAgentImpl::evaluateInWebInspector(long callId, const WebString& script)
325 InspectorController* ic = inspectorController();
326 ic->evaluateForTestInFrontend(callId, script);
329 void WebDevToolsAgentImpl::setJavaScriptProfilingEnabled(bool enabled)
331 InspectorController* ic = inspectorController();
333 ic->enableProfiler();
335 ic->disableProfiler();
338 WebString WebDevToolsAgent::inspectorProtocolVersion()
340 return WebCore::inspectorProtocolVersion();
343 bool WebDevToolsAgent::supportsInspectorProtocolVersion(const WebString& version)
345 return WebCore::supportsInspectorProtocolVersion(version);
348 void WebDevToolsAgent::interruptAndDispatch(MessageDescriptor* rawDescriptor)
350 // rawDescriptor can't be a PassOwnPtr because interruptAndDispatch is a WebKit API function.
351 OwnPtr<MessageDescriptor> descriptor = adoptPtr(rawDescriptor);
352 OwnPtr<DebuggerTask> task = adoptPtr(new DebuggerTask(descriptor.release()));
353 PageScriptDebugServer::interruptAndRun(task.release());
356 bool WebDevToolsAgent::shouldInterruptForMessage(const WebString& message)
359 if (!InspectorBackendDispatcher::getCommandName(message, &commandName))
361 return commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kDebugger_pauseCmd]
362 || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kDebugger_setBreakpointCmd]
363 || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kDebugger_setBreakpointByUrlCmd]
364 || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kDebugger_removeBreakpointCmd]
365 || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kDebugger_setBreakpointsActiveCmd]
366 || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kProfiler_startCmd]
367 || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kProfiler_stopCmd]
368 || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kProfiler_getProfileCmd];
371 void WebDevToolsAgent::processPendingMessages()
373 PageScriptDebugServer::shared().runPendingTasks();
376 WebString WebDevToolsAgent::disconnectEventAsText()
378 class ChannelImpl : public InspectorFrontendChannel {
380 virtual bool sendMessageToFrontend(const String& message)
387 InspectorFrontend::Worker inspector(&channel);
388 inspector.disconnectedFromWorker();
389 return channel.m_message;
392 } // namespace WebKit