Chromium Code Reviews| 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..bb39bf668e817eb733d12164fb34a55cb7dedede 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"; |
| @@ -191,12 +192,19 @@ struct TypeConverter<WTF::Optional<WTF::Vector<uint8_t>>, blink::ScriptValue> { |
| 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); |
| + v8::MaybeLocal<v8::String> stringify_result = |
| + v8::JSON::Stringify(scriptValue.GetContext(), value.As<v8::Object>()); |
| + |
| + // Return null if object cannot be stringified. |
| + if (!stringify_result.ToLocal(&jsonString) || try_catch.HasCaught()) { |
|
haraken
2017/05/17 15:11:22
I prefer avoiding using a MaybeLocal handle.
v8::
shalamov
2017/05/18 08:34:03
I removed temporary MaybeLocal, but I left try_cat
|
| + return WTF::nullopt; |
| } |
| + |
| + WTF::String wtfString = blink::V8StringToWebCoreString<WTF::String>( |
|
haraken
2017/05/17 15:11:22
WTF:: won't be needed.
blink:: won't be needed.
shalamov
2017/05/18 08:34:03
WTF:: is not needed. Removed in other places as we
|
| + jsonString, blink::kDoNotExternalize); |
| + return mojo::ConvertTo<WTF::Vector<uint8_t>>(wtfString); |
| } |
| if (value->IsArrayBuffer()) |
| @@ -353,48 +361,94 @@ 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 the '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 the '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 the '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 the '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 +457,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 +685,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 +776,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); |
| @@ -817,13 +878,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(); |