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