Commit c3012c34 authored by Leon Han's avatar Leon Han Committed by Commit Bot

[webnfc] Add BufferSource into the union type NDEFMessageSource

This CL updates the definition of the union type NDEFMessageSource:
  (DOMString or ArrayBuffer or NDEFMessageInit)
==>
  (DOMString or BufferSource or NDEFMessageInit)

BUG=520391

Change-Id: I603616047814c56744eed94efd7f3579079b0875
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1886351Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Commit-Queue: Leon Han <leon.han@intel.com>
Cr-Commit-Position: refs/heads/master@{#711986}
parent 7d9601be
...@@ -84,8 +84,8 @@ bindings_modules_generated_union_type_files = [ ...@@ -84,8 +84,8 @@ bindings_modules_generated_union_type_files = [
"$bindings_modules_v8_output_dir/request_or_usv_string_or_request_or_usv_string_sequence.h", "$bindings_modules_v8_output_dir/request_or_usv_string_or_request_or_usv_string_sequence.h",
"$bindings_modules_v8_output_dir/rtc_ice_candidate_init_or_rtc_ice_candidate.cc", "$bindings_modules_v8_output_dir/rtc_ice_candidate_init_or_rtc_ice_candidate.cc",
"$bindings_modules_v8_output_dir/rtc_ice_candidate_init_or_rtc_ice_candidate.h", "$bindings_modules_v8_output_dir/rtc_ice_candidate_init_or_rtc_ice_candidate.h",
"$bindings_modules_v8_output_dir/string_or_array_buffer_or_ndef_message_init.cc", "$bindings_modules_v8_output_dir/string_or_array_buffer_or_array_buffer_view_or_ndef_message_init.cc",
"$bindings_modules_v8_output_dir/string_or_array_buffer_or_ndef_message_init.h", "$bindings_modules_v8_output_dir/string_or_array_buffer_or_array_buffer_view_or_ndef_message_init.h",
"$bindings_modules_v8_output_dir/string_or_canvas_gradient_or_canvas_pattern.cc", "$bindings_modules_v8_output_dir/string_or_canvas_gradient_or_canvas_pattern.cc",
"$bindings_modules_v8_output_dir/string_or_canvas_gradient_or_canvas_pattern.h", "$bindings_modules_v8_output_dir/string_or_canvas_gradient_or_canvas_pattern.h",
"$bindings_modules_v8_output_dir/string_or_string_sequence_or_constrain_dom_string_parameters.cc", "$bindings_modules_v8_output_dir/string_or_string_sequence_or_constrain_dom_string_parameters.cc",
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include "third_party/blink/renderer/modules/nfc/ndef_message.h" #include "third_party/blink/renderer/modules/nfc/ndef_message.h"
#include "services/device/public/mojom/nfc.mojom-blink.h" #include "services/device/public/mojom/nfc.mojom-blink.h"
#include "third_party/blink/renderer/bindings/modules/v8/string_or_array_buffer_or_ndef_message_init.h" #include "third_party/blink/renderer/bindings/modules/v8/string_or_array_buffer_or_array_buffer_view_or_ndef_message_init.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/nfc/ndef_message_init.h" #include "third_party/blink/renderer/modules/nfc/ndef_message_init.h"
#include "third_party/blink/renderer/modules/nfc/ndef_record.h" #include "third_party/blink/renderer/modules/nfc/ndef_record.h"
...@@ -44,9 +44,25 @@ NDEFMessage* NDEFMessage::Create(const ExecutionContext* execution_context, ...@@ -44,9 +44,25 @@ NDEFMessage* NDEFMessage::Create(const ExecutionContext* execution_context,
} }
if (source.IsArrayBuffer()) { if (source.IsArrayBuffer()) {
WTF::Vector<uint8_t> payload_data;
payload_data.Append(
static_cast<uint8_t*>(source.GetAsArrayBuffer()->Data()),
source.GetAsArrayBuffer()->ByteLength());
NDEFMessage* message = MakeGarbageCollected<NDEFMessage>(); NDEFMessage* message = MakeGarbageCollected<NDEFMessage>();
message->records_.push_back( message->records_.push_back(
MakeGarbageCollected<NDEFRecord>(source.GetAsArrayBuffer())); MakeGarbageCollected<NDEFRecord>(std::move(payload_data)));
return message;
}
if (source.IsArrayBufferView()) {
WTF::Vector<uint8_t> payload_data;
payload_data.Append(
static_cast<uint8_t*>(
source.GetAsArrayBufferView().View()->BaseAddress()),
source.GetAsArrayBufferView().View()->byteLength());
NDEFMessage* message = MakeGarbageCollected<NDEFMessage>();
message->records_.push_back(
MakeGarbageCollected<NDEFRecord>(std::move(payload_data)));
return message; return message;
} }
......
...@@ -17,9 +17,9 @@ class ExceptionState; ...@@ -17,9 +17,9 @@ class ExceptionState;
class ExecutionContext; class ExecutionContext;
class NDEFMessageInit; class NDEFMessageInit;
class NDEFRecord; class NDEFRecord;
class StringOrArrayBufferOrNDEFMessageInit; class StringOrArrayBufferOrArrayBufferViewOrNDEFMessageInit;
using NDEFMessageSource = StringOrArrayBufferOrNDEFMessageInit; using NDEFMessageSource = StringOrArrayBufferOrArrayBufferViewOrNDEFMessageInit;
class MODULES_EXPORT NDEFMessage final : public ScriptWrappable { class MODULES_EXPORT NDEFMessage final : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
......
...@@ -350,11 +350,10 @@ NDEFRecord::NDEFRecord(const ExecutionContext* execution_context, ...@@ -350,11 +350,10 @@ NDEFRecord::NDEFRecord(const ExecutionContext* execution_context,
lang_(getDocumentLanguage(execution_context)), lang_(getDocumentLanguage(execution_context)),
payload_data_(GetUTF8DataFromString(text)) {} payload_data_(GetUTF8DataFromString(text)) {}
NDEFRecord::NDEFRecord(DOMArrayBuffer* array_buffer) NDEFRecord::NDEFRecord(WTF::Vector<uint8_t> payload_data)
: record_type_("opaque"), media_type_("application/octet-stream") { : record_type_("opaque"),
payload_data_.Append(static_cast<uint8_t*>(array_buffer->Data()), media_type_("application/octet-stream"),
array_buffer->ByteLength()); payload_data_(std::move(payload_data)) {}
}
NDEFRecord::NDEFRecord(const device::mojom::blink::NDEFRecord& record) NDEFRecord::NDEFRecord(const device::mojom::blink::NDEFRecord& record)
: record_type_(record.record_type), : record_type_(record.record_type),
......
...@@ -33,8 +33,8 @@ class MODULES_EXPORT NDEFRecord final : public ScriptWrappable { ...@@ -33,8 +33,8 @@ class MODULES_EXPORT NDEFRecord final : public ScriptWrappable {
// Construct a "text" record from a string. // Construct a "text" record from a string.
explicit NDEFRecord(const ExecutionContext*, const String&); explicit NDEFRecord(const ExecutionContext*, const String&);
// Construct a "opaque" record from an array buffer. // Construct a "opaque" record from the raw payload bytes.
explicit NDEFRecord(DOMArrayBuffer*); explicit NDEFRecord(WTF::Vector<uint8_t> payload_data);
NDEFRecord(const String&, const String&, WTF::Vector<uint8_t>); NDEFRecord(const String&, const String&, WTF::Vector<uint8_t>);
NDEFRecord(const String&, NDEFRecord(const String&,
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include "mojo/public/cpp/bindings/callback_helpers.h" #include "mojo/public/cpp/bindings/callback_helpers.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h" #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/modules/v8/string_or_array_buffer_or_ndef_message_init.h" #include "third_party/blink/renderer/bindings/modules/v8/string_or_array_buffer_or_array_buffer_view_or_ndef_message_init.h"
#include "third_party/blink/renderer/core/dom/abort_signal.h" #include "third_party/blink/renderer/core/dom/abort_signal.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h" #include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/modules/nfc/ndef_push_options.h" #include "third_party/blink/renderer/modules/nfc/ndef_push_options.h"
......
...@@ -17,9 +17,9 @@ class NDEFPushOptions; ...@@ -17,9 +17,9 @@ class NDEFPushOptions;
class ExceptionState; class ExceptionState;
class ExecutionContext; class ExecutionContext;
class ScriptPromise; class ScriptPromise;
class StringOrArrayBufferOrNDEFMessageInit; class StringOrArrayBufferOrArrayBufferViewOrNDEFMessageInit;
using NDEFMessageSource = StringOrArrayBufferOrNDEFMessageInit; using NDEFMessageSource = StringOrArrayBufferOrArrayBufferViewOrNDEFMessageInit;
class NDEFWriter : public ScriptWrappable, public ContextClient { class NDEFWriter : public ScriptWrappable, public ContextClient {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
......
...@@ -4,7 +4,11 @@ ...@@ -4,7 +4,11 @@
// https://w3c.github.io/web-nfc/#dom-ndefmessagesource // https://w3c.github.io/web-nfc/#dom-ndefmessagesource
typedef (DOMString or ArrayBuffer or NDEFMessageInit) NDEFMessageSource; // The bindings generator does not support "typedef in union".
// Must be (DOMString or BufferSource or NDEFMessageInit).
// TODO(http://crbug.com/1019126): Support nested typedef in Web IDLs.
typedef (DOMString or ArrayBuffer or ArrayBufferView or NDEFMessageInit)
NDEFMessageSource;
// https://w3c.github.io/web-nfc/#dom-nfcwriter // https://w3c.github.io/web-nfc/#dom-nfcwriter
...@@ -15,5 +19,7 @@ typedef (DOMString or ArrayBuffer or NDEFMessageInit) NDEFMessageSource; ...@@ -15,5 +19,7 @@ typedef (DOMString or ArrayBuffer or NDEFMessageInit) NDEFMessageSource;
ConstructorCallWith=ExecutionContext, ConstructorCallWith=ExecutionContext,
Exposed=Window Exposed=Window
] interface NDEFWriter { ] interface NDEFWriter {
[CallWith=ScriptState, RaisesException] Promise<void> push (NDEFMessageSource message, optional NDEFPushOptions options); [CallWith=ScriptState, RaisesException] Promise<void> push(
NDEFMessageSource message,
optional NDEFPushOptions options);
}; };
...@@ -31,14 +31,16 @@ function toMojoNDEFRecord(record) { ...@@ -31,14 +31,16 @@ function toMojoNDEFRecord(record) {
return nfcRecord; return nfcRecord;
} }
// Converts JS objects to byte array.
function toByteArray(data) { function toByteArray(data) {
// Converts JS objects to byte array. if (data instanceof ArrayBuffer)
return new Uint8Array(data);
else if (ArrayBuffer.isView(data))
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
let byteArray = new Uint8Array(0); let byteArray = new Uint8Array(0);
let tmpData = data; let tmpData = data;
if (typeof tmpData === 'object' || typeof tmpData === 'number')
if (tmpData instanceof ArrayBuffer)
byteArray = new Uint8Array(tmpData);
else if (typeof tmpData === 'object' || typeof tmpData === 'number')
tmpData = JSON.stringify(tmpData); tmpData = JSON.stringify(tmpData);
if (typeof tmpData === 'string') if (typeof tmpData === 'string')
......
...@@ -19,6 +19,7 @@ PASS NDEFWriter.push should succeed when NFC HW is enabled ...@@ -19,6 +19,7 @@ PASS NDEFWriter.push should succeed when NFC HW is enabled
PASS NDEFWriter.push NDEFMessage containing text, json, opaque, url, absolute-url and external records with default NDEFPushOptions. PASS NDEFWriter.push NDEFMessage containing text, json, opaque, url, absolute-url and external records with default NDEFPushOptions.
PASS Test that NDEFWriter.push succeeds when message is DOMString. PASS Test that NDEFWriter.push succeeds when message is DOMString.
PASS Test that NDEFWriter.push succeeds when message is ArrayBuffer. PASS Test that NDEFWriter.push succeeds when message is ArrayBuffer.
PASS Test that NDEFWriter.push succeeds when message is ArrayBufferView.
PASS NDEFWriter.push with 'empty' record should succeed. PASS NDEFWriter.push with 'empty' record should succeed.
PASS Check that default NDEFPushOptions values are correctly set. PASS Check that default NDEFPushOptions values are correctly set.
PASS Check that provided NDEFPushOptions values are correctly converted. PASS Check that provided NDEFPushOptions values are correctly converted.
......
...@@ -322,6 +322,13 @@ nfc_test(async (t, mockNFC) => { ...@@ -322,6 +322,13 @@ nfc_test(async (t, mockNFC) => {
assertNDEFMessagesEqual(test_buffer_data, mockNFC.pushedMessage()); assertNDEFMessagesEqual(test_buffer_data, mockNFC.pushedMessage());
}, "Test that NDEFWriter.push succeeds when message is ArrayBuffer."); }, "Test that NDEFWriter.push succeeds when message is ArrayBuffer.");
nfc_test(async (t, mockNFC) => {
let buffer_view = new Uint8Array(test_buffer_data, 2, 5);
const writer = new NDEFWriter();
await writer.push(buffer_view);
assertNDEFMessagesEqual(buffer_view, mockNFC.pushedMessage());
}, "Test that NDEFWriter.push succeeds when message is ArrayBufferView.");
nfc_test(async () => { nfc_test(async () => {
const writer = new NDEFWriter(); const writer = new NDEFWriter();
await writer.push(createMessage([createRecord('empty')])); await writer.push(createMessage([createRecord('empty')]));
......
...@@ -64,8 +64,8 @@ const test_json_data = {level: 1, score: 100, label: 'Game'}; ...@@ -64,8 +64,8 @@ const test_json_data = {level: 1, score: 100, label: 'Game'};
const test_url_data = 'https://w3c.github.io/web-nfc/'; const test_url_data = 'https://w3c.github.io/web-nfc/';
const test_message_origin = 'https://127.0.0.1:8443'; const test_message_origin = 'https://127.0.0.1:8443';
const test_buffer_data = new ArrayBuffer(test_text_byte_array.length); const test_buffer_data = new ArrayBuffer(test_text_byte_array.length);
const test_buffer_view = const test_buffer_view = new Uint8Array(test_buffer_data);
new Uint8Array(test_buffer_data).set(test_text_byte_array); test_buffer_view.set(test_text_byte_array);
const fake_tag_serial_number = 'c0:45:00:02'; const fake_tag_serial_number = 'c0:45:00:02';
const NFCHWStatus = {}; const NFCHWStatus = {};
...@@ -126,11 +126,12 @@ function createNDEFPushOptions(target, timeout, ignoreRead) { ...@@ -126,11 +126,12 @@ function createNDEFPushOptions(target, timeout, ignoreRead) {
// (e.g. NDEFWriter.push), and NDEFMessage that was received by the // (e.g. NDEFWriter.push), and NDEFMessage that was received by the
// mock NFC service. // mock NFC service.
function assertNDEFMessagesEqual(providedMessage, receivedMessage) { function assertNDEFMessagesEqual(providedMessage, receivedMessage) {
// If simple data type is passed, e.g. String or ArrayBuffer, convert it // If simple data type is passed, e.g. String or ArrayBuffer or
// to NDEFMessage before comparing. // ArrayBufferView, convert it to NDEFMessage before comparing.
// https://w3c.github.io/web-nfc/#dom-ndefmessagesource // https://w3c.github.io/web-nfc/#dom-ndefmessagesource
let provided = providedMessage; let provided = providedMessage;
if (providedMessage instanceof ArrayBuffer) if (providedMessage instanceof ArrayBuffer ||
ArrayBuffer.isView(providedMessage))
provided = createMessage([createOpaqueRecord(providedMessage)]); provided = createMessage([createOpaqueRecord(providedMessage)]);
else if (typeof providedMessage === 'string') else if (typeof providedMessage === 'string')
provided = createMessage([createTextRecord(providedMessage)]); provided = createMessage([createTextRecord(providedMessage)]);
......
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