1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebInspectorProxy.h"
28
29#if ENABLE(INSPECTOR)
30
31#include "WebInspectorMessages.h"
32#include "WebPageProxy.h"
33#include "WebPageCreationParameters.h"
34#include "WebProcessProxy.h"
35#include "WebPageGroup.h"
36
37#if PLATFORM(WIN)
38#include "WebView.h"
39#endif
40
41#include <WebCore/NotImplemented.h>
42
43using namespace WebCore;
44
45namespace WebKit {
46
47WebPageGroup* WebInspectorProxy::inspectorPageGroup()
48{
49    static WebPageGroup* pageGroup = WebPageGroup::create("__WebInspectorPageGroup__", false, false).leakRef();
50    return pageGroup;
51}
52
53WebInspectorProxy::WebInspectorProxy(WebPageProxy* page)
54    : m_page(page)
55    , m_isVisible(false)
56    , m_isAttached(false)
57    , m_isDebuggingJavaScript(false)
58    , m_isProfilingJavaScript(false)
59    , m_isProfilingPage(false)
60#if PLATFORM(WIN)
61    , m_inspectorWindow(0)
62#endif
63{
64}
65
66WebInspectorProxy::~WebInspectorProxy()
67{
68}
69
70void WebInspectorProxy::invalidate()
71{
72    m_page->close();
73    platformClose();
74
75    m_page = 0;
76
77    m_isVisible = false;
78    m_isDebuggingJavaScript = false;
79    m_isProfilingJavaScript = false;
80    m_isProfilingPage = false;
81}
82
83// Public APIs
84void WebInspectorProxy::show()
85{
86    if (!m_page)
87        return;
88
89    m_page->process()->send(Messages::WebInspector::Show(), m_page->pageID());
90}
91
92void WebInspectorProxy::close()
93{
94    if (!m_page)
95        return;
96
97    m_page->process()->send(Messages::WebInspector::Close(), m_page->pageID());
98}
99
100void WebInspectorProxy::showConsole()
101{
102    if (!m_page)
103        return;
104
105    m_page->process()->send(Messages::WebInspector::ShowConsole(), m_page->pageID());
106}
107
108void WebInspectorProxy::attach()
109{
110    notImplemented();
111}
112
113void WebInspectorProxy::detach()
114{
115    notImplemented();
116}
117
118void WebInspectorProxy::toggleJavaScriptDebugging()
119{
120    if (!m_page)
121        return;
122
123    if (m_isDebuggingJavaScript)
124        m_page->process()->send(Messages::WebInspector::StopJavaScriptDebugging(), m_page->pageID());
125    else
126        m_page->process()->send(Messages::WebInspector::StartJavaScriptDebugging(), m_page->pageID());
127
128    // FIXME: have the WebProcess notify us on state changes.
129    m_isDebuggingJavaScript = !m_isDebuggingJavaScript;
130}
131
132void WebInspectorProxy::toggleJavaScriptProfiling()
133{
134    if (!m_page)
135        return;
136
137    if (m_isProfilingJavaScript)
138        m_page->process()->send(Messages::WebInspector::StopJavaScriptProfiling(), m_page->pageID());
139    else
140        m_page->process()->send(Messages::WebInspector::StartJavaScriptProfiling(), m_page->pageID());
141
142    // FIXME: have the WebProcess notify us on state changes.
143    m_isProfilingJavaScript = !m_isProfilingJavaScript;
144}
145
146void WebInspectorProxy::togglePageProfiling()
147{
148    if (!m_page)
149        return;
150
151    if (m_isProfilingPage)
152        m_page->process()->send(Messages::WebInspector::StopPageProfiling(), m_page->pageID());
153    else
154        m_page->process()->send(Messages::WebInspector::StartPageProfiling(), m_page->pageID());
155
156    // FIXME: have the WebProcess notify us on state changes.
157    m_isProfilingPage = !m_isProfilingPage;
158}
159
160bool WebInspectorProxy::isInspectorPage(WebPageProxy* page)
161{
162    return page->pageGroup() == inspectorPageGroup();
163}
164
165// Called by WebInspectorProxy messages
166void WebInspectorProxy::createInspectorPage(uint64_t& inspectorPageID, WebPageCreationParameters& inspectorPageParameters)
167{
168    inspectorPageID = 0;
169
170    if (!m_page)
171        return;
172
173    WebPageProxy* inspectorPage = platformCreateInspectorPage();
174    ASSERT(inspectorPage);
175    if (!inspectorPage)
176        return;
177
178    inspectorPageID = inspectorPage->pageID();
179    inspectorPageParameters = inspectorPage->creationParameters();
180
181    inspectorPage->loadURL(inspectorPageURL());
182}
183
184void WebInspectorProxy::didLoadInspectorPage()
185{
186    m_isVisible = true;
187
188    platformOpen();
189}
190
191void WebInspectorProxy::didClose()
192{
193    platformClose();
194
195    m_isVisible = false;
196}
197
198void WebInspectorProxy::inspectedURLChanged(const String& urlString)
199{
200    platformInspectedURLChanged(urlString);
201}
202
203} // namespace WebKit
204
205#endif // ENABLE(INSPECTOR)
206