Files
palemoon27/image/SVGDocumentWrapper.cpp
T
roytam1 cbd579ebc8 import changes from wicknix/Arctic-Fox:
- backport of Bug 1188696 - Remove the XPCOM dependencies in nsRefPtr.h (6c2860799)
- backport of Bug 1188696 - Hoist nsRefPtr.h into MFBT (e892acb58)
- Backport of Bug 1138967 - Part 3: Add D3D11 YCbCr texture clients and upload on the client side (2e4218167)
- Bug 1038536 - Flatten image/src/ directory (3adb2d635)
- Bug 1038536 - Flatten image/public/ directory (22329f3b4)
- Bug 1038536 - Flatten image/decoders/icon/qt/public/ directory. (7b5b8b2af)
- Bug 1038536 - Update header guard after flatten image/build (20e1614ac)
- Bug 1116905 - part 2 - add MakeAndAddRef helper function to facilitate constructing TemporaryRef (9c85f45a0)
- update (ab2c6eccf)
- Bug 1139781 - Implement VideoPlaybackQuality for MediaCodecReader. (1a7c6c0a7)
- Bug 1138825 - Fix the crash at mAudioPromise: call decode audio data when the audio queue is empty and check the mAudioPromise is empty or not. (3f5d3a1c5)
- Bug 875247 - Add support for DXVA2 via D3D11 (2ca491206)
- Bug 1145513 - Upload YCbCr image data on the client side when using d3d9 layers. (50f7a69fa)
- Bug 1053563 - Use a static create function to replace InitWith for TextureClient. (dd1c8fc89)
- Bug 1145764 - Add some default-disabled logging to TextureClientPool (1cfc0d1b9)
- Bug 1120780 - Fallback on lock_ycbcr when ColorConvertor fails (3a9b893f0)
- Bug 1161815: Use a single ID2D1SolidColorBrush per DrawTarget. (a70b72ef6)
- Bug 1160485 - remove implicit conversion from RefPtr<T> to TemporaryR…ef<T>; (e6e6224c5)
- Bug 1116905 - part 3 - remove dependence on implicit conversion from T* to TemporaryRef<T>, gfx changes; (f71d3ffa8)
- Bug 1116905 - part 1 - remove dependence on implicit conversion from T* to TemporaryRef<T>, non-gfx changes; (f66714955)
- implement Event.srcElement as alias (6c1ee1c6d)
- 1116905 - part 4 - remove implicit conversion from non-nullptr T* to TemporaryRef<T> (f94c680f9)
- Bug 1031152 - Define a JS public API for working with SavedFrame instances (2aa41721a)
- Update TLD's from ESR 60 (b9dbe0ca3)
with some changes to fix building, reported to upstream:
- https://github.com/wicknix/Arctic-Fox/commit/2e421816773b6a57502907ab22c285d994d8b024#r31893045
- https://github.com/wicknix/Arctic-Fox/commit/50f7a69fa9a36634aef1ae5a221415ca98284435#r31892913
- https://github.com/wicknix/Arctic-Fox/commit/f667149556ae0e64c9dbce08836bb5d957db2464#r31892928
2019-01-10 15:30:35 +08:00

422 lines
12 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "SVGDocumentWrapper.h"
#include "mozilla/dom/Element.h"
#include "nsICategoryManager.h"
#include "nsIChannel.h"
#include "nsIContentViewer.h"
#include "nsIDocument.h"
#include "nsIDocumentLoaderFactory.h"
#include "nsIDOMSVGLength.h"
#include "nsIHttpChannel.h"
#include "nsIObserverService.h"
#include "nsIParser.h"
#include "nsIPresShell.h"
#include "nsIRequest.h"
#include "nsIStreamListener.h"
#include "nsIXMLContentSink.h"
#include "nsNetCID.h"
#include "nsComponentManagerUtils.h"
#include "nsSMILAnimationController.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/dom/SVGSVGElement.h"
#include "nsSVGEffects.h"
#include "mozilla/dom/SVGAnimatedLength.h"
#include "nsMimeTypes.h"
#include "DOMSVGLength.h"
// undef the GetCurrentTime macro defined in WinBase.h from the MS Platform SDK
#undef GetCurrentTime
using namespace mozilla::dom;
namespace mozilla {
namespace image {
NS_IMPL_ISUPPORTS(SVGDocumentWrapper,
nsIStreamListener,
nsIRequestObserver,
nsIObserver,
nsISupportsWeakReference)
SVGDocumentWrapper::SVGDocumentWrapper()
: mIgnoreInvalidation(false),
mRegisteredForXPCOMShutdown(false)
{
}
SVGDocumentWrapper::~SVGDocumentWrapper()
{
DestroyViewer();
if (mRegisteredForXPCOMShutdown) {
UnregisterForXPCOMShutdown();
}
}
void
SVGDocumentWrapper::DestroyViewer()
{
if (mViewer) {
mViewer->GetDocument()->OnPageHide(false, nullptr);
mViewer->Close(nullptr);
mViewer->Destroy();
mViewer = nullptr;
}
}
nsIFrame*
SVGDocumentWrapper::GetRootLayoutFrame()
{
Element* rootElem = GetRootSVGElem();
return rootElem ? rootElem->GetPrimaryFrame() : nullptr;
}
void
SVGDocumentWrapper::UpdateViewportBounds(const nsIntSize& aViewportSize)
{
MOZ_ASSERT(!mIgnoreInvalidation, "shouldn't be reentrant");
mIgnoreInvalidation = true;
nsIntRect currentBounds;
mViewer->GetBounds(currentBounds);
// If the bounds have changed, we need to do a layout flush.
if (currentBounds.Size() != aViewportSize) {
mViewer->SetBounds(nsIntRect(nsIntPoint(0, 0), aViewportSize));
FlushLayout();
}
mIgnoreInvalidation = false;
}
void
SVGDocumentWrapper::FlushImageTransformInvalidation()
{
MOZ_ASSERT(!mIgnoreInvalidation, "shouldn't be reentrant");
SVGSVGElement* svgElem = GetRootSVGElem();
if (!svgElem)
return;
mIgnoreInvalidation = true;
svgElem->FlushImageTransformInvalidation();
FlushLayout();
mIgnoreInvalidation = false;
}
bool
SVGDocumentWrapper::IsAnimated()
{
nsIDocument* doc = mViewer->GetDocument();
return doc && doc->HasAnimationController() &&
doc->GetAnimationController()->HasRegisteredAnimations();
}
void
SVGDocumentWrapper::StartAnimation()
{
// Can be called for animated images during shutdown, after we've
// already Observe()'d XPCOM shutdown and cleared out our mViewer pointer.
if (!mViewer)
return;
nsIDocument* doc = mViewer->GetDocument();
if (doc) {
nsSMILAnimationController* controller = doc->GetAnimationController();
if (controller) {
controller->Resume(nsSMILTimeContainer::PAUSE_IMAGE);
}
doc->SetImagesNeedAnimating(true);
}
}
void
SVGDocumentWrapper::StopAnimation()
{
// Can be called for animated images during shutdown, after we've
// already Observe()'d XPCOM shutdown and cleared out our mViewer pointer.
if (!mViewer)
return;
nsIDocument* doc = mViewer->GetDocument();
if (doc) {
nsSMILAnimationController* controller = doc->GetAnimationController();
if (controller) {
controller->Pause(nsSMILTimeContainer::PAUSE_IMAGE);
}
doc->SetImagesNeedAnimating(false);
}
}
void
SVGDocumentWrapper::ResetAnimation()
{
SVGSVGElement* svgElem = GetRootSVGElem();
if (!svgElem)
return;
svgElem->SetCurrentTime(0.0f);
}
float
SVGDocumentWrapper::GetCurrentTime()
{
SVGSVGElement* svgElem = GetRootSVGElem();
return svgElem ? svgElem->GetCurrentTime()
: 0.0f;
}
void
SVGDocumentWrapper::SetCurrentTime(float aTime)
{
SVGSVGElement* svgElem = GetRootSVGElem();
if (svgElem && svgElem->GetCurrentTime() != aTime) {
svgElem->SetCurrentTime(aTime);
}
}
void
SVGDocumentWrapper::TickRefreshDriver()
{
nsCOMPtr<nsIPresShell> presShell;
mViewer->GetPresShell(getter_AddRefs(presShell));
if (presShell) {
nsPresContext* presContext = presShell->GetPresContext();
if (presContext) {
presContext->RefreshDriver()->DoTick();
}
}
}
/** nsIStreamListener methods **/
/* void onDataAvailable (in nsIRequest request, in nsISupports ctxt,
in nsIInputStream inStr, in unsigned long sourceOffset,
in unsigned long count); */
NS_IMETHODIMP
SVGDocumentWrapper::OnDataAvailable(nsIRequest* aRequest, nsISupports* ctxt,
nsIInputStream* inStr,
uint64_t sourceOffset,
uint32_t count)
{
return mListener->OnDataAvailable(aRequest, ctxt, inStr,
sourceOffset, count);
}
/** nsIRequestObserver methods **/
/* void onStartRequest (in nsIRequest request, in nsISupports ctxt); */
NS_IMETHODIMP
SVGDocumentWrapper::OnStartRequest(nsIRequest* aRequest, nsISupports* ctxt)
{
nsresult rv = SetupViewer(aRequest,
getter_AddRefs(mViewer),
getter_AddRefs(mLoadGroup));
if (NS_SUCCEEDED(rv) &&
NS_SUCCEEDED(mListener->OnStartRequest(aRequest, nullptr))) {
mViewer->GetDocument()->SetIsBeingUsedAsImage();
StopAnimation(); // otherwise animations start automatically in helper doc
rv = mViewer->Init(nullptr, nsIntRect(0, 0, 0, 0));
if (NS_SUCCEEDED(rv)) {
rv = mViewer->Open(nullptr, nullptr);
}
}
return rv;
}
/* void onStopRequest (in nsIRequest request, in nsISupports ctxt,
in nsresult status); */
NS_IMETHODIMP
SVGDocumentWrapper::OnStopRequest(nsIRequest* aRequest, nsISupports* ctxt,
nsresult status)
{
if (mListener) {
mListener->OnStopRequest(aRequest, ctxt, status);
mListener = nullptr;
}
return NS_OK;
}
/** nsIObserver Methods **/
NS_IMETHODIMP
SVGDocumentWrapper::Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t *aData)
{
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
// Sever ties from rendering observers to helper-doc's root SVG node
SVGSVGElement* svgElem = GetRootSVGElem();
if (svgElem) {
nsSVGEffects::RemoveAllRenderingObservers(svgElem);
}
// Clean up at XPCOM shutdown time.
DestroyViewer();
if (mListener)
mListener = nullptr;
if (mLoadGroup)
mLoadGroup = nullptr;
// Turn off "registered" flag, or else we'll try to unregister when we die.
// (No need for that now, and the try would fail anyway -- it's too late.)
mRegisteredForXPCOMShutdown = false;
} else {
NS_ERROR("Unexpected observer topic.");
}
return NS_OK;
}
/** Private helper methods **/
// This method is largely cribbed from
// nsExternalResourceMap::PendingLoad::SetupViewer.
nsresult
SVGDocumentWrapper::SetupViewer(nsIRequest* aRequest,
nsIContentViewer** aViewer,
nsILoadGroup** aLoadGroup)
{
nsCOMPtr<nsIChannel> chan(do_QueryInterface(aRequest));
NS_ENSURE_TRUE(chan, NS_ERROR_UNEXPECTED);
// Check for HTTP error page
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(aRequest));
if (httpChannel) {
bool requestSucceeded;
if (NS_FAILED(httpChannel->GetRequestSucceeded(&requestSucceeded)) ||
!requestSucceeded) {
return NS_ERROR_FAILURE;
}
}
// Give this document its own loadgroup
nsCOMPtr<nsILoadGroup> loadGroup;
chan->GetLoadGroup(getter_AddRefs(loadGroup));
nsCOMPtr<nsILoadGroup> newLoadGroup =
do_CreateInstance(NS_LOADGROUP_CONTRACTID);
NS_ENSURE_TRUE(newLoadGroup, NS_ERROR_OUT_OF_MEMORY);
newLoadGroup->SetLoadGroup(loadGroup);
nsCOMPtr<nsICategoryManager> catMan =
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
NS_ENSURE_TRUE(catMan, NS_ERROR_NOT_AVAILABLE);
nsXPIDLCString contractId;
nsresult rv = catMan->GetCategoryEntry("Goanna-Content-Viewers", IMAGE_SVG_XML,
getter_Copies(contractId));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDocumentLoaderFactory> docLoaderFactory =
do_GetService(contractId);
NS_ENSURE_TRUE(docLoaderFactory, NS_ERROR_NOT_AVAILABLE);
nsCOMPtr<nsIContentViewer> viewer;
nsCOMPtr<nsIStreamListener> listener;
rv = docLoaderFactory->CreateInstance("external-resource", chan,
newLoadGroup,
NS_LITERAL_CSTRING(IMAGE_SVG_XML),
nullptr, nullptr,
getter_AddRefs(listener),
getter_AddRefs(viewer));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(viewer, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIParser> parser = do_QueryInterface(listener);
NS_ENSURE_TRUE(parser, NS_ERROR_UNEXPECTED);
// XML-only, because this is for SVG content
nsIContentSink* sink = parser->GetContentSink();
nsCOMPtr<nsIXMLContentSink> xmlSink = do_QueryInterface(sink);
NS_ENSURE_TRUE(sink, NS_ERROR_UNEXPECTED);
listener.swap(mListener);
viewer.forget(aViewer);
newLoadGroup.forget(aLoadGroup);
RegisterForXPCOMShutdown();
return NS_OK;
}
void
SVGDocumentWrapper::RegisterForXPCOMShutdown()
{
MOZ_ASSERT(!mRegisteredForXPCOMShutdown,
"re-registering for XPCOM shutdown");
// Listen for xpcom-shutdown so that we can drop references to our
// helper-document at that point. (Otherwise, we won't get cleaned up
// until imgLoader::Shutdown, which can happen after the JAR service
// and RDF service have been unregistered.)
nsresult rv;
nsCOMPtr<nsIObserverService> obsSvc = do_GetService(OBSERVER_SVC_CID, &rv);
if (NS_FAILED(rv) ||
NS_FAILED(obsSvc->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
true))) {
NS_WARNING("Failed to register as observer of XPCOM shutdown");
} else {
mRegisteredForXPCOMShutdown = true;
}
}
void
SVGDocumentWrapper::UnregisterForXPCOMShutdown()
{
MOZ_ASSERT(mRegisteredForXPCOMShutdown,
"unregistering for XPCOM shutdown w/out being registered");
nsresult rv;
nsCOMPtr<nsIObserverService> obsSvc = do_GetService(OBSERVER_SVC_CID, &rv);
if (NS_FAILED(rv) ||
NS_FAILED(obsSvc->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID))) {
NS_WARNING("Failed to unregister as observer of XPCOM shutdown");
} else {
mRegisteredForXPCOMShutdown = false;
}
}
void
SVGDocumentWrapper::FlushLayout()
{
nsCOMPtr<nsIPresShell> presShell;
mViewer->GetPresShell(getter_AddRefs(presShell));
if (presShell) {
presShell->FlushPendingNotifications(Flush_Layout);
}
}
nsIDocument*
SVGDocumentWrapper::GetDocument()
{
if (!mViewer)
return nullptr;
return mViewer->GetDocument(); // May be nullptr.
}
SVGSVGElement*
SVGDocumentWrapper::GetRootSVGElem()
{
if (!mViewer)
return nullptr; // Can happen during destruction
nsIDocument* doc = mViewer->GetDocument();
if (!doc)
return nullptr; // Can happen during destruction
Element* rootElem = mViewer->GetDocument()->GetRootElement();
if (!rootElem || !rootElem->IsSVG(nsGkAtoms::svg)) {
return nullptr;
}
return static_cast<SVGSVGElement*>(rootElem);
}
} // namespace image
} // namespace mozilla