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)
126 ScriptWrappable::init(this);
129 InspectorFrontendHost::~InspectorFrontendHost()
134 void InspectorFrontendHost::trace(Visitor* visitor)
136 visitor->trace(m_frontendPage);
139 void InspectorFrontendHost::disconnectClient()
143 m_menuProvider->disconnect();
144 m_frontendPage = nullptr;
147 void InspectorFrontendHost::setZoomFactor(float zoom)
149 m_frontendPage->deprecatedLocalMainFrame()->setPageAndTextZoomFactors(zoom, 1);
152 float InspectorFrontendHost::zoomFactor()
154 return m_frontendPage->deprecatedLocalMainFrame()->pageZoomFactor();
157 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
159 m_frontendPage->inspectorController().setInjectedScriptForOrigin(origin, script);
162 void InspectorFrontendHost::copyText(const String& text)
164 Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
167 static String escapeUnicodeNonCharacters(const String& str)
169 const UChar nonChar = 0xD800;
172 while (i < str.length() && str[i] < nonChar)
174 if (i == str.length())
178 dst.append(str, 0, i);
179 for (; i < str.length(); ++i) {
182 unsigned symbol = static_cast<unsigned>(c);
183 String symbolCode = String::format("\\u%04X", symbol);
184 dst.append(symbolCode);
189 return dst.toString();
192 void InspectorFrontendHost::sendMessageToBackend(const String& message)
195 m_client->sendMessageToBackend(escapeUnicodeNonCharacters(message));
198 void InspectorFrontendHost::sendMessageToEmbedder(const String& message)
201 m_client->sendMessageToEmbedder(escapeUnicodeNonCharacters(message));
204 void InspectorFrontendHost::showContextMenu(Page* page, float x, float y, const Vector<ContextMenuItem>& items)
206 ASSERT(m_frontendPage);
207 ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
208 ScriptValue frontendApiObject = frontendScriptState->getFromGlobalObject("InspectorFrontendAPI");
209 ASSERT(frontendApiObject.isObject());
211 RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
212 m_menuProvider = menuProvider.get();
213 float zoom = page->deprecatedLocalMainFrame()->pageZoomFactor();
214 page->inspectorController().showContextMenu(x * zoom, y * zoom, menuProvider);
217 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
222 ASSERT(m_frontendPage);
223 ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
224 ScriptValue frontendApiObject = frontendScriptState->getFromGlobalObject("InspectorFrontendAPI");
225 ASSERT(frontendApiObject.isObject());
227 Page* targetPage = m_frontendPage;
228 if (event->target() && event->target()->executionContext() && event->target()->executionContext()->executingWindow()) {
229 LocalDOMWindow* window = event->target()->executionContext()->executingWindow();
230 if (window->document() && window->document()->page())
231 targetPage = window->document()->page();
234 RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
235 targetPage->contextMenuController().showContextMenu(event, menuProvider);
236 m_menuProvider = menuProvider.get();
239 String InspectorFrontendHost::getSelectionBackgroundColor()
241 return RenderTheme::theme().activeSelectionBackgroundColor().serialized();
244 String InspectorFrontendHost::getSelectionForegroundColor()
246 return RenderTheme::theme().activeSelectionForegroundColor().serialized();
249 bool InspectorFrontendHost::isUnderTest()
251 return m_client && m_client->isUnderTest();
254 bool InspectorFrontendHost::isHostedMode()