Commit 5f758d23 authored by alexander.shalamov's avatar alexander.shalamov Committed by Commit bot

[webnfc] Nfc.push method must reject promise with TypeError

According to latest changes in specification [1,2], when argument
of invalid type is provided to nfc.push function, promise must be
rejected with TypeError, instead of SyntaxError.

[1] https://w3c.github.io/web-nfc/#dom-nfc-push
[2] https://github.com/w3c/web-nfc/issues/110

BUG=714034

Review-Url: https://codereview.chromium.org/2836063004
Cr-Commit-Position: refs/heads/master@{#467258}
parent ea157e49
......@@ -9,6 +9,9 @@
const invalid_messages =
[
// Invalid NFCPushMessage type
undefined,
// NFCMessage.data: should have at least 1 valid record.
// https://w3c.github.io/web-nfc/#the-push-method - Step 8.
createMessage([{}]),
......@@ -55,19 +58,14 @@ const invalid_messages =
createMessage([createOpaqueRecord(test_json_data)])
];
nfc_test(nfc => {
return assertRejectsWithError(navigator.nfc.push(undefined),
'TypeMismatchError');
}, 'Test that passing undefined to nfc.push would raise TypeMismatchError.');
nfc_test(nfc => {
let promises = [];
invalid_messages.forEach(message => {
promises.push(
assertRejectsWithError(navigator.nfc.push(message), 'SyntaxError'));
assertRejectsWithError(navigator.nfc.push(message), 'TypeError'));
});
return Promise.all(promises)
}, 'Test that promise is rejected with SyntaxError if NFCMessage is invalid.');
}, 'Test that promise is rejected with TypeError if NFCMessage is invalid.');
nfc_test(nfc => {
nfc.mockNFC.setHWStatus(NFCHWStatus.DISABLED);
......
......@@ -431,33 +431,34 @@ bool IsValidNFCRecord(const NFCRecord& record) {
return false;
}
DOMException* IsValidNFCRecordArray(const HeapVector<NFCRecord>& records) {
// https://w3c.github.io/web-nfc/#the-push-method
// If NFCMessage.data is empty, reject promise with SyntaxError
bool IsValidNFCRecordArray(const HeapVector<NFCRecord>& records) {
if (records.IsEmpty())
return DOMException::Create(kSyntaxError);
return false;
for (const auto& record : records) {
if (!IsValidNFCRecord(record))
return DOMException::Create(kSyntaxError);
return false;
}
return nullptr;
return true;
}
DOMException* IsValidNFCPushMessage(const NFCPushMessage& message) {
bool IsValidNFCPushMessage(const NFCPushMessage& message) {
// If NFCPushMessage of invalid type, reject promise with TypeError
if (!message.isNFCMessage() && !message.isString() &&
!message.isArrayBuffer())
return DOMException::Create(kTypeMismatchError);
return false;
if (message.isNFCMessage()) {
// https://w3c.github.io/web-nfc/#the-push-method
// If NFCMessage.data is empty, reject promise with TypeError
if (!message.getAsNFCMessage().hasData())
return DOMException::Create(kTypeMismatchError);
return false;
return IsValidNFCRecordArray(message.getAsNFCMessage().data());
}
return nullptr;
return true;
}
bool SetURL(const String& origin,
......@@ -614,9 +615,12 @@ ScriptPromise NFC::push(ScriptState* script_state,
if (!promise.IsEmpty())
return promise;
DOMException* exception = IsValidNFCPushMessage(push_message);
if (exception)
return ScriptPromise::RejectWithDOMException(script_state, exception);
if (!IsValidNFCPushMessage(push_message)) {
return ScriptPromise::Reject(
script_state, V8ThrowException::CreateTypeError(
script_state->GetIsolate(),
"Invalid NFCPushMessage type was provided."));
}
device::nfc::mojom::blink::NFCMessagePtr message =
device::nfc::mojom::blink::NFCMessage::From(push_message);
......
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