gecko-dev/dom/xml/nsXMLPrettyPrinter.cpp

273 lines
8.5 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-05-21 11:12:37 +00:00
/* 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 "nsXMLPrettyPrinter.h"
#include "nsContentUtils.h"
#include "nsICSSDeclaration.h"
#include "nsIObserver.h"
#include "nsSyncLoadService.h"
#include "nsPIDOMWindow.h"
#include "nsIServiceManager.h"
#include "nsNetUtil.h"
#include "mozilla/dom/Element.h"
#include "nsBindingManager.h"
#include "nsXBLService.h"
#include "nsIScriptSecurityManager.h"
#include "mozilla/Preferences.h"
#include "nsIDocument.h"
#include "nsVariant.h"
#include "mozilla/dom/CustomEvent.h"
#include "mozilla/dom/DocumentFragment.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/ToJSValue.h"
#include "mozilla/dom/txMozillaXSLTProcessor.h"
using namespace mozilla;
using namespace mozilla::dom;
NS_IMPL_ISUPPORTS(nsXMLPrettyPrinter,
nsIDocumentObserver,
nsIMutationObserver)
nsXMLPrettyPrinter::nsXMLPrettyPrinter() : mDocument(nullptr),
mUnhookPending(false)
{
}
nsXMLPrettyPrinter::~nsXMLPrettyPrinter()
{
NS_ASSERTION(!mDocument, "we shouldn't be referencing the document still");
}
nsresult
nsXMLPrettyPrinter::PrettyPrint(nsIDocument* aDocument,
bool* aDidPrettyPrint)
{
*aDidPrettyPrint = false;
// Check for iframe with display:none. Such iframes don't have presshells
nsCOMPtr<nsIPresShell> shell = aDocument->GetShell();
if (!shell) {
return NS_OK;
}
// check if we're in an invisible iframe
nsPIDOMWindowOuter *internalWin = aDocument->GetWindow();
nsCOMPtr<Element> frameElem;
if (internalWin) {
frameElem = internalWin->GetFrameElementInternal();
}
if (frameElem) {
nsCOMPtr<nsICSSDeclaration> computedStyle;
if (nsIDocument* frameOwnerDoc = frameElem->OwnerDoc()) {
nsPIDOMWindowOuter* window = frameOwnerDoc->GetDefaultView();
if (window) {
nsCOMPtr<nsPIDOMWindowInner> innerWindow =
window->GetCurrentInnerWindow();
ErrorResult dummy;
computedStyle = innerWindow->GetComputedStyle(*frameElem,
EmptyString(),
dummy);
dummy.SuppressException();
}
}
if (computedStyle) {
nsAutoString visibility;
computedStyle->GetPropertyValue(NS_LITERAL_STRING("visibility"),
visibility);
if (!visibility.EqualsLiteral("visible")) {
return NS_OK;
}
}
}
// check the pref
if (!Preferences::GetBool("layout.xml.prettyprint", true)) {
return NS_OK;
}
// Ok, we should prettyprint. Let's do it!
*aDidPrettyPrint = true;
nsresult rv = NS_OK;
// Load the XSLT
nsCOMPtr<nsIURI> xslUri;
rv = NS_NewURI(getter_AddRefs(xslUri),
NS_LITERAL_CSTRING("chrome://global/content/xml/XMLPrettyPrint.xsl"));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDocument> xslDocument;
rv = nsSyncLoadService::LoadDocument(xslUri, nsIContentPolicy::TYPE_XSLT,
nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
nullptr, true, mozilla::net::RP_Unset,
getter_AddRefs(xslDocument));
NS_ENSURE_SUCCESS(rv, rv);
// Transform the document
RefPtr<txMozillaXSLTProcessor> transformer = new txMozillaXSLTProcessor();
ErrorResult err;
transformer->ImportStylesheet(*xslDocument, err);
if (NS_WARN_IF(err.Failed())) {
return err.StealNSResult();
}
RefPtr<DocumentFragment> resultFragment =
transformer->TransformToFragment(*aDocument, *aDocument, err);
if (NS_WARN_IF(err.Failed())) {
return err.StealNSResult();
}
//
// Apply the prettprint XBL binding.
//
// We take some shortcuts here. In particular, we don't bother invoking the
// contstructor (since the binding has no constructor), and we don't bother
// calling LoadBindingDocument because it's a chrome:// URI and thus will get
// sync loaded no matter what.
//
// Grab the XBL service.
nsXBLService* xblService = nsXBLService::GetInstance();
NS_ENSURE_TRUE(xblService, NS_ERROR_NOT_AVAILABLE);
// Compute the binding URI.
nsCOMPtr<nsIURI> bindingUri;
rv = NS_NewURI(getter_AddRefs(bindingUri),
NS_LITERAL_STRING("chrome://global/content/xml/XMLPrettyPrint.xml#prettyprint"));
NS_ENSURE_SUCCESS(rv, rv);
// Compute the bound element.
RefPtr<Element> rootElement = aDocument->GetRootElement();
NS_ENSURE_TRUE(rootElement, NS_ERROR_UNEXPECTED);
// Grab the system principal.
nsCOMPtr<nsIPrincipal> sysPrincipal;
nsContentUtils::GetSecurityManager()->
GetSystemPrincipal(getter_AddRefs(sysPrincipal));
// Destroy any existing frames before we unbind anonymous content.
// Note that the shell might be Destroy'ed by now (see bug 1415541).
if (!shell->IsDestroying()) {
shell->DestroyFramesForAndRestyle(rootElement);
}
// Load the bindings.
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
RefPtr<nsXBLBinding> unused;
bool ignored;
rv = xblService->LoadBindings(rootElement, bindingUri, sysPrincipal,
getter_AddRefs(unused), &ignored);
NS_ENSURE_SUCCESS(rv, rv);
// Fire an event at the bound element to pass it |resultFragment|.
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
RefPtr<CustomEvent> event =
NS_NewDOMCustomEvent(rootElement, nullptr, nullptr);
MOZ_ASSERT(event);
AutoJSAPI jsapi;
if (!jsapi.Init(event->GetParentObject())) {
return NS_ERROR_UNEXPECTED;
}
JSContext* cx = jsapi.cx();
JS::Rooted<JS::Value> detail(cx);
if (!ToJSValue(cx, resultFragment, &detail)) {
return NS_ERROR_UNEXPECTED;
}
event->InitCustomEvent(cx, NS_LITERAL_STRING("prettyprint-dom-created"),
/* bubbles = */ false, /* cancelable = */ false,
detail);
event->SetTrusted(true);
rootElement->DispatchEvent(*event, err);
if (NS_WARN_IF(err.Failed())) {
return err.StealNSResult();
}
// Observe the document so we know when to switch to "normal" view
aDocument->AddObserver(this);
mDocument = aDocument;
NS_ADDREF_THIS();
return NS_OK;
}
void
nsXMLPrettyPrinter::MaybeUnhook(nsIContent* aContent)
{
// If there either aContent is null (the document-node was modified) or
// there isn't a binding parent we know it's non-anonymous content.
if ((!aContent || !aContent->GetBindingParent()) && !mUnhookPending) {
// Can't blindly to mUnhookPending after AddScriptRunner,
// since AddScriptRunner _could_ in theory run us
// synchronously
mUnhookPending = true;
nsContentUtils::AddScriptRunner(NewRunnableMethod(
"nsXMLPrettyPrinter::Unhook", this, &nsXMLPrettyPrinter::Unhook));
}
}
void
nsXMLPrettyPrinter::Unhook()
{
mDocument->RemoveObserver(this);
nsCOMPtr<Element> element = mDocument->GetDocumentElement();
if (element) {
mDocument->BindingManager()->ClearBinding(element);
}
mDocument = nullptr;
NS_RELEASE_THIS();
}
void
nsXMLPrettyPrinter::AttributeChanged(Element* aElement,
int32_t aNameSpaceID,
nsAtom* aAttribute,
int32_t aModType,
const nsAttrValue* aOldValue)
{
MaybeUnhook(aElement);
}
void
nsXMLPrettyPrinter::ContentAppended(nsIContent* aFirstNewContent)
{
MaybeUnhook(aFirstNewContent->GetParent());
}
void
nsXMLPrettyPrinter::ContentInserted(nsIContent* aChild)
{
MaybeUnhook(aChild->GetParent());
}
void
nsXMLPrettyPrinter::ContentRemoved(nsIContent* aChild,
nsIContent* aPreviousSibling)
{
MaybeUnhook(aChild->GetParent());
}
void
nsXMLPrettyPrinter::NodeWillBeDestroyed(const nsINode* aNode)
{
mDocument = nullptr;
NS_RELEASE_THIS();
}
nsresult NS_NewXMLPrettyPrinter(nsXMLPrettyPrinter** aPrinter)
{
*aPrinter = new nsXMLPrettyPrinter;
NS_ADDREF(*aPrinter);
return NS_OK;
}