Bug 1200124 - Pass order and search key with uint32_t instead of nsString to avoid string mismatch error, r=shuang
authorBen Tian <btian@mozilla.com>
Mon, 07 Sep 2015 10:53:40 +0800
changeset 293976 582d68dfb107d0a66bd30b13c0b74a1faded7128
parent 293975 fa0de2cded57cc344972e434a3a479986fa64986
child 293977 4d520d9c6b850e3476cbff22319cae050a5e2fd6
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersshuang
bugs1200124
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1200124 - Pass order and search key with uint32_t instead of nsString to avoid string mismatch error, r=shuang
dom/bluetooth/bluedroid/BluetoothPbapManager.cpp
dom/bluetooth/common/webapi/BluetoothAdapter.cpp
dom/bluetooth/common/webapi/BluetoothAdapter.h
--- a/dom/bluetooth/bluedroid/BluetoothPbapManager.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothPbapManager.cpp
@@ -7,16 +7,17 @@
 #include "base/basictypes.h"
 #include "BluetoothPbapManager.h"
 
 #include "BluetoothService.h"
 #include "BluetoothSocket.h"
 #include "BluetoothUuid.h"
 #include "ObexBase.h"
 
+#include "mozilla/dom/BluetoothPbapParametersBinding.h"
 #include "mozilla/dom/File.h"
 #include "mozilla/dom/ipc/BlobParent.h"
 #include "mozilla/RefPtr.h"
 #include "mozilla/Services.h"
 #include "mozilla/StaticPtr.h"
 #include "nsAutoPtr.h"
 #include "nsIInputStream.h"
 #include "nsIObserver.h"
@@ -528,50 +529,40 @@ BluetoothPbapManager::AppendBtNamedValue
 {
   uint8_t buf[64];
   if (!aHeader.GetAppParameter(aTagId, buf, 64)) {
     return;
   }
 
   switch (aTagId) {
     case AppParameterTag::Order: {
-      static const nsString sOrderStr[] = {NS_LITERAL_STRING("alphabetical"),
-                                           NS_LITERAL_STRING("indexed"),
-                                           NS_LITERAL_STRING("phonetical")};
-      uint8_t order = buf[0];
-      if (order < MOZ_ARRAY_LENGTH(sOrderStr)) {
-        BT_APPEND_NAMED_VALUE(aValues, "order", sOrderStr[order]);
-      } else {
-        BT_LOGR("Unexpected value %d of 'Order'", order);
-      }
+      using namespace mozilla::dom::vCardOrderTypeValues;
+      uint32_t order = buf[0] < ArrayLength(strings) ? (uint32_t) buf[0]
+                                                     : 0; // default: indexed
+      BT_APPEND_NAMED_VALUE(aValues, "order", order);
+      break;
+    }
+    case AppParameterTag::SearchProperty: {
+      using namespace mozilla::dom::vCardSearchKeyTypeValues;
+      uint32_t searchKey = buf[0] < ArrayLength(strings) ? (uint32_t) buf[0]
+                                                         : 0; // default: name
+      BT_APPEND_NAMED_VALUE(aValues, "searchKey", searchKey);
       break;
     }
     case AppParameterTag::SearchValue: {
       // Section 5.3.4.3 "SearchValue {<text string>}", PBAP 1.2
       // The UTF-8 character set shall be used for <text string>.
 
       // Use nsCString to store UTF-8 string here to follow the suggestion of
       // 'MDN:Internal_strings'.
       nsCString text((char *) buf);
 
       BT_APPEND_NAMED_VALUE(aValues, "searchText", text);
       break;
     }
-    case AppParameterTag::SearchProperty: {
-      static const nsString sSearchKeyStr[] = {NS_LITERAL_STRING("name"),
-                                               NS_LITERAL_STRING("number"),
-                                               NS_LITERAL_STRING("sound")};
-      uint8_t searchKey = buf[0];
-      if (searchKey < MOZ_ARRAY_LENGTH(sSearchKeyStr)) {
-        BT_APPEND_NAMED_VALUE(aValues, "searchKey", sSearchKeyStr[searchKey]);
-      } else {
-        BT_LOGR("Unexpected value %d of 'SearchProperty'", searchKey);
-      }
-      break;
-    }
     case AppParameterTag::MaxListCount: {
       uint16_t maxListCount = *((uint16_t *)buf);
 
       // convert big endian to little endian
       maxListCount = (maxListCount >> 8) | (maxListCount << 8);
 
       // Section 5 "Phone Book Access Profile Functions", PBAP 1.2
       // Replying 'PhonebookSize' is mandatory if 'MaxListCount' parameter is
--- a/dom/bluetooth/common/webapi/BluetoothAdapter.cpp
+++ b/dom/bluetooth/common/webapi/BluetoothAdapter.cpp
@@ -1290,21 +1290,21 @@ BluetoothAdapter::HandlePullVCardListing
   BluetoothVCardListingEventInit init;
 
   for (uint32_t i = 0, propCount = arr.Length(); i < propCount; ++i) {
     const nsString& name = arr[i].name();
     const BluetoothValue& value = arr[i].value();
     if (name.EqualsLiteral("name")) {
       init.mName = value.get_nsString();
     } else if (name.EqualsLiteral("order")) {
-      init.mOrder = ConvertStringToVCardOrderType(value.get_nsString());
+      init.mOrder = static_cast<vCardOrderType>(value.get_uint32_t());
+    } else if (name.EqualsLiteral("searchKey")) {
+      init.mSearchKey = static_cast<vCardSearchKeyType>(value.get_uint32_t());
     } else if (name.EqualsLiteral("searchText")) {
       init.mSearchValue = value.get_nsString();
-    } else if (name.EqualsLiteral("searchKey")) {
-      init.mSearchKey = ConvertStringToVCardSearchKeyType(value.get_nsString());
     } else if (name.EqualsLiteral("maxListCount")) {
       init.mMaxListCount = value.get_uint32_t();
     } else if (name.EqualsLiteral("listStartOffset")) {
       init.mListStartOffset = value.get_uint32_t();
     } else if (name.EqualsLiteral("vCardSelector_AND")) {
       init.mVcardSelector = getVCardProperties(value);
       init.mVcardSelectorOperator = vCardSelectorOp::AND;
     } else if (name.EqualsLiteral("vCardSelector_OR")) {
@@ -1333,50 +1333,16 @@ BluetoothAdapter::getVCardProperties(con
   for (uint32_t i = 0; i < propSelectorArr.Length(); ++i) {
     propSelector.AppendElement(
       static_cast<vCardProperties>(propSelectorArr[i]), mozilla::fallible);
   }
 
   return propSelector;
 }
 
-vCardOrderType
-BluetoothAdapter::ConvertStringToVCardOrderType(const nsAString& aString)
-{
-  using namespace mozilla::dom::vCardOrderTypeValues;
-
-  for (size_t index = 0; index < ArrayLength(strings) - 1; index++) {
-    if (aString.LowerCaseEqualsASCII(strings[index].value,
-                                     strings[index].length)) {
-      return static_cast<vCardOrderType>(index);
-    }
-  }
-
-  BT_WARNING("Treat the unexpected string '%s' as vCardOrderType::Indexed",
-    NS_ConvertUTF16toUTF8(aString).get());
-  return vCardOrderType::Indexed; // The default value is 'Indexed'.
-}
-
-vCardSearchKeyType
-BluetoothAdapter::ConvertStringToVCardSearchKeyType(const nsAString& aString)
-{
-  using namespace mozilla::dom::vCardSearchKeyTypeValues;
-
-  for (size_t index = 0; index < ArrayLength(strings) - 1; index++) {
-    if (aString.LowerCaseEqualsASCII(strings[index].value,
-                                     strings[index].length)) {
-      return static_cast<vCardSearchKeyType>(index);
-    }
-  }
-
-  BT_WARNING("Treat the unexpected string '%s' as vCardSearchKeyType::Name",
-    NS_ConvertUTF16toUTF8(aString).get());
-  return vCardSearchKeyType::Name; // The default value is 'Name'.
-}
-
 void
 BluetoothAdapter::DispatchAttributeEvent(const Sequence<nsString>& aTypes)
 {
   MOZ_ASSERT(!aTypes.IsEmpty());
 
   BluetoothAttributeEventInit init;
   init.mAttrs = aTypes;
 
--- a/dom/bluetooth/common/webapi/BluetoothAdapter.h
+++ b/dom/bluetooth/common/webapi/BluetoothAdapter.h
@@ -338,31 +338,16 @@ private:
    *
    * @param aValue [in] a BluetoothValue with 'TArrayOfuint32_t' type
    *                    The name of BluetoothValue must be 'propSelector',
    *                    'vCardSelector_OR' or 'vCardSelector_AND'.
    */
   Sequence<vCardProperties> getVCardProperties(const BluetoothValue &aValue);
 
   /**
-   * Convert string to vCardOrderType.
-   *
-   * @param aString [in] String to convert
-   */
-  vCardOrderType ConvertStringToVCardOrderType(const nsAString& aString);
-
-  /**
-   * Convert string to vCardSearchKeyType.
-   *
-   * @param aString [in] String to convert
-   */
-  vCardSearchKeyType ConvertStringToVCardSearchKeyType(
-    const nsAString& aString);
-
-  /**
    * Fire BluetoothAttributeEvent to trigger onattributechanged event handler.
    *
    * @param aTypes [in] Array of changed attributes. Must be non-empty.
    */
   void DispatchAttributeEvent(const Sequence<nsString>& aTypes);
 
   /**
    * Fire BluetoothDeviceEvent to trigger