Upstream version 10.39.225.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/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"
57
58 namespace blink {
59
60 class FrontendMenuProvider FINAL : public ContextMenuProvider {
61 public:
62     static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptValue frontendApiObject, const Vector<ContextMenuItem>& items)
63     {
64         return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
65     }
66
67     void disconnect()
68     {
69         m_frontendApiObject = ScriptValue();
70         m_frontendHost = 0;
71     }
72
73 private:
74     FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptValue frontendApiObject, const Vector<ContextMenuItem>& items)
75         : m_frontendHost(frontendHost)
76         , m_frontendApiObject(frontendApiObject)
77         , m_items(items)
78     {
79     }
80
81     virtual ~FrontendMenuProvider()
82     {
83         contextMenuCleared();
84     }
85
86     virtual void populateContextMenu(ContextMenu* menu) OVERRIDE
87     {
88         for (size_t i = 0; i < m_items.size(); ++i)
89             menu->appendItem(m_items[i]);
90     }
91
92     virtual void contextMenuItemSelected(const ContextMenuItem* item) OVERRIDE
93     {
94         if (m_frontendHost) {
95             UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
96             int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
97
98             ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected");
99             function.appendArgument(itemNumber);
100             function.call();
101         }
102     }
103
104     virtual void contextMenuCleared() OVERRIDE
105     {
106         if (m_frontendHost) {
107             ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared");
108             function.call();
109
110             m_frontendHost->m_menuProvider = 0;
111         }
112         m_items.clear();
113         m_frontendHost = 0;
114     }
115
116     InspectorFrontendHost* m_frontendHost;
117     ScriptValue m_frontendApiObject;
118     Vector<ContextMenuItem> m_items;
119 };
120
121 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
122     : m_client(client)
123     , m_frontendPage(frontendPage)
124     , m_menuProvider(0)
125 {
126 }
127
128 InspectorFrontendHost::~InspectorFrontendHost()
129 {
130     ASSERT(!m_client);
131 }
132
133 void InspectorFrontendHost::trace(Visitor* visitor)
134 {
135     visitor->trace(m_frontendPage);
136 }
137
138 void InspectorFrontendHost::disconnectClient()
139 {
140     m_client = 0;
141     if (m_menuProvider)
142         m_menuProvider->disconnect();
143     m_frontendPage = nullptr;
144 }
145
146 void InspectorFrontendHost::setZoomFactor(float zoom)
147 {
148     if (!m_frontendPage)
149         return;
150     if (LocalFrame* frame = m_frontendPage->deprecatedLocalMainFrame())
151         frame->setPageAndTextZoomFactors(zoom, 1);
152 }
153
154 float InspectorFrontendHost::zoomFactor()
155 {
156     if (!m_frontendPage)
157         return 1;
158     if (LocalFrame* frame = m_frontendPage->deprecatedLocalMainFrame())
159         return frame->pageZoomFactor();
160     return 1;
161 }
162
163 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
164 {
165     if (!m_frontendPage)
166         return;
167     m_frontendPage->inspectorController().setInjectedScriptForOrigin(origin, script);
168 }
169
170 void InspectorFrontendHost::copyText(const String& text)
171 {
172     Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
173 }
174
175 static String escapeUnicodeNonCharacters(const String& str)
176 {
177     const UChar nonChar = 0xD800;
178
179     unsigned i = 0;
180     while (i < str.length() && str[i] < nonChar)
181         ++i;
182     if (i == str.length())
183         return str;
184
185     StringBuilder dst;
186     dst.append(str, 0, i);
187     for (; i < str.length(); ++i) {
188         UChar c = str[i];
189         if (c >= nonChar) {
190             unsigned symbol = static_cast<unsigned>(c);
191             String symbolCode = String::format("\\u%04X", symbol);
192             dst.append(symbolCode);
193         } else {
194             dst.append(c);
195         }
196     }
197     return dst.toString();
198 }
199
200 void InspectorFrontendHost::sendMessageToBackend(const String& message)
201 {
202     if (m_client)
203         m_client->sendMessageToBackend(escapeUnicodeNonCharacters(message));
204 }
205
206 void InspectorFrontendHost::sendMessageToEmbedder(const String& message)
207 {
208     if (m_client)
209         m_client->sendMessageToEmbedder(escapeUnicodeNonCharacters(message));
210 }
211
212 void InspectorFrontendHost::showContextMenu(Page* page, float x, float y, const Vector<ContextMenuItem>& items)
213 {
214     ASSERT(m_frontendPage);
215     ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
216     ScriptValue frontendApiObject = frontendScriptState->getFromGlobalObject("InspectorFrontendAPI");
217     ASSERT(frontendApiObject.isObject());
218
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);
223 }
224
225 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
226 {
227     if (!event)
228         return;
229
230     ASSERT(m_frontendPage);
231     ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
232     ScriptValue frontendApiObject = frontendScriptState->getFromGlobalObject("InspectorFrontendAPI");
233     ASSERT(frontendApiObject.isObject());
234
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();
240     }
241
242     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
243     targetPage->contextMenuController().showContextMenu(event, menuProvider);
244     m_menuProvider = menuProvider.get();
245 }
246
247 String InspectorFrontendHost::getSelectionBackgroundColor()
248 {
249     return RenderTheme::theme().activeSelectionBackgroundColor().serialized();
250 }
251
252 String InspectorFrontendHost::getSelectionForegroundColor()
253 {
254     return RenderTheme::theme().activeSelectionForegroundColor().serialized();
255 }
256
257 bool InspectorFrontendHost::isUnderTest()
258 {
259     return m_client && m_client->isUnderTest();
260 }
261
262 bool InspectorFrontendHost::isHostedMode()
263 {
264     return false;
265 }
266
267 } // namespace blink