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"
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"
39 NS_IMPL_ISUPPORTS(SVGDocumentWrapper
, nsIStreamListener
, nsIRequestObserver
,
40 nsIObserver
, nsISupportsWeakReference
)
42 SVGDocumentWrapper::SVGDocumentWrapper()
43 : mIgnoreInvalidation(false), mRegisteredForXPCOMShutdown(false) {}
45 SVGDocumentWrapper::~SVGDocumentWrapper() {
47 if (mRegisteredForXPCOMShutdown
) {
48 UnregisterForXPCOMShutdown();
52 void SVGDocumentWrapper::DestroyViewer() {
54 mViewer
->GetDocument()->OnPageHide(false, nullptr);
55 mViewer
->Close(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
));
79 mIgnoreInvalidation
= false;
82 void SVGDocumentWrapper::FlushImageTransformInvalidation() {
83 MOZ_ASSERT(!mIgnoreInvalidation
, "shouldn't be reentrant");
85 SVGSVGElement
* svgElem
= GetRootSVGElem();
90 mIgnoreInvalidation
= true;
91 svgElem
->FlushImageTransformInvalidation();
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.
103 Document
* doc
= mViewer
->GetDocument();
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).
113 if (doc
->HasAnimationController() &&
114 doc
->GetAnimationController()->HasRegisteredAnimations()) {
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.
128 Document
* doc
= mViewer
->GetDocument();
130 SMILAnimationController
* controller
= doc
->GetAnimationController();
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.
145 Document
* doc
= mViewer
->GetDocument();
147 SMILAnimationController
* controller
= doc
->GetAnimationController();
149 controller
->Pause(SMILTimeContainer::PAUSE_IMAGE
);
151 doc
->ImageTracker()->SetAnimatingState(false);
155 void SVGDocumentWrapper::ResetAnimation() {
156 SVGSVGElement
* svgElem
= GetRootSVGElem();
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();
179 nsPresContext
* presContext
= presShell
->GetPresContext();
181 presContext
->RefreshDriver()->DoTick();
186 /** nsIStreamListener methods **/
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 **/
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);
214 SVGDocumentWrapper::OnStopRequest(nsIRequest
* aRequest
, nsresult status
) {
216 mListener
->OnStopRequest(aRequest
, status
);
223 /** nsIObserver Methods **/
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();
231 SVGObserverUtils::RemoveAllRenderingObservers(svgElem
);
234 // Clean up at XPCOM shutdown time.
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;
247 NS_ERROR("Unexpected observer topic.");
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
));
265 bool requestSucceeded
;
266 if (NS_FAILED(httpChannel
->GetRequestSucceeded(&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
,
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
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
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();
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.)
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");
344 mRegisteredForXPCOMShutdown
= true;
348 void SVGDocumentWrapper::UnregisterForXPCOMShutdown() {
349 MOZ_ASSERT(mRegisteredForXPCOMShutdown
,
350 "unregistering for XPCOM shutdown w/out being registered");
353 nsCOMPtr
<nsIObserverService
> obsSvc
= do_GetService(OBSERVER_SVC_CID
, &rv
);
355 NS_FAILED(obsSvc
->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID
))) {
356 NS_WARNING("Failed to unregister as observer of XPCOM shutdown");
358 mRegisteredForXPCOMShutdown
= false;
362 void SVGDocumentWrapper::FlushLayout() {
363 if (SVGDocument
* doc
= GetDocument()) {
364 doc
->FlushPendingNotifications(FlushType::Layout
);
368 SVGDocument
* SVGDocumentWrapper::GetDocument() {
372 Document
* doc
= mViewer
->GetDocument();
376 return doc
->AsSVGDocument();
379 SVGSVGElement
* SVGDocumentWrapper::GetRootSVGElem() {
381 return nullptr; // Can happen during destruction
384 Document
* doc
= mViewer
->GetDocument();
386 return nullptr; // Can happen during destruction
389 Element
* rootElem
= mViewer
->GetDocument()->GetRootElement();
390 if (!rootElem
|| !rootElem
->IsSVGElement(nsGkAtoms::svg
)) {
394 return static_cast<SVGSVGElement
*>(rootElem
);
398 } // namespace mozilla