Web Inspector: Move all includes behind ENABLE(INSPECTOR) guards
[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 "Element.h"
41 #include "Frame.h"
42 #include "FrameLoader.h"
43 #include "HitTestResult.h"
44 #include "HTMLFrameOwnerElement.h"
45 #include "InspectorAgent.h"
46 #include "InspectorController.h"
47 #include "InspectorFrontendClient.h"
48 #include "Page.h"
49 #include "Pasteboard.h"
50 #include "ScriptFunctionCall.h"
51 #include "UserGestureIndicator.h"
52
53 #include <wtf/RefPtr.h>
54 #include <wtf/StdLibExtras.h>
55
56 using namespace std;
57
58 namespace WebCore {
59
60 #if ENABLE(CONTEXT_MENUS)
61 class FrontendMenuProvider : public ContextMenuProvider {
62 public:
63     static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject webInspector, const Vector<ContextMenuItem*>& items)
64     {
65         return adoptRef(new FrontendMenuProvider(frontendHost, webInspector, items));
66     }
67     
68     void disconnect()
69     {
70         m_webInspector = ScriptObject();
71         m_frontendHost = 0;
72     }
73     
74 private:
75     FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject webInspector,  const Vector<ContextMenuItem*>& items)
76         : m_frontendHost(frontendHost)
77         , m_webInspector(webInspector)
78         , m_items(items)
79     {
80     }
81
82     virtual ~FrontendMenuProvider()
83     {
84         contextMenuCleared();
85     }
86     
87     virtual void populateContextMenu(ContextMenu* menu)
88     {
89         for (size_t i = 0; i < m_items.size(); ++i)
90             menu->appendItem(*m_items[i]);
91     }
92     
93     virtual void contextMenuItemSelected(ContextMenuItem* item)
94     {
95         if (m_frontendHost) {
96             UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
97             int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
98
99             ScriptFunctionCall function(m_webInspector, "contextMenuItemSelected");
100             function.appendArgument(itemNumber);
101             function.call();
102         }
103     }
104     
105     virtual void contextMenuCleared()
106     {
107         if (m_frontendHost) {
108             ScriptFunctionCall function(m_webInspector, "contextMenuCleared");
109             function.call();
110
111             m_frontendHost->m_menuProvider = 0;
112         }
113         deleteAllValues(m_items);
114         m_items.clear();
115     }
116
117     InspectorFrontendHost* m_frontendHost;
118     ScriptObject m_webInspector;
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::requestAttachWindow()
154 {
155     if (m_client)
156         m_client->requestAttachWindow();
157 }
158
159 void InspectorFrontendHost::requestDetachWindow()
160 {
161     if (m_client)
162         m_client->requestDetachWindow();
163 }
164
165 void InspectorFrontendHost::requestSetDockSide(const String& side)
166 {
167     if (m_client)
168         m_client->requestSetDockSide(side);
169 }
170
171 void InspectorFrontendHost::closeWindow()
172 {
173     if (m_client) {
174         m_client->closeWindow();
175         disconnectClient(); // Disconnect from client.
176     }
177 }
178
179 void InspectorFrontendHost::bringToFront()
180 {
181     if (m_client)
182         m_client->bringToFront();
183 }
184
185 void InspectorFrontendHost::setZoomFactor(float zoom)
186 {
187     m_frontendPage->mainFrame()->setPageAndTextZoomFactors(zoom, 1);
188 }
189
190 void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
191 {
192     if (m_client)
193         m_client->inspectedURLChanged(newURL);
194 }
195
196 void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
197 {
198     if (m_client)
199         m_client->changeAttachedWindowHeight(height);
200 }
201
202 void InspectorFrontendHost::moveWindowBy(float x, float y) const
203 {
204     if (m_client)
205         m_client->moveWindowBy(x, y);
206 }
207
208 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
209 {
210     ASSERT(m_frontendPage->inspectorController());
211     m_frontendPage->inspectorController()->setInjectedScriptForOrigin(origin, script);
212 }
213
214 String InspectorFrontendHost::localizedStringsURL()
215 {
216     return m_client ? m_client->localizedStringsURL() : "";
217 }
218
219 String InspectorFrontendHost::hiddenPanels()
220 {
221     return m_client ? m_client->hiddenPanels() : "";
222 }
223
224 void InspectorFrontendHost::copyText(const String& text)
225 {
226     Pasteboard::generalPasteboard()->writePlainText(text);
227 }
228
229 void InspectorFrontendHost::openInNewTab(const String& url)
230 {
231     if (m_client)
232         m_client->openInNewTab(url);
233 }
234
235 bool InspectorFrontendHost::canSaveAs()
236 {
237     if (m_client)
238         return m_client->canSaveAs();
239     return false;
240 }
241
242 void InspectorFrontendHost::saveAs(const String& fileName, const String& content)
243 {
244     if (m_client)
245         m_client->saveAs(fileName, content);
246 }
247
248 void InspectorFrontendHost::sendMessageToBackend(const String& message)
249 {
250     if (m_client)
251         m_client->sendMessageToBackend(message);
252 }
253
254 #if ENABLE(CONTEXT_MENUS)
255 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem*>& items)
256 {
257     ASSERT(m_frontendPage);
258     ScriptState* frontendScriptState = scriptStateFromPage(debuggerWorld(), m_frontendPage);
259     ScriptObject webInspectorObj;
260     if (!ScriptGlobalObject::get(frontendScriptState, "WebInspector", webInspectorObj)) {
261         ASSERT_NOT_REACHED();
262         return;
263     }
264     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, webInspectorObj, items);
265     ContextMenuController* menuController = m_frontendPage->contextMenuController();
266     menuController->showContextMenu(event, menuProvider);
267     m_menuProvider = menuProvider.get();
268 }
269 #endif
270
271 String InspectorFrontendHost::loadResourceSynchronously(const String& url)
272 {
273     ResourceRequest request(url);
274     request.setHTTPMethod("GET");
275
276     Vector<char> data;
277     ResourceError error;
278     ResourceResponse response;
279     m_frontendPage->mainFrame()->loader()->loadResourceSynchronously(request, DoNotAllowStoredCredentials, error, response, data);
280     return String(data.data(), data.size());
281 }
282
283 } // namespace WebCore
284
285 #endif // ENABLE(INSPECTOR)