2 * Copyright (C) 2010, 2014, 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebInspectorClient.h"
29 #include "DrawingArea.h"
30 #include "WebInspector.h"
32 #include <WebCore/Frame.h>
33 #include <WebCore/InspectorController.h>
34 #include <WebCore/Page.h>
35 #include <WebCore/PageOverlayController.h>
36 #include <WebCore/Settings.h>
39 #include <WebCore/InspectorOverlay.h>
43 using namespace WebCore;
45 class RepaintIndicatorLayerClient final : public GraphicsLayerClient {
47 RepaintIndicatorLayerClient(WebInspectorClient& inspectorClient)
48 : m_inspectorClient(inspectorClient)
51 virtual ~RepaintIndicatorLayerClient() { }
53 void notifyAnimationEnded(const GraphicsLayer* layer, const String&) override
55 m_inspectorClient.animationEndedForLayer(layer);
58 WebInspectorClient& m_inspectorClient;
61 WebInspectorClient::WebInspectorClient(WebPage* page)
63 , m_highlightOverlay(nullptr)
67 WebInspectorClient::~WebInspectorClient()
69 for (auto layer : m_paintRectLayers) {
70 layer->removeFromParent();
74 if (m_paintRectOverlay && m_page->corePage())
75 m_page->corePage()->pageOverlayController().uninstallPageOverlay(*m_paintRectOverlay, PageOverlay::FadeMode::Fade);
78 void WebInspectorClient::inspectedPageDestroyed()
80 if (WebInspector* inspector = m_page->inspector(WebPage::LazyCreationPolicy::UseExistingOnly))
86 void WebInspectorClient::frontendCountChanged(unsigned count)
88 m_page->inspectorFrontendCountChanged(count);
91 Inspector::FrontendChannel* WebInspectorClient::openLocalFrontend(InspectorController* controller)
93 m_page->inspector()->openFrontendConnection(controller->isUnderTest());
95 return m_page->inspector();
98 void WebInspectorClient::bringFrontendToFront()
100 if (m_page->inspector())
101 m_page->inspector()->bringToFront();
104 void WebInspectorClient::didResizeMainFrame(Frame*)
106 if (m_page->inspector())
107 m_page->inspector()->updateDockingAvailability();
110 void WebInspectorClient::highlight()
112 if (!m_page->corePage()->settings().acceleratedCompositingEnabled())
116 if (!m_highlightOverlay) {
117 auto highlightOverlay = PageOverlay::create(*this);
118 m_highlightOverlay = highlightOverlay.ptr();
119 m_page->corePage()->pageOverlayController().installPageOverlay(WTFMove(highlightOverlay), PageOverlay::FadeMode::Fade);
120 m_highlightOverlay->setNeedsDisplay();
122 m_highlightOverlay->stopFadeOutAnimation();
123 m_highlightOverlay->setNeedsDisplay();
127 m_page->corePage()->inspectorController().getHighlight(highlight, InspectorOverlay::CoordinateSystem::Document);
128 m_page->showInspectorHighlight(highlight);
132 void WebInspectorClient::hideHighlight()
135 if (m_highlightOverlay)
136 m_page->corePage()->pageOverlayController().uninstallPageOverlay(*m_highlightOverlay, PageOverlay::FadeMode::Fade);
138 m_page->hideInspectorHighlight();
142 void WebInspectorClient::showPaintRect(const FloatRect& rect)
144 if (!m_page->corePage()->settings().acceleratedCompositingEnabled())
147 if (!m_paintRectOverlay) {
148 m_paintRectOverlay = PageOverlay::create(*this, PageOverlay::OverlayType::Document);
149 m_page->corePage()->pageOverlayController().installPageOverlay(*m_paintRectOverlay, PageOverlay::FadeMode::DoNotFade);
152 if (!m_paintIndicatorLayerClient)
153 m_paintIndicatorLayerClient = std::make_unique<RepaintIndicatorLayerClient>(*this);
155 std::unique_ptr<GraphicsLayer> paintLayer = GraphicsLayer::create(m_page->drawingArea()->graphicsLayerFactory(), *m_paintIndicatorLayerClient);
157 paintLayer->setName("paint rect");
158 paintLayer->setAnchorPoint(FloatPoint3D());
159 paintLayer->setPosition(rect.location());
160 paintLayer->setSize(rect.size());
161 paintLayer->setBackgroundColor(Color(1.0f, 0.0f, 0.0f, 0.2f));
163 KeyframeValueList fadeKeyframes(AnimatedPropertyOpacity);
164 fadeKeyframes.insert(std::make_unique<FloatAnimationValue>(0, 1));
166 fadeKeyframes.insert(std::make_unique<FloatAnimationValue>(0.25, 0));
168 RefPtr<Animation> opacityAnimation = Animation::create();
169 opacityAnimation->setDuration(0.25);
171 paintLayer->addAnimation(fadeKeyframes, FloatSize(), opacityAnimation.get(), "opacity"_s, 0);
173 m_paintRectLayers.add(paintLayer.get());
175 GraphicsLayer& overlayRootLayer = m_paintRectOverlay->layer();
176 overlayRootLayer.addChild(paintLayer.release());
179 void WebInspectorClient::animationEndedForLayer(const GraphicsLayer* layer)
181 const_cast<GraphicsLayer*>(layer)->removeFromParent();
182 m_paintRectLayers.remove(const_cast<GraphicsLayer*>(layer));
187 void WebInspectorClient::showInspectorIndication()
189 m_page->showInspectorIndication();
192 void WebInspectorClient::hideInspectorIndication()
194 m_page->hideInspectorIndication();
197 void WebInspectorClient::didSetSearchingForNode(bool enabled)
200 m_page->enableInspectorNodeSearch();
202 m_page->disableInspectorNodeSearch();
206 void WebInspectorClient::elementSelectionChanged(bool active)
208 if (m_page->inspector())
209 m_page->inspector()->elementSelectionChanged(active);
212 void WebInspectorClient::willMoveToPage(PageOverlay&, Page* page)
217 // The page overlay is moving away from the web page, reset it.
218 ASSERT(m_highlightOverlay);
219 m_highlightOverlay = nullptr;
222 void WebInspectorClient::didMoveToPage(PageOverlay&, Page*)
226 void WebInspectorClient::drawRect(PageOverlay&, WebCore::GraphicsContext& context, const WebCore::IntRect& /*dirtyRect*/)
228 m_page->corePage()->inspectorController().drawHighlight(context);
231 bool WebInspectorClient::mouseEvent(PageOverlay&, const PlatformMouseEvent&)
236 } // namespace WebKit