Bug 1602422 - Remove xpidl [array] use in calIICSService. r=mkmelin,darktrojan
authorBen Campbell <benc@thunderbird.net>
Fri, 20 Dec 2019 07:36:25 +1300
changeset 37834 ebbfde7f5adde2b8e22f5366cb2cf3569e112f07
parent 37833 047a04a5198d3e0d1862279a25b19e549c67782d
child 37835 5ba4422e867f7f2b922673b2b8bb58dac0a788e3
push id397
push userclokep@gmail.com
push dateMon, 10 Feb 2020 21:16:13 +0000
reviewersmkmelin, darktrojan
bugs1602422
Bug 1602422 - Remove xpidl [array] use in calIICSService. r=mkmelin,darktrojan
calendar/base/backend/icaljs/calICSService.js
calendar/base/backend/libical/calICSService.cpp
calendar/base/public/calIICSService.idl
--- a/calendar/base/backend/icaljs/calICSService.js
+++ b/calendar/base/backend/icaljs/calICSService.js
@@ -449,17 +449,17 @@ calIcalComponent.prototype = {
   serializeToICS: function() {
     return this.innerObject.toString() + ICAL.newLineChar;
   },
   toString: function() {
     return this.innerObject.toString();
   },
 
   addSubcomponent: function(comp) {
-    comp.getReferencedTimezones({}).forEach(this.addTimezoneReference, this);
+    comp.getReferencedTimezones().forEach(this.addTimezoneReference, this);
     let jscomp = unwrapSingle(ICAL.Component, comp);
     this.innerObject.addSubcomponent(jscomp);
   },
 
   propertyIterator: null,
   getFirstProperty: function(kind) {
     if (kind == "ANY") {
       kind = null;
@@ -537,19 +537,17 @@ calIcalComponent.prototype = {
         }
       }
 
       this.mReferencedZones[timezone.tzid] = timezone;
     }
   },
 
   getReferencedTimezones: function(aCount) {
-    let vals = Object.keys(this.mReferencedZones).map(timezone => this.mReferencedZones[timezone]);
-    aCount.value = vals.length;
-    return vals;
+    return Object.keys(this.mReferencedZones).map(timezone => this.mReferencedZones[timezone]);
   },
 
   serializeToICSStream: function() {
     let unicodeConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(
       Ci.nsIScriptableUnicodeConverter
     );
     unicodeConverter.charset = "UTF-8";
     return unicodeConverter.convertToInputStream(this.innerObject.toString());
--- a/calendar/base/backend/libical/calICSService.cpp
+++ b/calendar/base/backend/libical/calICSService.cpp
@@ -465,41 +465,28 @@ calIcalComponent::AddTimezoneReference(c
   nsresult rv = aTimezone->GetTzid(tzid);
   NS_ENSURE_SUCCESS(rv, rv);
   mReferencedTimezones.Put(tzid, aTimezone);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
-calIcalComponent::GetReferencedTimezones(uint32_t *aCount,
-                                         calITimezone ***aTimezones) {
-  NS_ENSURE_ARG_POINTER(aCount);
-  NS_ENSURE_ARG_POINTER(aTimezones);
-
+calIcalComponent::GetReferencedTimezones(
+    nsTArray<RefPtr<calITimezone>> &aTimezones) {
+  aTimezones.ClearAndRetainStorage();
   uint32_t const count = mReferencedTimezones.Count();
   if (count == 0) {
-    *aCount = 0;
-    *aTimezones = nullptr;
     return NS_OK;
   }
-
-  calITimezone **const timezones =
-      static_cast<calITimezone **>(moz_xmalloc(sizeof(calITimezone *) * count));
-  CAL_ENSURE_MEMORY(timezones);
-  // tzptr will get used as an iterator by the enumerator function
-  calITimezone **tzptr = timezones;
+  aTimezones.SetCapacity(count);
   for (auto iter = mReferencedTimezones.ConstIter(); !iter.Done();
        iter.Next()) {
-    NS_ADDREF(*tzptr = iter.Data());
-    ++tzptr;
+    aTimezones.AppendElement(iter.Data());
   }
-
-  *aTimezones = timezones;
-  *aCount = count;
   return NS_OK;
 }
 
 nsresult calIcalComponent::SetPropertyValue(icalproperty_kind kind,
                                             icalvalue *val) {
   ClearAllProperties(kind);
   if (!val) return NS_OK;
 
@@ -998,36 +985,24 @@ calIcalComponent::AddSubcomponent(calIIc
    */
 
   nsresult rv;
   nsCOMPtr<calIIcalComponentLibical> icalcomp = do_QueryInterface(aComp, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   calIcalComponent *const ical = toIcalComponent(icalcomp);
 
-  uint32_t tzCount = 0;
-  calITimezone **timezones = nullptr;
-  rv = ical->GetReferencedTimezones(&tzCount, &timezones);
-  NS_ENSURE_SUCCESS(rv, rv);
-
+  nsTArray<RefPtr<calITimezone>> timezones;
+  rv = ical->GetReferencedTimezones(timezones);
   calIcalComponent *const vcal = getParentVCalendarOrThis();
-  bool failed = false;
-  for (uint32_t i = 0; i < tzCount; i++) {
-    if (!failed) {
-      rv = vcal->AddTimezoneReference(timezones[i]);
-      if (NS_FAILED(rv)) failed = true;
-    }
-
-    NS_RELEASE(timezones[i]);
+  for (auto &tz : timezones) {
+    rv = vcal->AddTimezoneReference(tz);
+    NS_ENSURE_SUCCESS(rv, rv);
   }
 
-  free(timezones);
-
-  if (failed) return rv;
-
   if (ical->mParent) {
     ical->mComponent = icalcomponent_new_clone(ical->mComponent);
   }
   ical->mParent = this;
   icalcomponent_add_component(mComponent, ical->mComponent);
   return NS_OK;
 }
 
--- a/calendar/base/public/calIICSService.idl
+++ b/calendar/base/public/calIICSService.idl
@@ -135,18 +135,17 @@ interface calIIcalComponent : nsISupport
      */
     void addTimezoneReference(in calITimezone aTimezone);
 
     /**
      * Returns an array of VTIMEZONE components.
      * These are the timezones that are in use by this
      * component and its children.
      */
-    void getReferencedTimezones(out uint32_t aCount,
-                                [array,size_is(aCount),retval] out calITimezone aTimezones);
+    Array<calITimezone> getReferencedTimezones();
 
     /**
      * Clones the component. The cloned component is decoupled from any parent.
      * @return cloned component
      */
     calIIcalComponent clone();
 };