initial import
[vuplus_webkit] / Source / WebKit / chromium / src / WebDevToolsAgentImpl.cpp
1 /*
2  * Copyright (C) 2010-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 "WebDevToolsAgentImpl.h"
33
34 #include "DebuggerAgentImpl.h"
35 #include "DebuggerAgentManager.h"
36 #include "ExceptionCode.h"
37 #include "GraphicsContext.h"
38 #include "InjectedScriptHost.h"
39 #include "InspectorBackendDispatcher.h"
40 #include "InspectorController.h"
41 #include "InspectorInstrumentation.h"
42 #include "MemoryCache.h"
43 #include "Page.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"
52 #include "V8Proxy.h"
53 #include "V8Utilities.h"
54 #include "WebDataSource.h"
55 #include "WebDevToolsAgentClient.h"
56 #include "WebFrameImpl.h"
57 #include "WebRect.h"
58 #include "WebString.h"
59 #include "WebURL.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>
68
69 using namespace WebCore;
70
71 namespace WebKit {
72
73 static const char kFrontendConnectedFeatureName[] = "frontend-connected";
74 static const char kInspectorStateFeatureName[] = "inspector-state";
75
76 class ClientMessageLoopAdapter : public PageScriptDebugServer::ClientMessageLoop {
77 public:
78     static void ensureClientMessageLoopCreated(WebDevToolsAgentClient* client)
79     {
80         if (s_instance)
81             return;
82         OwnPtr<ClientMessageLoopAdapter> instance = adoptPtr(new ClientMessageLoopAdapter(adoptPtr(client->createClientMessageLoop())));
83         s_instance = instance.get();
84         PageScriptDebugServer::shared().setClientMessageLoop(instance.release());
85     }
86
87     static void inspectedViewClosed(WebViewImpl* view)
88     {
89         if (s_instance)
90             s_instance->m_frozenViews.remove(view);
91     }
92
93     static void didNavigate()
94     {
95         // Release render thread if necessary.
96         if (s_instance && s_instance->m_running)
97             PageScriptDebugServer::shared().continueProgram();
98     }
99
100 private:
101     ClientMessageLoopAdapter(PassOwnPtr<WebKit::WebDevToolsAgentClient::WebKitClientMessageLoop> messageLoop)
102         : m_running(false)
103         , m_messageLoop(messageLoop) { }
104
105
106     virtual void run(Page* page)
107     {
108         if (m_running)
109             return;
110         m_running = true;
111
112         Vector<WebViewImpl*> views;
113
114         // 1. Disable input events.
115         HashSet<Page*>::const_iterator end =  page->group().pages().end();
116         for (HashSet<Page*>::const_iterator it =  page->group().pages().begin(); it != end; ++it) {
117             WebViewImpl* view = WebViewImpl::fromPage(*it);
118             m_frozenViews.add(view);
119             views.append(view);
120             view->setIgnoreInputEvents(true);
121         }
122
123         // 2. Disable active objects
124         WebView::willEnterModalLoop();
125
126         // 3. Process messages until quitNow is called.
127         m_messageLoop->run();
128
129         // 4. Resume active objects
130         WebView::didExitModalLoop();
131
132         // 5. Resume input events.
133         for (Vector<WebViewImpl*>::iterator it = views.begin(); it != views.end(); ++it) {
134             if (m_frozenViews.contains(*it)) {
135                 // The view was not closed during the dispatch.
136                 (*it)->setIgnoreInputEvents(false);
137             }
138         }
139
140         // 6. All views have been resumed, clear the set.
141         m_frozenViews.clear();
142
143         m_running = false;
144     }
145
146     virtual void quitNow()
147     {
148         m_messageLoop->quitNow();
149     }
150
151     bool m_running;
152     OwnPtr<WebKit::WebDevToolsAgentClient::WebKitClientMessageLoop> m_messageLoop;
153     typedef HashSet<WebViewImpl*> FrozenViewsSet;
154     FrozenViewsSet m_frozenViews;
155     // FIXME: The ownership model for s_instance is somewhat complicated. Can we make this simpler?
156     static ClientMessageLoopAdapter* s_instance;
157 };
158
159 ClientMessageLoopAdapter* ClientMessageLoopAdapter::s_instance = 0;
160
161 class DebuggerTask : public PageScriptDebugServer::Task {
162 public:
163     DebuggerTask(PassOwnPtr<WebDevToolsAgent::MessageDescriptor> descriptor)
164         : m_descriptor(descriptor)
165     {
166     }
167
168     virtual ~DebuggerTask() { }
169     virtual void run()
170     {
171         if (WebDevToolsAgent* webagent = m_descriptor->agent())
172             webagent->dispatchOnInspectorBackend(m_descriptor->message());
173     }
174
175 private:
176     OwnPtr<WebDevToolsAgent::MessageDescriptor> m_descriptor;
177 };
178
179 WebDevToolsAgentImpl::WebDevToolsAgentImpl(
180     WebViewImpl* webViewImpl,
181     WebDevToolsAgentClient* client)
182     : m_hostId(client->hostIdentifier())
183     , m_client(client)
184     , m_webViewImpl(webViewImpl)
185     , m_attached(false)
186 {
187     DebuggerAgentManager::setExposeV8DebuggerProtocol(
188         client->exposeV8DebuggerProtocol());
189 }
190
191 WebDevToolsAgentImpl::~WebDevToolsAgentImpl()
192 {
193     DebuggerAgentManager::onWebViewClosed(m_webViewImpl);
194     ClientMessageLoopAdapter::inspectedViewClosed(m_webViewImpl);
195 }
196
197 void WebDevToolsAgentImpl::attach()
198 {
199     if (m_attached)
200         return;
201
202     if (!m_client->exposeV8DebuggerProtocol())
203         ClientMessageLoopAdapter::ensureClientMessageLoopCreated(m_client);
204
205     m_debuggerAgentImpl = adoptPtr(new DebuggerAgentImpl(m_webViewImpl, this, m_client));
206     m_attached = true;
207 }
208
209 void WebDevToolsAgentImpl::detach()
210 {
211     // Prevent controller from sending messages to the frontend.
212     InspectorController* ic = inspectorController();
213     ic->disconnectFrontend();
214     ic->hideHighlight();
215     ic->close();
216     m_debuggerAgentImpl.clear();
217     m_attached = false;
218 }
219
220 void WebDevToolsAgentImpl::frontendLoaded()
221 {
222     inspectorController()->connectFrontend();
223 }
224
225 void WebDevToolsAgentImpl::didNavigate()
226 {
227     ClientMessageLoopAdapter::didNavigate();
228     DebuggerAgentManager::onNavigate();
229 }
230
231 void WebDevToolsAgentImpl::didClearWindowObject(WebFrameImpl* webframe)
232 {
233     DebuggerAgentManager::setHostId(webframe, m_hostId);
234 }
235
236 void WebDevToolsAgentImpl::dispatchOnInspectorBackend(const WebString& message)
237 {
238     inspectorController()->dispatchMessageFromFrontend(message);
239 }
240
241 void WebDevToolsAgentImpl::inspectElementAt(const WebPoint& point)
242 {
243     m_webViewImpl->inspectElementAt(point);
244 }
245
246 void WebDevToolsAgentImpl::setRuntimeProperty(const WebString& name, const WebString& value)
247 {
248     if (name == kInspectorStateFeatureName) {
249         InspectorController* ic = inspectorController();
250         ic->restoreInspectorStateFromCookie(value);
251     }
252 }
253
254 InspectorController* WebDevToolsAgentImpl::inspectorController()
255 {
256     if (Page* page = m_webViewImpl->page())
257         return page->inspectorController();
258     return 0;
259 }
260
261 Frame* WebDevToolsAgentImpl::mainFrame()
262 {
263     if (Page* page = m_webViewImpl->page())
264         return page->mainFrame();
265     return 0;
266 }
267
268 void WebDevToolsAgentImpl::inspectorDestroyed()
269 {
270     // Our lifetime is bound to the WebViewImpl.
271 }
272
273 void WebDevToolsAgentImpl::openInspectorFrontend(InspectorController*)
274 {
275 }
276
277 // PageOverlayClient
278 void WebDevToolsAgentImpl::paintPageOverlay(GraphicsContext& gc)
279 {
280     InspectorController* ic = inspectorController();
281     if (ic)
282         ic->drawHighlight(gc);
283 }
284
285 void WebDevToolsAgentImpl::highlight()
286 {
287     m_webViewImpl->setPageOverlayClient(this);
288 }
289
290 void WebDevToolsAgentImpl::hideHighlight()
291 {
292     m_webViewImpl->setPageOverlayClient(0);
293 }
294
295 bool WebDevToolsAgentImpl::sendMessageToFrontend(const String& message)
296 {
297     WebDevToolsAgentImpl* devToolsAgent = static_cast<WebDevToolsAgentImpl*>(m_webViewImpl->devToolsAgent());
298     if (!devToolsAgent)
299         return false;
300
301     m_client->sendMessageToInspectorFrontend(message);
302     return true;
303 }
304
305 void WebDevToolsAgentImpl::updateInspectorStateCookie(const String& state)
306 {
307     m_client->runtimePropertyChanged(kInspectorStateFeatureName, state);
308 }
309
310 void WebDevToolsAgentImpl::clearBrowserCache()
311 {
312     m_client->clearBrowserCache();
313 }
314
315 void WebDevToolsAgentImpl::clearBrowserCookies()
316 {
317     m_client->clearBrowserCookies();
318 }
319
320 void WebDevToolsAgentImpl::setProcessId(long processId)
321 {
322     inspectorController()->setProcessId(processId);
323 }
324
325 void WebDevToolsAgentImpl::evaluateInWebInspector(long callId, const WebString& script)
326 {
327     InspectorController* ic = inspectorController();
328     ic->evaluateForTestInFrontend(callId, script);
329 }
330
331 void WebDevToolsAgentImpl::setJavaScriptProfilingEnabled(bool enabled)
332 {
333     InspectorController* ic = inspectorController();
334     if (enabled)
335         ic->enableProfiler();
336     else
337         ic->disableProfiler();
338 }
339
340 void WebDevToolsAgent::executeDebuggerCommand(const WebString& command, int callerId)
341 {
342     DebuggerAgentManager::executeDebuggerCommand(command, callerId);
343 }
344
345 void WebDevToolsAgent::debuggerPauseScript()
346 {
347     DebuggerAgentManager::pauseScript();
348 }
349
350 void WebDevToolsAgent::interruptAndDispatch(MessageDescriptor* rawDescriptor)
351 {
352     // rawDescriptor can't be a PassOwnPtr because interruptAndDispatch is a WebKit API function.
353     OwnPtr<MessageDescriptor> descriptor = adoptPtr(rawDescriptor);
354     OwnPtr<DebuggerTask> task = adoptPtr(new DebuggerTask(descriptor.release()));
355     PageScriptDebugServer::interruptAndRun(task.release());
356 }
357
358 bool WebDevToolsAgent::shouldInterruptForMessage(const WebString& message)
359 {
360     String commandName;
361     if (!InspectorBackendDispatcher::getCommandName(message, &commandName))
362         return false;
363     return commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kDebugger_pauseCmd]
364         || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kDebugger_setBreakpointCmd]
365         || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kDebugger_setBreakpointByUrlCmd]
366         || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kDebugger_removeBreakpointCmd]
367         || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kDebugger_setBreakpointsActiveCmd]
368         || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kProfiler_startCmd]
369         || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kProfiler_stopCmd]
370         || commandName == InspectorBackendDispatcher::commandNames[InspectorBackendDispatcher::kProfiler_getProfileCmd];
371 }
372
373 void WebDevToolsAgent::processPendingMessages()
374 {
375     PageScriptDebugServer::shared().runPendingTasks();
376 }
377
378 void WebDevToolsAgent::setMessageLoopDispatchHandler(MessageLoopDispatchHandler handler)
379 {
380     DebuggerAgentManager::setMessageLoopDispatchHandler(handler);
381 }
382
383 } // namespace WebKit