Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsXMLPrettyPrinter.h" |
michael@0 | 7 | #include "nsContentUtils.h" |
michael@0 | 8 | #include "nsIDOMCSSStyleDeclaration.h" |
michael@0 | 9 | #include "nsIDOMDocumentXBL.h" |
michael@0 | 10 | #include "nsIObserver.h" |
michael@0 | 11 | #include "nsIXSLTProcessor.h" |
michael@0 | 12 | #include "nsSyncLoadService.h" |
michael@0 | 13 | #include "nsPIDOMWindow.h" |
michael@0 | 14 | #include "nsIDOMElement.h" |
michael@0 | 15 | #include "nsIDOMDocument.h" |
michael@0 | 16 | #include "nsIServiceManager.h" |
michael@0 | 17 | #include "nsNetUtil.h" |
michael@0 | 18 | #include "mozilla/dom/Element.h" |
michael@0 | 19 | #include "nsIDOMDocumentFragment.h" |
michael@0 | 20 | #include "nsBindingManager.h" |
michael@0 | 21 | #include "nsXBLService.h" |
michael@0 | 22 | #include "nsIScriptSecurityManager.h" |
michael@0 | 23 | #include "mozilla/Preferences.h" |
michael@0 | 24 | #include "nsIDocument.h" |
michael@0 | 25 | #include "nsVariant.h" |
michael@0 | 26 | #include "nsIDOMCustomEvent.h" |
michael@0 | 27 | #include "GeneratedEvents.h" |
michael@0 | 28 | |
michael@0 | 29 | using namespace mozilla; |
michael@0 | 30 | using namespace mozilla::dom; |
michael@0 | 31 | |
michael@0 | 32 | NS_IMPL_ISUPPORTS(nsXMLPrettyPrinter, |
michael@0 | 33 | nsIDocumentObserver, |
michael@0 | 34 | nsIMutationObserver) |
michael@0 | 35 | |
michael@0 | 36 | nsXMLPrettyPrinter::nsXMLPrettyPrinter() : mDocument(nullptr), |
michael@0 | 37 | mUnhookPending(false) |
michael@0 | 38 | { |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | nsXMLPrettyPrinter::~nsXMLPrettyPrinter() |
michael@0 | 42 | { |
michael@0 | 43 | NS_ASSERTION(!mDocument, "we shouldn't be referencing the document still"); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | nsresult |
michael@0 | 47 | nsXMLPrettyPrinter::PrettyPrint(nsIDocument* aDocument, |
michael@0 | 48 | bool* aDidPrettyPrint) |
michael@0 | 49 | { |
michael@0 | 50 | *aDidPrettyPrint = false; |
michael@0 | 51 | |
michael@0 | 52 | // Check for iframe with display:none. Such iframes don't have presshells |
michael@0 | 53 | if (!aDocument->GetShell()) { |
michael@0 | 54 | return NS_OK; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // check if we're in an invisible iframe |
michael@0 | 58 | nsPIDOMWindow *internalWin = aDocument->GetWindow(); |
michael@0 | 59 | nsCOMPtr<nsIDOMElement> frameElem; |
michael@0 | 60 | if (internalWin) { |
michael@0 | 61 | internalWin->GetFrameElement(getter_AddRefs(frameElem)); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | if (frameElem) { |
michael@0 | 65 | nsCOMPtr<nsIDOMCSSStyleDeclaration> computedStyle; |
michael@0 | 66 | nsCOMPtr<nsIDOMDocument> frameOwnerDoc; |
michael@0 | 67 | frameElem->GetOwnerDocument(getter_AddRefs(frameOwnerDoc)); |
michael@0 | 68 | if (frameOwnerDoc) { |
michael@0 | 69 | nsCOMPtr<nsIDOMWindow> window; |
michael@0 | 70 | frameOwnerDoc->GetDefaultView(getter_AddRefs(window)); |
michael@0 | 71 | if (window) { |
michael@0 | 72 | window->GetComputedStyle(frameElem, |
michael@0 | 73 | EmptyString(), |
michael@0 | 74 | getter_AddRefs(computedStyle)); |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | if (computedStyle) { |
michael@0 | 79 | nsAutoString visibility; |
michael@0 | 80 | computedStyle->GetPropertyValue(NS_LITERAL_STRING("visibility"), |
michael@0 | 81 | visibility); |
michael@0 | 82 | if (!visibility.EqualsLiteral("visible")) { |
michael@0 | 83 | |
michael@0 | 84 | return NS_OK; |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // check the pref |
michael@0 | 90 | if (!Preferences::GetBool("layout.xml.prettyprint", true)) { |
michael@0 | 91 | return NS_OK; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | // Ok, we should prettyprint. Let's do it! |
michael@0 | 95 | *aDidPrettyPrint = true; |
michael@0 | 96 | nsresult rv = NS_OK; |
michael@0 | 97 | |
michael@0 | 98 | // Load the XSLT |
michael@0 | 99 | nsCOMPtr<nsIURI> xslUri; |
michael@0 | 100 | rv = NS_NewURI(getter_AddRefs(xslUri), |
michael@0 | 101 | NS_LITERAL_CSTRING("chrome://global/content/xml/XMLPrettyPrint.xsl")); |
michael@0 | 102 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 103 | |
michael@0 | 104 | nsCOMPtr<nsIDOMDocument> xslDocument; |
michael@0 | 105 | rv = nsSyncLoadService::LoadDocument(xslUri, nullptr, nullptr, true, |
michael@0 | 106 | getter_AddRefs(xslDocument)); |
michael@0 | 107 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 108 | |
michael@0 | 109 | // Transform the document |
michael@0 | 110 | nsCOMPtr<nsIXSLTProcessor> transformer = |
michael@0 | 111 | do_CreateInstance("@mozilla.org/document-transformer;1?type=xslt", &rv); |
michael@0 | 112 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 113 | |
michael@0 | 114 | rv = transformer->ImportStylesheet(xslDocument); |
michael@0 | 115 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 116 | |
michael@0 | 117 | nsCOMPtr<nsIDOMDocumentFragment> resultFragment; |
michael@0 | 118 | nsCOMPtr<nsIDOMDocument> sourceDocument = do_QueryInterface(aDocument); |
michael@0 | 119 | rv = transformer->TransformToFragment(sourceDocument, sourceDocument, |
michael@0 | 120 | getter_AddRefs(resultFragment)); |
michael@0 | 121 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 122 | |
michael@0 | 123 | // |
michael@0 | 124 | // Apply the prettprint XBL binding. |
michael@0 | 125 | // |
michael@0 | 126 | // We take some shortcuts here. In particular, we don't bother invoking the |
michael@0 | 127 | // contstructor (since the binding has no constructor), and we don't bother |
michael@0 | 128 | // calling LoadBindingDocument because it's a chrome:// URI and thus will get |
michael@0 | 129 | // sync loaded no matter what. |
michael@0 | 130 | // |
michael@0 | 131 | |
michael@0 | 132 | // Grab the XBL service. |
michael@0 | 133 | nsXBLService* xblService = nsXBLService::GetInstance(); |
michael@0 | 134 | NS_ENSURE_TRUE(xblService, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 135 | |
michael@0 | 136 | // Compute the binding URI. |
michael@0 | 137 | nsCOMPtr<nsIURI> bindingUri; |
michael@0 | 138 | rv = NS_NewURI(getter_AddRefs(bindingUri), |
michael@0 | 139 | NS_LITERAL_STRING("chrome://global/content/xml/XMLPrettyPrint.xml#prettyprint")); |
michael@0 | 140 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 141 | |
michael@0 | 142 | // Compute the bound element. |
michael@0 | 143 | nsCOMPtr<nsIContent> rootCont = aDocument->GetRootElement(); |
michael@0 | 144 | NS_ENSURE_TRUE(rootCont, NS_ERROR_UNEXPECTED); |
michael@0 | 145 | |
michael@0 | 146 | // Grab the system principal. |
michael@0 | 147 | nsCOMPtr<nsIPrincipal> sysPrincipal; |
michael@0 | 148 | nsContentUtils::GetSecurityManager()-> |
michael@0 | 149 | GetSystemPrincipal(getter_AddRefs(sysPrincipal)); |
michael@0 | 150 | |
michael@0 | 151 | // Load the bindings. |
michael@0 | 152 | nsRefPtr<nsXBLBinding> unused; |
michael@0 | 153 | bool ignored; |
michael@0 | 154 | rv = xblService->LoadBindings(rootCont, bindingUri, sysPrincipal, |
michael@0 | 155 | getter_AddRefs(unused), &ignored); |
michael@0 | 156 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 157 | |
michael@0 | 158 | // Fire an event at the bound element to pass it |resultFragment|. |
michael@0 | 159 | nsCOMPtr<nsIDOMEvent> domEvent; |
michael@0 | 160 | rv = NS_NewDOMCustomEvent(getter_AddRefs(domEvent), rootCont, |
michael@0 | 161 | nullptr, nullptr); |
michael@0 | 162 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 163 | nsCOMPtr<nsIDOMCustomEvent> customEvent = do_QueryInterface(domEvent); |
michael@0 | 164 | MOZ_ASSERT(customEvent); |
michael@0 | 165 | nsCOMPtr<nsIWritableVariant> resultFragmentVariant = new nsVariant(); |
michael@0 | 166 | rv = resultFragmentVariant->SetAsISupports(resultFragment); |
michael@0 | 167 | MOZ_ASSERT(NS_SUCCEEDED(rv)); |
michael@0 | 168 | rv = customEvent->InitCustomEvent(NS_LITERAL_STRING("prettyprint-dom-created"), |
michael@0 | 169 | /* bubbles = */ false, /* cancelable = */ false, |
michael@0 | 170 | /* detail = */ resultFragmentVariant); |
michael@0 | 171 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 172 | customEvent->SetTrusted(true); |
michael@0 | 173 | bool dummy; |
michael@0 | 174 | rv = rootCont->DispatchEvent(domEvent, &dummy); |
michael@0 | 175 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 176 | |
michael@0 | 177 | // Observe the document so we know when to switch to "normal" view |
michael@0 | 178 | aDocument->AddObserver(this); |
michael@0 | 179 | mDocument = aDocument; |
michael@0 | 180 | |
michael@0 | 181 | NS_ADDREF_THIS(); |
michael@0 | 182 | |
michael@0 | 183 | return NS_OK; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | void |
michael@0 | 187 | nsXMLPrettyPrinter::MaybeUnhook(nsIContent* aContent) |
michael@0 | 188 | { |
michael@0 | 189 | // If there either aContent is null (the document-node was modified) or |
michael@0 | 190 | // there isn't a binding parent we know it's non-anonymous content. |
michael@0 | 191 | if ((!aContent || !aContent->GetBindingParent()) && !mUnhookPending) { |
michael@0 | 192 | // Can't blindly to mUnhookPending after AddScriptRunner, |
michael@0 | 193 | // since AddScriptRunner _could_ in theory run us |
michael@0 | 194 | // synchronously |
michael@0 | 195 | mUnhookPending = true; |
michael@0 | 196 | nsContentUtils::AddScriptRunner( |
michael@0 | 197 | NS_NewRunnableMethod(this, &nsXMLPrettyPrinter::Unhook)); |
michael@0 | 198 | } |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | void |
michael@0 | 202 | nsXMLPrettyPrinter::Unhook() |
michael@0 | 203 | { |
michael@0 | 204 | mDocument->RemoveObserver(this); |
michael@0 | 205 | nsCOMPtr<Element> element = mDocument->GetDocumentElement(); |
michael@0 | 206 | |
michael@0 | 207 | if (element) { |
michael@0 | 208 | mDocument->BindingManager()->ClearBinding(element); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | mDocument = nullptr; |
michael@0 | 212 | |
michael@0 | 213 | NS_RELEASE_THIS(); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | void |
michael@0 | 217 | nsXMLPrettyPrinter::AttributeChanged(nsIDocument* aDocument, |
michael@0 | 218 | Element* aElement, |
michael@0 | 219 | int32_t aNameSpaceID, |
michael@0 | 220 | nsIAtom* aAttribute, |
michael@0 | 221 | int32_t aModType) |
michael@0 | 222 | { |
michael@0 | 223 | MaybeUnhook(aElement); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | void |
michael@0 | 227 | nsXMLPrettyPrinter::ContentAppended(nsIDocument* aDocument, |
michael@0 | 228 | nsIContent* aContainer, |
michael@0 | 229 | nsIContent* aFirstNewContent, |
michael@0 | 230 | int32_t aNewIndexInContainer) |
michael@0 | 231 | { |
michael@0 | 232 | MaybeUnhook(aContainer); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | void |
michael@0 | 236 | nsXMLPrettyPrinter::ContentInserted(nsIDocument* aDocument, |
michael@0 | 237 | nsIContent* aContainer, |
michael@0 | 238 | nsIContent* aChild, |
michael@0 | 239 | int32_t aIndexInContainer) |
michael@0 | 240 | { |
michael@0 | 241 | MaybeUnhook(aContainer); |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | void |
michael@0 | 245 | nsXMLPrettyPrinter::ContentRemoved(nsIDocument* aDocument, |
michael@0 | 246 | nsIContent* aContainer, |
michael@0 | 247 | nsIContent* aChild, |
michael@0 | 248 | int32_t aIndexInContainer, |
michael@0 | 249 | nsIContent* aPreviousSibling) |
michael@0 | 250 | { |
michael@0 | 251 | MaybeUnhook(aContainer); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | void |
michael@0 | 255 | nsXMLPrettyPrinter::NodeWillBeDestroyed(const nsINode* aNode) |
michael@0 | 256 | { |
michael@0 | 257 | mDocument = nullptr; |
michael@0 | 258 | NS_RELEASE_THIS(); |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | |
michael@0 | 262 | nsresult NS_NewXMLPrettyPrinter(nsXMLPrettyPrinter** aPrinter) |
michael@0 | 263 | { |
michael@0 | 264 | *aPrinter = new nsXMLPrettyPrinter; |
michael@0 | 265 | NS_ADDREF(*aPrinter); |
michael@0 | 266 | return NS_OK; |
michael@0 | 267 | } |