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/dom/Pasteboard.h"
36 #include "core/fetch/ResourceFetcher.h"
37 #include "core/frame/Frame.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 "modules/filesystem/DOMFileSystem.h"
47 #include "platform/ContextMenu.h"
48 #include "platform/ContextMenuItem.h"
49 #include "platform/JSONValues.h"
50 #include "platform/SharedBuffer.h"
51 #include "platform/UserGestureIndicator.h"
52 #include "platform/network/ResourceError.h"
53 #include "platform/network/ResourceRequest.h"
54 #include "platform/network/ResourceResponse.h"
58 class FrontendMenuProvider FINAL : public ContextMenuProvider {
60 static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
62 return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
67 m_frontendApiObject = ScriptObject();
72 FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
73 : m_frontendHost(frontendHost)
74 , m_frontendApiObject(frontendApiObject)
79 virtual ~FrontendMenuProvider()
84 virtual void populateContextMenu(ContextMenu* menu) OVERRIDE
86 for (size_t i = 0; i < m_items.size(); ++i)
87 menu->appendItem(m_items[i]);
90 virtual void contextMenuItemSelected(const ContextMenuItem* item) OVERRIDE
93 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
94 int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
96 ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected");
97 function.appendArgument(itemNumber);
102 virtual void contextMenuCleared() OVERRIDE
104 if (m_frontendHost) {
105 ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared");
108 m_frontendHost->m_menuProvider = 0;
113 InspectorFrontendHost* m_frontendHost;
114 ScriptObject m_frontendApiObject;
115 Vector<ContextMenuItem> m_items;
118 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
120 , m_frontendPage(frontendPage)
123 ScriptWrappable::init(this);
126 InspectorFrontendHost::~InspectorFrontendHost()
131 void InspectorFrontendHost::disconnectClient()
135 m_menuProvider->disconnect();
139 void InspectorFrontendHost::setZoomFactor(float zoom)
141 m_frontendPage->mainFrame()->setPageAndTextZoomFactors(zoom, 1);
144 float InspectorFrontendHost::zoomFactor()
146 return m_frontendPage->mainFrame()->pageZoomFactor();
149 void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
152 m_client->inspectedURLChanged(newURL);
155 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
157 m_frontendPage->inspectorController().setInjectedScriptForOrigin(origin, script);
160 void InspectorFrontendHost::copyText(const String& text)
162 Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
165 void InspectorFrontendHost::sendMessageToBackend(const String& message)
168 m_client->sendMessageToBackend(message);
171 void InspectorFrontendHost::sendMessageToEmbedder(const String& message)
174 m_client->sendMessageToEmbedder(message);
177 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
182 ASSERT(m_frontendPage);
183 ScriptState* frontendScriptState = mainWorldScriptState(m_frontendPage->mainFrame());
184 ScriptObject frontendApiObject;
185 if (!ScriptGlobalObject::get(frontendScriptState, "InspectorFrontendAPI", frontendApiObject)) {
186 ASSERT_NOT_REACHED();
189 RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
190 m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
191 m_menuProvider = menuProvider.get();
194 String InspectorFrontendHost::getSelectionBackgroundColor()
196 return RenderTheme::theme().activeSelectionBackgroundColor().serialized();
199 String InspectorFrontendHost::getSelectionForegroundColor()
201 return RenderTheme::theme().activeSelectionForegroundColor().serialized();
204 PassRefPtr<DOMFileSystem> InspectorFrontendHost::isolatedFileSystem(const String& fileSystemName, const String& rootURL)
206 ExecutionContext* context = m_frontendPage->mainFrame()->document();
207 return DOMFileSystem::create(context, fileSystemName, FileSystemTypeIsolated, KURL(ParsedURLString, rootURL));
210 void InspectorFrontendHost::upgradeDraggedFileSystemPermissions(DOMFileSystem* domFileSystem)
214 RefPtr<JSONObject> message = JSONObject::create();
215 message->setNumber("id", 0);
216 message->setString("method", "upgradeDraggedFileSystemPermissions");
217 RefPtr<JSONArray> params = JSONArray::create();
218 message->setArray("params", params);
219 params->pushString(domFileSystem->rootURL().string());
220 sendMessageToEmbedder(message->toJSONString());
223 bool InspectorFrontendHost::isUnderTest()
225 return m_client && m_client->isUnderTest();
228 } // namespace WebCore