2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include "InspectorFrontendHost.h"
35 #include "ContextMenu.h"
36 #include "ContextMenuItem.h"
37 #include "ContextMenuController.h"
38 #include "ContextMenuProvider.h"
41 #include "FrameLoader.h"
42 #include "HitTestResult.h"
43 #include "HTMLFrameOwnerElement.h"
44 #include "InspectorAgent.h"
45 #include "InspectorController.h"
46 #include "InspectorFrontendClient.h"
48 #include "Pasteboard.h"
49 #include "ScriptFunctionCall.h"
50 #include "UserGestureIndicator.h"
52 #include <wtf/RefPtr.h>
53 #include <wtf/StdLibExtras.h>
59 #if ENABLE(CONTEXT_MENUS)
60 class FrontendMenuProvider : public ContextMenuProvider {
62 static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject webInspector, const Vector<ContextMenuItem*>& items)
64 return adoptRef(new FrontendMenuProvider(frontendHost, webInspector, items));
69 m_webInspector = ScriptObject();
74 FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject webInspector, const Vector<ContextMenuItem*>& items)
75 : m_frontendHost(frontendHost)
76 , m_webInspector(webInspector)
81 virtual ~FrontendMenuProvider()
86 virtual void populateContextMenu(ContextMenu* menu)
88 for (size_t i = 0; i < m_items.size(); ++i)
89 menu->appendItem(*m_items[i]);
92 virtual void contextMenuItemSelected(ContextMenuItem* item)
95 UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
96 int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
98 ScriptFunctionCall function(m_webInspector, "contextMenuItemSelected");
99 function.appendArgument(itemNumber);
104 virtual void contextMenuCleared()
106 if (m_frontendHost) {
107 ScriptFunctionCall function(m_webInspector, "contextMenuCleared");
110 m_frontendHost->m_menuProvider = 0;
112 deleteAllValues(m_items);
116 InspectorFrontendHost* m_frontendHost;
117 ScriptObject m_webInspector;
118 Vector<ContextMenuItem*> m_items;
122 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
124 , m_frontendPage(frontendPage)
125 #if ENABLE(CONTEXT_MENUS)
131 InspectorFrontendHost::~InspectorFrontendHost()
136 void InspectorFrontendHost::disconnectClient()
139 #if ENABLE(CONTEXT_MENUS)
141 m_menuProvider->disconnect();
146 void InspectorFrontendHost::loaded()
149 m_client->frontendLoaded();
152 void InspectorFrontendHost::requestAttachWindow()
155 m_client->requestAttachWindow();
158 void InspectorFrontendHost::requestDetachWindow()
161 m_client->requestDetachWindow();
164 void InspectorFrontendHost::closeWindow()
167 m_client->closeWindow();
168 disconnectClient(); // Disconnect from client.
172 void InspectorFrontendHost::bringToFront()
175 m_client->bringToFront();
178 void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
181 m_client->inspectedURLChanged(newURL);
184 void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
187 m_client->changeAttachedWindowHeight(height);
190 void InspectorFrontendHost::moveWindowBy(float x, float y) const
193 m_client->moveWindowBy(x, y);
196 void InspectorFrontendHost::setExtensionAPI(const String& script)
198 ASSERT(m_frontendPage->inspectorController());
199 m_frontendPage->inspectorController()->setInspectorExtensionAPI(script);
202 String InspectorFrontendHost::localizedStringsURL()
204 return m_client->localizedStringsURL();
207 String InspectorFrontendHost::hiddenPanels()
209 return m_client->hiddenPanels();
212 void InspectorFrontendHost::copyText(const String& text)
214 Pasteboard::generalPasteboard()->writePlainText(text);
217 void InspectorFrontendHost::saveAs(const String& fileName, const String& content)
220 m_client->saveAs(fileName, content);
223 void InspectorFrontendHost::sendMessageToBackend(const String& message)
226 m_client->sendMessageToBackend(message);
229 #if ENABLE(CONTEXT_MENUS)
230 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem*>& items)
232 ASSERT(m_frontendPage);
233 ScriptState* frontendScriptState = scriptStateFromPage(debuggerWorld(), m_frontendPage);
234 ScriptObject webInspectorObj;
235 if (!ScriptGlobalObject::get(frontendScriptState, "WebInspector", webInspectorObj)) {
236 ASSERT_NOT_REACHED();
239 RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, webInspectorObj, items);
240 ContextMenuController* menuController = m_frontendPage->contextMenuController();
241 menuController->showContextMenu(event, menuProvider);
242 m_menuProvider = menuProvider.get();
246 String InspectorFrontendHost::loadResourceSynchronously(const String& url)
248 ResourceRequest request(url);
249 request.setHTTPMethod("GET");
253 ResourceResponse response;
254 m_frontendPage->mainFrame()->loader()->loadResourceSynchronously(request, DoNotAllowStoredCredentials, error, response, data);
255 return String(data.data(), data.size());
258 } // namespace WebCore
260 #endif // ENABLE(INSPECTOR)