[webnfc] Make NFCScanOptions#id filter record ids rather than the author record url
The plan is:
1. Introduces NDEFRecord{Init}#id and supports reading.
Already done by crrev.com/c/1861574.
2. Supports writing.
crrev.com/c/1928891 is focused on impl in Blink, with the following
CLs focused on impl in Device Service.
crrev.com/c/1910906
crrev.com/c/1911385
crrev.com/c/1915940
crrev.com/c/1916261
3. Introduces NFCScanOptions#id and filters records by it when scanning.
This CL.
4. Removes NDEFMessage{Init}#url and old impl of the author record.
The spec changes:
https://github.com/w3c/web-nfc/pull/338
https://github.com/w3c/web-nfc/pull/340
https://github.com/w3c/web-nfc/pull/446
BUG=520391
Change-Id: I8d7165bc2833501ce96187ada9f8c3b5053b3bad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1936187
Reviewed-by: Rijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Leon Han <leon.han@intel.com>
Cr-Commit-Position: refs/heads/master@{#722901}
diff --git a/resources/chromium/nfc-mock.js b/resources/chromium/nfc-mock.js
index 224a607..4778ce2 100644
--- a/resources/chromium/nfc-mock.js
+++ b/resources/chromium/nfc-mock.js
@@ -112,49 +112,29 @@
// Checks whether NDEFReaderOptions are matched with given message.
function matchesWatchOptions(message, options) {
- // Filter by Web NFC id.
- if (!matchesWebNfcId(message.url, options.id)) return false;
-
- // Matches any record / media type.
- if ((options.mediaType == null || options.mediaType === '') &&
- options.recordType == null) {
+ // A message with no records is to notify that the tag is already formatted to
+ // support NDEF but does not contain a message yet. We always dispatch it for
+ // all options.
+ if (message.records.length == 0)
return true;
- }
- // Filter by mediaType and recordType.
for (let record of message.records) {
- if (options.mediaType != null && options.mediaType !== ""
- && options.mediaType !== record.mediaType) {
- return false;
+ if (options.id != null && options.id !== record.id) {
+ continue;
}
if (options.recordType != null &&
options.recordType !== record.recordType) {
- return false;
+ continue;
}
+ if (options.mediaType !== '' && options.mediaType !== record.mediaType) {
+ continue;
+ }
+
+ // Found one record matches, means the message matches.
+ return true;
}
- return true;
-}
-
-// Web NFC id match algorithm.
-// https://w3c.github.io/web-nfc/#url-pattern-match-algorithm
-function matchesWebNfcId(id, pattern) {
- if (id != null && id !== "" && pattern != null && pattern !== "") {
- const id_url = new URL(id);
- const pattern_url = new URL(pattern);
-
- if (id_url.protocol !== pattern_url.protocol) return false;
- if (!id_url.host.endsWith("." + pattern_url.host)
- && id_url.host !== pattern_url.host) {
- return false;
- }
- if (pattern_url.pathname === "/*") return true;
- if (id_url.pathname.startsWith(pattern_url.pathname)) return true;
-
- return false;
- }
-
- return true;
+ return false;
}
function createNDEFError(type) {
diff --git a/web-nfc/NDEFReader_scan.https.html b/web-nfc/NDEFReader_scan.https.html
index 78160fb..e9a34df 100644
--- a/web-nfc/NDEFReader_scan.https.html
+++ b/web-nfc/NDEFReader_scan.https.html
@@ -36,20 +36,6 @@
}, "Test that NDEFReader.scan rejects if signal is not an AbortSignal.");
promise_test(async t => {
- await waitSyntaxErrorPromise(t, {id: "www.a.com"});
-}, "Test that NDEFReader.scan rejects if NDEFScanOptions.id is missing \
-components.");
-
-promise_test(async t => {
- await waitSyntaxErrorPromise(t, {id: "invalid"});
-}, "Test that NDEFReader.scan rejects if NDEFScanOptions.id is invalid.");
-
-promise_test(async t => {
- await waitSyntaxErrorPromise(t, {id: "http://a.com"});
-}, "Test that NDEFReader.scan rejects if NDEFScanOptions.id has wrong \
-protocol.");
-
-promise_test(async t => {
if (window.testRunner) {
// Deny nfc permissions for Chromium testrunner.
window.testRunner.setPermission('nfc', 'denied',
@@ -93,56 +79,11 @@
assert_true(event instanceof NDEFReadingEvent);
controller.abort();
});
- await reader.scan({signal : controller.signal, url: "https://a.com"});
+ await reader.scan({signal : controller.signal});
mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
await promise;
-}, "Test that NDEFReader.scan succeeds if NDEFScanOptions.url is valid URL.");
-
-nfc_test(async (t, mockNFC) => {
- const reader = new NDEFReader();
- const controller = new AbortController();
- const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
- const promise = readerWatcher.wait_for("reading").then(event => {
- assert_true(event instanceof NDEFReadingEvent);
- controller.abort();
- });
- await reader.scan({signal : controller.signal, url: "https://a.com/*"});
-
- mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
- await promise;
-}, "Test that NDEFReader.scan succeeds if NDEFScanOptions.url is valid URL \
-with '*' wildcard character in path.");
-
-nfc_test(async (t, mockNFC) => {
- const reader = new NDEFReader();
- const controller = new AbortController();
- const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
- const promise = readerWatcher.wait_for("reading").then(event => {
- assert_true(event instanceof NDEFReadingEvent);
- controller.abort();
- });
- await reader.scan({signal : controller.signal, url: "https://a.com/*/bar"});
-
- mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
- await promise;
-}, "Test that NDEFReader.scan succeeds if NDEFScanOptions.url is valid URL \
-with '*' wildcard character in the beginning of path component followed by \
-subpath.");
-
-nfc_test(async (t, mockNFC) => {
- const reader = new NDEFReader();
- const controller = new AbortController();
- const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
- const promise = readerWatcher.wait_for("reading").then(event => {
- assert_true(event instanceof NDEFReadingEvent);
- controller.abort();
- });
- await reader.scan({signal : controller.signal, url: ""});
-
- mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
- await promise;
-}, "Test that NDEFReader.scan succeeds if NDEFScanOptions.url is empty.");
+}, "Test that NDEFReader.scan matches any ids if NDEFScanOptions.id is undefined.");
nfc_test(async (t, mockNFC) => {
const reader = new NDEFReader();
diff --git a/web-nfc/NDEFReader_scan_filter.https.html b/web-nfc/NDEFReader_scan_filter.https.html
index 4c1e7de..850d9b9 100644
--- a/web-nfc/NDEFReader_scan_filter.https.html
+++ b/web-nfc/NDEFReader_scan_filter.https.html
@@ -62,12 +62,11 @@
message: createMessage([createRecord('w3.org:xyz', test_buffer_data)])
},
{
- desc: "Test that the url of NDEFScanOptions filters relevant data" +
+ desc: "Test that the id of NDEFScanOptions filters relevant data" +
" sources correctly.",
- scanOptions: {id: `${location.origin}/custom/path`},
- unmatchedScanOptions: {id: `${location.origin}/custom/invalid`},
- message: {url: `${location.origin}/custom/path/update`,
- records: [createTextRecord(test_text_data)]}
+ scanOptions: {id: test_record_id},
+ unmatchedScanOptions: {id: 'non_sense_id'},
+ message: {records: [createTextRecord(test_text_data)]}
},
{
desc: "Test that the mediaType of NDEFScanOptions filters relevant data" +
@@ -131,12 +130,10 @@
},
{
desc: "Test that filtering 'text' record from different messages" +
- " correctly with NDEFScanOptions' url set.",
- scanOptions: {id: `${location.origin}/custom/path`},
- message: {url: `${location.origin}/custom/path/update`,
- records: [createTextRecord(test_text_data)]},
- unmatchedMessage: {url: `${location.origin}/custom/invalid`,
- records: [createUrlRecord(test_url_data)]}
+ " correctly with NDEFScanOptions' id set.",
+ scanOptions: {id: test_record_id},
+ message: {records: [createTextRecord(test_text_data)]},
+ unmatchedMessage: {records: [createRecord('url', test_url_data, 'random_record_id')]}
},
{
desc: "Test that filtering 'mime' record from different messages" +