Web Inspector: Support inspector file system access with isolated file system through...
[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 "ScriptFunctionCall.h"
52 #include "UserGestureIndicator.h"
53
54 #include <wtf/RefPtr.h>
55 #include <wtf/StdLibExtras.h>
56
57 using namespace std;
58
59 namespace WebCore {
60
61 #if ENABLE(CONTEXT_MENUS)
62 class FrontendMenuProvider : public ContextMenuProvider {
63 public:
64     static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
65     {
66         return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
67     }
68     
69     void disconnect()
70     {
71         m_frontendApiObject = ScriptObject();
72         m_frontendHost = 0;
73     }
74     
75 private:
76     FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
77         : m_frontendHost(frontendHost)
78         , m_frontendApiObject(frontendApiObject)
79         , m_items(items)
80     {
81     }
82
83     virtual ~FrontendMenuProvider()
84     {
85         contextMenuCleared();
86     }
87     
88     virtual void populateContextMenu(ContextMenu* menu)
89     {
90         for (size_t i = 0; i < m_items.size(); ++i)
91             menu->appendItem(m_items[i]);
92     }
93     
94     virtual void contextMenuItemSelected(ContextMenuItem* item)
95     {
96         if (m_frontendHost) {
97             UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
98             int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
99
100             ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected");
101             function.appendArgument(itemNumber);
102             function.call();
103         }
104     }
105     
106     virtual void contextMenuCleared()
107     {
108         if (m_frontendHost) {
109             ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared");
110             function.call();
111
112             m_frontendHost->m_menuProvider = 0;
113         }
114         m_items.clear();
115     }
116
117     InspectorFrontendHost* m_frontendHost;
118     ScriptObject m_frontendApiObject;
119     Vector<ContextMenuItem> m_items;
120 };
121 #endif
122
123 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
124     : m_client(client)
125     , m_frontendPage(frontendPage)
126 #if ENABLE(CONTEXT_MENUS)
127     , m_menuProvider(0)
128 #endif
129 {
130 }
131
132 InspectorFrontendHost::~InspectorFrontendHost()
133 {
134     ASSERT(!m_client);
135 }
136
137 void InspectorFrontendHost::disconnectClient()
138 {
139     m_client = 0;
140 #if ENABLE(CONTEXT_MENUS)
141     if (m_menuProvider)
142         m_menuProvider->disconnect();
143 #endif
144     m_frontendPage = 0;
145 }
146
147 void InspectorFrontendHost::loaded()
148 {
149     if (m_client)
150         m_client->frontendLoaded();
151 }
152
153 void InspectorFrontendHost::requestSetDockSide(const String& side)
154 {
155     if (!m_client)
156         return;
157     if (side == "undocked")
158         m_client->requestSetDockSide(InspectorFrontendClient::UNDOCKED);
159     else if (side == "right")
160         m_client->requestSetDockSide(InspectorFrontendClient::DOCKED_TO_RIGHT);
161     else if (side == "bottom")
162         m_client->requestSetDockSide(InspectorFrontendClient::DOCKED_TO_BOTTOM);
163 }
164
165 void InspectorFrontendHost::closeWindow()
166 {
167     if (m_client) {
168         m_client->closeWindow();
169         disconnectClient(); // Disconnect from client.
170     }
171 }
172
173 void InspectorFrontendHost::bringToFront()
174 {
175     if (m_client)
176         m_client->bringToFront();
177 }
178
179 void InspectorFrontendHost::setZoomFactor(float zoom)
180 {
181     m_frontendPage->mainFrame()->setPageAndTextZoomFactors(zoom, 1);
182 }
183
184 void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
185 {
186     if (m_client)
187         m_client->inspectedURLChanged(newURL);
188 }
189
190 void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
191 {
192     if (m_client)
193         m_client->changeAttachedWindowHeight(height);
194 }
195
196 void InspectorFrontendHost::moveWindowBy(float x, float y) const
197 {
198     if (m_client)
199         m_client->moveWindowBy(x, y);
200 }
201
202 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
203 {
204     ASSERT(m_frontendPage->inspectorController());
205     m_frontendPage->inspectorController()->setInjectedScriptForOrigin(origin, script);
206 }
207
208 String InspectorFrontendHost::localizedStringsURL()
209 {
210     return m_client ? m_client->localizedStringsURL() : "";
211 }
212
213 String InspectorFrontendHost::hiddenPanels()
214 {
215     return m_client ? m_client->hiddenPanels() : "";
216 }
217
218 void InspectorFrontendHost::copyText(const String& text)
219 {
220     Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
221 }
222
223 void InspectorFrontendHost::openInNewTab(const String& url)
224 {
225     if (m_client)
226         m_client->openInNewTab(url);
227 }
228
229 bool InspectorFrontendHost::canSave()
230 {
231     if (m_client)
232         return m_client->canSave();
233     return false;
234 }
235
236 void InspectorFrontendHost::save(const String& url, const String& content, bool forceSaveAs)
237 {
238     if (m_client)
239         m_client->save(url, content, forceSaveAs);
240 }
241
242 void InspectorFrontendHost::append(const String& url, const String& content)
243 {
244     if (m_client)
245         m_client->append(url, content);
246 }
247
248 void InspectorFrontendHost::close(const String&)
249 {
250 }
251
252 bool InspectorFrontendHost::canInspectWorkers()
253 {
254 #if ENABLE(WORKERS)
255     if (m_client)
256         return m_client->canInspectWorkers();
257     return false;
258 #else
259     return false;
260 #endif
261 }
262
263 void InspectorFrontendHost::sendMessageToBackend(const String& message)
264 {
265     if (m_client)
266         m_client->sendMessageToBackend(message);
267 }
268
269 #if ENABLE(CONTEXT_MENUS)
270 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
271 {
272     ASSERT(m_frontendPage);
273     ScriptState* frontendScriptState = scriptStateFromPage(debuggerWorld(), m_frontendPage);
274     ScriptObject frontendApiObject;
275     if (!ScriptGlobalObject::get(frontendScriptState, "InspectorFrontendAPI", frontendApiObject)) {
276         ASSERT_NOT_REACHED();
277         return;
278     }
279     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
280     ContextMenuController* menuController = m_frontendPage->contextMenuController();
281     menuController->showContextMenu(event, menuProvider);
282     m_menuProvider = menuProvider.get();
283 }
284 #endif
285
286 String InspectorFrontendHost::loadResourceSynchronously(const String& url)
287 {
288     ResourceRequest request(url);
289     request.setHTTPMethod("GET");
290
291     Vector<char> data;
292     ResourceError error;
293     ResourceResponse response;
294     m_frontendPage->mainFrame()->loader()->loadResourceSynchronously(request, DoNotAllowStoredCredentials, error, response, data);
295     return String(data.data(), data.size());
296 }
297
298 bool InspectorFrontendHost::supportsFileSystems()
299 {
300     if (m_client)
301         return m_client->supportsFileSystems();
302     return false;
303 }
304
305 void InspectorFrontendHost::requestFileSystems()
306 {
307     if (m_client)
308         m_client->requestFileSystems();
309 }
310
311 void InspectorFrontendHost::addFileSystem()
312 {
313     if (m_client)
314         m_client->addFileSystem();
315 }
316
317 void InspectorFrontendHost::removeFileSystem(const String& fileSystemPath)
318 {
319     if (m_client)
320         m_client->removeFileSystem(fileSystemPath);
321 }
322
323 #if ENABLE(FILE_SYSTEM)
324 PassRefPtr<DOMFileSystem> InspectorFrontendHost::isolatedFileSystem(const String& fileSystemName, const String& rootURL)
325 {
326     ScriptExecutionContext* context = m_frontendPage->mainFrame()->document();
327     return DOMFileSystem::create(context, fileSystemName, FileSystemTypeIsolated, KURL(ParsedURLString, rootURL), AsyncFileSystem::create());
328 }
329 #endif
330
331 } // namespace WebCore
332
333 #endif // ENABLE(INSPECTOR)