Commit 872dd3ce authored by Leon Han's avatar Leon Han Committed by Commit Bot

[webnfc] Support NDEFWriteOptions#overwrite

To indicate whether it's allowed to overwrite an NFC tag that already
contains some records.

We do not do any special checks for the case of overwrite = true, just
write into the NFC tag in any cases.

In case of overwrite = false,

- Writing non-formatted tags that are NDEF-formatable
  --> format + write

- Writing (formatted + empty) NDEF tags
  --> write

- Writing (formatted and non-empty) NDEF tags
  --> not allowed

The spec change:
https://github.com/w3c/web-nfc/pull/373/commits/b71a81e33ba568f3a8e3cd3de8271bd45be789e8
https://github.com/w3c/web-nfc/pull/526

BUG=1023231,520391

Change-Id: Ibf271f88e6e395370f994864e966f55ef44f5d2a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2003893
Commit-Queue: Leon Han <leon.han@intel.com>
Reviewed-by: default avatarFrançois Beaufort <beaufort.francois@gmail.com>
Reviewed-by: default avatarRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#734339}
parent f934acd8
...@@ -490,6 +490,15 @@ public class NfcImpl implements Nfc { ...@@ -490,6 +490,15 @@ public class NfcImpl implements Nfc {
try { try {
mTagHandler.connect(); mTagHandler.connect();
if (!mPendingPushOperation.ndefWriteOptions.overwrite
&& !mTagHandler.canAlwaysOverwrite()) {
Log.w(TAG, "Cannot overwrite the NFC tag due to existing data on it.");
pendingPushOperationCompleted(createError(NdefErrorType.NOT_ALLOWED,
"NDEFWriteOptions#overwrite does not allow overwrite."));
return;
}
mTagHandler.write(NdefMessageUtils.toNdefMessage(mPendingPushOperation.ndefMessage)); mTagHandler.write(NdefMessageUtils.toNdefMessage(mPendingPushOperation.ndefMessage));
pendingPushOperationCompleted(null); pendingPushOperationCompleted(null);
} catch (InvalidNdefMessageException e) { } catch (InvalidNdefMessageException e) {
...@@ -523,11 +532,9 @@ public class NfcImpl implements Nfc { ...@@ -523,11 +532,9 @@ public class NfcImpl implements Nfc {
return; return;
} }
android.nfc.NdefMessage message = null;
try { try {
mTagHandler.connect(); mTagHandler.connect();
message = mTagHandler.read(); android.nfc.NdefMessage message = mTagHandler.read();
if (message == null) { if (message == null) {
// Tag is formatted to support NDEF but does not contain a message yet. // Tag is formatted to support NDEF but does not contain a message yet.
// Let's create one with no records so that watchers can be notified. // Let's create one with no records so that watchers can be notified.
......
...@@ -56,6 +56,8 @@ public class NfcTagHandler { ...@@ -56,6 +56,8 @@ public class NfcTagHandler {
throws IOException, TagLostException, FormatException, IllegalStateException; throws IOException, TagLostException, FormatException, IllegalStateException;
public NdefMessage read() public NdefMessage read()
throws IOException, TagLostException, FormatException, IllegalStateException; throws IOException, TagLostException, FormatException, IllegalStateException;
public boolean canAlwaysOverwrite()
throws IOException, TagLostException, FormatException, IllegalStateException;
} }
/** /**
...@@ -80,6 +82,13 @@ public class NfcTagHandler { ...@@ -80,6 +82,13 @@ public class NfcTagHandler {
throws IOException, TagLostException, FormatException, IllegalStateException { throws IOException, TagLostException, FormatException, IllegalStateException {
return mNdef.getNdefMessage(); return mNdef.getNdefMessage();
} }
@Override
public boolean canAlwaysOverwrite()
throws IOException, TagLostException, FormatException, IllegalStateException {
// Getting null means the tag is empty, overwrite is safe.
return mNdef.getNdefMessage() == null;
}
} }
/** /**
...@@ -103,6 +112,11 @@ public class NfcTagHandler { ...@@ -103,6 +112,11 @@ public class NfcTagHandler {
public NdefMessage read() throws FormatException { public NdefMessage read() throws FormatException {
return NdefMessageUtils.emptyNdefMessage(); return NdefMessageUtils.emptyNdefMessage();
} }
@Override
public boolean canAlwaysOverwrite() {
return true;
}
} }
protected NfcTagHandler(TagTechnology tech, TagTechnologyHandler handler, byte[] id) { protected NfcTagHandler(TagTechnology tech, TagTechnologyHandler handler, byte[] id) {
...@@ -183,4 +197,13 @@ public class NfcTagHandler { ...@@ -183,4 +197,13 @@ public class NfcTagHandler {
} }
return false; return false;
} }
/**
* Returns false only if the tag is already NDEF formatted and contains some records. Otherwise
* true.
*/
public boolean canAlwaysOverwrite()
throws IOException, TagLostException, FormatException, IllegalStateException {
return mTechHandler.canAlwaysOverwrite();
}
} }
...@@ -1279,6 +1279,7 @@ public class NFCTest { ...@@ -1279,6 +1279,7 @@ public class NFCTest {
private NdefWriteOptions createNdefWriteOptions() { private NdefWriteOptions createNdefWriteOptions() {
NdefWriteOptions pushOptions = new NdefWriteOptions(); NdefWriteOptions pushOptions = new NdefWriteOptions();
pushOptions.ignoreRead = false; pushOptions.ignoreRead = false;
pushOptions.overwrite = true;
return pushOptions; return pushOptions;
} }
......
...@@ -88,9 +88,15 @@ struct NDEFMessage { ...@@ -88,9 +88,15 @@ struct NDEFMessage {
}; };
struct NDEFWriteOptions { struct NDEFWriteOptions {
// If the property is true, the write operation will suspend active watchers // Only the case of |ignore_read| being |true| matters: while this push
// until its completion. // operation is pending, even a NFC tag comes in proximity range, do not read
// it for all active watchers.
bool ignore_read; bool ignore_read;
// Only the case of |overwrite| being |false| matters: the push operation will
// read the NFC tag regardless of |ignore_read| to determine if it has NDEF
// records on it, and if yes, do not execute write.
bool overwrite;
}; };
struct NDEFScanOptions { struct NDEFScanOptions {
......
...@@ -6,5 +6,6 @@ ...@@ -6,5 +6,6 @@
dictionary NDEFWriteOptions { dictionary NDEFWriteOptions {
boolean ignoreRead = true; boolean ignoreRead = true;
boolean overwrite = true;
AbortSignal? signal; AbortSignal? signal;
}; };
...@@ -57,9 +57,10 @@ TypeConverter<NDEFWriteOptionsPtr, const blink::NDEFWriteOptions*>::Convert( ...@@ -57,9 +57,10 @@ TypeConverter<NDEFWriteOptionsPtr, const blink::NDEFWriteOptions*>::Convert(
const blink::NDEFWriteOptions* write_options) { const blink::NDEFWriteOptions* write_options) {
// https://w3c.github.io/web-nfc/#the-ndefwriteoptions-dictionary // https://w3c.github.io/web-nfc/#the-ndefwriteoptions-dictionary
// Default values for NDEFWriteOptions dictionary are: // Default values for NDEFWriteOptions dictionary are:
// ignoreRead = true // ignoreRead = true, overwrite = true
NDEFWriteOptionsPtr write_options_ptr = NDEFWriteOptions::New(); NDEFWriteOptionsPtr write_options_ptr = NDEFWriteOptions::New();
write_options_ptr->ignore_read = write_options->ignoreRead(); write_options_ptr->ignore_read = write_options->ignoreRead();
write_options_ptr->overwrite = write_options->overwrite();
return write_options_ptr; return write_options_ptr;
} }
......
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