Bug 1121298 - Part 3. add getAsURI. r=smaug, dimi
authorYoshi Huang <allstars.chh@mozilla.com>
Wed, 21 Jan 2015 17:37:45 +0800
changeset 232806 e45e1dc34fb0031ec9db12be1889d1cd0420aab6
parent 232805 84773d851be6e7b0dac73f0cd4f4d1e5e80df6dd
child 232807 efbd4db9291f42b6799e60d0f23113212a363e1d
push id28393
push userkwierso@gmail.com
push dateTue, 10 Mar 2015 22:45:23 +0000
treeherdermozilla-central@a9aff724afc7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, dimi
bugs1121298
milestone39.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 1121298 - Part 3. add getAsURI. r=smaug, dimi From 1b93a62187c24dd176d48021b2a765a19beb453a Mon Sep 17 00:00:00 2001 --- dom/nfc/MozNDEFRecord.cpp | 29 +++++++++++++++++++++++++++++ dom/nfc/MozNDEFRecord.h | 1 + dom/webidl/MozNDEFRecord.webidl | 6 ++++++ 3 files changed, 36 insertions(+)
dom/nfc/MozNDEFRecord.cpp
dom/nfc/MozNDEFRecord.h
dom/webidl/MozNDEFRecord.webidl
--- a/dom/nfc/MozNDEFRecord.cpp
+++ b/dom/nfc/MozNDEFRecord.cpp
@@ -148,16 +148,45 @@ MozNDEFRecord::MozNDEFRecord(nsPIDOMWind
   : mWindow(aWindow) // For GetParentObject()
   , mTnf(aTnf)
   , mSize(3) // 1(flags) + 1(type_length) + 1(payload_length)
 {
   HoldData();
 }
 
 void
+MozNDEFRecord::GetAsURI(nsAString& aRetVal)
+{
+  aRetVal.SetIsVoid(true);
+  if (mTnf != TNF::Well_known) {
+    return;
+  }
+
+  JS::AutoCheckCannotGC nogc;
+  uint8_t* typeData = JS_GetUint8ArrayData(mType, nogc);
+  const char* uVal = RTDValues::strings[static_cast<uint32_t>(RTD::U)].value;
+  if (typeData[0] != uVal[0]) {
+    return;
+  }
+
+  uint32_t payloadLen;
+  uint8_t* payloadData;
+  js::GetUint8ArrayLengthAndData(mPayload, &payloadLen, &payloadData);
+  uint8_t id = payloadData[0];
+  if (id >= static_cast<uint8_t>(WellKnownURIPrefix::EndGuard_)) {
+    return;
+  }
+
+  using namespace mozilla::dom::WellKnownURIPrefixValues;
+  aRetVal.AssignASCII(strings[id].value);
+  aRetVal.Append(NS_ConvertUTF8toUTF16(
+    nsDependentCSubstring(reinterpret_cast<char*>(&payloadData[1]), payloadLen - 1)));
+}
+
+void
 MozNDEFRecord::InitType(JSContext* aCx, const Optional<Uint8Array>& aType)
 {
   if (!aType.WasPassed()) {
     return;
   }
 
   const Uint8Array& type = aType.Value();
   type.ComputeLengthAndData();
--- a/dom/nfc/MozNDEFRecord.h
+++ b/dom/nfc/MozNDEFRecord.h
@@ -86,16 +86,17 @@ public:
     aRetVal.set(mPayload);
   }
 
   uint32_t Size() const
   {
     return mSize;
   }
 
+  void GetAsURI(nsAString& aRetVal);
 private:
   MozNDEFRecord() = delete;
   nsRefPtr<nsPIDOMWindow> mWindow;
   void HoldData();
   void DropData();
   void InitType(JSContext* aCx, const Optional<Uint8Array>& aType);
   void InitType(JSContext* aCx, const RTD rtd);
   void InitId(JSContext* aCx, const Optional<Uint8Array>& aId);
--- a/dom/webidl/MozNDEFRecord.webidl
+++ b/dom/webidl/MozNDEFRecord.webidl
@@ -103,16 +103,22 @@ interface MozNDEFRecord
   [Constant]
   readonly attribute Uint8Array? payload;
 
   /**
    * Get the size of this NDEF Record.
    */
   [Constant]
   readonly attribute unsigned long size;
+
+  /**
+   * Returns this NDEF Record as URI, return null if this record cannot be
+   * decoded as a well-known URI record.
+   */
+  DOMString? getAsURI();
 };
 
 dictionary MozNDEFRecordOptions {
   TNF tnf = "empty";
   Uint8Array type;
   Uint8Array id;
   Uint8Array payload;
 };