Commit 919b7cee authored by Leon Han's avatar Leon Han Committed by Commit Bot

[webnfc] Use AbortController for NFCReader

This CL uses AbortController to replace the stop() method of NFCReader
interface.

The corresponding spec change is from:
https://github.com/w3c/web-nfc/pull/300

BUG=520391

Change-Id: Ia28bfb283a7ad66fb9c4cb0fc744582671a62fa0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1769245Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Commit-Queue: Leon Han <leon.han@intel.com>
Cr-Commit-Position: refs/heads/master@{#691988}
parent f52f5199
...@@ -31,7 +31,7 @@ import org.chromium.device.mojom.NfcError; ...@@ -31,7 +31,7 @@ import org.chromium.device.mojom.NfcError;
import org.chromium.device.mojom.NfcErrorType; import org.chromium.device.mojom.NfcErrorType;
import org.chromium.device.mojom.NfcPushOptions; import org.chromium.device.mojom.NfcPushOptions;
import org.chromium.device.mojom.NfcPushTarget; import org.chromium.device.mojom.NfcPushTarget;
import org.chromium.device.mojom.NfcReaderOptions; import org.chromium.device.mojom.NfcScanOptions;
import org.chromium.mojo.bindings.Callbacks; import org.chromium.mojo.bindings.Callbacks;
import org.chromium.mojo.system.MojoException; import org.chromium.mojo.system.MojoException;
...@@ -103,12 +103,12 @@ public class NfcImpl implements Nfc { ...@@ -103,12 +103,12 @@ public class NfcImpl implements Nfc {
private int mWatcherId; private int mWatcherId;
/** /**
* Map of watchId <-> NfcReaderOptions. All NfcReaderOptions are matched against tag that is in * Map of watchId <-> NfcScanOptions. All NfcScanOptions are matched against tag that is in
* proximity, when match algorithm (@see #matchesWatchOptions) returns true, watcher with * proximity, when match algorithm (@see #matchesWatchOptions) returns true, watcher with
* corresponding ID would be notified using NfcClient interface. * corresponding ID would be notified using NfcClient interface.
* @see NfcClient#onWatch(int[] id, String serial_number, NdefMessage message) * @see NfcClient#onWatch(int[] id, String serial_number, NdefMessage message)
*/ */
private final SparseArray<NfcReaderOptions> mWatchers = new SparseArray<>(); private final SparseArray<NfcScanOptions> mWatchers = new SparseArray<>();
/** /**
* Handler that runs delayed push timeout task. * Handler that runs delayed push timeout task.
...@@ -163,8 +163,8 @@ public class NfcImpl implements Nfc { ...@@ -163,8 +163,8 @@ public class NfcImpl implements Nfc {
/** /**
* Sets NfcClient. NfcClient interface is used to notify mojo NFC service client when NFC * Sets NfcClient. NfcClient interface is used to notify mojo NFC service client when NFC
* device is in proximity and has NdefMessage that matches NfcReaderOptions criteria. * device is in proximity and has NdefMessage that matches NfcScanOptions criteria.
* @see Nfc#watch(NfcReaderOptions options, int id, WatchResponse callback) * @see Nfc#watch(NfcScanOptions options, int id, WatchResponse callback)
* *
* @param client @see NfcClient * @param client @see NfcClient
*/ */
...@@ -237,15 +237,15 @@ public class NfcImpl implements Nfc { ...@@ -237,15 +237,15 @@ public class NfcImpl implements Nfc {
/** /**
* Watch method allows to set filtering criteria for NdefMessages that are found when NFC device * Watch method allows to set filtering criteria for NdefMessages that are found when NFC device
* is within proximity. When NdefMessage that matches NfcReaderOptions is found, it is passed to * is within proximity. When NdefMessage that matches NfcScanOptions is found, it is passed to
* NfcClient interface together with corresponding watch ID. * NfcClient interface together with corresponding watch ID.
* @see NfcClient#onWatch(int[] id, String serial_number, NdefMessage message) * @see NfcClient#onWatch(int[] id, String serial_number, NdefMessage message)
* *
* @param options used to filter NdefMessages, @see NfcReaderOptions. * @param options used to filter NdefMessages, @see NfcScanOptions.
* @param callback that is used to notify caller when watch() is completed. * @param callback that is used to notify caller when watch() is completed.
*/ */
@Override @Override
public void watch(NfcReaderOptions options, int id, WatchResponse callback) { public void watch(NfcScanOptions options, int id, WatchResponse callback) {
if (!checkIfReady(callback)) return; if (!checkIfReady(callback)) return;
// We received a duplicate |id| here that should never happen, in such a case we should // We received a duplicate |id| here that should never happen, in such a case we should
// report a bad message to Mojo but unfortunately Mojo bindings for Java does not support // report a bad message to Mojo but unfortunately Mojo bindings for Java does not support
...@@ -577,7 +577,7 @@ public class NfcImpl implements Nfc { ...@@ -577,7 +577,7 @@ public class NfcImpl implements Nfc {
} }
/** /**
* Iterates through active watchers and if any of those match NfcReaderOptions criteria, * Iterates through active watchers and if any of those match NfcScanOptions criteria,
* delivers NdefMessage to the client. * delivers NdefMessage to the client.
*/ */
private void notifyMatchingWatchers(android.nfc.NdefMessage message, int compatibility) { private void notifyMatchingWatchers(android.nfc.NdefMessage message, int compatibility) {
...@@ -585,7 +585,7 @@ public class NfcImpl implements Nfc { ...@@ -585,7 +585,7 @@ public class NfcImpl implements Nfc {
NdefMessage ndefMessage = NdefMessageUtils.toNdefMessage(message); NdefMessage ndefMessage = NdefMessageUtils.toNdefMessage(message);
List<Integer> watchIds = new ArrayList<Integer>(); List<Integer> watchIds = new ArrayList<Integer>();
for (int i = 0; i < mWatchers.size(); i++) { for (int i = 0; i < mWatchers.size(); i++) {
NfcReaderOptions options = mWatchers.valueAt(i); NfcScanOptions options = mWatchers.valueAt(i);
if (matchesWatchOptions(ndefMessage, compatibility, options)) if (matchesWatchOptions(ndefMessage, compatibility, options))
watchIds.add(mWatchers.keyAt(i)); watchIds.add(mWatchers.keyAt(i));
} }
...@@ -606,7 +606,7 @@ public class NfcImpl implements Nfc { ...@@ -606,7 +606,7 @@ public class NfcImpl implements Nfc {
* Implements matching algorithm. * Implements matching algorithm.
*/ */
private boolean matchesWatchOptions( private boolean matchesWatchOptions(
NdefMessage message, int compatibility, NfcReaderOptions options) { NdefMessage message, int compatibility, NfcScanOptions options) {
// 'nfc-forum' option can only read messages from NFC standard devices and 'vendor' option // 'nfc-forum' option can only read messages from NFC standard devices and 'vendor' option
// can only read from vendor specific ones. // can only read from vendor specific ones.
if (options.compatibility != NdefCompatibility.ANY if (options.compatibility != NdefCompatibility.ANY
......
...@@ -58,7 +58,7 @@ import org.chromium.device.mojom.NfcError; ...@@ -58,7 +58,7 @@ import org.chromium.device.mojom.NfcError;
import org.chromium.device.mojom.NfcErrorType; import org.chromium.device.mojom.NfcErrorType;
import org.chromium.device.mojom.NfcPushOptions; import org.chromium.device.mojom.NfcPushOptions;
import org.chromium.device.mojom.NfcPushTarget; import org.chromium.device.mojom.NfcPushTarget;
import org.chromium.device.mojom.NfcReaderOptions; import org.chromium.device.mojom.NfcScanOptions;
import org.chromium.testing.local.LocalRobolectricTestRunner; import org.chromium.testing.local.LocalRobolectricTestRunner;
import java.io.IOException; import java.io.IOException;
...@@ -202,7 +202,7 @@ public class NFCTest { ...@@ -202,7 +202,7 @@ public class NFCTest {
TestNfcImpl nfc = new TestNfcImpl(mContext, mDelegate); TestNfcImpl nfc = new TestNfcImpl(mContext, mDelegate);
mDelegate.invokeCallback(); mDelegate.invokeCallback();
WatchResponse mockCallback = mock(WatchResponse.class); WatchResponse mockCallback = mock(WatchResponse.class);
nfc.watch(createNfcReaderOptions(), mNextWatchId, mockCallback); nfc.watch(createNfcScanOptions(), mNextWatchId, mockCallback);
verify(mockCallback).call(mErrorCaptor.capture()); verify(mockCallback).call(mErrorCaptor.capture());
assertNull(mErrorCaptor.getValue()); assertNull(mErrorCaptor.getValue());
} }
...@@ -388,7 +388,7 @@ public class NFCTest { ...@@ -388,7 +388,7 @@ public class NFCTest {
mDelegate.invokeCallback(); mDelegate.invokeCallback();
nfc.setClient(mNfcClient); nfc.setClient(mNfcClient);
WatchResponse mockCallback = mock(WatchResponse.class); WatchResponse mockCallback = mock(WatchResponse.class);
nfc.watch(createNfcReaderOptions(), mNextWatchId, mockCallback); nfc.watch(createNfcScanOptions(), mNextWatchId, mockCallback);
nfc.suspendNfcOperations(); nfc.suspendNfcOperations();
verify(mNfcAdapter, times(1)).disableReaderMode(mActivity); verify(mNfcAdapter, times(1)).disableReaderMode(mActivity);
nfc.resumeNfcOperations(); nfc.resumeNfcOperations();
...@@ -529,7 +529,7 @@ public class NFCTest { ...@@ -529,7 +529,7 @@ public class NFCTest {
nfc.setClient(mNfcClient); nfc.setClient(mNfcClient);
int watchId1 = mNextWatchId++; int watchId1 = mNextWatchId++;
WatchResponse mockWatchCallback1 = mock(WatchResponse.class); WatchResponse mockWatchCallback1 = mock(WatchResponse.class);
nfc.watch(createNfcReaderOptions(), watchId1, mockWatchCallback1); nfc.watch(createNfcScanOptions(), watchId1, mockWatchCallback1);
// Check that watch requests were completed successfully. // Check that watch requests were completed successfully.
verify(mockWatchCallback1).call(mErrorCaptor.capture()); verify(mockWatchCallback1).call(mErrorCaptor.capture());
...@@ -537,7 +537,7 @@ public class NFCTest { ...@@ -537,7 +537,7 @@ public class NFCTest {
int watchId2 = mNextWatchId++; int watchId2 = mNextWatchId++;
WatchResponse mockWatchCallback2 = mock(WatchResponse.class); WatchResponse mockWatchCallback2 = mock(WatchResponse.class);
nfc.watch(createNfcReaderOptions(), watchId2, mockWatchCallback2); nfc.watch(createNfcScanOptions(), watchId2, mockWatchCallback2);
verify(mockWatchCallback2).call(mErrorCaptor.capture()); verify(mockWatchCallback2).call(mErrorCaptor.capture());
assertNull(mErrorCaptor.getValue()); assertNull(mErrorCaptor.getValue());
...@@ -563,7 +563,7 @@ public class NFCTest { ...@@ -563,7 +563,7 @@ public class NFCTest {
nfc.setClient(mNfcClient); nfc.setClient(mNfcClient);
// Should match by WebNFC Id (exact match). // Should match by WebNFC Id (exact match).
NfcReaderOptions options1 = createNfcReaderOptions(); NfcScanOptions options1 = createNfcScanOptions();
options1.compatibility = NdefCompatibility.NFC_FORUM; options1.compatibility = NdefCompatibility.NFC_FORUM;
options1.url = TEST_URL; options1.url = TEST_URL;
int watchId1 = mNextWatchId++; int watchId1 = mNextWatchId++;
...@@ -573,7 +573,7 @@ public class NFCTest { ...@@ -573,7 +573,7 @@ public class NFCTest {
assertNull(mErrorCaptor.getValue()); assertNull(mErrorCaptor.getValue());
// Should match by media type. // Should match by media type.
NfcReaderOptions options2 = createNfcReaderOptions(); NfcScanOptions options2 = createNfcScanOptions();
options2.compatibility = NdefCompatibility.ANY; options2.compatibility = NdefCompatibility.ANY;
options2.mediaType = TEXT_MIME; options2.mediaType = TEXT_MIME;
int watchId2 = mNextWatchId++; int watchId2 = mNextWatchId++;
...@@ -583,7 +583,7 @@ public class NFCTest { ...@@ -583,7 +583,7 @@ public class NFCTest {
assertNull(mErrorCaptor.getValue()); assertNull(mErrorCaptor.getValue());
// Should match by record type. // Should match by record type.
NfcReaderOptions options3 = createNfcReaderOptions(); NfcScanOptions options3 = createNfcScanOptions();
options3.compatibility = NdefCompatibility.ANY; options3.compatibility = NdefCompatibility.ANY;
NdefRecordTypeFilter typeFilter = new NdefRecordTypeFilter(); NdefRecordTypeFilter typeFilter = new NdefRecordTypeFilter();
typeFilter.recordType = NdefRecordType.URL; typeFilter.recordType = NdefRecordType.URL;
...@@ -595,7 +595,7 @@ public class NFCTest { ...@@ -595,7 +595,7 @@ public class NFCTest {
assertNull(mErrorCaptor.getValue()); assertNull(mErrorCaptor.getValue());
// Should not match // Should not match
NfcReaderOptions options4 = createNfcReaderOptions(); NfcScanOptions options4 = createNfcScanOptions();
options4.compatibility = NdefCompatibility.NFC_FORUM; options4.compatibility = NdefCompatibility.NFC_FORUM;
options4.url = AUTHOR_RECORD_DOMAIN; options4.url = AUTHOR_RECORD_DOMAIN;
int watchId4 = mNextWatchId++; int watchId4 = mNextWatchId++;
...@@ -625,7 +625,7 @@ public class NFCTest { ...@@ -625,7 +625,7 @@ public class NFCTest {
TestNfcImpl nfc = new TestNfcImpl(mContext, mDelegate); TestNfcImpl nfc = new TestNfcImpl(mContext, mDelegate);
mDelegate.invokeCallback(); mDelegate.invokeCallback();
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
nfc.watch(createNfcReaderOptions(), mNextWatchId, mockWatchCallback); nfc.watch(createNfcScanOptions(), mNextWatchId, mockWatchCallback);
verify(mockWatchCallback).call(mErrorCaptor.capture()); verify(mockWatchCallback).call(mErrorCaptor.capture());
assertNull(mErrorCaptor.getValue()); assertNull(mErrorCaptor.getValue());
...@@ -653,11 +653,11 @@ public class NFCTest { ...@@ -653,11 +653,11 @@ public class NFCTest {
mDelegate.invokeCallback(); mDelegate.invokeCallback();
WatchResponse mockWatchCallback1 = mock(WatchResponse.class); WatchResponse mockWatchCallback1 = mock(WatchResponse.class);
WatchResponse mockWatchCallback2 = mock(WatchResponse.class); WatchResponse mockWatchCallback2 = mock(WatchResponse.class);
nfc.watch(createNfcReaderOptions(), mNextWatchId++, mockWatchCallback1); nfc.watch(createNfcScanOptions(), mNextWatchId++, mockWatchCallback1);
verify(mockWatchCallback1).call(mErrorCaptor.capture()); verify(mockWatchCallback1).call(mErrorCaptor.capture());
assertNull(mErrorCaptor.getValue()); assertNull(mErrorCaptor.getValue());
nfc.watch(createNfcReaderOptions(), mNextWatchId++, mockWatchCallback2); nfc.watch(createNfcScanOptions(), mNextWatchId++, mockWatchCallback2);
verify(mockWatchCallback2).call(mErrorCaptor.capture()); verify(mockWatchCallback2).call(mErrorCaptor.capture());
assertNull(mErrorCaptor.getValue()); assertNull(mErrorCaptor.getValue());
...@@ -678,7 +678,7 @@ public class NFCTest { ...@@ -678,7 +678,7 @@ public class NFCTest {
TestNfcImpl nfc = new TestNfcImpl(mContext, mDelegate); TestNfcImpl nfc = new TestNfcImpl(mContext, mDelegate);
mDelegate.invokeCallback(); mDelegate.invokeCallback();
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
nfc.watch(createNfcReaderOptions(), mNextWatchId, mockWatchCallback); nfc.watch(createNfcScanOptions(), mNextWatchId, mockWatchCallback);
verify(mockWatchCallback).call(mErrorCaptor.capture()); verify(mockWatchCallback).call(mErrorCaptor.capture());
assertNull(mErrorCaptor.getValue()); assertNull(mErrorCaptor.getValue());
...@@ -716,7 +716,7 @@ public class NFCTest { ...@@ -716,7 +716,7 @@ public class NFCTest {
mDelegate.invokeCallback(); mDelegate.invokeCallback();
nfc.setClient(mNfcClient); nfc.setClient(mNfcClient);
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
nfc.watch(createNfcReaderOptions(), mNextWatchId, mockWatchCallback); nfc.watch(createNfcScanOptions(), mNextWatchId, mockWatchCallback);
// Force read operation to fail // Force read operation to fail
doThrow(IllegalStateException.class).when(mNfcTagHandler).read(); doThrow(IllegalStateException.class).when(mNfcTagHandler).read();
...@@ -927,7 +927,7 @@ public class NFCTest { ...@@ -927,7 +927,7 @@ public class NFCTest {
TestNfcImpl nfc = new TestNfcImpl(mContext, mDelegate); TestNfcImpl nfc = new TestNfcImpl(mContext, mDelegate);
mDelegate.invokeCallback(); mDelegate.invokeCallback();
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
nfc.watch(createNfcReaderOptions(), mNextWatchId, mockWatchCallback); nfc.watch(createNfcScanOptions(), mNextWatchId, mockWatchCallback);
PushResponse mockPushCallback = mock(PushResponse.class); PushResponse mockPushCallback = mock(PushResponse.class);
// Should be cancelled with TIMER_EXPIRED. // Should be cancelled with TIMER_EXPIRED.
...@@ -998,7 +998,7 @@ public class NFCTest { ...@@ -998,7 +998,7 @@ public class NFCTest {
// Should match. // Should match.
int watchId1 = mNextWatchId++; int watchId1 = mNextWatchId++;
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.NFC_FORUM; options.compatibility = NdefCompatibility.NFC_FORUM;
options.url = TEST_URL; options.url = TEST_URL;
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1010,7 +1010,7 @@ public class NFCTest { ...@@ -1010,7 +1010,7 @@ public class NFCTest {
// Should not match. // Should not match.
int watchId2 = mNextWatchId++; int watchId2 = mNextWatchId++;
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.VENDOR; options.compatibility = NdefCompatibility.VENDOR;
options.url = TEST_URL; options.url = TEST_URL;
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1022,7 +1022,7 @@ public class NFCTest { ...@@ -1022,7 +1022,7 @@ public class NFCTest {
// Should match. // Should match.
int watchId3 = mNextWatchId++; int watchId3 = mNextWatchId++;
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.ANY; options.compatibility = NdefCompatibility.ANY;
options.url = TEST_URL; options.url = TEST_URL;
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1058,7 +1058,7 @@ public class NFCTest { ...@@ -1058,7 +1058,7 @@ public class NFCTest {
// Should not match. // Should not match.
int watchId1 = mNextWatchId++; int watchId1 = mNextWatchId++;
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.NFC_FORUM; options.compatibility = NdefCompatibility.NFC_FORUM;
options.url = TEST_URL; options.url = TEST_URL;
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1070,7 +1070,7 @@ public class NFCTest { ...@@ -1070,7 +1070,7 @@ public class NFCTest {
// Should match. // Should match.
int watchId2 = mNextWatchId++; int watchId2 = mNextWatchId++;
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.VENDOR; options.compatibility = NdefCompatibility.VENDOR;
options.url = TEST_URL; options.url = TEST_URL;
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1082,7 +1082,7 @@ public class NFCTest { ...@@ -1082,7 +1082,7 @@ public class NFCTest {
// Should match. // Should match.
int watchId3 = mNextWatchId++; int watchId3 = mNextWatchId++;
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.ANY; options.compatibility = NdefCompatibility.ANY;
options.url = TEST_URL; options.url = TEST_URL;
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1117,7 +1117,7 @@ public class NFCTest { ...@@ -1117,7 +1117,7 @@ public class NFCTest {
// Should match. // Should match.
int watchId1 = mNextWatchId++; int watchId1 = mNextWatchId++;
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.NFC_FORUM; options.compatibility = NdefCompatibility.NFC_FORUM;
options.url = "https://test.com/*"; options.url = "https://test.com/*";
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1129,7 +1129,7 @@ public class NFCTest { ...@@ -1129,7 +1129,7 @@ public class NFCTest {
// Should match. // Should match.
int watchId2 = mNextWatchId++; int watchId2 = mNextWatchId++;
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.NFC_FORUM; options.compatibility = NdefCompatibility.NFC_FORUM;
options.url = "https://test.com/contact/42"; options.url = "https://test.com/contact/42";
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1141,7 +1141,7 @@ public class NFCTest { ...@@ -1141,7 +1141,7 @@ public class NFCTest {
// Should match. // Should match.
int watchId3 = mNextWatchId++; int watchId3 = mNextWatchId++;
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.NFC_FORUM; options.compatibility = NdefCompatibility.NFC_FORUM;
options.url = "https://subdomain.test.com/*"; options.url = "https://subdomain.test.com/*";
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1153,7 +1153,7 @@ public class NFCTest { ...@@ -1153,7 +1153,7 @@ public class NFCTest {
// Should match. // Should match.
int watchId4 = mNextWatchId++; int watchId4 = mNextWatchId++;
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.NFC_FORUM; options.compatibility = NdefCompatibility.NFC_FORUM;
options.url = "https://subdomain.test.com/contact"; options.url = "https://subdomain.test.com/contact";
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1164,7 +1164,7 @@ public class NFCTest { ...@@ -1164,7 +1164,7 @@ public class NFCTest {
// Should not match. // Should not match.
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.NFC_FORUM; options.compatibility = NdefCompatibility.NFC_FORUM;
options.url = "https://www.test.com/*"; options.url = "https://www.test.com/*";
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1175,7 +1175,7 @@ public class NFCTest { ...@@ -1175,7 +1175,7 @@ public class NFCTest {
// Should not match. // Should not match.
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.NFC_FORUM; options.compatibility = NdefCompatibility.NFC_FORUM;
options.url = "http://test.com/*"; options.url = "http://test.com/*";
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1186,7 +1186,7 @@ public class NFCTest { ...@@ -1186,7 +1186,7 @@ public class NFCTest {
// Should not match. // Should not match.
{ {
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.NFC_FORUM; options.compatibility = NdefCompatibility.NFC_FORUM;
options.url = "invalid pattern url"; options.url = "invalid pattern url";
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1226,7 +1226,7 @@ public class NFCTest { ...@@ -1226,7 +1226,7 @@ public class NFCTest {
nfc.setClient(mNfcClient); nfc.setClient(mNfcClient);
// Should not match when invalid WebNFC Id is received. // Should not match when invalid WebNFC Id is received.
NfcReaderOptions options = createNfcReaderOptions(); NfcScanOptions options = createNfcScanOptions();
options.compatibility = NdefCompatibility.NFC_FORUM; options.compatibility = NdefCompatibility.NFC_FORUM;
options.url = "https://test.com/*"; options.url = "https://test.com/*";
WatchResponse mockWatchCallback = mock(WatchResponse.class); WatchResponse mockWatchCallback = mock(WatchResponse.class);
...@@ -1292,8 +1292,8 @@ public class NFCTest { ...@@ -1292,8 +1292,8 @@ public class NFCTest {
return pushOptions; return pushOptions;
} }
private NfcReaderOptions createNfcReaderOptions() { private NfcScanOptions createNfcScanOptions() {
NfcReaderOptions options = new NfcReaderOptions(); NfcScanOptions options = new NfcScanOptions();
options.url = ""; options.url = "";
options.mediaType = ""; options.mediaType = "";
options.compatibility = NdefCompatibility.ANY; options.compatibility = NdefCompatibility.ANY;
......
...@@ -69,7 +69,7 @@ struct NDEFMessage { ...@@ -69,7 +69,7 @@ struct NDEFMessage {
// The |url| field is an ASCII serialized origin, optionally followed by a URL // The |url| field is an ASCII serialized origin, optionally followed by a URL
// path. It represents Web NFC id, that can be used for matching Web NFC // path. It represents Web NFC id, that can be used for matching Web NFC
// content with the filter specified by |url| field in NFCReaderOptions. // content with the filter specified by |url| field in NFCScanOptions.
string? url; string? url;
// Maximum size of NFC message that can be sent over IPC is 32KB. // Maximum size of NFC message that can be sent over IPC is 32KB.
...@@ -95,7 +95,7 @@ struct NDEFRecordTypeFilter { ...@@ -95,7 +95,7 @@ struct NDEFRecordTypeFilter {
NDEFRecordType record_type; NDEFRecordType record_type;
}; };
struct NFCReaderOptions { struct NFCScanOptions {
// Defines filtering constraint for NFC messages with specified |url|. // Defines filtering constraint for NFC messages with specified |url|.
string? url; string? url;
...@@ -124,9 +124,9 @@ interface NFC { ...@@ -124,9 +124,9 @@ interface NFC {
CancelPush(NFCPushTarget target) => (NFCError? error); CancelPush(NFCPushTarget target) => (NFCError? error);
// Starts watching for nearby NFC devices with data that matches // Starts watching for nearby NFC devices with data that matches
// NFCReaderOptions filtering criteria. |id| identifies each watch request on // NFCScanOptions filtering criteria. |id| identifies each watch request on
// the current Mojo connection. // the current Mojo connection.
Watch(NFCReaderOptions options, uint32 id) => (NFCError? error); Watch(NFCScanOptions options, uint32 id) => (NFCError? error);
// Cancels watch operation with provided id. // Cancels watch operation with provided id.
CancelWatch (uint32 id) => (NFCError? error); CancelWatch (uint32 id) => (NFCError? error);
......
...@@ -687,11 +687,11 @@ modules_dictionary_idl_files = ...@@ -687,11 +687,11 @@ modules_dictionary_idl_files =
"native_file_system/get_system_directory_options.idl", "native_file_system/get_system_directory_options.idl",
"native_file_system/native_file_system_directory_iterator_entry.idl", "native_file_system/native_file_system_directory_iterator_entry.idl",
"nfc/ndef_message_init.idl", "nfc/ndef_message_init.idl",
"nfc/nfc_push_options.idl",
"nfc/ndef_record_init.idl", "nfc/ndef_record_init.idl",
"nfc/nfc_reader_options.idl",
"nfc/nfc_error_event_init.idl", "nfc/nfc_error_event_init.idl",
"nfc/nfc_push_options.idl",
"nfc/nfc_reading_event_init.idl", "nfc/nfc_reading_event_init.idl",
"nfc/nfc_scan_options.idl",
"notifications/get_notification_options.idl", "notifications/get_notification_options.idl",
"notifications/notification_action.idl", "notifications/notification_action.idl",
"notifications/notification_event_init.idl", "notifications/notification_event_init.idl",
......
...@@ -54,15 +54,14 @@ void NFCProxy::Trace(blink::Visitor* visitor) { ...@@ -54,15 +54,14 @@ void NFCProxy::Trace(blink::Visitor* visitor) {
Supplement<Document>::Trace(visitor); Supplement<Document>::Trace(visitor);
} }
void NFCProxy::StartReading(NFCReader* reader) { void NFCProxy::StartReading(NFCReader* reader, const NFCScanOptions* options) {
DCHECK(reader); DCHECK(reader);
if (readers_.Contains(reader)) if (readers_.Contains(reader))
return; return;
EnsureMojoConnection(); EnsureMojoConnection();
nfc_remote_->Watch( nfc_remote_->Watch(
device::mojom::blink::NFCReaderOptions::From(reader->options()), device::mojom::blink::NFCScanOptions::From(options), next_watch_id_,
next_watch_id_,
WTF::Bind(&NFCProxy::OnReaderRegistered, WrapPersistent(this), WTF::Bind(&NFCProxy::OnReaderRegistered, WrapPersistent(this),
WrapPersistent(reader), next_watch_id_)); WrapPersistent(reader), next_watch_id_));
readers_.insert(reader, next_watch_id_); readers_.insert(reader, next_watch_id_);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
namespace blink { namespace blink {
class Document; class Document;
class NFCScanOptions;
class NFCReader; class NFCReader;
class NFCWriter; class NFCWriter;
...@@ -47,7 +48,7 @@ class MODULES_EXPORT NFCProxy final ...@@ -47,7 +48,7 @@ class MODULES_EXPORT NFCProxy final
// collected. // collected.
void AddWriter(NFCWriter*); void AddWriter(NFCWriter*);
void StartReading(NFCReader*); void StartReading(NFCReader*, const NFCScanOptions*);
void StopReading(NFCReader*); void StopReading(NFCReader*);
bool IsReading(const NFCReader*); bool IsReading(const NFCReader*);
void Push(device::mojom::blink::NDEFMessagePtr, void Push(device::mojom::blink::NDEFMessagePtr,
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#include "third_party/blink/renderer/core/testing/page_test_base.h" #include "third_party/blink/renderer/core/testing/page_test_base.h"
#include "third_party/blink/renderer/modules/nfc/nfc_proxy.h" #include "third_party/blink/renderer/modules/nfc/nfc_proxy.h"
#include "third_party/blink/renderer/modules/nfc/nfc_reader.h" #include "third_party/blink/renderer/modules/nfc/nfc_reader.h"
#include "third_party/blink/renderer/modules/nfc/nfc_reader_options.h" #include "third_party/blink/renderer/modules/nfc/nfc_scan_options.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h" #include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
namespace blink { namespace blink {
...@@ -46,9 +46,8 @@ MATCHER_P(MessageEquals, expected, "") { ...@@ -46,9 +46,8 @@ MATCHER_P(MessageEquals, expected, "") {
class MockNFCReader : public NFCReader { class MockNFCReader : public NFCReader {
public: public:
explicit MockNFCReader(ExecutionContext* execution_context, explicit MockNFCReader(ExecutionContext* execution_context)
NFCReaderOptions* options) : NFCReader(execution_context) {}
: NFCReader(execution_context, options) {}
MOCK_METHOD2(OnReading, MOCK_METHOD2(OnReading,
void(const String& serial_number, void(const String& serial_number,
...@@ -119,7 +118,7 @@ class FakeNfcService : public device::mojom::blink::NFC { ...@@ -119,7 +118,7 @@ class FakeNfcService : public device::mojom::blink::NFC {
CancelPushCallback callback) override { CancelPushCallback callback) override {
std::move(callback).Run(nullptr); std::move(callback).Run(nullptr);
} }
void Watch(device::mojom::blink::NFCReaderOptionsPtr options, void Watch(device::mojom::blink::NFCScanOptionsPtr options,
uint32_t id, uint32_t id,
WatchCallback callback) override { WatchCallback callback) override {
watches_.emplace(id, std::move(options)); watches_.emplace(id, std::move(options));
...@@ -142,7 +141,7 @@ class FakeNfcService : public device::mojom::blink::NFC { ...@@ -142,7 +141,7 @@ class FakeNfcService : public device::mojom::blink::NFC {
device::mojom::blink::NDEFMessagePtr tag_message_; device::mojom::blink::NDEFMessagePtr tag_message_;
mojo::Remote<device::mojom::blink::NFCClient> client_; mojo::Remote<device::mojom::blink::NFCClient> client_;
std::map<uint32_t, device::mojom::blink::NFCReaderOptionsPtr> watches_; std::map<uint32_t, device::mojom::blink::NFCScanOptionsPtr> watches_;
mojo::Receiver<device::mojom::blink::NFC> receiver_; mojo::Receiver<device::mojom::blink::NFC> receiver_;
}; };
...@@ -173,11 +172,11 @@ class NFCProxyTest : public PageTestBase { ...@@ -173,11 +172,11 @@ class NFCProxyTest : public PageTestBase {
TEST_F(NFCProxyTest, SuccessfulPath) { TEST_F(NFCProxyTest, SuccessfulPath) {
auto& document = GetDocument(); auto& document = GetDocument();
auto* nfc_proxy = NFCProxy::From(document); auto* nfc_proxy = NFCProxy::From(document);
auto* read_options = NFCReaderOptions::Create(); auto* scan_options = NFCScanOptions::Create();
read_options->setURL(kTestUrl); scan_options->setURL(kTestUrl);
auto* reader = MakeGarbageCollected<MockNFCReader>(&document, read_options); auto* reader = MakeGarbageCollected<MockNFCReader>(&document);
nfc_proxy->StartReading(reader); nfc_proxy->StartReading(reader, scan_options);
EXPECT_TRUE(nfc_proxy->IsReading(reader)); EXPECT_TRUE(nfc_proxy->IsReading(reader));
test::RunPendingTasks(); test::RunPendingTasks();
EXPECT_EQ(nfc_service()->GetWatches().size(), 1u); EXPECT_EQ(nfc_service()->GetWatches().size(), 1u);
...@@ -216,11 +215,11 @@ TEST_F(NFCProxyTest, SuccessfulPath) { ...@@ -216,11 +215,11 @@ TEST_F(NFCProxyTest, SuccessfulPath) {
TEST_F(NFCProxyTest, ErrorPath) { TEST_F(NFCProxyTest, ErrorPath) {
auto& document = GetDocument(); auto& document = GetDocument();
auto* nfc_proxy = NFCProxy::From(document); auto* nfc_proxy = NFCProxy::From(document);
auto* read_options = NFCReaderOptions::Create(); auto* scan_options = NFCScanOptions::Create();
read_options->setURL(kTestUrl); scan_options->setURL(kTestUrl);
auto* reader = MakeGarbageCollected<MockNFCReader>(&document, read_options); auto* reader = MakeGarbageCollected<MockNFCReader>(&document);
nfc_proxy->StartReading(reader); nfc_proxy->StartReading(reader, scan_options);
EXPECT_TRUE(nfc_proxy->IsReading(reader)); EXPECT_TRUE(nfc_proxy->IsReading(reader));
test::RunPendingTasks(); test::RunPendingTasks();
......
...@@ -7,26 +7,26 @@ ...@@ -7,26 +7,26 @@
#include <utility> #include <utility>
#include "services/device/public/mojom/nfc.mojom-blink.h" #include "services/device/public/mojom/nfc.mojom-blink.h"
#include "third_party/blink/renderer/core/dom/abort_signal.h"
#include "third_party/blink/renderer/modules/event_target_modules.h" #include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/modules/nfc/ndef_message.h" #include "third_party/blink/renderer/modules/nfc/ndef_message.h"
#include "third_party/blink/renderer/modules/nfc/nfc_constants.h" #include "third_party/blink/renderer/modules/nfc/nfc_constants.h"
#include "third_party/blink/renderer/modules/nfc/nfc_error_event.h" #include "third_party/blink/renderer/modules/nfc/nfc_error_event.h"
#include "third_party/blink/renderer/modules/nfc/nfc_proxy.h" #include "third_party/blink/renderer/modules/nfc/nfc_proxy.h"
#include "third_party/blink/renderer/modules/nfc/nfc_reader_options.h"
#include "third_party/blink/renderer/modules/nfc/nfc_reading_event.h" #include "third_party/blink/renderer/modules/nfc/nfc_reading_event.h"
#include "third_party/blink/renderer/modules/nfc/nfc_scan_options.h"
#include "third_party/blink/renderer/modules/nfc/nfc_utils.h" #include "third_party/blink/renderer/modules/nfc/nfc_utils.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h" #include "third_party/blink/renderer/platform/weborigin/kurl.h"
namespace blink { namespace blink {
// static // static
NFCReader* NFCReader::Create(ExecutionContext* context, NFCReader* NFCReader::Create(ExecutionContext* context) {
NFCReaderOptions* options) { return MakeGarbageCollected<NFCReader>(context);
return MakeGarbageCollected<NFCReader>(context, options);
} }
NFCReader::NFCReader(ExecutionContext* context, NFCReaderOptions* options) NFCReader::NFCReader(ExecutionContext* context)
: ContextLifecycleObserver(context), options_(options) {} : ContextLifecycleObserver(context) {}
NFCReader::~NFCReader() = default; NFCReader::~NFCReader() = default;
...@@ -43,32 +43,39 @@ bool NFCReader::HasPendingActivity() const { ...@@ -43,32 +43,39 @@ bool NFCReader::HasPendingActivity() const {
HasEventListeners(); HasEventListeners();
} }
void NFCReader::start() { // https://w3c.github.io/web-nfc/#the-scan-method
void NFCReader::scan(const NFCScanOptions* options) {
if (!CheckSecurity()) if (!CheckSecurity())
return; return;
// https://w3c.github.io/web-nfc/#dom-nfcreader-start (Step 3.4) if (options->hasSignal()) {
if (options_->hasURL() && !options_->url().IsEmpty()) { // 6. If reader.[[Signal]]'s aborted flag is set, then return.
KURL pattern_url(options_->url()); if (options->signal()->aborted())
return;
// 7. If reader.[[Signal]] is not null, then add the following abort steps
// to reader.[[Signal]]:
options->signal()->AddAlgorithm(
WTF::Bind(&NFCReader::Abort, WrapPersistent(this)));
}
// Step 8.4, if the url is not an empty string and it is not a valid URL
// pattern, fire a NFCErrorEvent with "SyntaxError" DOMException, then return.
if (options->hasURL() && !options->url().IsEmpty()) {
KURL pattern_url(options->url());
if (!pattern_url.IsValid() || pattern_url.Protocol() != kNfcProtocolHttps) { if (!pattern_url.IsValid() || pattern_url.Protocol() != kNfcProtocolHttps) {
DispatchEvent(*MakeGarbageCollected<NFCErrorEvent>( DispatchEvent(*MakeGarbageCollected<NFCErrorEvent>(
event_type_names::kError, event_type_names::kError,
MakeGarbageCollected<DOMException>(DOMExceptionCode::kSyntaxError, MakeGarbageCollected<DOMException>(DOMExceptionCode::kSyntaxError,
kNfcUrlPatternError))); kNfcUrlPatternError)));
return;
} }
} }
GetNfcProxy()->StartReading(this); GetNfcProxy()->StartReading(this, options);
}
void NFCReader::stop() {
if (!CheckSecurity())
return;
GetNfcProxy()->StopReading(this);
} }
void NFCReader::Trace(blink::Visitor* visitor) { void NFCReader::Trace(blink::Visitor* visitor) {
visitor->Trace(options_);
EventTargetWithInlineData::Trace(visitor); EventTargetWithInlineData::Trace(visitor);
ActiveScriptWrappable::Trace(visitor); ActiveScriptWrappable::Trace(visitor);
ContextLifecycleObserver::Trace(visitor); ContextLifecycleObserver::Trace(visitor);
...@@ -91,6 +98,10 @@ void NFCReader::ContextDestroyed(ExecutionContext*) { ...@@ -91,6 +98,10 @@ void NFCReader::ContextDestroyed(ExecutionContext*) {
GetNfcProxy()->StopReading(this); GetNfcProxy()->StopReading(this);
} }
void NFCReader::Abort() {
GetNfcProxy()->StopReading(this);
}
bool NFCReader::CheckSecurity() { bool NFCReader::CheckSecurity() {
ExecutionContext* execution_context = GetExecutionContext(); ExecutionContext* execution_context = GetExecutionContext();
if (!execution_context) if (!execution_context)
......
...@@ -16,7 +16,7 @@ namespace blink { ...@@ -16,7 +16,7 @@ namespace blink {
class ExecutionContext; class ExecutionContext;
class NFCProxy; class NFCProxy;
class NFCReaderOptions; class NFCScanOptions;
class MODULES_EXPORT NFCReader : public EventTargetWithInlineData, class MODULES_EXPORT NFCReader : public EventTargetWithInlineData,
public ActiveScriptWrappable<NFCReader>, public ActiveScriptWrappable<NFCReader>,
...@@ -25,9 +25,9 @@ class MODULES_EXPORT NFCReader : public EventTargetWithInlineData, ...@@ -25,9 +25,9 @@ class MODULES_EXPORT NFCReader : public EventTargetWithInlineData,
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
public: public:
static NFCReader* Create(ExecutionContext*, NFCReaderOptions*); static NFCReader* Create(ExecutionContext*);
NFCReader(ExecutionContext*, NFCReaderOptions*); NFCReader(ExecutionContext*);
~NFCReader() override; ~NFCReader() override;
// EventTarget overrides. // EventTarget overrides.
...@@ -39,13 +39,10 @@ class MODULES_EXPORT NFCReader : public EventTargetWithInlineData, ...@@ -39,13 +39,10 @@ class MODULES_EXPORT NFCReader : public EventTargetWithInlineData,
DEFINE_ATTRIBUTE_EVENT_LISTENER(error, kError) DEFINE_ATTRIBUTE_EVENT_LISTENER(error, kError)
DEFINE_ATTRIBUTE_EVENT_LISTENER(reading, kReading) DEFINE_ATTRIBUTE_EVENT_LISTENER(reading, kReading)
void start(); void scan(const NFCScanOptions*);
void stop();
void Trace(blink::Visitor*) override; void Trace(blink::Visitor*) override;
const NFCReaderOptions* options() const { return options_; }
// Called by NFCProxy for dispatching events. // Called by NFCProxy for dispatching events.
virtual void OnReading(const String& serial_number, virtual void OnReading(const String& serial_number,
const device::mojom::blink::NDEFMessage& message); const device::mojom::blink::NDEFMessage& message);
...@@ -55,11 +52,11 @@ class MODULES_EXPORT NFCReader : public EventTargetWithInlineData, ...@@ -55,11 +52,11 @@ class MODULES_EXPORT NFCReader : public EventTargetWithInlineData,
// ContextLifecycleObserver overrides. // ContextLifecycleObserver overrides.
void ContextDestroyed(ExecutionContext*) override; void ContextDestroyed(ExecutionContext*) override;
void Abort();
NFCProxy* GetNfcProxy() const; NFCProxy* GetNfcProxy() const;
bool CheckSecurity(); bool CheckSecurity();
const Member<NFCReaderOptions> options_;
}; };
} // namespace blink } // namespace blink
......
...@@ -8,13 +8,12 @@ ...@@ -8,13 +8,12 @@
RuntimeEnabled=WebNFC, RuntimeEnabled=WebNFC,
ActiveScriptWrappable, ActiveScriptWrappable,
SecureContext, SecureContext,
Constructor(optional NFCReaderOptions options), Constructor(),
ConstructorCallWith=ExecutionContext, ConstructorCallWith=ExecutionContext,
Exposed=Window Exposed=Window
] interface NFCReader : EventTarget { ] interface NFCReader : EventTarget {
attribute EventHandler onreading; attribute EventHandler onreading;
attribute EventHandler onerror; attribute EventHandler onerror;
void start(); void scan(optional NFCScanOptions options);
void stop();
}; };
...@@ -6,8 +6,9 @@ ...@@ -6,8 +6,9 @@
// http://w3c.github.io/web-nfc/#dom-ndefcompatibility // http://w3c.github.io/web-nfc/#dom-ndefcompatibility
enum NDEFCompatibility { "nfc-forum", "vendor", "any" }; enum NDEFCompatibility { "nfc-forum", "vendor", "any" };
// https://w3c.github.io/web-nfc/#dom-nfcreaderoptions // https://w3c.github.io/web-nfc/#the-nfcscanoptions-dictionary
dictionary NFCReaderOptions { dictionary NFCScanOptions {
AbortSignal? signal;
USVString url = ""; USVString url = "";
NDEFRecordType? recordType; NDEFRecordType? recordType;
USVString mediaType = ""; USVString mediaType = "";
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "third_party/blink/renderer/modules/nfc/ndef_message.h" #include "third_party/blink/renderer/modules/nfc/ndef_message.h"
#include "third_party/blink/renderer/modules/nfc/ndef_record.h" #include "third_party/blink/renderer/modules/nfc/ndef_record.h"
#include "third_party/blink/renderer/modules/nfc/nfc_push_options.h" #include "third_party/blink/renderer/modules/nfc/nfc_push_options.h"
#include "third_party/blink/renderer/modules/nfc/nfc_reader_options.h" #include "third_party/blink/renderer/modules/nfc/nfc_scan_options.h"
#include "third_party/blink/renderer/modules/nfc/nfc_utils.h" #include "third_party/blink/renderer/modules/nfc/nfc_utils.h"
#include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h" #include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
...@@ -27,8 +27,8 @@ using device::mojom::blink::NDEFRecordTypeFilter; ...@@ -27,8 +27,8 @@ using device::mojom::blink::NDEFRecordTypeFilter;
using device::mojom::blink::NFCPushOptions; using device::mojom::blink::NFCPushOptions;
using device::mojom::blink::NFCPushOptionsPtr; using device::mojom::blink::NFCPushOptionsPtr;
using device::mojom::blink::NFCPushTarget; using device::mojom::blink::NFCPushTarget;
using device::mojom::blink::NFCReaderOptions; using device::mojom::blink::NFCScanOptions;
using device::mojom::blink::NFCReaderOptionsPtr; using device::mojom::blink::NFCScanOptionsPtr;
using WTF::String; using WTF::String;
...@@ -236,25 +236,25 @@ TypeConverter<NFCPushOptionsPtr, const blink::NFCPushOptions*>::Convert( ...@@ -236,25 +236,25 @@ TypeConverter<NFCPushOptionsPtr, const blink::NFCPushOptions*>::Convert(
return pushOptionsPtr; return pushOptionsPtr;
} }
NFCReaderOptionsPtr NFCScanOptionsPtr
TypeConverter<NFCReaderOptionsPtr, const blink::NFCReaderOptions*>::Convert( TypeConverter<NFCScanOptionsPtr, const blink::NFCScanOptions*>::Convert(
const blink::NFCReaderOptions* watchOptions) { const blink::NFCScanOptions* scanOptions) {
// https://w3c.github.io/web-nfc/#dom-nfcreaderoptions // https://w3c.github.io/web-nfc/#dom-nfcscanoptions
// Default values for NFCReaderOptions dictionary are: // Default values for NFCScanOptions dictionary are:
// url = "", recordType = null, mediaType = "", compatibility = "nfc-forum" // url = "", recordType = null, mediaType = "", compatibility = "nfc-forum"
NFCReaderOptionsPtr watchOptionsPtr = NFCReaderOptions::New(); NFCScanOptionsPtr scanOptionsPtr = NFCScanOptions::New();
watchOptionsPtr->url = watchOptions->url(); scanOptionsPtr->url = scanOptions->url();
watchOptionsPtr->media_type = watchOptions->mediaType(); scanOptionsPtr->media_type = scanOptions->mediaType();
watchOptionsPtr->compatibility = scanOptionsPtr->compatibility =
blink::StringToNDEFCompatibility(watchOptions->compatibility()); blink::StringToNDEFCompatibility(scanOptions->compatibility());
if (watchOptions->hasRecordType()) { if (scanOptions->hasRecordType()) {
watchOptionsPtr->record_filter = NDEFRecordTypeFilter::New(); scanOptionsPtr->record_filter = NDEFRecordTypeFilter::New();
watchOptionsPtr->record_filter->record_type = scanOptionsPtr->record_filter->record_type =
blink::StringToNDEFRecordType(watchOptions->recordType()); blink::StringToNDEFRecordType(scanOptions->recordType());
} }
return watchOptionsPtr; return scanOptionsPtr;
} }
} // namespace mojo } // namespace mojo
...@@ -14,7 +14,7 @@ namespace blink { ...@@ -14,7 +14,7 @@ namespace blink {
class DOMArrayBuffer; class DOMArrayBuffer;
class NDEFRecordInit; class NDEFRecordInit;
class NDEFMessageInit; class NDEFMessageInit;
class NFCReaderOptions; class NFCScanOptions;
class NFCPushOptions; class NFCPushOptions;
} // namespace blink } // namespace blink
...@@ -100,10 +100,10 @@ struct TypeConverter<device::mojom::blink::NFCPushOptionsPtr, ...@@ -100,10 +100,10 @@ struct TypeConverter<device::mojom::blink::NFCPushOptionsPtr,
}; };
template <> template <>
struct TypeConverter<device::mojom::blink::NFCReaderOptionsPtr, struct TypeConverter<device::mojom::blink::NFCScanOptionsPtr,
const blink::NFCReaderOptions*> { const blink::NFCScanOptions*> {
static device::mojom::blink::NFCReaderOptionsPtr Convert( static device::mojom::blink::NFCScanOptionsPtr Convert(
const blink::NFCReaderOptions* watchOptions); const blink::NFCScanOptions* scanOptions);
}; };
} // namespace mojo } // namespace mojo
......
...@@ -10,122 +10,125 @@ ...@@ -10,122 +10,125 @@
"use strict"; "use strict";
function waitSyntaxErrorPromise(t, reader) { function waitSyntaxErrorPromise(t, scan_options) {
const reader = new NFCReader();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
const promise = readerWatcher.wait_for("error").then(event => { const promise = readerWatcher.wait_for("error").then(event => {
assert_equals(event.error.name, 'SyntaxError'); assert_equals(event.error.name, 'SyntaxError');
}); });
// NFCReader#start() asynchronously dispatches the syntax error event. // NFCReader#scan() asynchronously dispatches the syntax error event.
reader.start(); reader.scan(scan_options);
return promise; return promise;
} }
promise_test(async t => { promise_test(async t => {
const reader = new NFCReader({url: "www.a.com"}); await waitSyntaxErrorPromise(t, {url: "www.a.com"});
await waitSyntaxErrorPromise(t, reader); }, "Test that NFCReader.scan fails if NFCScanOptions.url is missing \
}, "Test that NFCReader.start fails if NFCReaderOptions.url is missing \
components."); components.");
promise_test(async t => { promise_test(async t => {
const reader = new NFCReader({url: "invalid"}); await waitSyntaxErrorPromise(t, {url: "invalid"});
await waitSyntaxErrorPromise(t, reader); }, "Test that NFCReader.scan fails if NFCScanOptions.url is invalid.");
}, "Test that NFCReader.start fails if NFCReaderOptions.url is invalid.");
promise_test(async t => { promise_test(async t => {
const reader = new NFCReader({url: "http://a.com"}); await waitSyntaxErrorPromise(t, {url: "http://a.com"});
await waitSyntaxErrorPromise(t, reader); }, "Test that NFCReader.scan fails if NFCScanOptions.url has wrong \
}, "Test that NFCReader.start fails if NFCReaderOptions.url has wrong \
protocol."); protocol.");
nfc_test(async (t, mockNFC) => { nfc_test(async (t, mockNFC) => {
const reader = new NFCReader(); const reader = new NFCReader();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
reader.start(); reader.scan();
mockNFC.setHWStatus(NFCHWStatus.DISABLED); mockNFC.setHWStatus(NFCHWStatus.DISABLED);
const event = await readerWatcher.wait_for("error"); const event = await readerWatcher.wait_for("error");
assert_equals(event.error.name, 'NotReadableError'); assert_equals(event.error.name, 'NotReadableError');
}, "NFCReader.start should fail if NFC HW is disabled."); }, "NFCReader.scan should fail if NFC HW is disabled.");
nfc_test(async (t, mockNFC) => { nfc_test(async (t, mockNFC) => {
const reader = new NFCReader(); const reader = new NFCReader();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
reader.start(); reader.scan();
mockNFC.setHWStatus(NFCHWStatus.NOT_SUPPORTED); mockNFC.setHWStatus(NFCHWStatus.NOT_SUPPORTED);
const event = await readerWatcher.wait_for("error"); const event = await readerWatcher.wait_for("error");
assert_equals(event.error.name, 'NotSupportedError'); assert_equals(event.error.name, 'NotSupportedError');
}, "NFCReader.start should fail if NFC HW is not supported."); }, "NFCReader.scan should fail if NFC HW is not supported.");
nfc_test(async (t, mockNFC) => { nfc_test(async (t, mockNFC) => {
const reader = new NFCReader(); const reader = new NFCReader();
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)])); mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
const promise = readerWatcher.wait_for("reading").then(event => { const promise = readerWatcher.wait_for("reading").then(event => {
assert_true(event instanceof NFCReadingEvent); assert_true(event instanceof NFCReadingEvent);
reader.stop(); controller.abort();
}); });
// NFCReader#start() asynchronously dispatches the reading event. // NFCReader#scan() asynchronously dispatches the reading event.
reader.start(); reader.scan({signal : controller.signal});
await promise; await promise;
}, "Test that nfc watch success if NFC HW is enabled."); }, "Test that nfc watch success if NFC HW is enabled.");
nfc_test(async (t, mockNFC) => { nfc_test(async (t, mockNFC) => {
const reader = new NFCReader({url: "https://a.com"}); const reader = new NFCReader();
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)])); mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
const promise = readerWatcher.wait_for("reading").then(event => { const promise = readerWatcher.wait_for("reading").then(event => {
assert_true(event instanceof NFCReadingEvent); assert_true(event instanceof NFCReadingEvent);
reader.stop(); controller.abort();
}); });
// NFCReader#start() asynchronously dispatches the reading event. // NFCReader#scan() asynchronously dispatches the reading event.
reader.start(); reader.scan({signal : controller.signal, url: "https://a.com"});
await promise; await promise;
}, "Test that NFCReader.start succeeds if NFCReaderOptions.url is valid URL."); }, "Test that NFCReader.scan succeeds if NFCScanOptions.url is valid URL.");
nfc_test(async (t, mockNFC) => { nfc_test(async (t, mockNFC) => {
const reader = new NFCReader({url: "https://a.com/*"}); const reader = new NFCReader();
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)])); mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
const promise = readerWatcher.wait_for("reading").then(event => { const promise = readerWatcher.wait_for("reading").then(event => {
assert_true(event instanceof NFCReadingEvent); assert_true(event instanceof NFCReadingEvent);
reader.stop(); controller.abort();
}); });
// NFCReader#start() asynchronously dispatches the reading event. // NFCReader#scan() asynchronously dispatches the reading event.
reader.start(); reader.scan({signal : controller.signal, url: "https://a.com/*"});
await promise; await promise;
}, "Test that NFCReader.start succeeds if NFCReaderOptions.url is valid URL \ }, "Test that NFCReader.scan succeeds if NFCScanOptions.url is valid URL \
with '*' wildcard character in path."); with '*' wildcard character in path.");
nfc_test(async (t, mockNFC) => { nfc_test(async (t, mockNFC) => {
const reader = new NFCReader({url: "https://a.com/*/bar"}); const reader = new NFCReader();
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)])); mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
const promise = readerWatcher.wait_for("reading").then(event => { const promise = readerWatcher.wait_for("reading").then(event => {
assert_true(event instanceof NFCReadingEvent); assert_true(event instanceof NFCReadingEvent);
reader.stop(); controller.abort();
}); });
// NFCReader#start() asynchronously dispatches the reading event. // NFCReader#scan() asynchronously dispatches the reading event.
reader.start(); reader.scan({signal : controller.signal, url: "https://a.com/*/bar"});
await promise; await promise;
}, "Test that NFCReader.start succeeds if NFCReaderOptions.url is valid URL \ }, "Test that NFCReader.scan succeeds if NFCScanOptions.url is valid URL \
with '*' wildcard character in the beginning of path component followed by \ with '*' wildcard character in the beginning of path component followed by \
subpath."); subpath.");
nfc_test(async (t, mockNFC) => { nfc_test(async (t, mockNFC) => {
const reader = new NFCReader({url: ""}); const reader = new NFCReader({url: ""});
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)])); mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
const promise = readerWatcher.wait_for("reading").then(event => { const promise = readerWatcher.wait_for("reading").then(event => {
assert_true(event instanceof NFCReadingEvent); assert_true(event instanceof NFCReadingEvent);
reader.stop(); controller.abort();
}); });
// NFCReader#start() asynchronously dispatches the reading event. // NFCReader#scan() asynchronously dispatches the reading event.
reader.start(); reader.scan({signal : controller.signal, url: ""});
await promise; await promise;
}, "Test that NFCReader.start succeeds if NFCReaderOptions.url is empty."); }, "Test that NFCReader.scan succeeds if NFCScanOptions.url is empty.");
</script> </script>
...@@ -13,60 +13,60 @@ ...@@ -13,60 +13,60 @@
const NFCReaderOptionTests = const NFCReaderOptionTests =
[ [
{ {
desc: "Test that reading data succeed when NFCReaderOptions'" + desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to 'empty'.", " recordType is set to 'empty'.",
readOptions: {recordType: "empty"}, scanOptions: {recordType: "empty"},
unmatchedReadOptions: {recordType: "json"}, unmatchedScanOptions: {recordType: "json"},
message: createMessage([createRecord('empty', '')]) message: createMessage([createRecord('empty', '')])
}, },
{ {
desc: "Test that reading data succeed when NFCReaderOptions'" + desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to 'json'.", " recordType is set to 'json'.",
readOptions: {recordType: "json"}, scanOptions: {recordType: "json"},
unmatchedReadOptions: {recordType: "url"}, unmatchedScanOptions: {recordType: "url"},
message: createMessage([createJsonRecord(test_json_data)]) message: createMessage([createJsonRecord(test_json_data)])
}, },
{ {
desc: "Test that reading data succeed when NFCReaderOptions'" + desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to 'opaque'.", " recordType is set to 'opaque'.",
readOptions: {recordType: "opaque"}, scanOptions: {recordType: "opaque"},
unmatchedReadOptions: {recordType: "json"}, unmatchedScanOptions: {recordType: "json"},
message: createMessage([createOpaqueRecord(test_buffer_data)]) message: createMessage([createOpaqueRecord(test_buffer_data)])
}, },
{ {
desc: "Test that reading data succeed when NFCReaderOptions'" + desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to 'text'.", " recordType is set to 'text'.",
readOptions: {recordType: "text"}, scanOptions: {recordType: "text"},
unmatchedReadOptions: {recordType: "json"}, unmatchedScanOptions: {recordType: "json"},
message: createMessage([createTextRecord(test_text_data)]) message: createMessage([createTextRecord(test_text_data)])
}, },
{ {
desc: "Test that reading data succeed when NFCReaderOptions'" + desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to 'url'.", " recordType is set to 'url'.",
readOptions: {recordType: "url"}, scanOptions: {recordType: "url"},
unmatchedReadOptions: {recordType: "json"}, unmatchedScanOptions: {recordType: "json"},
message: createMessage([createUrlRecord(test_url_data)]) message: createMessage([createUrlRecord(test_url_data)])
}, },
{ {
desc: "Test that the url of NFCReaderOptions filters relevant data" + desc: "Test that the url of NFCScanOptions filters relevant data" +
" sources correctly.", " sources correctly.",
readOptions: {url: `${location.origin}/custom/path`}, scanOptions: {url: `${location.origin}/custom/path`},
unmatchedReadOptions: {url: `${location.origin}/custom/invalid`}, unmatchedScanOptions: {url: `${location.origin}/custom/invalid`},
message: {url: `${location.origin}/custom/path/update`, message: {url: `${location.origin}/custom/path/update`,
records: [createTextRecord(test_text_data)]} records: [createTextRecord(test_text_data)]}
}, },
{ {
desc: "Test that the mediaType of NFCReaderOptions filters relevant data" + desc: "Test that the mediaType of NFCScanOptions filters relevant data" +
" sources correctly.", " sources correctly.",
readOptions: {mediaType: "application/octet-stream"}, scanOptions: {mediaType: "application/octet-stream"},
unmatchedReadOptions: {mediaType: "application/json"}, unmatchedScanOptions: {mediaType: "application/json"},
message: createMessage([createOpaqueRecord(test_buffer_data)]) message: createMessage([createOpaqueRecord(test_buffer_data)])
}, },
{ {
desc: "Test that the compatibility of NFCReaderOptions filters relevant data" + desc: "Test that the compatibility of NFCScanOptions filters relevant data" +
" sources correctly.", " sources correctly.",
readOptions: {compatibility: "vendor"}, scanOptions: {compatibility: "vendor"},
unmatchedReadOptions: {compatibility: "nfc-forum"}, unmatchedScanOptions: {compatibility: "nfc-forum"},
message: createMessage([createTextRecord(test_text_data)]), message: createMessage([createTextRecord(test_text_data)]),
} }
]; ];
...@@ -75,43 +75,43 @@ const ReadMultiMessagesTests = ...@@ -75,43 +75,43 @@ const ReadMultiMessagesTests =
[ [
{ {
desc: "Test that filtering 'empty' record from different messages" + desc: "Test that filtering 'empty' record from different messages" +
" correctly with NFCReaderOptions' recordType is set to 'empty'.", " correctly with NFCScanOptions' recordType is set to 'empty'.",
readOptions: {recordType: "empty"}, scanOptions: {recordType: "empty"},
message: createMessage([createRecord('empty', '')]), message: createMessage([createRecord('empty', '')]),
unmatchedMessage: createMessage([createJsonRecord(test_json_data)]), unmatchedMessage: createMessage([createJsonRecord(test_json_data)]),
}, },
{ {
desc: "Test that filtering 'json' record from different messages" + desc: "Test that filtering 'json' record from different messages" +
" correctly with NFCReaderOptions' recordType is set to 'json'.", " correctly with NFCScanOptions' recordType is set to 'json'.",
readOptions: {recordType: "json"}, scanOptions: {recordType: "json"},
message: createMessage([createJsonRecord(test_json_data)]), message: createMessage([createJsonRecord(test_json_data)]),
unmatchedMessage: createMessage([createUrlRecord(test_url_data)]) unmatchedMessage: createMessage([createUrlRecord(test_url_data)])
}, },
{ {
desc: "Test that filtering 'opaque' record from different messages" + desc: "Test that filtering 'opaque' record from different messages" +
" correctly with NFCReaderOptions' recordType is set to 'opaque'.", " correctly with NFCScanOptions' recordType is set to 'opaque'.",
readOptions: {recordType: "opaque"}, scanOptions: {recordType: "opaque"},
message: createMessage([createOpaqueRecord(test_buffer_data)]), message: createMessage([createOpaqueRecord(test_buffer_data)]),
unmatchedMessage: createMessage([createJsonRecord(test_json_data)]) unmatchedMessage: createMessage([createJsonRecord(test_json_data)])
}, },
{ {
desc: "Test that filtering 'text' record from different messages" + desc: "Test that filtering 'text' record from different messages" +
" correctly with NFCReaderOptions' recordType is set to 'text'.", " correctly with NFCScanOptions' recordType is set to 'text'.",
readOptions: {recordType: "text"}, scanOptions: {recordType: "text"},
message: createMessage([createTextRecord(test_text_data)]), message: createMessage([createTextRecord(test_text_data)]),
unmatchedMessage: createMessage([createUrlRecord(test_url_data)]) unmatchedMessage: createMessage([createUrlRecord(test_url_data)])
}, },
{ {
desc: "Test that filtering 'url' record from different messages" + desc: "Test that filtering 'url' record from different messages" +
" correctly with NFCReaderOptions' recordType is set to 'url'.", " correctly with NFCScanOptions' recordType is set to 'url'.",
readOptions: {recordType: "url"}, scanOptions: {recordType: "url"},
message: createMessage([createUrlRecord(test_url_data)]), message: createMessage([createUrlRecord(test_url_data)]),
unmatchedMessage: createMessage([createTextRecord(test_text_data)]) unmatchedMessage: createMessage([createTextRecord(test_text_data)])
}, },
{ {
desc: "Test that filtering 'text' record from different messages" + desc: "Test that filtering 'text' record from different messages" +
" correctly with NFCReaderOptions' url set.", " correctly with NFCScanOptions' url set.",
readOptions: {url: `${location.origin}/custom/path`}, scanOptions: {url: `${location.origin}/custom/path`},
message: {url: `${location.origin}/custom/path/update`, message: {url: `${location.origin}/custom/path/update`,
records: [createTextRecord(test_text_data)]}, records: [createTextRecord(test_text_data)]},
unmatchedMessage: {url: `${location.origin}/custom/invalid`, unmatchedMessage: {url: `${location.origin}/custom/invalid`,
...@@ -119,15 +119,15 @@ const ReadMultiMessagesTests = ...@@ -119,15 +119,15 @@ const ReadMultiMessagesTests =
}, },
{ {
desc: "Test that filtering 'opaque' record from different messages" + desc: "Test that filtering 'opaque' record from different messages" +
" correctly with NFCReaderOptions' mediaType set.", " correctly with NFCScanOptions' mediaType set.",
readOptions: {mediaType: "application/octet-stream"}, scanOptions: {mediaType: "application/octet-stream"},
message: createMessage([createOpaqueRecord(test_buffer_data)]), message: createMessage([createOpaqueRecord(test_buffer_data)]),
unmatchedMessage: createMessage([createJsonRecord(test_json_data)]) unmatchedMessage: createMessage([createJsonRecord(test_json_data)])
}, },
{ {
desc: "Test that filtering 'text' record from different messages" + desc: "Test that filtering 'text' record from different messages" +
" correctly with NFCReaderOptions' compatibility set.", " correctly with NFCScanOptions' compatibility set.",
readOptions: {compatibility: "nfc-forum"}, scanOptions: {compatibility: "nfc-forum"},
message: createMessage([createTextRecord(test_text_data)]), message: createMessage([createTextRecord(test_text_data)]),
unmatchedMessage: createMessage([createJsonRecord(test_json_data)]), unmatchedMessage: createMessage([createJsonRecord(test_json_data)]),
unmatchedCompatibility: "vendor" unmatchedCompatibility: "vendor"
...@@ -135,10 +135,10 @@ const ReadMultiMessagesTests = ...@@ -135,10 +135,10 @@ const ReadMultiMessagesTests =
]; ];
for (let NFCReaderOptionTest of NFCReaderOptionTests) { for (let NFCReaderOptionTest of NFCReaderOptionTests) {
testNFCReaderOptions( testNFCScanOptions(
NFCReaderOptionTest.message, NFCReaderOptionTest.message,
NFCReaderOptionTest.readOptions, NFCReaderOptionTest.scanOptions,
NFCReaderOptionTest.unmatchedReadOptions, NFCReaderOptionTest.unmatchedScanOptions,
NFCReaderOptionTest.desc NFCReaderOptionTest.desc
); );
} }
...@@ -151,7 +151,7 @@ for (let readMultiMessagesTest of ReadMultiMessagesTests) { ...@@ -151,7 +151,7 @@ for (let readMultiMessagesTest of ReadMultiMessagesTests) {
testReadingMultiMessages( testReadingMultiMessages(
readMultiMessagesTest.message, readMultiMessagesTest.message,
readMultiMessagesTest.readOptions, readMultiMessagesTest.scanOptions,
readMultiMessagesTest.unmatchedMessage, readMultiMessagesTest.unmatchedMessage,
unmatchedCompatibility, unmatchedCompatibility,
readMultiMessagesTest.desc readMultiMessagesTest.desc
......
This is a testharness.js-based test.
Found 68 tests; 65 PASS, 3 FAIL, 0 TIMEOUT, 0 NOTRUN.
PASS idl_test setup
PASS NDEFMessage interface: existence and properties of interface object
PASS NDEFMessage interface object length
PASS NDEFMessage interface object name
PASS NDEFMessage interface: existence and properties of interface prototype object
PASS NDEFMessage interface: existence and properties of interface prototype object's "constructor" property
PASS NDEFMessage interface: existence and properties of interface prototype object's @@unscopables property
PASS NDEFMessage interface: attribute url
PASS NDEFMessage interface: attribute records
PASS NDEFRecord interface: existence and properties of interface object
PASS NDEFRecord interface object length
PASS NDEFRecord interface object name
PASS NDEFRecord interface: existence and properties of interface prototype object
PASS NDEFRecord interface: existence and properties of interface prototype object's "constructor" property
PASS NDEFRecord interface: existence and properties of interface prototype object's @@unscopables property
PASS NDEFRecord interface: attribute recordType
PASS NDEFRecord interface: attribute mediaType
PASS NDEFRecord interface: operation toText()
PASS NDEFRecord interface: operation toArrayBuffer()
PASS NDEFRecord interface: operation toJSON()
PASS NFCWriter interface: existence and properties of interface object
PASS NFCWriter interface object length
PASS NFCWriter interface object name
PASS NFCWriter interface: existence and properties of interface prototype object
PASS NFCWriter interface: existence and properties of interface prototype object's "constructor" property
PASS NFCWriter interface: existence and properties of interface prototype object's @@unscopables property
PASS NFCWriter interface: operation push(NDEFMessageSource, NFCPushOptions)
PASS NFCWriter must be primary interface of new NFCWriter();
PASS Stringification of new NFCWriter();
PASS NFCWriter interface: new NFCWriter(); must inherit property "push(NDEFMessageSource, NFCPushOptions)" with the proper type
PASS NFCWriter interface: calling push(NDEFMessageSource, NFCPushOptions) on new NFCWriter(); with too few arguments must throw TypeError
PASS NFCReader interface: existence and properties of interface object
PASS NFCReader interface object length
PASS NFCReader interface object name
PASS NFCReader interface: existence and properties of interface prototype object
PASS NFCReader interface: existence and properties of interface prototype object's "constructor" property
PASS NFCReader interface: existence and properties of interface prototype object's @@unscopables property
PASS NFCReader interface: attribute onreading
PASS NFCReader interface: attribute onerror
FAIL NFCReader interface: operation scan(NFCScanOptions) assert_own_property: interface prototype object missing non-static operation expected property "scan" missing
PASS NFCReader must be primary interface of new NFCReader();
PASS Stringification of new NFCReader();
PASS NFCReader interface: new NFCReader(); must inherit property "onreading" with the proper type
PASS NFCReader interface: new NFCReader(); must inherit property "onerror" with the proper type
FAIL NFCReader interface: new NFCReader(); must inherit property "scan(NFCScanOptions)" with the proper type assert_inherits: property "scan" not found in prototype chain
FAIL NFCReader interface: calling scan(NFCScanOptions) on new NFCReader(); with too few arguments must throw TypeError assert_inherits: property "scan" not found in prototype chain
PASS NFCReadingEvent interface: existence and properties of interface object
PASS NFCReadingEvent interface object length
PASS NFCReadingEvent interface object name
PASS NFCReadingEvent interface: existence and properties of interface prototype object
PASS NFCReadingEvent interface: existence and properties of interface prototype object's "constructor" property
PASS NFCReadingEvent interface: existence and properties of interface prototype object's @@unscopables property
PASS NFCReadingEvent interface: attribute serialNumber
PASS NFCReadingEvent interface: attribute message
PASS NFCReadingEvent must be primary interface of new NFCReadingEvent("reading", { message: {"url":"/custom/path","records":[{"recordType":"text","data":"Hello World"}]} })
PASS Stringification of new NFCReadingEvent("reading", { message: {"url":"/custom/path","records":[{"recordType":"text","data":"Hello World"}]} })
PASS NFCReadingEvent interface: new NFCReadingEvent("reading", { message: {"url":"/custom/path","records":[{"recordType":"text","data":"Hello World"}]} }) must inherit property "serialNumber" with the proper type
PASS NFCReadingEvent interface: new NFCReadingEvent("reading", { message: {"url":"/custom/path","records":[{"recordType":"text","data":"Hello World"}]} }) must inherit property "message" with the proper type
PASS NFCErrorEvent interface: existence and properties of interface object
PASS NFCErrorEvent interface object length
PASS NFCErrorEvent interface object name
PASS NFCErrorEvent interface: existence and properties of interface prototype object
PASS NFCErrorEvent interface: existence and properties of interface prototype object's "constructor" property
PASS NFCErrorEvent interface: existence and properties of interface prototype object's @@unscopables property
PASS NFCErrorEvent interface: attribute error
PASS NFCErrorEvent must be primary interface of new NFCErrorEvent("error", { error: new DOMException() });
PASS Stringification of new NFCErrorEvent("error", { error: new DOMException() });
PASS NFCErrorEvent interface: new NFCErrorEvent("error", { error: new DOMException() }); must inherit property "error" with the proper type
Harness: the test ran to completion.
...@@ -172,42 +172,46 @@ function assertWebNDEFMessagesEqual(message, expectedMessage) { ...@@ -172,42 +172,46 @@ function assertWebNDEFMessagesEqual(message, expectedMessage) {
} }
} }
function testNFCReaderOptions(message, readOptions, unmatchedReadOptions, desc) { function testNFCScanOptions(message, scanOptions, unmatchedScanOptions, desc) {
nfc_test(async (t, mockNFC) => { nfc_test(async (t, mockNFC) => {
const reader1 = new NFCReader(unmatchedReadOptions); const reader1 = new NFCReader();
const reader2 = new NFCReader(readOptions); const reader2 = new NFCReader();
const controller = new AbortController();
mockNFC.setReadingMessage(message, readOptions.compatibility); mockNFC.setReadingMessage(message, scanOptions.compatibility);
// Reading from unmatched reader will not be triggered // Reading from unmatched reader will not be triggered
reader1.onreading = t.unreached_func("reading event should not be fired."); reader1.onreading = t.unreached_func("reading event should not be fired.");
reader1.start(); unmatchedScanOptions.signal = controller.signal;
reader1.scan(unmatchedScanOptions);
const readerWatcher = new EventWatcher(t, reader2, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader2, ["reading", "error"]);
const promise = readerWatcher.wait_for("reading").then(event => { const promise = readerWatcher.wait_for("reading").then(event => {
reader1.stop(); controller.abort();
reader2.stop();
assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message)); assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message));
}); });
// NFCReader#start() asynchronously dispatches the onreading event. // NFCReader#scan() asynchronously dispatches the onreading event.
reader2.start(); scanOptions.signal = controller.signal;
reader2.scan(scanOptions);
await promise; await promise;
}, desc); }, desc);
} }
function testReadingMultiMessages(message, readOptions, unmatchedMessage, function testReadingMultiMessages(message, scanOptions, unmatchedMessage,
unmatchedCompatibility, desc) { unmatchedCompatibility, desc) {
nfc_test(async (t, mockNFC) => { nfc_test(async (t, mockNFC) => {
const reader = new NFCReader(readOptions); const reader = new NFCReader(scanOptions);
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
const promise = readerWatcher.wait_for("reading").then(event => { const promise = readerWatcher.wait_for("reading").then(event => {
reader.stop(); controller.abort();
assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message)); assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message));
}); });
// NFCReader#start() asynchronously dispatches the onreading event. // NFCReader#scan() asynchronously dispatches the onreading event.
reader.start(); scanOptions.signal = controller.signal;
reader.scan(scanOptions);
// Unmatched message will not be read // Unmatched message will not be read
mockNFC.setReadingMessage(unmatchedMessage, unmatchedCompatibility); mockNFC.setReadingMessage(unmatchedMessage, unmatchedCompatibility);
......
...@@ -5051,8 +5051,7 @@ interface NFCReader : EventTarget ...@@ -5051,8 +5051,7 @@ interface NFCReader : EventTarget
getter onerror getter onerror
getter onreading getter onreading
method constructor method constructor
method start method scan
method stop
setter onerror setter onerror
setter onreading setter onreading
interface NFCReadingEvent : Event interface NFCReadingEvent : Event
......
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