From af74d602e4138afc76e251b00580a63c843de42a Mon Sep 17 00:00:00 2001 From: Leon Han Date: Thu, 5 Sep 2019 03:51:15 -0700 Subject: [PATCH] Reland "[webnfc] Refactor/Refine impl for "Creating Web NFC message" algorithm" This is a reland of 581b273e3743fcbd168775edfeebae8b092fc706 Original change's description: > [webnfc] Refactor/Refine impl for "Creating Web NFC message" algorithm > > This CL implements strictly the "Creating Web NFC message" algorithm > described at http://w3c.github.io/web-nfc/#creating-web-nfc-message. > > 1. > Changes the Web IDL definition for NDEFRecordData from > "typedef (DOMString or unrestricted double or ArrayBuffer or Dictionary) NDEFRecordData;" > to > "typedef any NDEFRecordData;" > > 2. > Adds verification of NDEFRecordInit when creating NDEFRecord, i.e. makes > NDEFRecord ctor throw exceptions in case of invalid NDEFRecordInit. > Also, the same thing for NDEFMessage and NFCReadingEvent. > > 3. > Changes code flow for Step 10.9 of "9.10.1 The push() method" described > at https://w3c.github.io/web-nfc/#the-push-method. > " > Let output be the notation for the NDEF message to be created by UA, as > the result of passing message to create Web NFC message. If this throws > an exception, reject p with that exception and abort these steps. > " > Previously, > NDEFMessageSource (NDEFMessageInit) -Validity check-> > -Mojo TypeConverter-> Mojom NDEFMessagePtr > Now, > Remove "Validity check" and "Mojo TypeConverter" mentioned above, > instead, we reuse the process of creating NDEFMessage from > NDEFMessageSource (NDEFMessageInit), then the next step converting > NDEFMessage to Mojom NDEFMessagePtr is quite an easy job. > > BUG=520391 > > Change-Id: I628981e556f89ffdd0f883da0cfa99b20a6f8300 > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1767719 > Commit-Queue: Leon Han > Reviewed-by: Daniel Cheng > Reviewed-by: Reilly Grant > Reviewed-by: Rijubrata Bhaumik > Cr-Commit-Position: refs/heads/master@{#693539} Bug: 520391,923974 TBR: dcheng@,reillyg@ Change-Id: I608e28638c0909c9d4f675774514cd990f9878c8 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1787557 Commit-Queue: Leon Han Reviewed-by: Kentaro Hara Cr-Commit-Position: refs/heads/master@{#693638} --- web-nfc/NDEFRecord_constructor.https.html | 8 ++------ web-nfc/NFCWriter_push.https.html | 22 +++++++++------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/web-nfc/NDEFRecord_constructor.https.html b/web-nfc/NDEFRecord_constructor.https.html index 4d6ec956eacfe0..9ce41a40321743 100644 --- a/web-nfc/NDEFRecord_constructor.https.html +++ b/web-nfc/NDEFRecord_constructor.https.html @@ -12,12 +12,8 @@ }, 'NDEFRecord constructor without init dict'); test(() => { - const record = new NDEFRecord(null); - assert_equals(record.recordType.length, 0, 'empty recordType'); - assert_equals(record.mediaType.length, 0, 'empty mediaType'); - assert_equals(record.toText(), null, 'toText() returns null'); - assert_equals(record.toArrayBuffer(), null, 'toArrayBuffer() returns null'); - assert_equals(record.toJSON(), null, 'toJSON() returns null'); + assert_throws(new TypeError, () => new NDEFRecord(null), + "The record has neither type nor data."); }, 'NDEFRecord constructor with null init dict'); test(() => { diff --git a/web-nfc/NFCWriter_push.https.html b/web-nfc/NFCWriter_push.https.html index 33471a18d8e1f9..dcd23307a2e4ba 100644 --- a/web-nfc/NFCWriter_push.https.html +++ b/web-nfc/NFCWriter_push.https.html @@ -23,19 +23,15 @@ // NDEFRecord must have data. createMessage([createTextRecord()]), - // NDEFRecord.data for 'text' record must be number or string. + // NDEFRecord.data for 'text' record must be a string. createMessage([createTextRecord(test_buffer_data)]), createMessage([createTextRecord(test_json_data)]), + createMessage([createTextRecord(test_number_data)]), // https://w3c.github.io/web-nfc/#dfn-map-a-json-object-to-ndef // NDEFRecord must have data. createMessage([createJsonRecord()]), - // NDEFRecord.data for 'json' record must be object. - createMessage([createJsonRecord(test_buffer_data)]), - createMessage([createJsonRecord(test_number_data)]), - createMessage([createJsonRecord(test_text_data)]), - // https://w3c.github.io/web-nfc/#dfn-map-a-url-to-ndef // NDEFRecord must have data. createMessage([createUrlRecord()]), @@ -59,16 +55,16 @@ [ // NDEFRecord.mediaType for 'text' record must be 'text/*'. createMessage([createRecord('text', 'application/json', - test_number_data)]), + test_text_data)]), // Data for 'url' record, must be a valid URL. createMessage([createUrlRecord('Invalid URL:// Data')]), - // NDEFRecord.mediaType for 'json' record must be 'application/json' or - // starts with 'application/' and ends with '+json'. + // A JSON MIME type is any MIME type whose subtype ends in "+json" or + // whose essence is "application/json" or "text/json". createMessage([createRecord('json', 'image/png', test_json_data)]), createMessage([createRecord('json', 'application/x+y', test_json_data)]), - createMessage([createRecord('json', 'custom/app+json', test_json_data)]), + createMessage([createRecord('json', 'custom/app+jsonx', test_json_data)]), ]; const invalid_signals = [ @@ -195,8 +191,8 @@ const writer = new NFCWriter(); const message = createMessage([createRecord('json','application/json', { get x(){ return this; } })]); - await promise_rejects(t, 'SyntaxError', writer.push(message)); -}, "Reject promise with SyntaxError if 'json' record cannot be serialized."); + await promise_rejects(t, new TypeError(), writer.push(message)); +}, "Reject promise with exceptions thrown from serializing the 'json' record data."); promise_test(async t => { const writer = new NFCWriter(); @@ -251,8 +247,8 @@ const writer = new NFCWriter(); let message = createMessage([createTextRecord(test_text_data), createJsonRecord(test_json_data), + createJsonRecord(test_number_data), createOpaqueRecord(test_buffer_data), - createTextRecord(test_number_data), createUrlRecord(test_url_data)], test_message_origin); await writer.push(message);