Bug 914618 - Reimplement XML pretty printing with events. r=smaug, a=lsblakk
authorBobby Holley <bobbyholley@gmail.com>
Thu, 03 Oct 2013 16:02:36 +0200
changeset 160569 d6e2c73a4ea608c88f7831583b19a27fe138ec65
parent 160568 d628f443792965acde48679ad203163acc8bd4ad
child 160570 6a2632a328b3b4359593bc414a79f8acca381b4c
push id2961
push userlsblakk@mozilla.com
push dateMon, 28 Oct 2013 21:59:28 +0000
treeherdermozilla-beta@73ef4f13486f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, lsblakk
bugs914618
milestone26.0a2
Bug 914618 - Reimplement XML pretty printing with events. r=smaug, a=lsblakk
content/xml/document/resources/XMLPrettyPrint.xml
content/xml/document/src/nsXMLPrettyPrinter.cpp
--- a/content/xml/document/resources/XMLPrettyPrint.xml
+++ b/content/xml/document/resources/XMLPrettyPrint.xml
@@ -7,28 +7,16 @@
           xmlns:html="http://www.w3.org/1999/xhtml">
 
   <binding id="prettyprint">
 
     <content><html:div id='top'/>
       <html:span style="display: none;"><children/></html:span>
     </content>
 
-    <implementation implements="nsIObserver">
-      <method name="observe">
-        <parameter name="aSubject"/>
-        <parameter name="aTopic"/>
-        <parameter name="aData"/>
-        <body>
-          if (aTopic == "prettyprint-dom-created")
-            document.getAnonymousNodes(this).item(0).appendChild(aSubject);
-        </body>
-      </method>
-    </implementation>
-
     <handlers>
       <handler event="click" button="0">
       <![CDATA[
         try {
           var par = event.originalTarget;
           if (par.nodeName == 'div' && par.className == 'expander') {
             if (par.parentNode.className == 'expander-closed') {
               par.parentNode.className = 'expander-open';
@@ -38,13 +26,18 @@
               par.parentNode.className = 'expander-closed';
               event.originalTarget.firstChild.data = '+';
             }
           }
         } catch (e) {
         }
       ]]>
       </handler>
+      <handler event="prettyprint-dom-created" allowuntrusted="false">
+        <![CDATA[
+          document.getAnonymousNodes(this).item(0).appendChild(event.detail);
+        ]]>
+      </handler>
     </handlers>
 
   </binding>
 
 </bindings>
--- a/content/xml/document/src/nsXMLPrettyPrinter.cpp
+++ b/content/xml/document/src/nsXMLPrettyPrinter.cpp
@@ -16,16 +16,19 @@
 #include "nsIServiceManager.h"
 #include "nsNetUtil.h"
 #include "mozilla/dom/Element.h"
 #include "nsIDOMDocumentFragment.h"
 #include "nsBindingManager.h"
 #include "nsXBLService.h"
 #include "nsIScriptSecurityManager.h"
 #include "mozilla/Preferences.h"
+#include "nsVariant.h"
+#include "nsIDOMCustomEvent.h"
+#include "GeneratedEvents.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 NS_IMPL_ISUPPORTS2(nsXMLPrettyPrinter,
                    nsIDocumentObserver,
                    nsIMutationObserver)
 
@@ -146,26 +149,33 @@ nsXMLPrettyPrinter::PrettyPrint(nsIDocum
 
     // Load the bindings.
     nsRefPtr<nsXBLBinding> unused;
     bool ignored;
     rv = xblService->LoadBindings(rootCont, bindingUri, sysPrincipal,
                                   getter_AddRefs(unused), &ignored);
     NS_ENSURE_SUCCESS(rv, rv);
 
-    // Hand the result document to the binding
-    nsCOMPtr<nsIObserver> binding;
-    aDocument->BindingManager()->GetBindingImplementation(rootCont,
-                                              NS_GET_IID(nsIObserver),
-                                              (void**)getter_AddRefs(binding));
-    NS_ASSERTION(binding, "Prettyprint binding doesn't implement nsIObserver");
-    NS_ENSURE_TRUE(binding, NS_ERROR_UNEXPECTED);
-    
-    rv = binding->Observe(resultFragment, "prettyprint-dom-created",
-                          EmptyString().get());
+    // Fire an event at the bound element to pass it |resultFragment|.
+    nsCOMPtr<nsIDOMEvent> domEvent;
+    rv = NS_NewDOMCustomEvent(getter_AddRefs(domEvent), rootCont,
+                              nullptr, nullptr);
+    NS_ENSURE_SUCCESS(rv, rv);
+    nsCOMPtr<nsIDOMCustomEvent> customEvent = do_QueryInterface(domEvent);
+    MOZ_ASSERT(customEvent);
+    nsCOMPtr<nsIWritableVariant> resultFragmentVariant = new nsVariant();
+    rv = resultFragmentVariant->SetAsISupports(resultFragment);
+    MOZ_ASSERT(NS_SUCCEEDED(rv));
+    rv = customEvent->InitCustomEvent(NS_LITERAL_STRING("prettyprint-dom-created"),
+                                      /* bubbles = */ false, /* cancelable = */ false,
+                                      /* detail = */ resultFragmentVariant);
+    NS_ENSURE_SUCCESS(rv, rv);
+    customEvent->SetTrusted(true);
+    bool dummy;
+    rv = rootCont->DispatchEvent(domEvent, &dummy);
     NS_ENSURE_SUCCESS(rv, rv);
 
     // Observe the document so we know when to switch to "normal" view
     aDocument->AddObserver(this);
     mDocument = aDocument;
 
     NS_ADDREF_THIS();