Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Unified Diff: third_party/WebKit/Source/modules/nfc/NFC.cpp

Issue 2885813002: [webnfc] Align nfc.push operation with the specification (Closed)
Patch Set: Removed 'the' articles Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/modules/nfc/NFC.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/modules/nfc/NFC.cpp
diff --git a/third_party/WebKit/Source/modules/nfc/NFC.cpp b/third_party/WebKit/Source/modules/nfc/NFC.cpp
index 172909088289ef512fde3338b1c1b9a58c16dd9a..c03f936ba336bf53e8d35edb6559bf01f5bb896e 100644
--- a/third_party/WebKit/Source/modules/nfc/NFC.cpp
+++ b/third_party/WebKit/Source/modules/nfc/NFC.cpp
@@ -22,6 +22,7 @@
#include "public/platform/Platform.h"
namespace {
+const char kJsonMimePostfix[] = "+json";
const char kJsonMimePrefix[] = "application/";
const char kJsonMimeType[] = "application/json";
const char kOpaqueMimeType[] = "application/octet-stream";
@@ -46,7 +47,7 @@ using device::nfc::mojom::blink::NFCWatchMode;
using device::nfc::mojom::blink::NFCWatchOptions;
using device::nfc::mojom::blink::NFCWatchOptionsPtr;
-NFCPushTarget toNFCPushTarget(const WTF::String& target) {
+NFCPushTarget toNFCPushTarget(const String& target) {
if (target == "tag")
return NFCPushTarget::TAG;
@@ -56,7 +57,7 @@ NFCPushTarget toNFCPushTarget(const WTF::String& target) {
return NFCPushTarget::ANY;
}
-NFCRecordType toNFCRecordType(const WTF::String& recordType) {
+NFCRecordType toNFCRecordType(const String& recordType) {
if (recordType == "empty")
return NFCRecordType::EMPTY;
@@ -76,7 +77,7 @@ NFCRecordType toNFCRecordType(const WTF::String& recordType) {
return NFCRecordType::EMPTY;
}
-NFCWatchMode toNFCWatchMode(const WTF::String& watchMode) {
+NFCWatchMode toNFCWatchMode(const String& watchMode) {
if (watchMode == "web-nfc-only")
return NFCWatchMode::WEBNFC_ONLY;
@@ -114,39 +115,39 @@ NFCRecordType deduceRecordTypeFromDataType(const blink::NFCRecord& record) {
}
void setMediaType(NFCRecordPtr& recordPtr,
- const WTF::String& recordMediaType,
- const WTF::String& defaultMediaType) {
+ const String& recordMediaType,
+ const String& defaultMediaType) {
recordPtr->media_type =
recordMediaType.IsEmpty() ? defaultMediaType : recordMediaType;
}
template <>
-struct TypeConverter<WTF::Vector<uint8_t>, WTF::String> {
- static WTF::Vector<uint8_t> Convert(const WTF::String& string) {
- WTF::CString utf8String = string.Utf8();
- WTF::Vector<uint8_t> array;
+struct TypeConverter<Vector<uint8_t>, String> {
+ static Vector<uint8_t> Convert(const String& string) {
+ CString utf8String = string.Utf8();
+ Vector<uint8_t> array;
array.Append(utf8String.data(), utf8String.length());
return array;
}
};
template <>
-struct TypeConverter<WTF::Vector<uint8_t>, blink::DOMArrayBuffer*> {
- static WTF::Vector<uint8_t> Convert(blink::DOMArrayBuffer* buffer) {
- WTF::Vector<uint8_t> array;
+struct TypeConverter<Vector<uint8_t>, blink::DOMArrayBuffer*> {
+ static Vector<uint8_t> Convert(blink::DOMArrayBuffer* buffer) {
+ Vector<uint8_t> array;
array.Append(static_cast<uint8_t*>(buffer->Data()), buffer->ByteLength());
return array;
}
};
template <>
-struct TypeConverter<NFCRecordPtr, WTF::String> {
- static NFCRecordPtr Convert(const WTF::String& string) {
+struct TypeConverter<NFCRecordPtr, String> {
+ static NFCRecordPtr Convert(const String& string) {
NFCRecordPtr record = NFCRecord::New();
record->record_type = NFCRecordType::TEXT;
record->media_type = kPlainTextMimeType;
record->media_type.append(kCharSetUTF8);
- record->data = mojo::ConvertTo<WTF::Vector<uint8_t>>(string);
+ record->data = mojo::ConvertTo<Vector<uint8_t>>(string);
return record;
}
};
@@ -157,14 +158,14 @@ struct TypeConverter<NFCRecordPtr, blink::DOMArrayBuffer*> {
NFCRecordPtr record = NFCRecord::New();
record->record_type = NFCRecordType::OPAQUE_RECORD;
record->media_type = kOpaqueMimeType;
- record->data = mojo::ConvertTo<WTF::Vector<uint8_t>>(buffer);
+ record->data = mojo::ConvertTo<Vector<uint8_t>>(buffer);
return record;
}
};
template <>
-struct TypeConverter<NFCMessagePtr, WTF::String> {
- static NFCMessagePtr Convert(const WTF::String& string) {
+struct TypeConverter<NFCMessagePtr, String> {
+ static NFCMessagePtr Convert(const String& string) {
NFCMessagePtr message = NFCMessage::New();
message->data.push_back(NFCRecord::From(string));
return message;
@@ -172,35 +173,43 @@ struct TypeConverter<NFCMessagePtr, WTF::String> {
};
template <>
-struct TypeConverter<WTF::Optional<WTF::Vector<uint8_t>>, blink::ScriptValue> {
- static WTF::Optional<WTF::Vector<uint8_t>> Convert(
+struct TypeConverter<Optional<Vector<uint8_t>>, blink::ScriptValue> {
+ static Optional<Vector<uint8_t>> Convert(
const blink::ScriptValue& scriptValue) {
v8::Local<v8::Value> value = scriptValue.V8Value();
- if (value->IsNumber())
- return mojo::ConvertTo<WTF::Vector<uint8_t>>(
- WTF::String::Number(value.As<v8::Number>()->Value()));
+ if (value->IsNumber()) {
+ return mojo::ConvertTo<Vector<uint8_t>>(
+ String::Number(value.As<v8::Number>()->Value()));
+ }
if (value->IsString()) {
blink::V8StringResource<> stringResource = value;
if (stringResource.Prepare()) {
- return mojo::ConvertTo<WTF::Vector<uint8_t>>(
- WTF::String(stringResource));
+ return mojo::ConvertTo<Vector<uint8_t>>(String(stringResource));
}
}
if (value->IsObject() && !value->IsArray() && !value->IsArrayBuffer()) {
v8::Local<v8::String> jsonString;
- if (v8::JSON::Stringify(scriptValue.GetContext(), value.As<v8::Object>())
- .ToLocal(&jsonString)) {
- WTF::String wtfString = blink::V8StringToWebCoreString<WTF::String>(
- jsonString, blink::kDoNotExternalize);
- return mojo::ConvertTo<WTF::Vector<uint8_t>>(wtfString);
+ v8::Isolate* isolate = scriptValue.GetIsolate();
+ v8::TryCatch try_catch(isolate);
+
+ // https://w3c.github.io/web-nfc/#mapping-json-to-ndef
+ // If serialization throws, reject promise with a "SyntaxError" exception.
+ if (!v8::JSON::Stringify(scriptValue.GetContext(), value.As<v8::Object>())
+ .ToLocal(&jsonString) ||
+ try_catch.HasCaught()) {
+ return WTF::nullopt;
}
+
+ String string = blink::V8StringToWebCoreString<String>(
+ jsonString, blink::kDoNotExternalize);
+ return mojo::ConvertTo<Vector<uint8_t>>(string);
}
if (value->IsArrayBuffer())
- return mojo::ConvertTo<WTF::Vector<uint8_t>>(
+ return mojo::ConvertTo<Vector<uint8_t>>(
blink::V8ArrayBuffer::toImpl(value.As<v8::Object>()));
return WTF::nullopt;
@@ -239,8 +248,7 @@ struct TypeConverter<NFCRecordPtr, blink::NFCRecord> {
break;
}
- auto recordData =
- mojo::ConvertTo<WTF::Optional<WTF::Vector<uint8_t>>>(record.data());
+ auto recordData = mojo::ConvertTo<Optional<Vector<uint8_t>>>(record.data());
// If JS object cannot be converted to uint8_t array, return null,
// interrupt NFCMessage conversion algorithm and reject promise with
// SyntaxError exception.
@@ -353,48 +361,92 @@ struct TypeConverter<NFCWatchOptionsPtr, blink::NFCWatchOptions> {
namespace blink {
namespace {
-bool IsValidTextRecord(const NFCRecord& record) {
+ScriptPromise RejectWithTypeError(ScriptState* script_state,
+ const String& message) {
+ return ScriptPromise::Reject(
+ script_state,
+ V8ThrowException::CreateTypeError(script_state->GetIsolate(), message));
+}
+
+ScriptPromise RejectWithDOMException(ScriptState* script_state,
+ ExceptionCode ec,
+ const String& message) {
+ return ScriptPromise::RejectWithDOMException(
+ script_state, DOMException::Create(ec, message));
+}
+
+ScriptPromise RejectIfInvalidTextRecord(ScriptState* script_state,
+ const NFCRecord& record) {
v8::Local<v8::Value> value = record.data().V8Value();
if (!value->IsString() &&
- !(value->IsNumber() && !std::isnan(value.As<v8::Number>()->Value())))
- return false;
+ !(value->IsNumber() && !std::isnan(value.As<v8::Number>()->Value()))) {
+ return RejectWithTypeError(script_state,
+ "The data for 'text' NFCRecords must be of "
+ "String or UnrestrctedDouble type.");
+ }
if (record.hasMediaType() &&
!record.mediaType().StartsWith(kPlainTextMimePrefix,
- kTextCaseUnicodeInsensitive))
- return false;
+ kTextCaseUnicodeInsensitive)) {
+ return RejectWithDOMException(script_state, kSyntaxError,
+ "Invalid media type for 'text' record.");
+ }
- return true;
+ return ScriptPromise();
}
-bool IsValidURLRecord(const NFCRecord& record) {
- if (!record.data().V8Value()->IsString())
- return false;
+ScriptPromise RejectIfInvalidURLRecord(ScriptState* script_state,
+ const NFCRecord& record) {
+ if (!record.data().V8Value()->IsString()) {
+ return RejectWithTypeError(
+ script_state, "The data for 'url' NFCRecord must be of String type.");
+ }
blink::V8StringResource<> string_resource = record.data().V8Value();
- if (!string_resource.Prepare())
- return false;
+ if (!string_resource.Prepare() || !KURL(KURL(), string_resource).IsValid()) {
+ return RejectWithDOMException(script_state, kSyntaxError,
+ "Cannot parse data for 'url' record.");
+ }
- return KURL(KURL(), string_resource).IsValid();
+ return ScriptPromise();
}
-bool IsValidJSONRecord(const NFCRecord& record) {
+ScriptPromise RejectIfInvalidJSONRecord(ScriptState* script_state,
+ const NFCRecord& record) {
v8::Local<v8::Value> value = record.data().V8Value();
- if (!value->IsObject() || value->IsArrayBuffer())
- return false;
+ if (!value->IsObject() || value->IsArrayBuffer()) {
+ return RejectWithTypeError(
+ script_state, "The data for 'json' NFCRecord must be of Object type.");
+ }
- if (record.hasMediaType() && !record.mediaType().StartsWith(
- kJsonMimePrefix, kTextCaseASCIIInsensitive))
- return false;
+ // If JSON record has media type, it must be equal to "application/json" or
+ // start with "application/" and end with "+json".
+ if (record.hasMediaType() &&
+ (record.mediaType() != kJsonMimeType &&
+ !(record.mediaType().StartsWith(kJsonMimePrefix,
+ kTextCaseASCIIInsensitive) &&
+ record.mediaType().EndsWith(kJsonMimePostfix,
+ kTextCaseASCIIInsensitive)))) {
+ return RejectWithDOMException(script_state, kSyntaxError,
+ "Invalid media type for 'json' record.");
+ }
- return true;
+ return ScriptPromise();
}
-bool IsValidOpaqueRecord(const NFCRecord& record) {
- return record.data().V8Value()->IsArrayBuffer();
+ScriptPromise RejectIfInvalidOpaqueRecord(ScriptState* script_state,
+ const NFCRecord& record) {
+ if (!record.data().V8Value()->IsArrayBuffer()) {
+ return RejectWithTypeError(
+ script_state,
+ "The data for 'opaque' NFCRecord must be of ArrayBuffer type.");
+ }
+
+ return ScriptPromise();
}
-bool IsValidNFCRecord(const NFCRecord& record) {
+ScriptPromise RejectIfInvalidNFCRecord(ScriptState* script_state,
+ const NFCRecord& record) {
device::nfc::mojom::blink::NFCRecordType type;
if (record.hasRecordType()) {
type = mojo::toNFCRecordType(record.recordType());
@@ -403,62 +455,72 @@ bool IsValidNFCRecord(const NFCRecord& record) {
// https://w3c.github.io/web-nfc/#creating-web-nfc-message
// If NFCRecord.recordType is not set and record type cannot be deduced
- // from NFCRecord.data, reject promise with SyntaxError.
+ // from NFCRecord.data, reject promise with TypeError.
if (type == device::nfc::mojom::blink::NFCRecordType::EMPTY)
- return false;
+ return RejectWithTypeError(script_state, "Unknown NFCRecord type.");
}
// Non-empty records must have data.
if (!record.hasData() &&
(type != device::nfc::mojom::blink::NFCRecordType::EMPTY)) {
- return false;
+ return RejectWithTypeError(script_state,
+ "Nonempty NFCRecord must have data.");
}
switch (type) {
case device::nfc::mojom::blink::NFCRecordType::TEXT:
- return IsValidTextRecord(record);
+ return RejectIfInvalidTextRecord(script_state, record);
case device::nfc::mojom::blink::NFCRecordType::URL:
- return IsValidURLRecord(record);
+ return RejectIfInvalidURLRecord(script_state, record);
case device::nfc::mojom::blink::NFCRecordType::JSON:
- return IsValidJSONRecord(record);
+ return RejectIfInvalidJSONRecord(script_state, record);
case device::nfc::mojom::blink::NFCRecordType::OPAQUE_RECORD:
- return IsValidOpaqueRecord(record);
+ return RejectIfInvalidOpaqueRecord(script_state, record);
case device::nfc::mojom::blink::NFCRecordType::EMPTY:
- return !record.hasData() && record.mediaType().IsEmpty();
+ return ScriptPromise();
}
NOTREACHED();
- return false;
+ return RejectWithTypeError(script_state,
+ "Invalid NFCRecordType was provided.");
}
-bool IsValidNFCRecordArray(const HeapVector<NFCRecord>& records) {
- if (records.IsEmpty())
- return false;
-
+ScriptPromise RejectIfInvalidNFCRecordArray(
+ ScriptState* script_state,
+ const HeapVector<NFCRecord>& records) {
for (const auto& record : records) {
- if (!IsValidNFCRecord(record))
- return false;
+ ScriptPromise isValidRecord =
+ RejectIfInvalidNFCRecord(script_state, record);
+ if (!isValidRecord.IsEmpty())
+ return isValidRecord;
}
- return true;
+ return ScriptPromise();
}
-bool IsValidNFCPushMessage(const NFCPushMessage& message) {
+ScriptPromise RejectIfInvalidNFCPushMessage(
+ ScriptState* script_state,
+ const NFCPushMessage& push_message) {
// If NFCPushMessage of invalid type, reject promise with TypeError
- if (!message.isNFCMessage() && !message.isString() &&
- !message.isArrayBuffer())
- return false;
+ if (!push_message.isNFCMessage() && !push_message.isString() &&
+ !push_message.isArrayBuffer()) {
+ return RejectWithTypeError(script_state,
+ "Invalid NFCPushMessage type was provided.");
+ }
- if (message.isNFCMessage()) {
+ if (push_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 false;
+ const NFCMessage& message = push_message.getAsNFCMessage();
+ if (!message.hasData() || message.data().IsEmpty()) {
+ return RejectWithTypeError(script_state,
+ "Empty NFCMessage was provided.");
+ }
- return IsValidNFCRecordArray(message.getAsNFCMessage().data());
+ return RejectIfInvalidNFCRecordArray(script_state, message.data());
}
- return true;
+ return ScriptPromise();
}
bool SetURL(const String& origin,
@@ -621,41 +683,38 @@ ScriptPromise NFC::push(ScriptState* script_state,
if (!promise.IsEmpty())
return promise;
- if (!IsValidNFCPushMessage(push_message)) {
- return ScriptPromise::Reject(
- script_state, V8ThrowException::CreateTypeError(
- script_state->GetIsolate(),
- "Invalid NFCPushMessage type was provided."));
- }
+ ScriptPromise isValidMessage =
+ RejectIfInvalidNFCPushMessage(script_state, push_message);
+ if (!isValidMessage.IsEmpty())
+ return isValidMessage;
// https://w3c.github.io/web-nfc/#dom-nfc-push
// 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 NFCPushOptions.timeout value was provided."));
+ return RejectWithTypeError(
+ script_state, "Invalid NFCPushOptions.timeout value was provided.");
}
device::nfc::mojom::blink::NFCMessagePtr message =
device::nfc::mojom::blink::NFCMessage::From(push_message);
- if (!message)
- return ScriptPromise::RejectWithDOMException(
- script_state, DOMException::Create(kSyntaxError));
+ if (!message) {
+ return RejectWithDOMException(script_state, kSyntaxError,
+ "Cannot convert NFCMessage.");
+ }
if (!SetURL(
ExecutionContext::From(script_state)->GetSecurityOrigin()->ToString(),
- message))
- return ScriptPromise::RejectWithDOMException(
- script_state, DOMException::Create(kSyntaxError));
+ message)) {
+ return RejectWithDOMException(script_state, kSyntaxError,
+ "Cannot set WebNFC Id.");
+ }
if (GetNFCMessageSize(message) >
device::nfc::mojom::blink::NFCMessage::kMaxSize) {
- return ScriptPromise::RejectWithDOMException(
- script_state, DOMException::Create(kNotSupportedError));
+ return RejectWithDOMException(script_state, kNotSupportedError,
+ "NFCMessage exceeds maximum supported size.");
}
ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
@@ -715,8 +774,8 @@ ScriptPromise NFC::cancelWatch(ScriptState* script_state, long id) {
if (id) {
callbacks_.erase(id);
} else {
- return ScriptPromise::RejectWithDOMException(
- script_state, DOMException::Create(kNotFoundError));
+ return RejectWithDOMException(script_state, kNotFoundError,
+ "Provided watch id cannot be found.");
}
ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
@@ -781,7 +840,7 @@ void NFC::OnConnectionError() {
requests_.clear();
}
-void NFC::OnWatch(const WTF::Vector<uint32_t>& ids,
+void NFC::OnWatch(const Vector<uint32_t>& ids,
device::nfc::mojom::blink::NFCMessagePtr message) {
for (const auto& id : ids) {
auto it = callbacks_.find(id);
@@ -817,13 +876,12 @@ ScriptPromise NFC::RejectIfNotSupported(ScriptState* script_state) {
String error_message;
if (!IsSupportedInContext(ExecutionContext::From(script_state),
error_message)) {
- return ScriptPromise::RejectWithDOMException(
- script_state, DOMException::Create(kSecurityError, error_message));
+ return RejectWithDOMException(script_state, kSecurityError, error_message);
}
if (!nfc_) {
- return ScriptPromise::RejectWithDOMException(
- script_state, DOMException::Create(kNotSupportedError));
+ return RejectWithDOMException(script_state, kNotSupportedError,
+ "WebNFC is not supported.");
}
return ScriptPromise();
« no previous file with comments | « third_party/WebKit/Source/modules/nfc/NFC.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698