Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / InspectorFrontendHost.cpp
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
17  *
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.
28  */
29
30 #include "config.h"
31 #include "core/inspector/InspectorFrontendHost.h"
32
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"
55
56 namespace WebCore {
57
58 class FrontendMenuProvider FINAL : public ContextMenuProvider {
59 public:
60     static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
61     {
62         return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
63     }
64
65     void disconnect()
66     {
67         m_frontendApiObject = ScriptObject();
68         m_frontendHost = 0;
69     }
70
71 private:
72     FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
73         : m_frontendHost(frontendHost)
74         , m_frontendApiObject(frontendApiObject)
75         , m_items(items)
76     {
77     }
78
79     virtual ~FrontendMenuProvider()
80     {
81         contextMenuCleared();
82     }
83
84     virtual void populateContextMenu(ContextMenu* menu) OVERRIDE
85     {
86         for (size_t i = 0; i < m_items.size(); ++i)
87             menu->appendItem(m_items[i]);
88     }
89
90     virtual void contextMenuItemSelected(const ContextMenuItem* item) OVERRIDE
91     {
92         if (m_frontendHost) {
93             UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
94             int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
95
96             ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected");
97             function.appendArgument(itemNumber);
98             function.call();
99         }
100     }
101
102     virtual void contextMenuCleared() OVERRIDE
103     {
104         if (m_frontendHost) {
105             ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared");
106             function.call();
107
108             m_frontendHost->m_menuProvider = 0;
109         }
110         m_items.clear();
111     }
112
113     InspectorFrontendHost* m_frontendHost;
114     ScriptObject m_frontendApiObject;
115     Vector<ContextMenuItem> m_items;
116 };
117
118 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
119     : m_client(client)
120     , m_frontendPage(frontendPage)
121     , m_menuProvider(0)
122 {
123     ScriptWrappable::init(this);
124 }
125
126 InspectorFrontendHost::~InspectorFrontendHost()
127 {
128     ASSERT(!m_client);
129 }
130
131 void InspectorFrontendHost::disconnectClient()
132 {
133     m_client = 0;
134     if (m_menuProvider)
135         m_menuProvider->disconnect();
136     m_frontendPage = 0;
137 }
138
139 void InspectorFrontendHost::setZoomFactor(float zoom)
140 {
141     m_frontendPage->mainFrame()->setPageAndTextZoomFactors(zoom, 1);
142 }
143
144 float InspectorFrontendHost::zoomFactor()
145 {
146     return m_frontendPage->mainFrame()->pageZoomFactor();
147 }
148
149 void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
150 {
151     if (m_client)
152         m_client->inspectedURLChanged(newURL);
153 }
154
155 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
156 {
157     m_frontendPage->inspectorController().setInjectedScriptForOrigin(origin, script);
158 }
159
160 void InspectorFrontendHost::copyText(const String& text)
161 {
162     Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
163 }
164
165 void InspectorFrontendHost::sendMessageToBackend(const String& message)
166 {
167     if (m_client)
168         m_client->sendMessageToBackend(message);
169 }
170
171 void InspectorFrontendHost::sendMessageToEmbedder(const String& message)
172 {
173     if (m_client)
174         m_client->sendMessageToEmbedder(message);
175 }
176
177 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
178 {
179     if (!event)
180         return;
181
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();
187         return;
188     }
189     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
190     m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
191     m_menuProvider = menuProvider.get();
192 }
193
194 String InspectorFrontendHost::getSelectionBackgroundColor()
195 {
196     return RenderTheme::theme().activeSelectionBackgroundColor().serialized();
197 }
198
199 String InspectorFrontendHost::getSelectionForegroundColor()
200 {
201     return RenderTheme::theme().activeSelectionForegroundColor().serialized();
202 }
203
204 PassRefPtr<DOMFileSystem> InspectorFrontendHost::isolatedFileSystem(const String& fileSystemName, const String& rootURL)
205 {
206     ExecutionContext* context = m_frontendPage->mainFrame()->document();
207     return DOMFileSystem::create(context, fileSystemName, FileSystemTypeIsolated, KURL(ParsedURLString, rootURL));
208 }
209
210 void InspectorFrontendHost::upgradeDraggedFileSystemPermissions(DOMFileSystem* domFileSystem)
211 {
212     if (!m_client)
213         return;
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());
221 }
222
223 bool InspectorFrontendHost::isUnderTest()
224 {
225     return m_client && m_client->isUnderTest();
226 }
227
228 } // namespace WebCore