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/PresShell.h"
9 #include "mozilla/SMILAnimationController.h"
10 #include "mozilla/SVGObserverUtils.h"
11 #include "mozilla/dom/Animation.h"
12 #include "mozilla/dom/Document.h"
13 #include "mozilla/dom/DocumentTimeline.h"
14 #include "mozilla/dom/Element.h"
15 #include "mozilla/dom/ImageTracker.h"
16 #include "mozilla/dom/SVGDocument.h"
17 #include "mozilla/dom/SVGSVGElement.h"
18 #include "nsICategoryManager.h"
19 #include "nsIChannel.h"
20 #include "nsIDocumentViewer.h"
21 #include "nsIDocumentLoaderFactory.h"
22 #include "nsIHttpChannel.h"
23 #include "nsIObserverService.h"
24 #include "nsIParser.h"
25 #include "nsIRequest.h"
26 #include "nsIStreamListener.h"
27 #include "nsIXMLContentSink.h"
29 #include "nsComponentManagerUtils.h"
30 #include "nsServiceManagerUtils.h"
31 #include "nsMimeTypes.h"
32 #include "nsRefreshDriver.h"
41 NS_IMPL_ISUPPORTS(SVGDocumentWrapper
, nsIStreamListener
, nsIRequestObserver
,
42 nsIObserver
, nsISupportsWeakReference
)
44 SVGDocumentWrapper::SVGDocumentWrapper()
45 : mIgnoreInvalidation(false),
46 mRegisteredForXPCOMShutdown(false),
49 SVGDocumentWrapper::~SVGDocumentWrapper() {
51 if (mRegisteredForXPCOMShutdown
) {
52 UnregisterForXPCOMShutdown();
56 void SVGDocumentWrapper::DestroyViewer() {
57 MOZ_ASSERT(NS_IsMainThread());
59 mViewer
->GetDocument()->OnPageHide(false, nullptr);
60 mViewer
->Close(nullptr);
66 nsIFrame
* SVGDocumentWrapper::GetRootLayoutFrame() {
67 Element
* rootElem
= GetRootSVGElem();
68 return rootElem
? rootElem
->GetPrimaryFrame() : nullptr;
71 void SVGDocumentWrapper::UpdateViewportBounds(const nsIntSize
& aViewportSize
) {
72 MOZ_ASSERT(!mIgnoreInvalidation
, "shouldn't be reentrant");
73 mIgnoreInvalidation
= true;
75 nsIntRect currentBounds
;
76 mViewer
->GetBounds(currentBounds
);
78 // If the bounds have changed, we need to do a layout flush.
79 if (currentBounds
.Size() != aViewportSize
) {
80 mViewer
->SetBounds(IntRect(IntPoint(0, 0), aViewportSize
));
84 mIgnoreInvalidation
= false;
87 void SVGDocumentWrapper::FlushImageTransformInvalidation() {
88 MOZ_ASSERT(!mIgnoreInvalidation
, "shouldn't be reentrant");
90 SVGSVGElement
* svgElem
= GetRootSVGElem();
95 mIgnoreInvalidation
= true;
96 svgElem
->FlushImageTransformInvalidation();
98 mIgnoreInvalidation
= false;
101 bool SVGDocumentWrapper::IsAnimated() {
102 // Can be called for animated images during shutdown, after we've
103 // already Observe()'d XPCOM shutdown and cleared out our mViewer pointer.
108 Document
* doc
= mViewer
->GetDocument();
112 if (doc
->Timeline()->HasAnimations()) {
113 // CSS animations (technically HasAnimations() also checks for CSS
114 // transitions and Web animations but since SVG-as-an-image doesn't run
115 // script they will never run in the document that we wrap).
118 if (doc
->HasAnimationController() &&
119 doc
->GetAnimationController()->HasRegisteredAnimations()) {
126 void SVGDocumentWrapper::StartAnimation() {
127 // Can be called for animated images during shutdown, after we've
128 // already Observe()'d XPCOM shutdown and cleared out our mViewer pointer.
133 Document
* doc
= mViewer
->GetDocument();
135 SMILAnimationController
* controller
= doc
->GetAnimationController();
137 controller
->Resume(SMILTimeContainer::PAUSE_IMAGE
);
139 doc
->ImageTracker()->SetAnimatingState(true);
143 void SVGDocumentWrapper::StopAnimation() {
144 // Can be called for animated images during shutdown, after we've
145 // already Observe()'d XPCOM shutdown and cleared out our mViewer pointer.
150 Document
* doc
= mViewer
->GetDocument();
152 SMILAnimationController
* controller
= doc
->GetAnimationController();
154 controller
->Pause(SMILTimeContainer::PAUSE_IMAGE
);
156 doc
->ImageTracker()->SetAnimatingState(false);
160 void SVGDocumentWrapper::ResetAnimation() {
161 SVGSVGElement
* svgElem
= GetRootSVGElem();
166 svgElem
->SetCurrentTime(0.0f
);
169 float SVGDocumentWrapper::GetCurrentTimeAsFloat() {
170 SVGSVGElement
* svgElem
= GetRootSVGElem();
171 return svgElem
? svgElem
->GetCurrentTimeAsFloat() : 0.0f
;
174 void SVGDocumentWrapper::SetCurrentTime(float aTime
) {
175 SVGSVGElement
* svgElem
= GetRootSVGElem();
176 if (svgElem
&& svgElem
->GetCurrentTimeAsFloat() != aTime
) {
177 svgElem
->SetCurrentTime(aTime
);
181 void SVGDocumentWrapper::TickRefreshDriver() {
182 if (RefPtr
<PresShell
> presShell
= mViewer
->GetPresShell()) {
183 if (RefPtr
<nsPresContext
> presContext
= presShell
->GetPresContext()) {
184 if (RefPtr
<nsRefreshDriver
> driver
= presContext
->RefreshDriver()) {
191 /** nsIStreamListener methods **/
194 SVGDocumentWrapper::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* inStr
,
195 uint64_t sourceOffset
, uint32_t count
) {
196 return mListener
->OnDataAvailable(aRequest
, inStr
, sourceOffset
, count
);
199 /** nsIRequestObserver methods **/
202 SVGDocumentWrapper::OnStartRequest(nsIRequest
* aRequest
) {
203 nsresult rv
= SetupViewer(aRequest
, getter_AddRefs(mViewer
),
204 getter_AddRefs(mLoadGroup
));
206 if (NS_SUCCEEDED(rv
) && NS_SUCCEEDED(mListener
->OnStartRequest(aRequest
))) {
207 mViewer
->GetDocument()->SetIsBeingUsedAsImage();
208 StopAnimation(); // otherwise animations start automatically in helper doc
210 rv
= mViewer
->Init(nullptr, nsIntRect(0, 0, 0, 0), nullptr);
211 if (NS_SUCCEEDED(rv
)) {
212 rv
= mViewer
->Open(nullptr, nullptr);
219 SVGDocumentWrapper::OnStopRequest(nsIRequest
* aRequest
, nsresult status
) {
221 mListener
->OnStopRequest(aRequest
, status
);
228 /** nsIObserver Methods **/
230 SVGDocumentWrapper::Observe(nsISupports
* aSubject
, const char* aTopic
,
231 const char16_t
* aData
) {
232 if (!strcmp(aTopic
, NS_XPCOM_SHUTDOWN_OBSERVER_ID
)) {
233 // Sever ties from rendering observers to helper-doc's root SVG node
234 SVGSVGElement
* svgElem
= GetRootSVGElem();
236 SVGObserverUtils::RemoveAllRenderingObservers(svgElem
);
239 // Clean up at XPCOM shutdown time.
245 mLoadGroup
= nullptr;
248 // Turn off "registered" flag, or else we'll try to unregister when we die.
249 // (No need for that now, and the try would fail anyway -- it's too late.)
250 mRegisteredForXPCOMShutdown
= false;
252 NS_ERROR("Unexpected observer topic.");
257 /** Private helper methods **/
259 // This method is largely cribbed from
260 // nsExternalResourceMap::PendingLoad::SetupViewer.
261 nsresult
SVGDocumentWrapper::SetupViewer(nsIRequest
* aRequest
,
262 nsIDocumentViewer
** aViewer
,
263 nsILoadGroup
** aLoadGroup
) {
264 nsCOMPtr
<nsIChannel
> chan(do_QueryInterface(aRequest
));
265 NS_ENSURE_TRUE(chan
, NS_ERROR_UNEXPECTED
);
267 // Check for HTTP error page
268 nsCOMPtr
<nsIHttpChannel
> httpChannel(do_QueryInterface(aRequest
));
270 bool requestSucceeded
;
271 if (NS_FAILED(httpChannel
->GetRequestSucceeded(&requestSucceeded
)) ||
273 return NS_ERROR_FAILURE
;
277 // Give this document its own loadgroup
278 nsCOMPtr
<nsILoadGroup
> loadGroup
;
279 chan
->GetLoadGroup(getter_AddRefs(loadGroup
));
281 nsCOMPtr
<nsILoadGroup
> newLoadGroup
=
282 do_CreateInstance(NS_LOADGROUP_CONTRACTID
);
283 NS_ENSURE_TRUE(newLoadGroup
, NS_ERROR_OUT_OF_MEMORY
);
284 newLoadGroup
->SetLoadGroup(loadGroup
);
286 nsCOMPtr
<nsIDocumentLoaderFactory
> docLoaderFactory
=
287 nsContentUtils::FindInternalDocumentViewer(
288 nsLiteralCString(IMAGE_SVG_XML
));
289 NS_ENSURE_TRUE(docLoaderFactory
, NS_ERROR_NOT_AVAILABLE
);
291 nsCOMPtr
<nsIDocumentViewer
> viewer
;
292 nsCOMPtr
<nsIStreamListener
> listener
;
293 nsresult rv
= docLoaderFactory
->CreateInstance(
294 "external-resource", chan
, newLoadGroup
, nsLiteralCString(IMAGE_SVG_XML
),
295 nullptr, nullptr, getter_AddRefs(listener
), getter_AddRefs(viewer
));
296 NS_ENSURE_SUCCESS(rv
, rv
);
298 NS_ENSURE_TRUE(viewer
, NS_ERROR_UNEXPECTED
);
300 // Create a navigation time object and pass it to the SVG document through
302 // The timeline(DocumentTimeline, used in CSS animation) of this SVG
303 // document needs this navigation timing object for time computation, such
304 // as to calculate current time stamp based on the start time of navigation
307 // For a root document, DocShell would do these sort of things
308 // automatically. Since there is no DocShell for this wrapped SVG document,
309 // we must set it up manually.
310 RefPtr
<nsDOMNavigationTiming
> timing
= new nsDOMNavigationTiming(nullptr);
311 timing
->NotifyNavigationStart(
312 nsDOMNavigationTiming::DocShellState::eInactive
);
313 viewer
->SetNavigationTiming(timing
);
315 nsCOMPtr
<nsIParser
> parser
= do_QueryInterface(listener
);
316 NS_ENSURE_TRUE(parser
, NS_ERROR_UNEXPECTED
);
318 // XML-only, because this is for SVG content
319 nsCOMPtr
<nsIContentSink
> sink
= parser
->GetContentSink();
320 NS_ENSURE_TRUE(sink
, NS_ERROR_UNEXPECTED
);
322 listener
.swap(mListener
);
323 viewer
.forget(aViewer
);
324 newLoadGroup
.forget(aLoadGroup
);
326 RegisterForXPCOMShutdown();
330 void SVGDocumentWrapper::RegisterForXPCOMShutdown() {
331 MOZ_ASSERT(!mRegisteredForXPCOMShutdown
, "re-registering for XPCOM shutdown");
332 // Listen for xpcom-shutdown so that we can drop references to our
333 // helper-document at that point. (Otherwise, we won't get cleaned up
334 // until imgLoader::Shutdown, which can happen after the JAR service
335 // and RDF service have been unregistered.)
337 nsCOMPtr
<nsIObserverService
> obsSvc
= do_GetService(OBSERVER_SVC_CID
, &rv
);
338 if (NS_FAILED(rv
) || NS_FAILED(obsSvc
->AddObserver(
339 this, NS_XPCOM_SHUTDOWN_OBSERVER_ID
, true))) {
340 NS_WARNING("Failed to register as observer of XPCOM shutdown");
342 mRegisteredForXPCOMShutdown
= true;
346 void SVGDocumentWrapper::UnregisterForXPCOMShutdown() {
347 MOZ_ASSERT(mRegisteredForXPCOMShutdown
,
348 "unregistering for XPCOM shutdown w/out being registered");
351 nsCOMPtr
<nsIObserverService
> obsSvc
= do_GetService(OBSERVER_SVC_CID
, &rv
);
353 NS_FAILED(obsSvc
->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID
))) {
354 NS_WARNING("Failed to unregister as observer of XPCOM shutdown");
356 mRegisteredForXPCOMShutdown
= false;
360 void SVGDocumentWrapper::FlushLayout() {
361 if (SVGDocument
* doc
= GetDocument()) {
362 doc
->FlushPendingNotifications(FlushType::Layout
);
366 SVGDocument
* SVGDocumentWrapper::GetDocument() {
370 Document
* doc
= mViewer
->GetDocument();
374 return doc
->AsSVGDocument();
377 SVGSVGElement
* SVGDocumentWrapper::GetRootSVGElem() {
379 return nullptr; // Can happen during destruction
382 Document
* doc
= mViewer
->GetDocument();
384 return nullptr; // Can happen during destruction
387 Element
* rootElem
= mViewer
->GetDocument()->GetRootElement();
388 if (!rootElem
|| !rootElem
->IsSVGElement(nsGkAtoms::svg
)) {
392 return static_cast<SVGSVGElement
*>(rootElem
);
396 } // namespace mozilla