Commit 00d311a0 authored by Leon Han's avatar Leon Han Committed by Commit Bot

[webnfc] Do not use 'any' to define NDEFRecordInit#data

Use the following union type instead of 'any' to define
NDEFRecordInit#data.
"
typedef (DOMString or BufferSource or NDEFMessageInit)
  NDEFRecordDataSource
"

The spec change:
https://github.com/w3c/web-nfc/pull/454

BUG=520391

Change-Id: Ic917be001d5e3502caeea29ff4a3401ff7197298
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1941083Reviewed-by: default avatarRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Commit-Queue: Leon Han <leon.han@intel.com>
Cr-Commit-Position: refs/heads/master@{#720803}
parent d34e05f2
......@@ -7,7 +7,7 @@
#include "services/device/public/mojom/nfc.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_array_buffer.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_array_buffer_view.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.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/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
......@@ -16,7 +16,6 @@
#include "third_party/blink/renderer/modules/nfc/ndef_record_init.h"
#include "third_party/blink/renderer/modules/nfc/nfc_utils.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/network/http_parsers.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
......@@ -25,6 +24,9 @@
namespace blink {
using NDEFRecordDataSource =
StringOrArrayBufferOrArrayBufferViewOrNDEFMessageInit;
namespace {
WTF::Vector<uint8_t> GetUTF8DataFromString(const String& string) {
......@@ -34,22 +36,21 @@ WTF::Vector<uint8_t> GetUTF8DataFromString(const String& string) {
return data;
}
bool IsBufferSource(const ScriptValue& data) {
return !data.IsEmpty() && (data.V8Value()->IsArrayBuffer() ||
data.V8Value()->IsArrayBufferView());
bool IsBufferSource(const NDEFRecordDataSource& data) {
return data.IsArrayBuffer() || data.IsArrayBufferView();
}
WTF::Vector<uint8_t> GetBytesOfBufferSource(const ScriptValue& buffer_source) {
WTF::Vector<uint8_t> GetBytesOfBufferSource(
const NDEFRecordDataSource& buffer_source) {
DCHECK(IsBufferSource(buffer_source));
WTF::Vector<uint8_t> bytes;
if (buffer_source.V8Value()->IsArrayBuffer()) {
DOMArrayBuffer* array_buffer =
V8ArrayBuffer::ToImpl(buffer_source.V8Value().As<v8::Object>());
if (buffer_source.IsArrayBuffer()) {
DOMArrayBuffer* array_buffer = buffer_source.GetAsArrayBuffer();
bytes.Append(static_cast<uint8_t*>(array_buffer->Data()),
array_buffer->DeprecatedByteLengthAsUnsigned());
} else if (buffer_source.V8Value()->IsArrayBufferView()) {
DOMArrayBufferView* array_buffer_view =
V8ArrayBufferView::ToImpl(buffer_source.V8Value().As<v8::Object>());
} else if (buffer_source.IsArrayBufferView()) {
const DOMArrayBufferView* array_buffer_view =
buffer_source.GetAsArrayBufferView().View();
bytes.Append(static_cast<uint8_t*>(array_buffer_view->BaseAddress()),
array_buffer_view->deprecatedByteLengthAsUnsigned());
} else {
......@@ -114,10 +115,10 @@ String getDocumentLanguage(const ExecutionContext* execution_context) {
static NDEFRecord* CreateTextRecord(const ExecutionContext* execution_context,
const String& encoding,
const String& lang,
const ScriptValue& data,
const NDEFRecordDataSource& data,
ExceptionState& exception_state) {
// https://w3c.github.io/web-nfc/#mapping-string-to-ndef
if (data.IsEmpty() || !(data.V8Value()->IsString() || IsBufferSource(data))) {
if (!(data.IsString() || IsBufferSource(data))) {
exception_state.ThrowTypeError(
"The data for 'text' NDEFRecords must be a String or a BufferSource.");
return nullptr;
......@@ -147,9 +148,8 @@ static NDEFRecord* CreateTextRecord(const ExecutionContext* execution_context,
}
WTF::Vector<uint8_t> bytes;
if (data.V8Value()->IsString()) {
String text = ToCoreString(data.V8Value().As<v8::String>());
StringUTF8Adaptor utf8_string(text);
if (data.IsString()) {
StringUTF8Adaptor utf8_string(data.GetAsString());
bytes.Append(utf8_string.data(), utf8_string.size());
} else {
DCHECK(IsBufferSource(data));
......@@ -162,17 +162,17 @@ static NDEFRecord* CreateTextRecord(const ExecutionContext* execution_context,
// Create a 'url' record or an 'absolute-url' record.
static NDEFRecord* CreateUrlRecord(const String& record_type,
const ScriptValue& data,
const NDEFRecordDataSource& data,
ExceptionState& exception_state) {
// https://w3c.github.io/web-nfc/#mapping-url-to-ndef
if (data.IsEmpty() || !data.V8Value()->IsString()) {
if (!data.IsString()) {
exception_state.ThrowTypeError(
"The data for url NDEFRecord must be a String.");
return nullptr;
}
// No need to check mediaType according to the spec.
String url = ToCoreString(data.V8Value().As<v8::String>());
String url = data.GetAsString();
if (!KURL(NullURL(), url).IsValid()) {
exception_state.ThrowDOMException(DOMExceptionCode::kSyntaxError,
"Cannot parse data for url record.");
......@@ -182,7 +182,7 @@ static NDEFRecord* CreateUrlRecord(const String& record_type,
GetUTF8DataFromString(url));
}
static NDEFRecord* CreateMimeRecord(const ScriptValue& data,
static NDEFRecord* CreateMimeRecord(const NDEFRecordDataSource& data,
const String& media_type,
ExceptionState& exception_state) {
// https://w3c.github.io/web-nfc/#mapping-binary-data-to-ndef
......@@ -196,7 +196,7 @@ static NDEFRecord* CreateMimeRecord(const ScriptValue& data,
media_type);
}
static NDEFRecord* CreateUnknownRecord(const ScriptValue& data,
static NDEFRecord* CreateUnknownRecord(const NDEFRecordDataSource& data,
ExceptionState& exception_state) {
if (!IsBufferSource(data)) {
exception_state.ThrowTypeError(
......@@ -209,7 +209,7 @@ static NDEFRecord* CreateUnknownRecord(const ScriptValue& data,
}
static NDEFRecord* CreateExternalRecord(const String& custom_type,
const ScriptValue& data,
const NDEFRecordDataSource& data,
ExceptionState& exception_state) {
// TODO(https://crbug.com/520391): Add support in case of |data| being an
// NDEFMessageInit.
......@@ -238,8 +238,7 @@ NDEFRecord* NDEFRecord::Create(const ExecutionContext* execution_context,
exception_state.ThrowTypeError("The record has neither type nor data.");
return nullptr;
}
v8::Local<v8::Value> data = init->data().V8Value();
if (data->IsString()) {
if (init->data().IsString()) {
record_type = "text";
} else {
record_type = "mime";
......
......@@ -6,7 +6,6 @@
#define THIRD_PARTY_BLINK_RENDERER_MODULES_NFC_NDEF_RECORD_H_
#include "services/device/public/mojom/nfc.mojom-blink-forward.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
......
......@@ -2,6 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// https://w3c.github.io/web-nfc/#idl-def-ndefrecorddatasource
// 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)
NDEFRecordDataSource;
// https://w3c.github.io/web-nfc/#dom-ndefrecordinit
dictionary NDEFRecordInit {
......@@ -12,5 +20,5 @@ dictionary NDEFRecordInit {
USVString encoding;
USVString lang;
any data;
NDEFRecordDataSource data;
};
......@@ -30,7 +30,6 @@ const invalid_type_messages =
// NDEFRecord.data for 'text' record must be either a string,
// an arrayBuffer, or an arrayBufferView.
createMessage([createTextRecord(test_json_data)]),
createMessage([createTextRecord(test_number_data)]),
// NDEFRecord.encoding for 'text' record must be either "utf-8",
// "utf-16", "utf-16le" or "utf-16be".
......@@ -49,12 +48,10 @@ const invalid_type_messages =
// NDEFRecord.data for 'url' record must be string.
createMessage([createUrlRecord(test_buffer_data)]),
createMessage([createUrlRecord(test_number_data)]),
createMessage([createUrlRecord(test_json_data)]),
// NDEFRecord.data for 'absolute-url' record must be string.
createMessage([createUrlRecord(test_buffer_data, true)]),
createMessage([createUrlRecord(test_number_data, true)]),
createMessage([createUrlRecord(test_json_data, true)]),
// https://w3c.github.io/web-nfc/#dfn-map-binary-data-to-ndef
......
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