<https://webkit.org/b/119859> Frame::loader() should return a reference
[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 "DOMWrapperWorld.h"
42 #include "Element.h"
43 #include "Frame.h"
44 #include "FrameLoader.h"
45 #include "HitTestResult.h"
46 #include "HTMLFrameOwnerElement.h"
47 #include "InspectorAgent.h"
48 #include "InspectorController.h"
49 #include "InspectorFrontendClient.h"
50 #include "Page.h"
51 #include "Pasteboard.h"
52 #include "ResourceError.h"
53 #include "ResourceRequest.h"
54 #include "ResourceResponse.h"
55 #include "ScriptFunctionCall.h"
56 #include "UserGestureIndicator.h"
57 #include <wtf/StdLibExtras.h>
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::setAttachedWindowWidth(unsigned width)
197 {
198     if (m_client)
199         m_client->changeAttachedWindowWidth(width);
200 }
201
202 void InspectorFrontendHost::setToolbarHeight(unsigned height)
203 {
204     if (m_client)
205         m_client->setToolbarHeight(height);
206 }
207
208 void InspectorFrontendHost::moveWindowBy(float x, float y) const
209 {
210     if (m_client)
211         m_client->moveWindowBy(x, y);
212 }
213
214 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
215 {
216     ASSERT(m_frontendPage->inspectorController());
217     m_frontendPage->inspectorController()->setInjectedScriptForOrigin(origin, script);
218 }
219
220 String InspectorFrontendHost::localizedStringsURL()
221 {
222     return m_client ? m_client->localizedStringsURL() : "";
223 }
224
225 void InspectorFrontendHost::copyText(const String& text)
226 {
227     Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
228 }
229
230 void InspectorFrontendHost::openInNewTab(const String& url)
231 {
232     if (m_client)
233         m_client->openInNewTab(url);
234 }
235
236 bool InspectorFrontendHost::canSave()
237 {
238     if (m_client)
239         return m_client->canSave();
240     return false;
241 }
242
243 void InspectorFrontendHost::save(const String& url, const String& content, bool forceSaveAs)
244 {
245     if (m_client)
246         m_client->save(url, content, forceSaveAs);
247 }
248
249 void InspectorFrontendHost::append(const String& url, const String& content)
250 {
251     if (m_client)
252         m_client->append(url, content);
253 }
254
255 void InspectorFrontendHost::close(const String&)
256 {
257 }
258
259 void InspectorFrontendHost::sendMessageToBackend(const String& message)
260 {
261     if (m_client)
262         m_client->sendMessageToBackend(message);
263 }
264
265 #if ENABLE(CONTEXT_MENUS)
266 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
267 {
268     if (!event)
269         return;
270
271     ASSERT(m_frontendPage);
272     ScriptState* frontendScriptState = scriptStateFromPage(debuggerWorld(), m_frontendPage);
273     ScriptObject frontendApiObject;
274     if (!ScriptGlobalObject::get(frontendScriptState, "InspectorFrontendAPI", frontendApiObject)) {
275         ASSERT_NOT_REACHED();
276         return;
277     }
278     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
279     ContextMenuController* menuController = m_frontendPage->contextMenuController();
280     menuController->showContextMenu(event, menuProvider);
281     m_menuProvider = menuProvider.get();
282 }
283 #endif
284
285 String InspectorFrontendHost::loadResourceSynchronously(const String& url)
286 {
287     ResourceRequest request(url);
288     request.setHTTPMethod("GET");
289
290     Vector<char> data;
291     ResourceError error;
292     ResourceResponse response;
293     m_frontendPage->mainFrame()->loader().loadResourceSynchronously(request, DoNotAllowStoredCredentials, DoNotAskClientForCrossOriginCredentials, error, response, data);
294     return String::fromUTF8(data.data(), data.size());
295 }
296
297 bool InspectorFrontendHost::supportsFileSystems()
298 {
299     if (m_client)
300         return m_client->supportsFileSystems();
301     return false;
302 }
303
304 void InspectorFrontendHost::requestFileSystems()
305 {
306     if (m_client)
307         m_client->requestFileSystems();
308 }
309
310 void InspectorFrontendHost::addFileSystem()
311 {
312     if (m_client)
313         m_client->addFileSystem();
314 }
315
316 void InspectorFrontendHost::removeFileSystem(const String& fileSystemPath)
317 {
318     if (m_client)
319         m_client->removeFileSystem(fileSystemPath);
320 }
321
322 #if ENABLE(FILE_SYSTEM)
323 PassRefPtr<DOMFileSystem> InspectorFrontendHost::isolatedFileSystem(const String& fileSystemName, const String& rootURL)
324 {
325     ScriptExecutionContext* context = m_frontendPage->mainFrame()->document();
326     return DOMFileSystem::create(context, fileSystemName, FileSystemTypeIsolated, KURL(ParsedURLString, rootURL), AsyncFileSystem::create());
327 }
328 #endif
329
330 bool InspectorFrontendHost::isUnderTest()
331 {
332     return m_client && m_client->isUnderTest();
333 }
334
335 bool InspectorFrontendHost::canSaveAs()
336 {
337     return false;
338 }
339
340 bool InspectorFrontendHost::canInspectWorkers()
341 {
342     return false;
343 }
344
345 } // namespace WebCore
346
347 #endif // ENABLE(INSPECTOR)