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/v8/ScriptFunctionCall.h"
34 #include "bindings/v8/ScriptState.h"
35 #include "core/clipboard/Pasteboard.h"
36 #include "core/fetch/ResourceFetcher.h"
37 #include "core/frame/LocalFrame.h"
38 #include "core/html/parser/TextResourceDecoder.h"
39 #include "core/inspector/InspectorController.h"
40 #include "core/inspector/InspectorFrontendClient.h"
41 #include "core/loader/FrameLoader.h"
42 #include "core/page/ContextMenuController.h"
43 #include "core/page/ContextMenuProvider.h"
44 #include "core/page/Page.h"
45 #include "core/rendering/RenderTheme.h"
46 #include "platform/ContextMenu.h"
47 #include "platform/ContextMenuItem.h"
48 #include "platform/SharedBuffer.h"
49 #include "platform/UserGestureIndicator.h"
50 #include "platform/network/ResourceError.h"
51 #include "platform/network/ResourceRequest.h"
52 #include "platform/network/ResourceResponse.h"
56 class FrontendMenuProvider FINAL : public ContextMenuProvider {
58 static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
60 return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
65 m_frontendApiObject = ScriptObject();
70 FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
71 : m_frontendHost(frontendHost)
72 , m_frontendApiObject(frontendApiObject)
77 virtual ~FrontendMenuProvider()
82 virtual void populateContextMenu(ContextMenu* menu) OVERRIDE
84 for (size_t i = 0; i < m_items.size(); ++i)
85 menu->appendItem(m_items[i]);
88 virtual void contextMenuItemSelected(const ContextMenuItem* item) OVERRIDE
91 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
92 int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
94 ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected");
95 function.appendArgument(itemNumber);
100 virtual void contextMenuCleared() OVERRIDE
102 if (m_frontendHost) {
103 ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared");
106 m_frontendHost->m_menuProvider = 0;
111 InspectorFrontendHost* m_frontendHost;
112 ScriptObject m_frontendApiObject;
113 Vector<ContextMenuItem> m_items;
116 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
118 , m_frontendPage(frontendPage)
121 ScriptWrappable::init(this);
124 InspectorFrontendHost::~InspectorFrontendHost()
129 void InspectorFrontendHost::disconnectClient()
133 m_menuProvider->disconnect();
137 void InspectorFrontendHost::setZoomFactor(float zoom)
139 m_frontendPage->mainFrame()->setPageAndTextZoomFactors(zoom, 1);
142 float InspectorFrontendHost::zoomFactor()
144 return m_frontendPage->mainFrame()->pageZoomFactor();
147 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
149 m_frontendPage->inspectorController().setInjectedScriptForOrigin(origin, script);
152 void InspectorFrontendHost::copyText(const String& text)
154 Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
157 static String escapeUnicodeNonCharacters(const String& str)
160 for (unsigned i = 0; i < str.length(); ++i) {
163 unsigned symbol = static_cast<unsigned>(c);
164 String symbolCode = String::format("\\u%04X", symbol);
165 dst.append(symbolCode);
171 return dst.toString();
174 void InspectorFrontendHost::sendMessageToBackend(const String& message)
177 m_client->sendMessageToBackend(escapeUnicodeNonCharacters(message));
180 void InspectorFrontendHost::sendMessageToEmbedder(const String& message)
183 m_client->sendMessageToEmbedder(escapeUnicodeNonCharacters(message));
186 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
191 ASSERT(m_frontendPage);
192 ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->mainFrame());
193 ScriptObject frontendApiObject;
194 if (!ScriptGlobalObject::get(frontendScriptState, "InspectorFrontendAPI", frontendApiObject)) {
195 ASSERT_NOT_REACHED();
198 RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
199 m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
200 m_menuProvider = menuProvider.get();
203 String InspectorFrontendHost::getSelectionBackgroundColor()
205 return RenderTheme::theme().activeSelectionBackgroundColor().serialized();
208 String InspectorFrontendHost::getSelectionForegroundColor()
210 return RenderTheme::theme().activeSelectionForegroundColor().serialized();
213 bool InspectorFrontendHost::isUnderTest()
215 return m_client && m_client->isUnderTest();
218 } // namespace WebCore