Backed out changeset 7b87be447ed8 (bug 1499821) for bustages on nsTransferable.h. CLOSED TREE
authorCsoregi Natalia <ncsoregi@mozilla.com>
Thu, 25 Oct 2018 01:26:25 +0300
changeset 491233 2ab590b3ed3f6534c07bae569ec3c84311b1e82b
parent 491232 fbe393405a4ac2fccae94a26d173fc9fbe30e276
child 491234 226a941a7069093cd5770168ee123fa5630c41c1
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
bugs1499821
milestone65.0a1
backs out7b87be447ed82263ea5e1e8cb6f5412dd427dc71
Backed out changeset 7b87be447ed8 (bug 1499821) for bustages on nsTransferable.h. CLOSED TREE
widget/nsTransferable.cpp
widget/nsTransferable.h
--- a/widget/nsTransferable.cpp
+++ b/widget/nsTransferable.cpp
@@ -34,16 +34,28 @@ Notes to self:
 #include "nsIOutputStream.h"
 #include "nsIInputStream.h"
 #include "nsIWeakReferenceUtils.h"
 #include "nsILoadContext.h"
 #include "mozilla/UniquePtr.h"
 
 NS_IMPL_ISUPPORTS(nsTransferable, nsITransferable)
 
+size_t
+GetDataForFlavor(const nsTArray<DataStruct>& aArray, const char* aDataFlavor)
+{
+  for (size_t i = 0; i < aArray.Length(); ++i) {
+    if (aArray[i].GetFlavor().Equals(aDataFlavor)) {
+      return i;
+    }
+  }
+
+  return aArray.NoIndex;
+}
+
 DataStruct::DataStruct(DataStruct&& aRHS)
   : mData(aRHS.mData.forget())
   , mDataLen(aRHS.mDataLen)
   , mCacheFD(aRHS.mCacheFD)
   , mFlavor(aRHS.mFlavor)
 {
   aRHS.mCacheFD = nullptr;
 }
@@ -218,31 +230,16 @@ nsTransferable::GetTransferDataFlavors(n
   MOZ_ASSERT(mInitialized);
 
   for (size_t i = 0; i < mDataArray.Length(); ++i) {
     DataStruct& data = mDataArray.ElementAt(i);
     aFlavors.AppendElement(data.GetFlavor());
   }
 }
 
-Maybe<size_t>
-nsTransferable::FindDataFlavor(const char* aFlavor)
-{
-  nsDependentCString flavor(aFlavor);
-
-  for (size_t i = 0; i < mDataArray.Length(); ++i) {
-    if (mDataArray[i].GetFlavor().Equals(flavor)) {
-      return Some(i);
-    }
-  }
-
-  return Nothing();
-}
-
-
 //
 // GetTransferData
 //
 // Returns the data of the requested flavor, obtained from either having the
 // data on hand or using a converter to get it. The data is wrapped in a
 // nsISupports primitive so that it is accessible from JS.
 //
 NS_IMETHODIMP
@@ -252,43 +249,47 @@ nsTransferable::GetTransferData(const ch
 {
   MOZ_ASSERT(mInitialized);
 
   *aData = nullptr;
   *aDataLen = 0;
 
   nsresult rv = NS_OK;
 
-  // First look and see if the data is present in one of the intrinsic flavors.
-  if (Maybe<size_t> index = FindDataFlavor(aFlavor)) {
-    nsCOMPtr<nsISupports> dataBytes;
-    uint32_t len;
-    mDataArray[index.value()].GetData(getter_AddRefs(dataBytes), &len);
+  // first look and see if the data is present in one of the intrinsic flavors
+  for (size_t i = 0; i < mDataArray.Length(); ++i) {
+    DataStruct& data = mDataArray.ElementAt(i);
+    if (data.GetFlavor().Equals(aFlavor)) {
+      nsCOMPtr<nsISupports> dataBytes;
+      uint32_t len;
+      data.GetData(getter_AddRefs(dataBytes), &len);
 
-    // Do we have a (lazy) data provider?
-    if (nsCOMPtr<nsIFlavorDataProvider> dataProvider =
-          do_QueryInterface(dataBytes)) {
-      rv = dataProvider->GetFlavorData(this, aFlavor,
-                                       getter_AddRefs(dataBytes), &len);
-      if (NS_FAILED(rv)) {
-        dataBytes = nullptr;
-        // The provider failed, fall into the converter code below.
+      // Do we have a (lazy) data provider?
+      if (nsCOMPtr<nsIFlavorDataProvider> dataProvider =
+            do_QueryInterface(dataBytes)) {
+        rv = dataProvider->GetFlavorData(this, aFlavor,
+                                         getter_AddRefs(dataBytes), &len);
+        if (NS_FAILED(rv)) {
+          // The provider failed, fall into the converter code below.
+          break;
+        }
       }
-    }
 
-    if (dataBytes) {
-      *aDataLen = len;
-      dataBytes.forget(aData);
-      return NS_OK;
+      if (dataBytes) {
+        *aDataLen = len;
+        dataBytes.forget(aData);
+        return NS_OK;
+      }
+
+      // Not found.
+      break;
     }
-
-    // Empty data
   }
 
-  // If not, try using a format converter to get the requested flavor.
+  // if not, try using a format converter to get the requested flavor
   if (mFormatConv) {
     for (size_t i = 0; i < mDataArray.Length(); ++i) {
       DataStruct& data = mDataArray.ElementAt(i);
       bool canConvert = false;
       mFormatConv->CanConvert(data.GetFlavor().get(), aFlavor, &canConvert);
       if (canConvert) {
         nsCOMPtr<nsISupports> dataBytes;
         uint32_t len;
@@ -347,20 +348,22 @@ nsTransferable::GetAnyTransferData(nsACS
 NS_IMETHODIMP
 nsTransferable::SetTransferData(const char* aFlavor,
                                 nsISupports* aData,
                                 uint32_t aDataLen)
 {
   MOZ_ASSERT(mInitialized);
 
   // first check our intrinsic flavors to see if one has been registered.
-  if (Maybe<size_t> index = FindDataFlavor(aFlavor)) {
-    DataStruct& data = mDataArray.ElementAt(index.value());
-    data.SetData(aData, aDataLen, mPrivateData);
-    return NS_OK;
+  for (size_t i = 0; i < mDataArray.Length(); ++i) {
+    DataStruct& data = mDataArray.ElementAt(i);
+    if (data.GetFlavor().Equals(aFlavor)) {
+      data.SetData(aData, aDataLen, mPrivateData);
+      return NS_OK;
+    }
   }
 
   // if not, try using a format converter to find a flavor to put the data in
   if (mFormatConv) {
     for (size_t i = 0; i < mDataArray.Length(); ++i) {
       DataStruct& data = mDataArray.ElementAt(i);
       bool canConvert = false;
       mFormatConv->CanConvert(aFlavor, data.GetFlavor().get(), &canConvert);
@@ -394,41 +397,42 @@ nsTransferable::SetTransferData(const ch
 //
 // Adds a data flavor to our list with no data. Error if it already exists.
 //
 NS_IMETHODIMP
 nsTransferable::AddDataFlavor(const char* aDataFlavor)
 {
   MOZ_ASSERT(mInitialized);
 
-  if (FindDataFlavor(aDataFlavor).isSome()) {
+  if (GetDataForFlavor(mDataArray, aDataFlavor) != mDataArray.NoIndex) {
     return NS_ERROR_FAILURE;
   }
 
   // Create a new "slot" for the data
   mDataArray.AppendElement(DataStruct(aDataFlavor));
+
   return NS_OK;
 }
 
 //
 // RemoveDataFlavor
 //
 // Removes a data flavor (and causes the data to be destroyed). Error if
 // the requested flavor is not present.
 //
 NS_IMETHODIMP
 nsTransferable::RemoveDataFlavor(const char* aDataFlavor)
 {
   MOZ_ASSERT(mInitialized);
 
-  if (Maybe<size_t> index = FindDataFlavor(aDataFlavor)) {
-    mDataArray.RemoveElementAt(index.value());
+  size_t idx = GetDataForFlavor(mDataArray, aDataFlavor);
+  if (idx != mDataArray.NoIndex) {
+    mDataArray.RemoveElementAt(idx);
     return NS_OK;
   }
-
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 nsTransferable::SetConverter(nsIFormatConverter* aConverter)
 {
   MOZ_ASSERT(mInitialized);
 
--- a/widget/nsTransferable.h
+++ b/widget/nsTransferable.h
@@ -8,17 +8,16 @@
 
 #include "nsIFormatConverter.h"
 #include "nsITransferable.h"
 #include "nsCOMPtr.h"
 #include "nsString.h"
 #include "nsTArray.h"
 #include "nsIPrincipal.h"
 #include "prio.h"
-#include "mozilla/Maybe.h"
 
 class nsIMutableArray;
 
 //
 // DataStruct
 //
 // Holds a flavor (a mime type) that describes the data and the associated data.
 //
@@ -69,22 +68,19 @@ public:
 
     // nsISupports
   NS_DECL_ISUPPORTS
   NS_DECL_NSITRANSFERABLE
 
 protected:
   virtual ~nsTransferable();
 
-  // Get flavors w/out converter
+  // get flavors w/out converter
   void GetTransferDataFlavors(nsTArray<nsCString>& aFlavors);
-
-  // Find index for data with the matching flavor in mDataArray.
-  Maybe<size_t> FindDataFlavor(const char* aFlavor);
-
+ 
   nsTArray<DataStruct> mDataArray;
   nsCOMPtr<nsIFormatConverter> mFormatConv;
   bool mPrivateData;
   nsCOMPtr<nsIPrincipal> mRequestingPrincipal;
   nsContentPolicyType mContentPolicyType;
 #if DEBUG
   bool mInitialized;
 #endif