Web Inspector: convert workers inspection into capability
[WebKit-https.git] / Source / WebCore / inspector / InspectorFrontendHost.cpp
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31
32 #if ENABLE(INSPECTOR)
33
34 #include "InspectorFrontendHost.h"
35
36 #include "ContextMenu.h"
37 #include "ContextMenuItem.h"
38 #include "ContextMenuController.h"
39 #include "ContextMenuProvider.h"
40 #include "DOMFileSystem.h"
41 #include "Element.h"
42 #include "Frame.h"
43 #include "FrameLoader.h"
44 #include "HitTestResult.h"
45 #include "HTMLFrameOwnerElement.h"
46 #include "InspectorAgent.h"
47 #include "InspectorController.h"
48 #include "InspectorFrontendClient.h"
49 #include "Page.h"
50 #include "Pasteboard.h"
51 #include "ResourceError.h"
52 #include "ResourceRequest.h"
53 #include "ResourceResponse.h"
54 #include "ScriptFunctionCall.h"
55 #include "UserGestureIndicator.h"
56 #include <wtf/StdLibExtras.h>
57
58 using namespace std;
59
60 namespace WebCore {
61
62 #if ENABLE(CONTEXT_MENUS)
63 class FrontendMenuProvider : public ContextMenuProvider {
64 public:
65     static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
66     {
67         return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
68     }
69     
70     void disconnect()
71     {
72         m_frontendApiObject = ScriptObject();
73         m_frontendHost = 0;
74     }
75     
76 private:
77     FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
78         : m_frontendHost(frontendHost)
79         , m_frontendApiObject(frontendApiObject)
80         , m_items(items)
81     {
82     }
83
84     virtual ~FrontendMenuProvider()
85     {
86         contextMenuCleared();
87     }
88     
89     virtual void populateContextMenu(ContextMenu* menu)
90     {
91         for (size_t i = 0; i < m_items.size(); ++i)
92             menu->appendItem(m_items[i]);
93     }
94     
95     virtual void contextMenuItemSelected(ContextMenuItem* item)
96     {
97         if (m_frontendHost) {
98             UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
99             int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
100
101             ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected");
102             function.appendArgument(itemNumber);
103             function.call();
104         }
105     }
106     
107     virtual void contextMenuCleared()
108     {
109         if (m_frontendHost) {
110             ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared");
111             function.call();
112
113             m_frontendHost->m_menuProvider = 0;
114         }
115         m_items.clear();
116     }
117
118     InspectorFrontendHost* m_frontendHost;
119     ScriptObject m_frontendApiObject;
120     Vector<ContextMenuItem> m_items;
121 };
122 #endif
123
124 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
125     : m_client(client)
126     , m_frontendPage(frontendPage)
127 #if ENABLE(CONTEXT_MENUS)
128     , m_menuProvider(0)
129 #endif
130 {
131 }
132
133 InspectorFrontendHost::~InspectorFrontendHost()
134 {
135     ASSERT(!m_client);
136 }
137
138 void InspectorFrontendHost::disconnectClient()
139 {
140     m_client = 0;
141 #if ENABLE(CONTEXT_MENUS)
142     if (m_menuProvider)
143         m_menuProvider->disconnect();
144 #endif
145     m_frontendPage = 0;
146 }
147
148 void InspectorFrontendHost::loaded()
149 {
150     if (m_client)
151         m_client->frontendLoaded();
152 }
153
154 void InspectorFrontendHost::requestSetDockSide(const String& side)
155 {
156     if (!m_client)
157         return;
158     if (side == "undocked")
159         m_client->requestSetDockSide(InspectorFrontendClient::UNDOCKED);
160     else if (side == "right")
161         m_client->requestSetDockSide(InspectorFrontendClient::DOCKED_TO_RIGHT);
162     else if (side == "bottom")
163         m_client->requestSetDockSide(InspectorFrontendClient::DOCKED_TO_BOTTOM);
164 }
165
166 void InspectorFrontendHost::closeWindow()
167 {
168     if (m_client) {
169         m_client->closeWindow();
170         disconnectClient(); // Disconnect from client.
171     }
172 }
173
174 void InspectorFrontendHost::bringToFront()
175 {
176     if (m_client)
177         m_client->bringToFront();
178 }
179
180 void InspectorFrontendHost::setZoomFactor(float zoom)
181 {
182     m_frontendPage->mainFrame()->setPageAndTextZoomFactors(zoom, 1);
183 }
184
185 void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
186 {
187     if (m_client)
188         m_client->inspectedURLChanged(newURL);
189 }
190
191 void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
192 {
193     if (m_client)
194         m_client->changeAttachedWindowHeight(height);
195 }
196
197 void InspectorFrontendHost::moveWindowBy(float x, float y) const
198 {
199     if (m_client)
200         m_client->moveWindowBy(x, y);
201 }
202
203 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
204 {
205     ASSERT(m_frontendPage->inspectorController());
206     m_frontendPage->inspectorController()->setInjectedScriptForOrigin(origin, script);
207 }
208
209 String InspectorFrontendHost::localizedStringsURL()
210 {
211     return m_client ? m_client->localizedStringsURL() : "";
212 }
213
214 String InspectorFrontendHost::hiddenPanels()
215 {
216     return m_client ? m_client->hiddenPanels() : "";
217 }
218
219 void InspectorFrontendHost::copyText(const String& text)
220 {
221     Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
222 }
223
224 void InspectorFrontendHost::openInNewTab(const String& url)
225 {
226     if (m_client)
227         m_client->openInNewTab(url);
228 }
229
230 bool InspectorFrontendHost::canSave()
231 {
232     if (m_client)
233         return m_client->canSave();
234     return false;
235 }
236
237 void InspectorFrontendHost::save(const String& url, const String& content, bool forceSaveAs)
238 {
239     if (m_client)
240         m_client->save(url, content, forceSaveAs);
241 }
242
243 void InspectorFrontendHost::append(const String& url, const String& content)
244 {
245     if (m_client)
246         m_client->append(url, content);
247 }
248
249 void InspectorFrontendHost::close(const String&)
250 {
251 }
252
253 void InspectorFrontendHost::sendMessageToBackend(const String& message)
254 {
255     if (m_client)
256         m_client->sendMessageToBackend(message);
257 }
258
259 #if ENABLE(CONTEXT_MENUS)
260 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
261 {
262     if (!event)
263         return;
264
265     ASSERT(m_frontendPage);
266     ScriptState* frontendScriptState = scriptStateFromPage(debuggerWorld(), m_frontendPage);
267     ScriptObject frontendApiObject;
268     if (!ScriptGlobalObject::get(frontendScriptState, "InspectorFrontendAPI", frontendApiObject)) {
269         ASSERT_NOT_REACHED();
270         return;
271     }
272     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
273     ContextMenuController* menuController = m_frontendPage->contextMenuController();
274     menuController->showContextMenu(event, menuProvider);
275     m_menuProvider = menuProvider.get();
276 }
277 #endif
278
279 String InspectorFrontendHost::loadResourceSynchronously(const String& url)
280 {
281     ResourceRequest request(url);
282     request.setHTTPMethod("GET");
283
284     Vector<char> data;
285     ResourceError error;
286     ResourceResponse response;
287     m_frontendPage->mainFrame()->loader()->loadResourceSynchronously(request, DoNotAllowStoredCredentials, error, response, data);
288     return String(data.data(), data.size());
289 }
290
291 bool InspectorFrontendHost::supportsFileSystems()
292 {
293     if (m_client)
294         return m_client->supportsFileSystems();
295     return false;
296 }
297
298 void InspectorFrontendHost::requestFileSystems()
299 {
300     if (m_client)
301         m_client->requestFileSystems();
302 }
303
304 void InspectorFrontendHost::addFileSystem()
305 {
306     if (m_client)
307         m_client->addFileSystem();
308 }
309
310 void InspectorFrontendHost::removeFileSystem(const String& fileSystemPath)
311 {
312     if (m_client)
313         m_client->removeFileSystem(fileSystemPath);
314 }
315
316 #if ENABLE(FILE_SYSTEM)
317 PassRefPtr<DOMFileSystem> InspectorFrontendHost::isolatedFileSystem(const String& fileSystemName, const String& rootURL)
318 {
319     ScriptExecutionContext* context = m_frontendPage->mainFrame()->document();
320     return DOMFileSystem::create(context, fileSystemName, FileSystemTypeIsolated, KURL(ParsedURLString, rootURL), AsyncFileSystem::create());
321 }
322 #endif
323
324 bool InspectorFrontendHost::isUnderTest()
325 {
326     return m_client && m_client->isUnderTest();
327 }
328
329 } // namespace WebCore
330
331 #endif // ENABLE(INSPECTOR)