Commit 8d478224 authored by Leon Han's avatar Leon Han Committed by Commit Bot

[webnfc] Add support for reading NDEFRecord#id

This is the 1st CL to support access to the id field of ndef records.

The plan is:
1. Introduces NDEFRecord{Init}#id and supports reading.

2. Supports writing.

3. Introduces NFCScanOptions#id and filters records by it when scanning.

4. Removes NDEFMessage{Init}#url and old impl of the author record.

BUG=520391

Change-Id: Id80fc22d8c0535e60edc3f1e16a2a1135c623458
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1861574
Commit-Queue: Leon Han <leon.han@intel.com>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708107}
parent bed28798
......@@ -139,23 +139,31 @@ public final class NdefMessageUtils {
*/
private static NdefRecord toNdefRecord(android.nfc.NdefRecord ndefRecord)
throws UnsupportedEncodingException {
NdefRecord record = null;
switch (ndefRecord.getTnf()) {
case android.nfc.NdefRecord.TNF_EMPTY:
return createEmptyRecord();
record = createEmptyRecord();
break;
case android.nfc.NdefRecord.TNF_MIME_MEDIA:
return createMIMERecord(
record = createMIMERecord(
new String(ndefRecord.getType(), "UTF-8"), ndefRecord.getPayload());
break;
case android.nfc.NdefRecord.TNF_ABSOLUTE_URI:
return createURLRecord(ndefRecord.toUri());
record = createURLRecord(ndefRecord.toUri());
break;
case android.nfc.NdefRecord.TNF_WELL_KNOWN:
return createWellKnownRecord(ndefRecord);
record = createWellKnownRecord(ndefRecord);
break;
case android.nfc.NdefRecord.TNF_UNKNOWN:
return createUnKnownRecord(ndefRecord.getPayload());
record = createUnKnownRecord(ndefRecord.getPayload());
break;
case android.nfc.NdefRecord.TNF_EXTERNAL_TYPE:
return createExternalTypeRecord(
record = createExternalTypeRecord(
new String(ndefRecord.getType(), "UTF-8"), ndefRecord.getPayload());
break;
}
return null;
record.id = new String(ndefRecord.getId(), "UTF-8");
return record;
}
/**
......
......@@ -89,6 +89,7 @@ public class NFCTest {
// Constants used for the test.
private static final String DUMMY_EXTERNAL_RECORD_DOMAIN = "abc.com";
private static final String DUMMY_EXTERNAL_RECORD_TYPE = "xyz";
private static final String DUMMY_RECORD_ID = "https://www.example.com/ids/1";
private static final String TEST_TEXT = "test";
private static final String TEST_URL = "https://google.com";
private static final String TEST_JSON = "{\"key1\":\"value1\",\"key2\":2}";
......@@ -221,6 +222,7 @@ public class NFCTest {
assertEquals(1, emptyMojoNdefMessage.data.length);
assertEquals(NdefMessageUtils.RECORD_TYPE_EMPTY, emptyMojoNdefMessage.data[0].recordType);
assertEquals(true, emptyMojoNdefMessage.data[0].mediaType.isEmpty());
assertEquals(true, emptyMojoNdefMessage.data[0].id.isEmpty());
assertEquals(0, emptyMojoNdefMessage.data[0].data.length);
// Test URL record conversion.
......@@ -231,6 +233,7 @@ public class NFCTest {
assertEquals(1, urlMojoNdefMessage.data.length);
assertEquals(NdefMessageUtils.RECORD_TYPE_URL, urlMojoNdefMessage.data[0].recordType);
assertEquals(TEXT_MIME, urlMojoNdefMessage.data[0].mediaType);
assertEquals(true, urlMojoNdefMessage.data[0].id.isEmpty());
assertEquals(TEST_URL, new String(urlMojoNdefMessage.data[0].data));
// Test TEXT record conversion.
......@@ -241,6 +244,7 @@ public class NFCTest {
assertEquals(1, textMojoNdefMessage.data.length);
assertEquals(NdefMessageUtils.RECORD_TYPE_TEXT, textMojoNdefMessage.data[0].recordType);
assertEquals(TEXT_MIME, textMojoNdefMessage.data[0].mediaType);
assertEquals(true, textMojoNdefMessage.data[0].id.isEmpty());
assertEquals(TEST_TEXT, new String(textMojoNdefMessage.data[0].data));
// Test MIME record conversion.
......@@ -251,7 +255,8 @@ public class NFCTest {
assertNull(mimeMojoNdefMessage.url);
assertEquals(1, mimeMojoNdefMessage.data.length);
assertEquals(NdefMessageUtils.RECORD_TYPE_OPAQUE, mimeMojoNdefMessage.data[0].recordType);
assertEquals(TEXT_MIME, textMojoNdefMessage.data[0].mediaType);
assertEquals(TEXT_MIME, mimeMojoNdefMessage.data[0].mediaType);
assertEquals(true, mimeMojoNdefMessage.data[0].id.isEmpty());
assertEquals(TEST_TEXT, new String(textMojoNdefMessage.data[0].data));
// Test JSON record conversion.
......@@ -263,11 +268,13 @@ public class NFCTest {
assertEquals(1, jsonMojoNdefMessage.data.length);
assertEquals(NdefMessageUtils.RECORD_TYPE_JSON, jsonMojoNdefMessage.data[0].recordType);
assertEquals(JSON_MIME, jsonMojoNdefMessage.data[0].mediaType);
assertEquals(true, jsonMojoNdefMessage.data[0].id.isEmpty());
assertEquals(TEST_JSON, new String(jsonMojoNdefMessage.data[0].data));
// Test Unknown record conversion.
android.nfc.NdefMessage unknownNdefMessage = new android.nfc.NdefMessage(
new android.nfc.NdefRecord(android.nfc.NdefRecord.TNF_UNKNOWN, null, null,
new android.nfc.NdefRecord(android.nfc.NdefRecord.TNF_UNKNOWN, null,
ApiCompatibilityUtils.getBytesUtf8(DUMMY_RECORD_ID),
ApiCompatibilityUtils.getBytesUtf8(TEST_TEXT)));
NdefMessage unknownMojoNdefMessage = NdefMessageUtils.toNdefMessage(unknownNdefMessage);
assertNull(unknownMojoNdefMessage.url);
......@@ -275,6 +282,7 @@ public class NFCTest {
assertEquals(
NdefMessageUtils.RECORD_TYPE_OPAQUE, unknownMojoNdefMessage.data[0].recordType);
assertEquals(OCTET_STREAM_MIME, unknownMojoNdefMessage.data[0].mediaType);
assertEquals(DUMMY_RECORD_ID, unknownMojoNdefMessage.data[0].id);
assertEquals(TEST_TEXT, new String(unknownMojoNdefMessage.data[0].data));
// Test external record conversion.
......@@ -287,6 +295,7 @@ public class NFCTest {
assertEquals(DUMMY_EXTERNAL_RECORD_DOMAIN + ':' + DUMMY_EXTERNAL_RECORD_TYPE,
extMojoNdefMessage.data[0].recordType);
assertEquals(OCTET_STREAM_MIME, extMojoNdefMessage.data[0].mediaType);
assertEquals(true, extMojoNdefMessage.data[0].id.isEmpty());
assertEquals(TEST_TEXT, new String(extMojoNdefMessage.data[0].data));
// Test NdefMessage with an additional WebNFC author record.
......
......@@ -34,6 +34,7 @@ struct NDEFError {
NDEFErrorType error_type;
};
// https://w3c.github.io/web-nfc/#dom-ndefrecord
struct NDEFRecord {
// The type of NDEFRecord.
string? record_type;
......@@ -41,6 +42,9 @@ struct NDEFRecord {
// Represents the IANA media type of the NDEFRecord data field.
string? media_type;
// The id of NDEFRecord. https://w3c.github.io/web-nfc/#dom-ndefrecord-id
string? id;
// Payload of the NDEFRecord.
array<uint8> data;
};
......
......@@ -273,6 +273,7 @@ NDEFRecord::NDEFRecord(DOMArrayBuffer* array_buffer)
NDEFRecord::NDEFRecord(const device::mojom::blink::NDEFRecord& record)
: record_type_(record.record_type),
media_type_(record.media_type),
id_(record.id),
data_(record.data) {}
const String& NDEFRecord::recordType() const {
......@@ -283,6 +284,10 @@ const String& NDEFRecord::mediaType() const {
return media_type_;
}
const String& NDEFRecord::id() const {
return id_;
}
String NDEFRecord::text() const {
if (record_type_ == "empty")
return String();
......
......@@ -37,6 +37,7 @@ class MODULES_EXPORT NDEFRecord final : public ScriptWrappable {
const String& recordType() const;
const String& mediaType() const;
const String& id() const;
String text() const;
DOMArrayBuffer* arrayBuffer() const;
ScriptValue json(ScriptState*, ExceptionState&) const;
......@@ -48,6 +49,7 @@ class MODULES_EXPORT NDEFRecord final : public ScriptWrappable {
private:
String record_type_;
String media_type_;
String id_;
// Holds the NDEFRecord.[[PayloadData]] bytes defined at
// https://w3c.github.io/web-nfc/#the-ndefrecord-interface.
WTF::Vector<uint8_t> data_;
......
......@@ -13,6 +13,7 @@
] interface NDEFRecord {
readonly attribute NDEFRecordType recordType;
readonly attribute USVString mediaType;
readonly attribute USVString id;
USVString? text();
[NewObject] ArrayBuffer? arrayBuffer();
[CallWith=ScriptState, RaisesException] object? json();
......
......@@ -6,12 +6,10 @@
typedef DOMString NDEFRecordType;
typedef any NDEFRecordData;
// https://w3c.github.io/web-nfc/#dom-ndefrecordinit
dictionary NDEFRecordInit {
NDEFRecordType recordType;
USVString mediaType;
NDEFRecordData data;
any data;
};
......@@ -31,7 +31,7 @@ namespace mojo {
NDEFRecordPtr TypeConverter<NDEFRecordPtr, ::blink::NDEFRecord*>::Convert(
const ::blink::NDEFRecord* record) {
return NDEFRecord::New(record->recordType(), record->mediaType(),
record->data());
record->id(), record->data());
}
NDEFMessagePtr TypeConverter<NDEFMessagePtr, ::blink::NDEFMessage*>::Convert(
......
This is a testharness.js-based test.
Found 88 tests; 78 PASS, 10 FAIL, 0 TIMEOUT, 0 NOTRUN.
Found 88 tests; 80 PASS, 8 FAIL, 0 TIMEOUT, 0 NOTRUN.
PASS idl_test setup
PASS idl_test validation
PASS NDEFMessage interface: existence and properties of interface object
......@@ -20,7 +20,7 @@ PASS NDEFRecord interface: existence and properties of interface prototype objec
PASS NDEFRecord interface: existence and properties of interface prototype object's @@unscopables property
PASS NDEFRecord interface: attribute recordType
PASS NDEFRecord interface: attribute mediaType
FAIL NDEFRecord interface: attribute id assert_true: The prototype object must have a property "id" expected true got false
PASS NDEFRecord interface: attribute id
FAIL NDEFRecord interface: attribute data assert_true: The prototype object must have a property "data" expected true got false
FAIL NDEFRecord interface: attribute encoding assert_true: The prototype object must have a property "encoding" expected true got false
FAIL NDEFRecord interface: attribute lang assert_true: The prototype object must have a property "lang" expected true got false
......@@ -32,7 +32,7 @@ PASS NDEFRecord must be primary interface of new NDEFRecord({"recordType":"text"
PASS Stringification of new NDEFRecord({"recordType":"text","mediaType":"text/plain","data":"Hello World","id":"/custom/path"});
PASS NDEFRecord interface: new NDEFRecord({"recordType":"text","mediaType":"text/plain","data":"Hello World","id":"/custom/path"}); must inherit property "recordType" with the proper type
PASS NDEFRecord interface: new NDEFRecord({"recordType":"text","mediaType":"text/plain","data":"Hello World","id":"/custom/path"}); must inherit property "mediaType" with the proper type
FAIL NDEFRecord interface: new NDEFRecord({"recordType":"text","mediaType":"text/plain","data":"Hello World","id":"/custom/path"}); must inherit property "id" with the proper type assert_inherits: property "id" not found in prototype chain
PASS NDEFRecord interface: new NDEFRecord({"recordType":"text","mediaType":"text/plain","data":"Hello World","id":"/custom/path"}); must inherit property "id" with the proper type
FAIL NDEFRecord interface: new NDEFRecord({"recordType":"text","mediaType":"text/plain","data":"Hello World","id":"/custom/path"}); must inherit property "data" with the proper type assert_inherits: property "data" not found in prototype chain
FAIL NDEFRecord interface: new NDEFRecord({"recordType":"text","mediaType":"text/plain","data":"Hello World","id":"/custom/path"}); must inherit property "encoding" with the proper type assert_inherits: property "encoding" not found in prototype chain
FAIL NDEFRecord interface: new NDEFRecord({"recordType":"text","mediaType":"text/plain","data":"Hello World","id":"/custom/path"}); must inherit property "lang" with the proper type assert_inherits: property "lang" not found in prototype chain
......
......@@ -5135,6 +5135,7 @@ interface NDEFReadingEvent : Event
method constructor
interface NDEFRecord
attribute @@toStringTag
getter id
getter mediaType
getter recordType
method arrayBuffer
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment