02ee1c7bd9c78a88e0cdb6450e0d6270f1e1d5d6
[WebKit-https.git] / Source / WebKit / WebProcess / WebCoreSupport / WebInspectorClient.cpp
1 /*
2  * Copyright (C) 2010, 2014, 2015 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 #include "DrawingArea.h"
30 #include "WebInspector.h"
31 #include "WebPage.h"
32 #include <WebCore/InspectorController.h>
33 #include <WebCore/MainFrame.h>
34 #include <WebCore/Page.h>
35 #include <WebCore/PageOverlayController.h>
36 #include <WebCore/Settings.h>
37 #include <wtf/CurrentTime.h>
38
39 #if PLATFORM(IOS)
40 #include <WebCore/InspectorOverlay.h>
41 #endif
42
43 using namespace WebCore;
44
45 namespace WebKit {
46
47 class RepaintIndicatorLayerClient final : public GraphicsLayerClient {
48 public:
49     RepaintIndicatorLayerClient(WebInspectorClient& inspectorClient)
50         : m_inspectorClient(inspectorClient)
51     {
52     }
53     virtual ~RepaintIndicatorLayerClient() { }
54 private:
55     void notifyAnimationEnded(const GraphicsLayer* layer, const String&) override
56     {
57         m_inspectorClient.animationEndedForLayer(layer);
58     }
59     
60     WebInspectorClient& m_inspectorClient;
61 };
62
63 WebInspectorClient::WebInspectorClient(WebPage* page)
64     : m_page(page)
65     , m_highlightOverlay(nullptr)
66 {
67 }
68
69 WebInspectorClient::~WebInspectorClient()
70 {
71     for (auto layer : m_paintRectLayers) {
72         layer->removeFromParent();
73         delete layer;
74     }
75
76     if (m_paintRectOverlay && m_page->mainFrame())
77         m_page->mainFrame()->pageOverlayController().uninstallPageOverlay(*m_paintRectOverlay, PageOverlay::FadeMode::Fade);
78 }
79
80 void WebInspectorClient::inspectedPageDestroyed()
81 {
82     if (WebInspector* inspector = m_page->inspector(WebPage::LazyCreationPolicy::UseExistingOnly))
83         inspector->close();
84
85     delete this;
86 }
87
88 Inspector::FrontendChannel* WebInspectorClient::openLocalFrontend(InspectorController* controller)
89 {
90     m_page->inspector()->openFrontendConnection(controller->isUnderTest());
91
92     return m_page->inspector();
93 }
94
95 void WebInspectorClient::bringFrontendToFront()
96 {
97     if (m_page->inspector())
98         m_page->inspector()->bringToFront();
99 }
100
101 void WebInspectorClient::didResizeMainFrame(Frame*)
102 {
103     if (m_page->inspector())
104         m_page->inspector()->updateDockingAvailability();
105 }
106
107 void WebInspectorClient::highlight()
108 {
109     if (!m_page->corePage()->settings().acceleratedCompositingEnabled())
110         return;
111
112 #if !PLATFORM(IOS)
113     if (!m_highlightOverlay) {
114         auto highlightOverlay = PageOverlay::create(*this);
115         m_highlightOverlay = highlightOverlay.ptr();
116         m_page->mainFrame()->pageOverlayController().installPageOverlay(WTFMove(highlightOverlay), PageOverlay::FadeMode::Fade);
117         m_highlightOverlay->setNeedsDisplay();
118     } else {
119         m_highlightOverlay->stopFadeOutAnimation();
120         m_highlightOverlay->setNeedsDisplay();
121     }
122 #else
123     Highlight highlight;
124     m_page->corePage()->inspectorController().getHighlight(highlight, InspectorOverlay::CoordinateSystem::Document);
125     m_page->showInspectorHighlight(highlight);
126 #endif
127 }
128
129 void WebInspectorClient::hideHighlight()
130 {
131 #if !PLATFORM(IOS)
132     if (m_highlightOverlay && m_page->mainFrame())
133         m_page->mainFrame()->pageOverlayController().uninstallPageOverlay(*m_highlightOverlay, PageOverlay::FadeMode::Fade);
134 #else
135     m_page->hideInspectorHighlight();
136 #endif
137 }
138
139 void WebInspectorClient::showPaintRect(const FloatRect& rect)
140 {
141     if (!m_page->corePage()->settings().acceleratedCompositingEnabled())
142         return;
143
144     if (!m_paintRectOverlay) {
145         m_paintRectOverlay = PageOverlay::create(*this, PageOverlay::OverlayType::Document);
146         m_page->mainFrame()->pageOverlayController().installPageOverlay(*m_paintRectOverlay, PageOverlay::FadeMode::DoNotFade);
147     }
148
149     if (!m_paintIndicatorLayerClient)
150         m_paintIndicatorLayerClient = std::make_unique<RepaintIndicatorLayerClient>(*this);
151
152     std::unique_ptr<GraphicsLayer> paintLayer = GraphicsLayer::create(m_page->drawingArea()->graphicsLayerFactory(), *m_paintIndicatorLayerClient);
153     
154     paintLayer->setName("paint rect");
155     paintLayer->setAnchorPoint(FloatPoint3D());
156     paintLayer->setPosition(rect.location());
157     paintLayer->setSize(rect.size());
158     paintLayer->setBackgroundColor(Color(1.0f, 0.0f, 0.0f, 0.2f));
159
160     KeyframeValueList fadeKeyframes(AnimatedPropertyOpacity);
161     fadeKeyframes.insert(std::make_unique<FloatAnimationValue>(0, 1));
162
163     fadeKeyframes.insert(std::make_unique<FloatAnimationValue>(0.25, 0));
164     
165     RefPtr<Animation> opacityAnimation = Animation::create();
166     opacityAnimation->setDuration(0.25);
167
168     paintLayer->addAnimation(fadeKeyframes, FloatSize(), opacityAnimation.get(), ASCIILiteral("opacity"), 0);
169     
170     m_paintRectLayers.add(paintLayer.get());
171
172     GraphicsLayer& overlayRootLayer = m_paintRectOverlay->layer();
173     overlayRootLayer.addChild(paintLayer.release());
174 }
175
176 void WebInspectorClient::animationEndedForLayer(const GraphicsLayer* layer)
177 {
178     const_cast<GraphicsLayer*>(layer)->removeFromParent();
179     m_paintRectLayers.remove(const_cast<GraphicsLayer*>(layer));
180     delete layer;
181 }
182
183 #if PLATFORM(IOS)
184 void WebInspectorClient::showInspectorIndication()
185 {
186     m_page->showInspectorIndication();
187 }
188
189 void WebInspectorClient::hideInspectorIndication()
190 {
191     m_page->hideInspectorIndication();
192 }
193
194 void WebInspectorClient::didSetSearchingForNode(bool enabled)
195 {
196     if (enabled)
197         m_page->enableInspectorNodeSearch();
198     else
199         m_page->disableInspectorNodeSearch();
200 }
201 #endif
202
203 void WebInspectorClient::elementSelectionChanged(bool active)
204 {
205     if (m_page->inspector())
206         m_page->inspector()->elementSelectionChanged(active);
207 }
208
209 void WebInspectorClient::willMoveToPage(PageOverlay&, Page* page)
210 {
211     if (page)
212         return;
213
214     // The page overlay is moving away from the web page, reset it.
215     ASSERT(m_highlightOverlay);
216     m_highlightOverlay = nullptr;
217 }
218
219 void WebInspectorClient::didMoveToPage(PageOverlay&, Page*)
220 {
221 }
222
223 void WebInspectorClient::drawRect(PageOverlay&, WebCore::GraphicsContext& context, const WebCore::IntRect& /*dirtyRect*/)
224 {
225     m_page->corePage()->inspectorController().drawHighlight(context);
226 }
227
228 bool WebInspectorClient::mouseEvent(PageOverlay&, const PlatformMouseEvent&)
229 {
230     return false;
231 }
232
233 } // namespace WebKit