Upstream version 7.36.149.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/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"
53
54 namespace WebCore {
55
56 class FrontendMenuProvider FINAL : public ContextMenuProvider {
57 public:
58     static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
59     {
60         return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
61     }
62
63     void disconnect()
64     {
65         m_frontendApiObject = ScriptObject();
66         m_frontendHost = 0;
67     }
68
69 private:
70     FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
71         : m_frontendHost(frontendHost)
72         , m_frontendApiObject(frontendApiObject)
73         , m_items(items)
74     {
75     }
76
77     virtual ~FrontendMenuProvider()
78     {
79         contextMenuCleared();
80     }
81
82     virtual void populateContextMenu(ContextMenu* menu) OVERRIDE
83     {
84         for (size_t i = 0; i < m_items.size(); ++i)
85             menu->appendItem(m_items[i]);
86     }
87
88     virtual void contextMenuItemSelected(const ContextMenuItem* item) OVERRIDE
89     {
90         if (m_frontendHost) {
91             UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
92             int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
93
94             ScriptFunctionCall function(m_frontendApiObject, "contextMenuItemSelected");
95             function.appendArgument(itemNumber);
96             function.call();
97         }
98     }
99
100     virtual void contextMenuCleared() OVERRIDE
101     {
102         if (m_frontendHost) {
103             ScriptFunctionCall function(m_frontendApiObject, "contextMenuCleared");
104             function.call();
105
106             m_frontendHost->m_menuProvider = 0;
107         }
108         m_items.clear();
109     }
110
111     InspectorFrontendHost* m_frontendHost;
112     ScriptObject m_frontendApiObject;
113     Vector<ContextMenuItem> m_items;
114 };
115
116 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
117     : m_client(client)
118     , m_frontendPage(frontendPage)
119     , m_menuProvider(0)
120 {
121     ScriptWrappable::init(this);
122 }
123
124 InspectorFrontendHost::~InspectorFrontendHost()
125 {
126     ASSERT(!m_client);
127 }
128
129 void InspectorFrontendHost::disconnectClient()
130 {
131     m_client = 0;
132     if (m_menuProvider)
133         m_menuProvider->disconnect();
134     m_frontendPage = 0;
135 }
136
137 void InspectorFrontendHost::setZoomFactor(float zoom)
138 {
139     m_frontendPage->mainFrame()->setPageAndTextZoomFactors(zoom, 1);
140 }
141
142 float InspectorFrontendHost::zoomFactor()
143 {
144     return m_frontendPage->mainFrame()->pageZoomFactor();
145 }
146
147 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
148 {
149     m_frontendPage->inspectorController().setInjectedScriptForOrigin(origin, script);
150 }
151
152 void InspectorFrontendHost::copyText(const String& text)
153 {
154     Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
155 }
156
157 static String escapeUnicodeNonCharacters(const String& str)
158 {
159     StringBuilder dst;
160     for (unsigned i = 0; i < str.length(); ++i) {
161         UChar c = str[i];
162         if (c >= 0xD800) {
163             unsigned symbol = static_cast<unsigned>(c);
164             String symbolCode = String::format("\\u%04X", symbol);
165             dst.append(symbolCode);
166         } else {
167             dst.append(c);
168         }
169
170     }
171     return dst.toString();
172 }
173
174 void InspectorFrontendHost::sendMessageToBackend(const String& message)
175 {
176     if (m_client)
177         m_client->sendMessageToBackend(escapeUnicodeNonCharacters(message));
178 }
179
180 void InspectorFrontendHost::sendMessageToEmbedder(const String& message)
181 {
182     if (m_client)
183         m_client->sendMessageToEmbedder(escapeUnicodeNonCharacters(message));
184 }
185
186 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
187 {
188     if (!event)
189         return;
190
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();
196         return;
197     }
198     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
199     m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
200     m_menuProvider = menuProvider.get();
201 }
202
203 String InspectorFrontendHost::getSelectionBackgroundColor()
204 {
205     return RenderTheme::theme().activeSelectionBackgroundColor().serialized();
206 }
207
208 String InspectorFrontendHost::getSelectionForegroundColor()
209 {
210     return RenderTheme::theme().activeSelectionForegroundColor().serialized();
211 }
212
213 bool InspectorFrontendHost::isUnderTest()
214 {
215     return m_client && m_client->isUnderTest();
216 }
217
218 } // namespace WebCore