Upstream version 5.34.104.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/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 "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::inspectedURLChanged(const String& newURL)
148 {
149     if (m_client)
150         m_client->inspectedURLChanged(newURL);
151 }
152
153 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
154 {
155     m_frontendPage->inspectorController().setInjectedScriptForOrigin(origin, script);
156 }
157
158 void InspectorFrontendHost::copyText(const String& text)
159 {
160     Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
161 }
162
163 void InspectorFrontendHost::sendMessageToBackend(const String& message)
164 {
165     if (m_client)
166         m_client->sendMessageToBackend(message);
167 }
168
169 void InspectorFrontendHost::sendMessageToEmbedder(const String& message)
170 {
171     if (m_client)
172         m_client->sendMessageToEmbedder(message);
173 }
174
175 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
176 {
177     if (!event)
178         return;
179
180     ASSERT(m_frontendPage);
181     ScriptState* frontendScriptState = mainWorldScriptState(m_frontendPage->mainFrame());
182     ScriptObject frontendApiObject;
183     if (!ScriptGlobalObject::get(frontendScriptState, "InspectorFrontendAPI", frontendApiObject)) {
184         ASSERT_NOT_REACHED();
185         return;
186     }
187     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
188     m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
189     m_menuProvider = menuProvider.get();
190 }
191
192 String InspectorFrontendHost::getSelectionBackgroundColor()
193 {
194     return RenderTheme::theme().activeSelectionBackgroundColor().serialized();
195 }
196
197 String InspectorFrontendHost::getSelectionForegroundColor()
198 {
199     return RenderTheme::theme().activeSelectionForegroundColor().serialized();
200 }
201
202 bool InspectorFrontendHost::isUnderTest()
203 {
204     return m_client && m_client->isUnderTest();
205 }
206
207 } // namespace WebCore