Bug 976651 - Encapsulate the Read of a new nsXBLPrototypeBinding::Read a little better. r=mrbkap
authorAndrew McCreight <continuation@gmail.com>
Thu, 27 Feb 2014 10:04:10 -0800
changeset 171344 1238bfc6607bfcfbecef5e57011676c0ea88f352
parent 171343 bfbdc27179851679f7f01a19660768a2e235c6fa
child 171345 752a28fd6b73241896958887bfcc1b8046ef7412
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersmrbkap
bugs976651
milestone30.0a1
Bug 976651 - Encapsulate the Read of a new nsXBLPrototypeBinding::Read a little better. r=mrbkap
dom/xbl/nsXBLDocumentInfo.cpp
dom/xbl/nsXBLPrototypeBinding.cpp
dom/xbl/nsXBLPrototypeBinding.h
--- a/dom/xbl/nsXBLDocumentInfo.cpp
+++ b/dom/xbl/nsXBLDocumentInfo.cpp
@@ -467,20 +467,18 @@ nsXBLDocumentInfo::ReadPrototypeBindings
 
   while (1) {
     uint8_t flags;
     nsresult rv = stream->Read8(&flags);
     NS_ENSURE_SUCCESS(rv, rv);
     if (flags == XBLBinding_Serialize_NoMoreBindings)
       break;
 
-    nsXBLPrototypeBinding* binding = new nsXBLPrototypeBinding();
-    rv = binding->Read(stream, docInfo, doc, flags);
+    rv = nsXBLPrototypeBinding::ReadNewBinding(stream, docInfo, doc, flags);
     if (NS_FAILED(rv)) {
-      delete binding;
       return rv;
     }
   }
 
   docInfo.swap(*aDocInfo);
   return NS_OK;
 }
 
--- a/dom/xbl/nsXBLPrototypeBinding.cpp
+++ b/dom/xbl/nsXBLPrototypeBinding.cpp
@@ -1034,16 +1034,33 @@ nsXBLPrototypeBinding::Read(nsIObjectInp
   if (isFirstBinding) {
     aDocInfo->SetFirstPrototypeBinding(this);
   }
 
   cleanup.Disconnect();
   return NS_OK;
 }
 
+// static
+nsresult
+nsXBLPrototypeBinding::ReadNewBinding(nsIObjectInputStream* aStream,
+                                      nsXBLDocumentInfo* aDocInfo,
+                                      nsIDocument* aDocument,
+                                      uint8_t aFlags)
+{
+  // If the Read() succeeds, |binding| will end up being owned by aDocInfo's
+  // binding table. Otherwise, we must manually delete it.
+  nsXBLPrototypeBinding* binding = new nsXBLPrototypeBinding();
+  nsresult rv = binding->Read(aStream, aDocInfo, aDocument, aFlags);
+  if (NS_FAILED(rv)) {
+    delete binding;
+  }
+  return rv;
+}
+
 static PLDHashOperator
 WriteInterfaceID(const nsIID& aKey, nsIContent* aData, void* aClosure)
 {
   // We can just write out the ids. The cache will be invalidated when a
   // different build is used, so we don't need to worry about ids changing.
   static_cast<nsIObjectOutputStream *>(aClosure)->WriteID(aKey);
   return PL_DHASH_NEXT;
 }
--- a/dom/xbl/nsXBLPrototypeBinding.h
+++ b/dom/xbl/nsXBLPrototypeBinding.h
@@ -140,29 +140,36 @@ public:
     if (!mKeyHandlersRegistered) {
       CreateKeyHandlers();
       mKeyHandlersRegistered = true;
     }
 
     return &mKeyHandlers;
   }
 
+private:
+  nsresult Read(nsIObjectInputStream* aStream,
+                nsXBLDocumentInfo* aDocInfo,
+                nsIDocument* aDocument,
+                uint8_t aFlags);
+
   /**
-   * Read this binding from the stream aStream into the xbl document aDocument.
+   * Read a new binding from the stream aStream into the xbl document aDocument.
    * aDocInfo should be the xbl document info for the binding document.
    * aFlags can contain XBLBinding_Serialize_InheritStyle to indicate that
    * mInheritStyle flag should be set, and XBLBinding_Serialize_IsFirstBinding
    * to indicate the first binding in a document.
    * XBLBinding_Serialize_ChromeOnlyContent indicates that
    * nsXBLPrototypeBinding::mChromeOnlyContent should be true.
    */
-  nsresult Read(nsIObjectInputStream* aStream,
-                nsXBLDocumentInfo* aDocInfo,
-                nsIDocument* aDocument,
-                uint8_t aFlags);
+public:
+  static nsresult ReadNewBinding(nsIObjectInputStream* aStream,
+                                 nsXBLDocumentInfo* aDocInfo,
+                                 nsIDocument* aDocument,
+                                 uint8_t aFlags);
 
   /**
    * Write this binding to the stream.
    */
   nsresult Write(nsIObjectOutputStream* aStream);
 
   /**
    * Read a content node from aStream and return it in aChild.