tizen beta release
[framework/web/webkit-efl.git] / 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 "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"
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 class ClientMessageLoopAdapter : public PageScriptDebugServer::ClientMessageLoop {
74 public:
75     static void ensureClientMessageLoopCreated(WebDevToolsAgentClient* client)
76     {
77         if (s_instance)
78             return;
79         OwnPtr<ClientMessageLoopAdapter> instance = adoptPtr(new ClientMessageLoopAdapter(adoptPtr(client->createClientMessageLoop())));
80         s_instance = instance.get();
81         PageScriptDebugServer::shared().setClientMessageLoop(instance.release());
82     }
83
84     static void inspectedViewClosed(WebViewImpl* view)
85     {
86         if (s_instance)
87             s_instance->m_frozenViews.remove(view);
88     }
89
90     static void didNavigate()
91     {
92         // Release render thread if necessary.
93         if (s_instance && s_instance->m_running)
94             PageScriptDebugServer::shared().continueProgram();
95     }
96
97 private:
98     ClientMessageLoopAdapter(PassOwnPtr<WebKit::WebDevToolsAgentClient::WebKitClientMessageLoop> messageLoop)
99         : m_running(false)
100         , m_messageLoop(messageLoop) { }
101
102
103     virtual void run(Page* page)
104     {
105         if (m_running)
106             return;
107         m_running = true;
108
109         Vector<WebViewImpl*> views;
110
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);
116             views.append(view);
117             view->setIgnoreInputEvents(true);
118         }
119
120         // 2. Disable active objects
121         WebView::willEnterModalLoop();
122
123         // 3. Process messages until quitNow is called.
124         m_messageLoop->run();
125
126         // 4. Resume active objects
127         WebView::didExitModalLoop();
128
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);
134             }
135         }
136
137         // 6. All views have been resumed, clear the set.
138         m_frozenViews.clear();
139
140         m_running = false;
141     }
142
143     virtual void quitNow()
144     {
145         m_messageLoop->quitNow();
146     }
147
148     bool m_running;
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;
154 };
155
156 ClientMessageLoopAdapter* ClientMessageLoopAdapter::s_instance = 0;
157
158 class DebuggerTask : public PageScriptDebugServer::Task {
159 public:
160     DebuggerTask(PassOwnPtr<WebDevToolsAgent::MessageDescriptor> descriptor)
161         : m_descriptor(descriptor)
162     {
163     }
164
165     virtual ~DebuggerTask() { }
166     virtual void run()
167     {
168         if (WebDevToolsAgent* webagent = m_descriptor->agent())
169             webagent->dispatchOnInspectorBackend(m_descriptor->message());
170     }
171
172 private:
173     OwnPtr<WebDevToolsAgent::MessageDescriptor> m_descriptor;
174 };
175
176 WebDevToolsAgentImpl::WebDevToolsAgentImpl(
177     WebViewImpl* webViewImpl,
178     WebDevToolsAgentClient* client)
179     : m_hostId(client->hostIdentifier())
180     , m_client(client)
181     , m_webViewImpl(webViewImpl)
182     , m_attached(false)
183 {
184     ASSERT(m_hostId > 0);
185 }
186
187 WebDevToolsAgentImpl::~WebDevToolsAgentImpl()
188 {
189     ClientMessageLoopAdapter::inspectedViewClosed(m_webViewImpl);
190 }
191
192 void WebDevToolsAgentImpl::attach()
193 {
194     if (m_attached)
195         return;
196
197     ClientMessageLoopAdapter::ensureClientMessageLoopCreated(m_client);
198     inspectorController()->connectFrontend();
199     m_attached = true;
200 }
201
202 void WebDevToolsAgentImpl::reattach(const WebString& savedState)
203 {
204     if (m_attached)
205         return;
206
207     ClientMessageLoopAdapter::ensureClientMessageLoopCreated(m_client);
208     inspectorController()->restoreInspectorStateFromCookie(savedState);
209     m_attached = true;
210 }
211
212 void WebDevToolsAgentImpl::detach()
213 {
214     // Prevent controller from sending messages to the frontend.
215     InspectorController* ic = inspectorController();
216     ic->disconnectFrontend();
217     ic->hideHighlight();
218     ic->close();
219     m_attached = false;
220 }
221
222 void WebDevToolsAgentImpl::didNavigate()
223 {
224     ClientMessageLoopAdapter::didNavigate();
225 }
226
227 void WebDevToolsAgentImpl::didClearWindowObject(WebFrameImpl* webframe)
228 {
229     WebCore::V8Proxy* proxy = WebCore::V8Proxy::retrieve(webframe->frame());
230     if (proxy)
231         proxy->setContextDebugId(m_hostId);
232 }
233
234 void WebDevToolsAgentImpl::dispatchOnInspectorBackend(const WebString& message)
235 {
236     inspectorController()->dispatchMessageFromFrontend(message);
237 }
238
239 void WebDevToolsAgentImpl::inspectElementAt(const WebPoint& point)
240 {
241     m_webViewImpl->inspectElementAt(point);
242 }
243
244 InspectorController* WebDevToolsAgentImpl::inspectorController()
245 {
246     if (Page* page = m_webViewImpl->page())
247         return page->inspectorController();
248     return 0;
249 }
250
251 Frame* WebDevToolsAgentImpl::mainFrame()
252 {
253     if (Page* page = m_webViewImpl->page())
254         return page->mainFrame();
255     return 0;
256 }
257
258 void WebDevToolsAgentImpl::inspectorDestroyed()
259 {
260     // Our lifetime is bound to the WebViewImpl.
261 }
262
263 void WebDevToolsAgentImpl::openInspectorFrontend(InspectorController*)
264 {
265 }
266
267 void WebDevToolsAgentImpl::closeInspectorFrontend()
268 {
269 }
270
271 void WebDevToolsAgentImpl::bringFrontendToFront()
272 {
273 }
274
275 // PageOverlayClient
276 void WebDevToolsAgentImpl::paintPageOverlay(GraphicsContext& gc)
277 {
278     InspectorController* ic = inspectorController();
279     if (ic)
280         ic->drawHighlight(gc);
281 }
282
283 void WebDevToolsAgentImpl::highlight()
284 {
285     m_webViewImpl->setPageOverlayClient(this);
286 }
287
288 void WebDevToolsAgentImpl::hideHighlight()
289 {
290     m_webViewImpl->setPageOverlayClient(0);
291 }
292
293 bool WebDevToolsAgentImpl::sendMessageToFrontend(const String& message)
294 {
295     WebDevToolsAgentImpl* devToolsAgent = static_cast<WebDevToolsAgentImpl*>(m_webViewImpl->devToolsAgent());
296     if (!devToolsAgent)
297         return false;
298
299     m_client->sendMessageToInspectorFrontend(message);
300     return true;
301 }
302
303 void WebDevToolsAgentImpl::updateInspectorStateCookie(const String& state)
304 {
305     m_client->saveAgentRuntimeState(state);
306 }
307
308 void WebDevToolsAgentImpl::clearBrowserCache()
309 {
310     m_client->clearBrowserCache();
311 }
312
313 void WebDevToolsAgentImpl::clearBrowserCookies()
314 {
315     m_client->clearBrowserCookies();
316 }
317
318 void WebDevToolsAgentImpl::setProcessId(long processId)
319 {
320     inspectorController()->setProcessId(processId);
321 }
322
323 void WebDevToolsAgentImpl::evaluateInWebInspector(long callId, const WebString& script)
324 {
325     InspectorController* ic = inspectorController();
326     ic->evaluateForTestInFrontend(callId, script);
327 }
328
329 void WebDevToolsAgentImpl::setJavaScriptProfilingEnabled(bool enabled)
330 {
331     InspectorController* ic = inspectorController();
332     if (enabled)
333         ic->enableProfiler();
334     else
335         ic->disableProfiler();
336 }
337
338 WebString WebDevToolsAgent::inspectorProtocolVersion()
339 {
340     return WebCore::inspectorProtocolVersion();
341 }
342
343 bool WebDevToolsAgent::supportsInspectorProtocolVersion(const WebString& version)
344 {
345     return WebCore::supportsInspectorProtocolVersion(version);
346 }
347
348 void WebDevToolsAgent::interruptAndDispatch(MessageDescriptor* rawDescriptor)
349 {
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());
354 }
355
356 bool WebDevToolsAgent::shouldInterruptForMessage(const WebString& message)
357 {
358     String commandName;
359     if (!InspectorBackendDispatcher::getCommandName(message, &commandName))
360         return false;
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];
369 }
370
371 void WebDevToolsAgent::processPendingMessages()
372 {
373     PageScriptDebugServer::shared().runPendingTasks();
374 }
375
376 WebString WebDevToolsAgent::disconnectEventAsText()
377 {
378     class ChannelImpl : public InspectorFrontendChannel {
379     public:
380         virtual bool sendMessageToFrontend(const String& message)
381         {
382             m_message = message;
383             return true;
384         }
385         String m_message;
386     } channel;
387     InspectorFrontend::Worker inspector(&channel);
388     inspector.disconnectedFromWorker();
389     return channel.m_message;
390 }
391
392 } // namespace WebKit