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.
34
#include "InspectorFrontendHost.h"
36
#include "ContextMenu.h"
37
#include "ContextMenuItem.h"
38
#include "ContextMenuController.h"
39
#include "ContextMenuProvider.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"
49
#include "Pasteboard.h"
50
#include "ScriptFunctionCall.h"
51
#include "UserGestureIndicator.h"
53
#include <wtf/RefPtr.h>
54
#include <wtf/StdLibExtras.h>
60
#if ENABLE(CONTEXT_MENUS)
61
class FrontendMenuProvider : public ContextMenuProvider {
63
static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
65
return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
70
m_frontendApiObject = ScriptObject();
75
FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
76
: m_frontendHost(frontendHost)
77
, m_frontendApiObject(frontendApiObject)
82
virtual ~FrontendMenuProvider()
87
virtual void populateContextMenu(ContextMenu* menu)
89
for (size_t i = 0; i < m_items.size(); ++i)
90
menu->appendItem(m_items[i]);
93
virtual void contextMenuItemSelected(ContextMenuItem* item)
96
UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
97
int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
99
ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected");
100
function.appendArgument(itemNumber);
105
virtual void contextMenuCleared()
107
if (m_frontendHost) {
108
ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared");
111
m_frontendHost->m_menuProvider = 0;
116
InspectorFrontendHost* m_frontendHost;
117
ScriptObject m_frontendApiObject;
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::requestSetDockSide(const String& side)
156
if (side == "undocked")
157
m_client->requestSetDockSide(InspectorFrontendClient::UNDOCKED);
158
else if (side == "right")
159
m_client->requestSetDockSide(InspectorFrontendClient::DOCKED_TO_RIGHT);
160
else if (side == "bottom")
161
m_client->requestSetDockSide(InspectorFrontendClient::DOCKED_TO_BOTTOM);
164
void InspectorFrontendHost::closeWindow()
167
m_client->closeWindow();
168
disconnectClient(); // Disconnect from client.
172
void InspectorFrontendHost::bringToFront()
175
m_client->bringToFront();
178
void InspectorFrontendHost::setZoomFactor(float zoom)
180
m_frontendPage->mainFrame()->setPageAndTextZoomFactors(zoom, 1);
183
void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
186
m_client->inspectedURLChanged(newURL);
189
void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
192
m_client->changeAttachedWindowHeight(height);
195
void InspectorFrontendHost::moveWindowBy(float x, float y) const
198
m_client->moveWindowBy(x, y);
201
void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
203
ASSERT(m_frontendPage->inspectorController());
204
m_frontendPage->inspectorController()->setInjectedScriptForOrigin(origin, script);
207
String InspectorFrontendHost::localizedStringsURL()
209
return m_client ? m_client->localizedStringsURL() : "";
212
String InspectorFrontendHost::hiddenPanels()
214
return m_client ? m_client->hiddenPanels() : "";
217
void InspectorFrontendHost::copyText(const String& text)
219
Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
222
void InspectorFrontendHost::openInNewTab(const String& url)
225
m_client->openInNewTab(url);
228
bool InspectorFrontendHost::canSave()
231
return m_client->canSave();
235
void InspectorFrontendHost::save(const String& url, const String& content, bool forceSaveAs)
238
m_client->save(url, content, forceSaveAs);
241
void InspectorFrontendHost::append(const String& url, const String& content)
244
m_client->append(url, content);
247
void InspectorFrontendHost::close(const String&)
251
bool InspectorFrontendHost::canInspectWorkers()
255
return m_client->canInspectWorkers();
262
void InspectorFrontendHost::sendMessageToBackend(const String& message)
265
m_client->sendMessageToBackend(message);
268
#if ENABLE(CONTEXT_MENUS)
269
void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
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();
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();
285
String InspectorFrontendHost::loadResourceSynchronously(const String& url)
287
ResourceRequest request(url);
288
request.setHTTPMethod("GET");
292
ResourceResponse response;
293
m_frontendPage->mainFrame()->loader()->loadResourceSynchronously(request, DoNotAllowStoredCredentials, error, response, data);
294
return String(data.data(), data.size());
297
} // namespace WebCore
299
#endif // ENABLE(INSPECTOR)