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 "WebInspectorClient.h"
28
29#if ENABLE(INSPECTOR)
30
31#include "WebInspector.h"
32#include "WebPage.h"
33#include <WebCore/InspectorController.h>
34#include <WebCore/Page.h>
35
36#if PLATFORM(IOS)
37#include <WebCore/InspectorOverlay.h>
38#endif
39
40using namespace WebCore;
41
42namespace WebKit {
43
44void WebInspectorClient::inspectorDestroyed()
45{
46    closeInspectorFrontend();
47    delete this;
48}
49
50WebCore::InspectorFrontendChannel* WebInspectorClient::openInspectorFrontend(InspectorController* controller)
51{
52    WebPage* inspectorPage = controller->isUnderTest() ? m_page->inspector()->createInspectorPageForTest() : m_page->inspector()->createInspectorPage();
53    ASSERT_UNUSED(inspectorPage, inspectorPage);
54    return this;
55}
56
57void WebInspectorClient::closeInspectorFrontend()
58{
59    if (m_page->inspector())
60        m_page->inspector()->didClose();
61}
62
63void WebInspectorClient::bringFrontendToFront()
64{
65    m_page->inspector()->bringToFront();
66}
67
68void WebInspectorClient::didResizeMainFrame(Frame*)
69{
70    if (m_page->inspector())
71        m_page->inspector()->updateDockingAvailability();
72}
73
74void WebInspectorClient::highlight()
75{
76#if !PLATFORM(IOS)
77    if (!m_highlightOverlay) {
78        RefPtr<PageOverlay> highlightOverlay = PageOverlay::create(this);
79        m_highlightOverlay = highlightOverlay.get();
80        m_page->installPageOverlay(highlightOverlay.release(), PageOverlay::FadeMode::Fade);
81        m_highlightOverlay->setNeedsDisplay();
82    } else {
83        m_highlightOverlay->stopFadeOutAnimation();
84        m_highlightOverlay->setNeedsDisplay();
85    }
86#else
87    Highlight highlight;
88    m_page->corePage()->inspectorController().getHighlight(&highlight, InspectorOverlay::CoordinateSystem::Document);
89    m_page->showInspectorHighlight(highlight);
90#endif
91}
92
93void WebInspectorClient::hideHighlight()
94{
95#if !PLATFORM(IOS)
96    if (m_highlightOverlay)
97        m_page->uninstallPageOverlay(m_highlightOverlay, PageOverlay::FadeMode::Fade);
98#else
99    m_page->hideInspectorHighlight();
100#endif
101}
102
103#if PLATFORM(IOS)
104void WebInspectorClient::showInspectorIndication()
105{
106    m_page->showInspectorIndication();
107}
108
109void WebInspectorClient::hideInspectorIndication()
110{
111    m_page->hideInspectorIndication();
112}
113
114void WebInspectorClient::didSetSearchingForNode(bool enabled)
115{
116    if (enabled)
117        m_page->enableInspectorNodeSearch();
118    else
119        m_page->disableInspectorNodeSearch();
120}
121#endif
122
123bool WebInspectorClient::sendMessageToFrontend(const String& message)
124{
125    WebInspector* inspector = m_page->inspector();
126    if (!inspector)
127        return false;
128
129#if ENABLE(INSPECTOR_SERVER)
130    if (inspector->hasRemoteFrontendConnected()) {
131        inspector->sendMessageToRemoteFrontend(message);
132        return true;
133    }
134#endif
135
136    WebPage* inspectorPage = inspector->inspectorPage();
137    if (inspectorPage)
138        return doDispatchMessageOnFrontendPage(inspectorPage->corePage(), message);
139
140    return false;
141}
142
143bool WebInspectorClient::supportsFrameInstrumentation()
144{
145#if USE(COORDINATED_GRAPHICS)
146    return true;
147#endif
148    return false;
149}
150
151void WebInspectorClient::pageOverlayDestroyed(PageOverlay*)
152{
153}
154
155void WebInspectorClient::willMoveToWebPage(PageOverlay*, WebPage* webPage)
156{
157    if (webPage)
158        return;
159
160    // The page overlay is moving away from the web page, reset it.
161    ASSERT(m_highlightOverlay);
162    m_highlightOverlay = 0;
163}
164
165void WebInspectorClient::didMoveToWebPage(PageOverlay*, WebPage*)
166{
167}
168
169void WebInspectorClient::drawRect(PageOverlay*, WebCore::GraphicsContext& context, const WebCore::IntRect& /*dirtyRect*/)
170{
171    m_page->corePage()->inspectorController().drawHighlight(context);
172}
173
174bool WebInspectorClient::mouseEvent(PageOverlay*, const WebMouseEvent&)
175{
176    return false;
177}
178
179} // namespace WebKit
180
181#endif // ENABLE(INSPECTOR)
182