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 "core/inspector/InspectorFrontendHost.h"
33 #include "bindings/core/v8/ScriptFunctionCall.h"
34 #include "bindings/core/v8/ScriptState.h"
35 #include "core/clipboard/Pasteboard.h"
36 #include "core/dom/ExecutionContext.h"
37 #include "core/events/Event.h"
38 #include "core/events/EventTarget.h"
39 #include "core/fetch/ResourceFetcher.h"
40 #include "core/frame/LocalDOMWindow.h"
41 #include "core/frame/LocalFrame.h"
42 #include "core/html/parser/TextResourceDecoder.h"
43 #include "core/inspector/InspectorController.h"
44 #include "core/inspector/InspectorFrontendClient.h"
45 #include "core/loader/FrameLoader.h"
46 #include "core/page/ContextMenuController.h"
47 #include "core/page/ContextMenuProvider.h"
48 #include "core/page/Page.h"
49 #include "core/rendering/RenderTheme.h"
50 #include "platform/ContextMenu.h"
51 #include "platform/ContextMenuItem.h"
52 #include "platform/SharedBuffer.h"
53 #include "platform/UserGestureIndicator.h"
54 #include "platform/network/ResourceError.h"
55 #include "platform/network/ResourceRequest.h"
56 #include "platform/network/ResourceResponse.h"
60 class FrontendMenuProvider FINAL : public ContextMenuProvider {
62 static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptValue frontendApiObject, const Vector<ContextMenuItem>& items)
64 return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
69 m_frontendApiObject = ScriptValue();
74 FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptValue frontendApiObject, const Vector<ContextMenuItem>& items)
75 : m_frontendHost(frontendHost)
76 , m_frontendApiObject(frontendApiObject)
81 virtual ~FrontendMenuProvider()
86 virtual void populateContextMenu(ContextMenu* menu) OVERRIDE
88 for (size_t i = 0; i < m_items.size(); ++i)
89 menu->appendItem(m_items[i]);
92 virtual void contextMenuItemSelected(const ContextMenuItem* item) OVERRIDE
95 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
96 int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
98 ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected");
99 function.appendArgument(itemNumber);
104 virtual void contextMenuCleared() OVERRIDE
106 if (m_frontendHost) {
107 ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared");
110 m_frontendHost->m_menuProvider = 0;
116 InspectorFrontendHost* m_frontendHost;
117 ScriptValue m_frontendApiObject;
118 Vector<ContextMenuItem> m_items;
121 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
123 , m_frontendPage(frontendPage)
128 InspectorFrontendHost::~InspectorFrontendHost()
133 void InspectorFrontendHost::trace(Visitor* visitor)
135 visitor->trace(m_frontendPage);
138 void InspectorFrontendHost::disconnectClient()
142 m_menuProvider->disconnect();
143 m_frontendPage = nullptr;
146 void InspectorFrontendHost::setZoomFactor(float zoom)
150 if (LocalFrame* frame = m_frontendPage->deprecatedLocalMainFrame())
151 frame->setPageAndTextZoomFactors(zoom, 1);
154 float InspectorFrontendHost::zoomFactor()
158 if (LocalFrame* frame = m_frontendPage->deprecatedLocalMainFrame())
159 return frame->pageZoomFactor();
163 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
167 m_frontendPage->inspectorController().setInjectedScriptForOrigin(origin, script);
170 void InspectorFrontendHost::copyText(const String& text)
172 Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
175 static String escapeUnicodeNonCharacters(const String& str)
177 const UChar nonChar = 0xD800;
180 while (i < str.length() && str[i] < nonChar)
182 if (i == str.length())
186 dst.append(str, 0, i);
187 for (; i < str.length(); ++i) {
190 unsigned symbol = static_cast<unsigned>(c);
191 String symbolCode = String::format("\\u%04X", symbol);
192 dst.append(symbolCode);
197 return dst.toString();
200 void InspectorFrontendHost::sendMessageToBackend(const String& message)
203 m_client->sendMessageToBackend(escapeUnicodeNonCharacters(message));
206 void InspectorFrontendHost::sendMessageToEmbedder(const String& message)
209 m_client->sendMessageToEmbedder(escapeUnicodeNonCharacters(message));
212 void InspectorFrontendHost::showContextMenu(Page* page, float x, float y, const Vector<ContextMenuItem>& items)
214 ASSERT(m_frontendPage);
215 ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
216 ScriptValue frontendApiObject = frontendScriptState->getFromGlobalObject("InspectorFrontendAPI");
217 ASSERT(frontendApiObject.isObject());
219 RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
220 m_menuProvider = menuProvider.get();
221 float zoom = page->deprecatedLocalMainFrame()->pageZoomFactor();
222 page->inspectorController().showContextMenu(x * zoom, y * zoom, menuProvider);
225 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
230 ASSERT(m_frontendPage);
231 ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
232 ScriptValue frontendApiObject = frontendScriptState->getFromGlobalObject("InspectorFrontendAPI");
233 ASSERT(frontendApiObject.isObject());
235 Page* targetPage = m_frontendPage;
236 if (event->target() && event->target()->executionContext() && event->target()->executionContext()->executingWindow()) {
237 LocalDOMWindow* window = event->target()->executionContext()->executingWindow();
238 if (window->document() && window->document()->page())
239 targetPage = window->document()->page();
242 RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
243 targetPage->contextMenuController().showContextMenu(event, menuProvider);
244 m_menuProvider = menuProvider.get();
247 String InspectorFrontendHost::getSelectionBackgroundColor()
249 return RenderTheme::theme().activeSelectionBackgroundColor().serialized();
252 String InspectorFrontendHost::getSelectionForegroundColor()
254 return RenderTheme::theme().activeSelectionForegroundColor().serialized();
257 bool InspectorFrontendHost::isUnderTest()
259 return m_client && m_client->isUnderTest();
262 bool InspectorFrontendHost::isHostedMode()