Upstream version 9.38.198.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     ScriptWrappable::init(this);
127 }
128
129 InspectorFrontendHost::~InspectorFrontendHost()
130 {
131     ASSERT(!m_client);
132 }
133
134 void InspectorFrontendHost::trace(Visitor* visitor)
135 {
136     visitor->trace(m_frontendPage);
137 }
138
139 void InspectorFrontendHost::disconnectClient()
140 {
141     m_client = 0;
142     if (m_menuProvider)
143         m_menuProvider->disconnect();
144     m_frontendPage = nullptr;
145 }
146
147 void InspectorFrontendHost::setZoomFactor(float zoom)
148 {
149     m_frontendPage->deprecatedLocalMainFrame()->setPageAndTextZoomFactors(zoom, 1);
150 }
151
152 float InspectorFrontendHost::zoomFactor()
153 {
154     return m_frontendPage->deprecatedLocalMainFrame()->pageZoomFactor();
155 }
156
157 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
158 {
159     m_frontendPage->inspectorController().setInjectedScriptForOrigin(origin, script);
160 }
161
162 void InspectorFrontendHost::copyText(const String& text)
163 {
164     Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
165 }
166
167 static String escapeUnicodeNonCharacters(const String& str)
168 {
169     const UChar nonChar = 0xD800;
170
171     unsigned i = 0;
172     while (i < str.length() && str[i] < nonChar)
173         ++i;
174     if (i == str.length())
175         return str;
176
177     StringBuilder dst;
178     dst.append(str, 0, i);
179     for (; i < str.length(); ++i) {
180         UChar c = str[i];
181         if (c >= nonChar) {
182             unsigned symbol = static_cast<unsigned>(c);
183             String symbolCode = String::format("\\u%04X", symbol);
184             dst.append(symbolCode);
185         } else {
186             dst.append(c);
187         }
188     }
189     return dst.toString();
190 }
191
192 void InspectorFrontendHost::sendMessageToBackend(const String& message)
193 {
194     if (m_client)
195         m_client->sendMessageToBackend(escapeUnicodeNonCharacters(message));
196 }
197
198 void InspectorFrontendHost::sendMessageToEmbedder(const String& message)
199 {
200     if (m_client)
201         m_client->sendMessageToEmbedder(escapeUnicodeNonCharacters(message));
202 }
203
204 void InspectorFrontendHost::showContextMenu(Page* page, float x, float y, const Vector<ContextMenuItem>& items)
205 {
206     ASSERT(m_frontendPage);
207     ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
208     ScriptValue frontendApiObject = frontendScriptState->getFromGlobalObject("InspectorFrontendAPI");
209     ASSERT(frontendApiObject.isObject());
210
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);
215 }
216
217 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
218 {
219     if (!event)
220         return;
221
222     ASSERT(m_frontendPage);
223     ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
224     ScriptValue frontendApiObject = frontendScriptState->getFromGlobalObject("InspectorFrontendAPI");
225     ASSERT(frontendApiObject.isObject());
226
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();
232     }
233
234     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
235     targetPage->contextMenuController().showContextMenu(event, menuProvider);
236     m_menuProvider = menuProvider.get();
237 }
238
239 String InspectorFrontendHost::getSelectionBackgroundColor()
240 {
241     return RenderTheme::theme().activeSelectionBackgroundColor().serialized();
242 }
243
244 String InspectorFrontendHost::getSelectionForegroundColor()
245 {
246     return RenderTheme::theme().activeSelectionForegroundColor().serialized();
247 }
248
249 bool InspectorFrontendHost::isUnderTest()
250 {
251     return m_client && m_client->isUnderTest();
252 }
253
254 bool InspectorFrontendHost::isHostedMode()
255 {
256     return false;
257 }
258
259 } // namespace blink