Web Inspector: implement import/export for timeline data.
[WebKit-https.git] / Source / WebCore / 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 "InspectorFrontendHost.h"
32
33 #if ENABLE(INSPECTOR)
34
35 #include "ContextMenu.h"
36 #include "ContextMenuItem.h"
37 #include "ContextMenuController.h"
38 #include "ContextMenuProvider.h"
39 #include "Element.h"
40 #include "Frame.h"
41 #include "FrameLoader.h"
42 #include "HitTestResult.h"
43 #include "HTMLFrameOwnerElement.h"
44 #include "InspectorAgent.h"
45 #include "InspectorController.h"
46 #include "InspectorFrontendClient.h"
47 #include "Page.h"
48 #include "Pasteboard.h"
49 #include "ScriptFunctionCall.h"
50 #include "UserGestureIndicator.h"
51
52 #include <wtf/RefPtr.h>
53 #include <wtf/StdLibExtras.h>
54
55 using namespace std;
56
57 namespace WebCore {
58
59 #if ENABLE(CONTEXT_MENUS)
60 class FrontendMenuProvider : public ContextMenuProvider {
61 public:
62     static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject webInspector, const Vector<ContextMenuItem*>& items)
63     {
64         return adoptRef(new FrontendMenuProvider(frontendHost, webInspector, items));
65     }
66     
67     void disconnect()
68     {
69         m_webInspector = ScriptObject();
70         m_frontendHost = 0;
71     }
72     
73 private:
74     FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject webInspector,  const Vector<ContextMenuItem*>& items)
75         : m_frontendHost(frontendHost)
76         , m_webInspector(webInspector)
77         , m_items(items)
78     {
79     }
80
81     virtual ~FrontendMenuProvider()
82     {
83         contextMenuCleared();
84     }
85     
86     virtual void populateContextMenu(ContextMenu* menu)
87     {
88         for (size_t i = 0; i < m_items.size(); ++i)
89             menu->appendItem(*m_items[i]);
90     }
91     
92     virtual void contextMenuItemSelected(ContextMenuItem* item)
93     {
94         if (m_frontendHost) {
95             UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
96             int itemNumber = item->action() - ContextMenuItemBaseCustomTag;
97
98             ScriptFunctionCall function(m_webInspector, "contextMenuItemSelected");
99             function.appendArgument(itemNumber);
100             function.call();
101         }
102     }
103     
104     virtual void contextMenuCleared()
105     {
106         if (m_frontendHost) {
107             ScriptFunctionCall function(m_webInspector, "contextMenuCleared");
108             function.call();
109
110             m_frontendHost->m_menuProvider = 0;
111         }
112         deleteAllValues(m_items);
113         m_items.clear();
114     }
115
116     InspectorFrontendHost* m_frontendHost;
117     ScriptObject m_webInspector;
118     Vector<ContextMenuItem*> m_items;
119 };
120 #endif
121
122 InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Page* frontendPage)
123     : m_client(client)
124     , m_frontendPage(frontendPage)
125 #if ENABLE(CONTEXT_MENUS)
126     , m_menuProvider(0)
127 #endif
128 {
129 }
130
131 InspectorFrontendHost::~InspectorFrontendHost()
132 {
133     ASSERT(!m_client);
134 }
135
136 void InspectorFrontendHost::disconnectClient()
137 {
138     m_client = 0;
139 #if ENABLE(CONTEXT_MENUS)
140     if (m_menuProvider)
141         m_menuProvider->disconnect();
142 #endif
143     m_frontendPage = 0;
144 }
145
146 void InspectorFrontendHost::loaded()
147 {
148     if (m_client)
149         m_client->frontendLoaded();
150 }
151
152 void InspectorFrontendHost::requestAttachWindow()
153 {
154     if (m_client)
155         m_client->requestAttachWindow();
156 }
157
158 void InspectorFrontendHost::requestDetachWindow()
159 {
160     if (m_client)
161         m_client->requestDetachWindow();
162 }
163
164 void InspectorFrontendHost::closeWindow()
165 {
166     if (m_client) {
167         m_client->closeWindow();
168         disconnectClient(); // Disconnect from client.
169     }
170 }
171
172 void InspectorFrontendHost::disconnectFromBackend()
173 {
174     if (m_client) {
175         m_client->disconnectFromBackend();
176         disconnectClient(); // Disconnect from client.
177     }
178 }
179
180 void InspectorFrontendHost::bringToFront()
181 {
182     if (m_client)
183         m_client->bringToFront();
184 }
185
186 void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
187 {
188     if (m_client)
189         m_client->inspectedURLChanged(newURL);
190 }
191
192 void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
193 {
194     if (m_client)
195         m_client->changeAttachedWindowHeight(height);
196 }
197
198 void InspectorFrontendHost::moveWindowBy(float x, float y) const
199 {
200     if (m_client)
201         m_client->moveWindowBy(x, y);
202 }
203
204 void InspectorFrontendHost::setExtensionAPI(const String& script)
205 {
206     ASSERT(m_frontendPage->inspectorController());
207     m_frontendPage->inspectorController()->setInspectorExtensionAPI(script);
208 }
209
210 String InspectorFrontendHost::localizedStringsURL()
211 {
212     return m_client->localizedStringsURL();
213 }
214
215 String InspectorFrontendHost::hiddenPanels()
216 {
217     return m_client->hiddenPanels();
218 }
219
220 void InspectorFrontendHost::copyText(const String& text)
221 {
222     Pasteboard::generalPasteboard()->writePlainText(text);
223 }
224
225 void InspectorFrontendHost::saveAs(const String& fileName, const String& content)
226 {
227     if (m_client)
228         m_client->saveAs(fileName, content);
229 }
230
231 void InspectorFrontendHost::saveSessionSetting(const String& key, const String& value)
232 {
233     if (m_client)
234         m_client->saveSessionSetting(key, value);
235 }
236
237 String InspectorFrontendHost::loadSessionSetting(const String& key)
238 {
239     String value;
240     if (m_client)
241         m_client->loadSessionSetting(key, &value);
242     return value;
243 }
244
245 void InspectorFrontendHost::sendMessageToBackend(const String& message)
246 {
247     if (m_client)
248         m_client->sendMessageToBackend(message);
249 }
250
251 #if ENABLE(CONTEXT_MENUS)
252 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem*>& items)
253 {
254     ASSERT(m_frontendPage);
255     ScriptState* frontendScriptState = scriptStateFromPage(debuggerWorld(), m_frontendPage);
256     ScriptObject webInspectorObj;
257     if (!ScriptGlobalObject::get(frontendScriptState, "WebInspector", webInspectorObj)) {
258         ASSERT_NOT_REACHED();
259         return;
260     }
261     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, webInspectorObj, items);
262     ContextMenuController* menuController = m_frontendPage->contextMenuController();
263     menuController->showContextMenu(event, menuProvider);
264     m_menuProvider = menuProvider.get();
265 }
266 #endif
267
268 } // namespace WebCore
269
270 #endif // ENABLE(INSPECTOR)