Commit 1d2cec0b authored by Wanming Lin's avatar Wanming Lin Committed by Commit Bot

[WebNFC] Remove NDEFPushOptions.timeout

This CL removes NDEFPushOptions.timeout relevant implementation and tests
per spec change at: https://github.com/w3c/web-nfc/pull/428/files

Bug: 520391
Change-Id: Ifea11f4a7bbe8b126d8a4a32169fcaacebfb0f44
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1894677
Commit-Queue: Wanming Lin <wanming.lin@intel.com>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarLeon Han <leon.han@intel.com>
Cr-Commit-Position: refs/heads/master@{#715542}
parent e609d761
......@@ -16,7 +16,6 @@ import android.nfc.NfcManager;
import android.nfc.Tag;
import android.nfc.TagLostException;
import android.os.Build;
import android.os.Handler;
import android.os.Process;
import android.os.Vibrator;
import android.util.SparseArray;
......@@ -111,16 +110,6 @@ public class NfcImpl implements Nfc {
*/
private final SparseArray<NdefScanOptions> mWatchers = new SparseArray<>();
/**
* Handler that runs delayed push timeout task.
*/
private final Handler mPushTimeoutHandler = new Handler();
/**
* Runnable responsible for cancelling push operation after specified timeout.
*/
private Runnable mPushTimeoutRunnable;
/**
* Vibrator. @see android.os.Vibrator
*/
......@@ -187,7 +176,7 @@ public class NfcImpl implements Nfc {
* passive NFC devices are supported (NdefPushTarget.TAG).
*
* @param message that should be pushed to NFC device.
* @param options that contain information about timeout and target device type.
* @param options that contain information about target device type.
* @param callback that is used to notify when push operation is completed.
*/
@Override
......@@ -200,8 +189,7 @@ public class NfcImpl implements Nfc {
}
// Check NdefPushOptions that are not supported by Android platform.
if (options.target == NdefPushTarget.PEER || options.timeout < 0
|| (options.timeout > Long.MAX_VALUE && !Double.isInfinite(options.timeout))) {
if (options.target == NdefPushTarget.PEER) {
callback.call(createError(NdefErrorType.NOT_SUPPORTED));
return;
}
......@@ -209,13 +197,10 @@ public class NfcImpl implements Nfc {
// If previous pending push operation is not completed, cancel it.
if (mPendingPushOperation != null) {
mPendingPushOperation.complete(createError(NdefErrorType.OPERATION_CANCELLED));
cancelPushTimeoutTask();
}
mPendingPushOperation = new PendingPushOperation(message, options, callback);
// Schedule push timeout task for new #mPendingPushOperation.
schedulePushTimeoutTask(options);
enableReaderModeIfNeeded();
processPendingPushOperation();
}
......@@ -481,8 +466,8 @@ public class NfcImpl implements Nfc {
}
/**
* Handles completion of pending push operation, cancels timeout task and completes push
* operation. On error, invalidates #mTagHandler.
* Handles completion of pending push operation, completes push operation.
* On error, invalidates #mTagHandler.
*/
private void pendingPushOperationCompleted(NdefError error) {
completePendingPushOperation(error);
......@@ -495,7 +480,6 @@ public class NfcImpl implements Nfc {
private void completePendingPushOperation(NdefError error) {
if (mPendingPushOperation == null) return;
cancelPushTimeoutTask();
mPendingPushOperation.complete(error);
mPendingPushOperation = null;
disableReaderModeIfNeeded();
......@@ -683,33 +667,4 @@ public class NfcImpl implements Nfc {
}
}
}
/**
* Schedules task that is executed after timeout and cancels pending push operation.
*/
private void schedulePushTimeoutTask(NdefPushOptions options) {
assert mPushTimeoutRunnable == null;
// Default timeout value.
if (Double.isInfinite(options.timeout)) return;
// Create and schedule timeout.
mPushTimeoutRunnable = new Runnable() {
@Override
public void run() {
completePendingPushOperation(createError(NdefErrorType.TIMER_EXPIRED));
}
};
mPushTimeoutHandler.postDelayed(mPushTimeoutRunnable, (long) options.timeout);
}
/**
* Cancels push timeout task.
*/
void cancelPushTimeoutTask() {
if (mPushTimeoutRunnable == null) return;
mPushTimeoutHandler.removeCallbacks(mPushTimeoutRunnable);
mPushTimeoutRunnable = null;
}
}
......@@ -15,7 +15,6 @@ enum NDEFErrorType {
NOT_FOUND,
INVALID_MESSAGE,
OPERATION_CANCELLED,
TIMER_EXPIRED,
CANNOT_CANCEL,
// Transfer data error.
IO_ERROR
......@@ -79,9 +78,6 @@ struct NDEFPushOptions {
// The target of the push operation.
NDEFPushTarget target;
// The timeout for the push operation, in milliseconds.
double timeout;
// If the property is true, the push operation will suspend active watchers
// until its completion.
bool ignore_read;
......@@ -104,9 +100,7 @@ interface NFC {
SetClient(pending_remote<NFCClient> client);
// Pushes data to NFC device.
// NDEFPushOptions specify timeout and type of device where data should be
// pushed. If timeout is defined and data is not pushed before timeout is
// expired, callback with corresponding error is called.
// NDEFPushOptions specify type of device where data should be pushed.
Push(NDEFMessage message, NDEFPushOptions? options) => (NDEFError? error);
// Cancels pending push request.
......
......@@ -8,7 +8,6 @@ enum NDEFPushTarget { "tag", "peer", "any" };
dictionary NDEFPushOptions {
NDEFPushTarget target = "any";
unrestricted double timeout; // in ms
boolean ignoreRead = true;
AbortSignal? signal;
};
......@@ -58,18 +58,7 @@ ScriptPromise NDEFWriter::push(ScriptState* script_state,
"The NFC operation was cancelled."));
}
// 9. If timeout value is NaN or negative, reject promise with "TypeError"
// and abort these steps.
if (options->hasTimeout() &&
(std::isnan(options->timeout()) || options->timeout() < 0)) {
return ScriptPromise::Reject(
script_state,
V8ThrowException::CreateTypeError(
script_state->GetIsolate(),
"Invalid NDEFPushOptions.timeout value was provided."));
}
// Step 10.8: Run "create Web NFC message", if this throws an exception,
// Step 10.10.1: Run "create NDEF message", if this throws an exception,
// reject p with that exception and abort these steps.
NDEFMessage* ndef_message =
NDEFMessage::Create(execution_context, push_message, exception_state);
......
......@@ -58,16 +58,11 @@ TypeConverter<NDEFPushOptionsPtr, const blink::NDEFPushOptions*>::Convert(
const blink::NDEFPushOptions* pushOptions) {
// https://w3c.github.io/web-nfc/#the-ndefpushoptions-dictionary
// Default values for NDEFPushOptions dictionary are:
// target = 'any', timeout = Infinity, ignoreRead = true
// target = 'any', ignoreRead = true
NDEFPushOptionsPtr pushOptionsPtr = NDEFPushOptions::New();
pushOptionsPtr->target = blink::StringToNDEFPushTarget(pushOptions->target());
pushOptionsPtr->ignore_read = pushOptions->ignoreRead();
if (pushOptions->hasTimeout())
pushOptionsPtr->timeout = pushOptions->timeout();
else
pushOptionsPtr->timeout = std::numeric_limits<double>::infinity();
return pushOptionsPtr;
}
......
......@@ -71,9 +71,6 @@ DOMException* NDEFErrorTypeToDOMException(
case device::mojom::blink::NDEFErrorType::OPERATION_CANCELLED:
return MakeGarbageCollected<DOMException>(
DOMExceptionCode::kAbortError, "The NFC operation was cancelled.");
case device::mojom::blink::NDEFErrorType::TIMER_EXPIRED:
return MakeGarbageCollected<DOMException>(DOMExceptionCode::kTimeoutError,
"NFC operation has timed out.");
case device::mojom::blink::NDEFErrorType::CANNOT_CANCEL:
return MakeGarbageCollected<DOMException>(
DOMExceptionCode::kNoModificationAllowedError,
......
......@@ -79,11 +79,6 @@ function assertNDEFPushOptionsEqual(provided, received) {
else
assert_equals(!!received.ignore_read, true);
if (provided.timeout !== undefined)
assert_equals(provided.timeout, received.timeout);
else
assert_equals(received.timeout, Infinity);
if (provided.target !== undefined)
assert_equals(toMojoNDEFPushTarget(provided.target), received.target);
else
......@@ -176,7 +171,6 @@ var WebNFCTest = (() => {
this.push_options_ = null;
this.pending_promise_func_ = null;
this.push_completed_ = true;
this.push_should_timeout_ = false;
this.client_ = null;
this.watchers_ = [];
this.reading_messages_ = [];
......@@ -202,13 +196,8 @@ var WebNFCTest = (() => {
this.pending_promise_func_ = resolve;
if (this.operations_suspended_) {
// Pends push operation if NFC operation is suspended.
} else if (options.timeout && options.timeout !== Infinity &&
!this.push_completed_) {
// Resolves with TimeoutError, else pend push operation.
if (this.push_should_timeout_) {
resolve(
createNDEFError(device.mojom.NDEFErrorType.TIMER_EXPIRED));
}
} else if (!this.push_completed_) {
// Leaves the push operating pending.
} else if (!this.is_ndef_tech_) {
// Resolves with NotSupportedError if the device does not expose
// NDEF technology.
......@@ -329,7 +318,6 @@ var WebNFCTest = (() => {
this.pushed_message_ = null;
this.push_options_ = null;
this.pending_promise_func_ = null;
this.push_should_timeout_ = false;
this.push_completed_ = true;
}
......@@ -353,10 +341,6 @@ var WebNFCTest = (() => {
}
}
setPushShouldTimeout(result) {
this.push_should_timeout_ = result;
}
// Suspends all pending NFC operations. Could be used when web page
// visibility is lost.
suspendNFCOperations() {
......
......@@ -7,9 +7,6 @@ PASS NDEFWriter.push should fail if signal is not an AbortSignal.
PASS Synchronously signaled abort.
PASS NDEFWriter.push should fail when NFC HW is disabled.
PASS NDEFWriter.push should fail when NFC HW is not supported.
PASS NDEFWriter.push should fail with TypeError when invalid timeout is provided.
PASS NDEFWriter.push should fail with TypeError when invalid negative timeout value is provided.
PASS NDEFWriter.push should fail with TimeoutError when timer expires.
PASS Reject promise with NotSupportedError if NFC message size exceeds 32KB.
PASS Reject promise with SyntaxError if WebNFC Id cannot be created from provided URL.
PASS NDEFWriter.push should fail with TypeError when invalid target value is provided.
......
......@@ -133,8 +133,7 @@ nfc_test(async (t, mockNFC) => {
//Make sure push is pending
mockNFC.setPendingPushCompleted(false);
const p = writer.push(test_text_data,
{ signal: controller.signal, timeout: 100 });
const p = writer.push(test_text_data, { signal: controller.signal });
const rejected = promise_rejects(t, 'AbortError', p);
let callback_called = false;
await new Promise(resolve => {
......@@ -173,8 +172,7 @@ nfc_test(async (t, mockNFC) => {
const writer2 = new NDEFWriter();
const controller = new AbortController();
mockNFC.setPendingPushCompleted(false);
const p1 = writer1.push(test_text_data,
{ signal: controller.signal, timeout: 100 });
const p1 = writer1.push(test_text_data, { signal: controller.signal });
// Even though push request is grantable,
// this abort should be processed synchronously.
......@@ -197,28 +195,6 @@ nfc_test(async (t, mockNFC) => {
await promise_rejects(t, 'NotSupportedError', writer.push(test_text_data));
}, "NDEFWriter.push should fail when NFC HW is not supported.");
promise_test(async t => {
const writer = new NDEFWriter();
await promise_rejects(
t, new TypeError(), writer.push(test_text_data, { timeout: "invalid"}));
}, "NDEFWriter.push should fail with TypeError when invalid timeout is \
provided.");
promise_test(async t => {
const writer = new NDEFWriter();
await promise_rejects(
t, new TypeError(), writer.push(test_text_data, { timeout: -1 }));
}, "NDEFWriter.push should fail with TypeError when invalid negative timeout \
value is provided.");
nfc_test(async (t, mockNFC) => {
const writer = new NDEFWriter();
mockNFC.setPendingPushCompleted(false);
const p = writer.push(test_text_data, { timeout: 1 });
mockNFC.setPushShouldTimeout(true);
await promise_rejects(t, 'TimeoutError', p);
}, "NDEFWriter.push should fail with TimeoutError when timer expires.");
promise_test(async t => {
const writer = new NDEFWriter();
await promise_rejects(
......@@ -279,7 +255,7 @@ promise_test(async () => {
nfc_test(async () => {
const writer = new NDEFWriter();
await writer.push(test_text_data, { timeout: 1 });
await writer.push(test_text_data);
}, 'NDEFWriter.push should succeed when NFC HW is enabled');
nfc_test(async (t, mockNFC) => {
......@@ -324,13 +300,13 @@ nfc_test(async () => {
nfc_test(async (t, mockNFC) => {
const writer = new NDEFWriter();
await writer.push(test_text_data);
assertNDEFPushOptionsEqual(createNDEFPushOptions('any', Infinity, true),
assertNDEFPushOptionsEqual(createNDEFPushOptions('any', true),
mockNFC.pushOptions());
}, "Check that default NDEFPushOptions values are correctly set.");
nfc_test(async (t, mockNFC) => {
const writer = new NDEFWriter();
let ndefPushOptions = createNDEFPushOptions('tag', 1, false);
let ndefPushOptions = createNDEFPushOptions('tag', false);
await writer.push(test_text_data, ndefPushOptions);
assertNDEFPushOptionsEqual(ndefPushOptions, mockNFC.pushOptions());
}, "Check that provided NDEFPushOptions values are correctly converted.");
......@@ -371,8 +347,8 @@ nfc_test(async (t, mockNFC) => {
const writer1 = new NDEFWriter();
const writer2 = new NDEFWriter();
const ndefPushOptions1 = createNDEFPushOptions('tag', 1000, false);
const ndefPushOptions2 = createNDEFPushOptions('tag', Infinity, true);
const ndefPushOptions1 = createNDEFPushOptions('tag', false);
const ndefPushOptions2 = createNDEFPushOptions('tag', true);
const p1 = writer1.push(test_text_data, ndefPushOptions1);
const p2 = writer2.push(test_url_data, ndefPushOptions2);
......
......@@ -14,7 +14,6 @@ The `WebNFCTest` interface is defined as:
setHWStatus(number status); // Sets the hardware status.
setReadingMessage(NDEFMessageInit message); // Sets message that is used to deliver NFC reading updates.
setPendingPushCompleted(boolean result); // Sets if the pending push is completed.
setPushShouldTimeout(boolean result); // Sets flag to trigger the pending push to timeout.
pushedMessage(); // Gets the pushed `NDEFMessageSource`.
pushOptions(); // Gets the pushed `NDEFPushOptions`.
setIsNDEFTech(boolean isNDEF); // Sets if the NFC device exposes NDEF technology.
......
......@@ -124,8 +124,8 @@ function createUrlRecord(url, isAbsUrl) {
return createRecord('url', url);
}
function createNDEFPushOptions(target, timeout, ignoreRead) {
return {target, timeout, ignoreRead};
function createNDEFPushOptions(target, ignoreRead) {
return {target, ignoreRead};
}
// Compares NDEFMessageSource that was provided to the API
......
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