Bug 1529208 [wpt PR 15469] - [Code Health] Fix incorrect test name, a=testonly
[gecko.git] / image / SVGDocumentWrapper.cpp
blobe51207dc21cd05b57f6c2918a42a0bc92bcd2d4d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "SVGDocumentWrapper.h"
8 #include "mozilla/dom/DocumentTimeline.h"
9 #include "mozilla/dom/Element.h"
10 #include "mozilla/dom/SVGDocument.h"
11 #include "nsICategoryManager.h"
12 #include "nsIChannel.h"
13 #include "nsIContentViewer.h"
14 #include "nsIDocumentLoaderFactory.h"
15 #include "nsIHttpChannel.h"
16 #include "nsIObserverService.h"
17 #include "nsIParser.h"
18 #include "nsIPresShell.h"
19 #include "nsIRequest.h"
20 #include "nsIStreamListener.h"
21 #include "nsIXMLContentSink.h"
22 #include "nsNetCID.h"
23 #include "nsComponentManagerUtils.h"
24 #include "mozilla/SMILAnimationController.h"
25 #include "nsServiceManagerUtils.h"
26 #include "mozilla/dom/SVGSVGElement.h"
27 #include "SVGObserverUtils.h"
28 #include "nsMimeTypes.h"
29 #include "mozilla/dom/Document.h"
30 #include "mozilla/dom/ImageTracker.h"
32 namespace mozilla {
34 using namespace dom;
35 using namespace gfx;
37 namespace image {
39 NS_IMPL_ISUPPORTS(SVGDocumentWrapper, nsIStreamListener, nsIRequestObserver,
40 nsIObserver, nsISupportsWeakReference)
42 SVGDocumentWrapper::SVGDocumentWrapper()
43 : mIgnoreInvalidation(false), mRegisteredForXPCOMShutdown(false) {}
45 SVGDocumentWrapper::~SVGDocumentWrapper() {
46 DestroyViewer();
47 if (mRegisteredForXPCOMShutdown) {
48 UnregisterForXPCOMShutdown();
52 void SVGDocumentWrapper::DestroyViewer() {
53 if (mViewer) {
54 mViewer->GetDocument()->OnPageHide(false, nullptr);
55 mViewer->Close(nullptr);
56 mViewer->Destroy();
57 mViewer = nullptr;
61 nsIFrame* SVGDocumentWrapper::GetRootLayoutFrame() {
62 Element* rootElem = GetRootSVGElem();
63 return rootElem ? rootElem->GetPrimaryFrame() : nullptr;
66 void SVGDocumentWrapper::UpdateViewportBounds(const nsIntSize& aViewportSize) {
67 MOZ_ASSERT(!mIgnoreInvalidation, "shouldn't be reentrant");
68 mIgnoreInvalidation = true;
70 nsIntRect currentBounds;
71 mViewer->GetBounds(currentBounds);
73 // If the bounds have changed, we need to do a layout flush.
74 if (currentBounds.Size() != aViewportSize) {
75 mViewer->SetBounds(IntRect(IntPoint(0, 0), aViewportSize));
76 FlushLayout();
79 mIgnoreInvalidation = false;
82 void SVGDocumentWrapper::FlushImageTransformInvalidation() {
83 MOZ_ASSERT(!mIgnoreInvalidation, "shouldn't be reentrant");
85 SVGSVGElement* svgElem = GetRootSVGElem();
86 if (!svgElem) {
87 return;
90 mIgnoreInvalidation = true;
91 svgElem->FlushImageTransformInvalidation();
92 FlushLayout();
93 mIgnoreInvalidation = false;
96 bool SVGDocumentWrapper::IsAnimated() {
97 // Can be called for animated images during shutdown, after we've
98 // already Observe()'d XPCOM shutdown and cleared out our mViewer pointer.
99 if (!mViewer) {
100 return false;
103 Document* doc = mViewer->GetDocument();
104 if (!doc) {
105 return false;
107 if (doc->Timeline()->HasAnimations()) {
108 // CSS animations (technically HasAnimations() also checks for CSS
109 // transitions and Web animations but since SVG-as-an-image doesn't run
110 // script they will never run in the document that we wrap).
111 return true;
113 if (doc->HasAnimationController() &&
114 doc->GetAnimationController()->HasRegisteredAnimations()) {
115 // SMIL animations
116 return true;
118 return false;
121 void SVGDocumentWrapper::StartAnimation() {
122 // Can be called for animated images during shutdown, after we've
123 // already Observe()'d XPCOM shutdown and cleared out our mViewer pointer.
124 if (!mViewer) {
125 return;
128 Document* doc = mViewer->GetDocument();
129 if (doc) {
130 SMILAnimationController* controller = doc->GetAnimationController();
131 if (controller) {
132 controller->Resume(SMILTimeContainer::PAUSE_IMAGE);
134 doc->ImageTracker()->SetAnimatingState(true);
138 void SVGDocumentWrapper::StopAnimation() {
139 // Can be called for animated images during shutdown, after we've
140 // already Observe()'d XPCOM shutdown and cleared out our mViewer pointer.
141 if (!mViewer) {
142 return;
145 Document* doc = mViewer->GetDocument();
146 if (doc) {
147 SMILAnimationController* controller = doc->GetAnimationController();
148 if (controller) {
149 controller->Pause(SMILTimeContainer::PAUSE_IMAGE);
151 doc->ImageTracker()->SetAnimatingState(false);
155 void SVGDocumentWrapper::ResetAnimation() {
156 SVGSVGElement* svgElem = GetRootSVGElem();
157 if (!svgElem) {
158 return;
161 svgElem->SetCurrentTime(0.0f);
164 float SVGDocumentWrapper::GetCurrentTimeAsFloat() {
165 SVGSVGElement* svgElem = GetRootSVGElem();
166 return svgElem ? svgElem->GetCurrentTimeAsFloat() : 0.0f;
169 void SVGDocumentWrapper::SetCurrentTime(float aTime) {
170 SVGSVGElement* svgElem = GetRootSVGElem();
171 if (svgElem && svgElem->GetCurrentTimeAsFloat() != aTime) {
172 svgElem->SetCurrentTime(aTime);
176 void SVGDocumentWrapper::TickRefreshDriver() {
177 nsCOMPtr<nsIPresShell> presShell = mViewer->GetPresShell();
178 if (presShell) {
179 nsPresContext* presContext = presShell->GetPresContext();
180 if (presContext) {
181 presContext->RefreshDriver()->DoTick();
186 /** nsIStreamListener methods **/
188 NS_IMETHODIMP
189 SVGDocumentWrapper::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
190 uint64_t sourceOffset, uint32_t count) {
191 return mListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
194 /** nsIRequestObserver methods **/
196 NS_IMETHODIMP
197 SVGDocumentWrapper::OnStartRequest(nsIRequest* aRequest) {
198 nsresult rv = SetupViewer(aRequest, getter_AddRefs(mViewer),
199 getter_AddRefs(mLoadGroup));
201 if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(mListener->OnStartRequest(aRequest))) {
202 mViewer->GetDocument()->SetIsBeingUsedAsImage();
203 StopAnimation(); // otherwise animations start automatically in helper doc
205 rv = mViewer->Init(nullptr, nsIntRect(0, 0, 0, 0));
206 if (NS_SUCCEEDED(rv)) {
207 rv = mViewer->Open(nullptr, nullptr);
210 return rv;
213 NS_IMETHODIMP
214 SVGDocumentWrapper::OnStopRequest(nsIRequest* aRequest, nsresult status) {
215 if (mListener) {
216 mListener->OnStopRequest(aRequest, status);
217 mListener = nullptr;
220 return NS_OK;
223 /** nsIObserver Methods **/
224 NS_IMETHODIMP
225 SVGDocumentWrapper::Observe(nsISupports* aSubject, const char* aTopic,
226 const char16_t* aData) {
227 if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
228 // Sever ties from rendering observers to helper-doc's root SVG node
229 SVGSVGElement* svgElem = GetRootSVGElem();
230 if (svgElem) {
231 SVGObserverUtils::RemoveAllRenderingObservers(svgElem);
234 // Clean up at XPCOM shutdown time.
235 DestroyViewer();
236 if (mListener) {
237 mListener = nullptr;
239 if (mLoadGroup) {
240 mLoadGroup = nullptr;
243 // Turn off "registered" flag, or else we'll try to unregister when we die.
244 // (No need for that now, and the try would fail anyway -- it's too late.)
245 mRegisteredForXPCOMShutdown = false;
246 } else {
247 NS_ERROR("Unexpected observer topic.");
249 return NS_OK;
252 /** Private helper methods **/
254 // This method is largely cribbed from
255 // nsExternalResourceMap::PendingLoad::SetupViewer.
256 nsresult SVGDocumentWrapper::SetupViewer(nsIRequest* aRequest,
257 nsIContentViewer** aViewer,
258 nsILoadGroup** aLoadGroup) {
259 nsCOMPtr<nsIChannel> chan(do_QueryInterface(aRequest));
260 NS_ENSURE_TRUE(chan, NS_ERROR_UNEXPECTED);
262 // Check for HTTP error page
263 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(aRequest));
264 if (httpChannel) {
265 bool requestSucceeded;
266 if (NS_FAILED(httpChannel->GetRequestSucceeded(&requestSucceeded)) ||
267 !requestSucceeded) {
268 return NS_ERROR_FAILURE;
272 // Give this document its own loadgroup
273 nsCOMPtr<nsILoadGroup> loadGroup;
274 chan->GetLoadGroup(getter_AddRefs(loadGroup));
276 nsCOMPtr<nsILoadGroup> newLoadGroup =
277 do_CreateInstance(NS_LOADGROUP_CONTRACTID);
278 NS_ENSURE_TRUE(newLoadGroup, NS_ERROR_OUT_OF_MEMORY);
279 newLoadGroup->SetLoadGroup(loadGroup);
281 nsCOMPtr<nsICategoryManager> catMan =
282 do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
283 NS_ENSURE_TRUE(catMan, NS_ERROR_NOT_AVAILABLE);
284 nsCString contractId;
285 nsresult rv = catMan->GetCategoryEntry("Gecko-Content-Viewers", IMAGE_SVG_XML,
286 contractId);
287 NS_ENSURE_SUCCESS(rv, rv);
288 nsCOMPtr<nsIDocumentLoaderFactory> docLoaderFactory =
289 do_GetService(contractId.get());
290 NS_ENSURE_TRUE(docLoaderFactory, NS_ERROR_NOT_AVAILABLE);
292 nsCOMPtr<nsIContentViewer> viewer;
293 nsCOMPtr<nsIStreamListener> listener;
294 rv = docLoaderFactory->CreateInstance(
295 "external-resource", chan, newLoadGroup,
296 NS_LITERAL_CSTRING(IMAGE_SVG_XML), nullptr, nullptr,
297 getter_AddRefs(listener), getter_AddRefs(viewer));
298 NS_ENSURE_SUCCESS(rv, rv);
300 NS_ENSURE_TRUE(viewer, NS_ERROR_UNEXPECTED);
302 // Create a navigation time object and pass it to the SVG document through
303 // the viewer.
304 // The timeline(DocumentTimeline, used in CSS animation) of this SVG
305 // document needs this navigation timing object for time computation, such
306 // as to calculate current time stamp based on the start time of navigation
307 // time object.
309 // For a root document, DocShell would do these sort of things
310 // automatically. Since there is no DocShell for this wrapped SVG document,
311 // we must set it up manually.
312 RefPtr<nsDOMNavigationTiming> timing = new nsDOMNavigationTiming(nullptr);
313 timing->NotifyNavigationStart(
314 nsDOMNavigationTiming::DocShellState::eInactive);
315 viewer->SetNavigationTiming(timing);
317 nsCOMPtr<nsIParser> parser = do_QueryInterface(listener);
318 NS_ENSURE_TRUE(parser, NS_ERROR_UNEXPECTED);
320 // XML-only, because this is for SVG content
321 nsCOMPtr<nsIContentSink> sink = parser->GetContentSink();
322 NS_ENSURE_TRUE(sink, NS_ERROR_UNEXPECTED);
324 listener.swap(mListener);
325 viewer.forget(aViewer);
326 newLoadGroup.forget(aLoadGroup);
328 RegisterForXPCOMShutdown();
329 return NS_OK;
332 void SVGDocumentWrapper::RegisterForXPCOMShutdown() {
333 MOZ_ASSERT(!mRegisteredForXPCOMShutdown, "re-registering for XPCOM shutdown");
334 // Listen for xpcom-shutdown so that we can drop references to our
335 // helper-document at that point. (Otherwise, we won't get cleaned up
336 // until imgLoader::Shutdown, which can happen after the JAR service
337 // and RDF service have been unregistered.)
338 nsresult rv;
339 nsCOMPtr<nsIObserverService> obsSvc = do_GetService(OBSERVER_SVC_CID, &rv);
340 if (NS_FAILED(rv) || NS_FAILED(obsSvc->AddObserver(
341 this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, true))) {
342 NS_WARNING("Failed to register as observer of XPCOM shutdown");
343 } else {
344 mRegisteredForXPCOMShutdown = true;
348 void SVGDocumentWrapper::UnregisterForXPCOMShutdown() {
349 MOZ_ASSERT(mRegisteredForXPCOMShutdown,
350 "unregistering for XPCOM shutdown w/out being registered");
352 nsresult rv;
353 nsCOMPtr<nsIObserverService> obsSvc = do_GetService(OBSERVER_SVC_CID, &rv);
354 if (NS_FAILED(rv) ||
355 NS_FAILED(obsSvc->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID))) {
356 NS_WARNING("Failed to unregister as observer of XPCOM shutdown");
357 } else {
358 mRegisteredForXPCOMShutdown = false;
362 void SVGDocumentWrapper::FlushLayout() {
363 if (SVGDocument* doc = GetDocument()) {
364 doc->FlushPendingNotifications(FlushType::Layout);
368 SVGDocument* SVGDocumentWrapper::GetDocument() {
369 if (!mViewer) {
370 return nullptr;
372 Document* doc = mViewer->GetDocument();
373 if (!doc) {
374 return nullptr;
376 return doc->AsSVGDocument();
379 SVGSVGElement* SVGDocumentWrapper::GetRootSVGElem() {
380 if (!mViewer) {
381 return nullptr; // Can happen during destruction
384 Document* doc = mViewer->GetDocument();
385 if (!doc) {
386 return nullptr; // Can happen during destruction
389 Element* rootElem = mViewer->GetDocument()->GetRootElement();
390 if (!rootElem || !rootElem->IsSVGElement(nsGkAtoms::svg)) {
391 return nullptr;
394 return static_cast<SVGSVGElement*>(rootElem);
397 } // namespace image
398 } // namespace mozilla