OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/nfc/NFC.h" | 5 #include "modules/nfc/NFC.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
8 #include "bindings/core/v8/V8ArrayBuffer.h" | 8 #include "bindings/core/v8/V8ArrayBuffer.h" |
9 #include "bindings/core/v8/V8StringResource.h" | 9 #include "bindings/core/v8/V8StringResource.h" |
10 #include "core/dom/DOMArrayBuffer.h" | 10 #include "core/dom/DOMArrayBuffer.h" |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 case device::nfc::mojom::blink::NFCRecordType::OPAQUE_RECORD: | 424 case device::nfc::mojom::blink::NFCRecordType::OPAQUE_RECORD: |
425 return IsValidOpaqueRecord(record); | 425 return IsValidOpaqueRecord(record); |
426 case device::nfc::mojom::blink::NFCRecordType::EMPTY: | 426 case device::nfc::mojom::blink::NFCRecordType::EMPTY: |
427 return !record.hasData() && record.mediaType().IsEmpty(); | 427 return !record.hasData() && record.mediaType().IsEmpty(); |
428 } | 428 } |
429 | 429 |
430 NOTREACHED(); | 430 NOTREACHED(); |
431 return false; | 431 return false; |
432 } | 432 } |
433 | 433 |
434 DOMException* IsValidNFCRecordArray(const HeapVector<NFCRecord>& records) { | 434 bool IsValidNFCRecordArray(const HeapVector<NFCRecord>& records) { |
435 // https://w3c.github.io/web-nfc/#the-push-method | |
436 // If NFCMessage.data is empty, reject promise with SyntaxError | |
437 if (records.IsEmpty()) | 435 if (records.IsEmpty()) |
438 return DOMException::Create(kSyntaxError); | 436 return false; |
439 | 437 |
440 for (const auto& record : records) { | 438 for (const auto& record : records) { |
441 if (!IsValidNFCRecord(record)) | 439 if (!IsValidNFCRecord(record)) |
442 return DOMException::Create(kSyntaxError); | 440 return false; |
443 } | 441 } |
444 | 442 |
445 return nullptr; | 443 return true; |
446 } | 444 } |
447 | 445 |
448 DOMException* IsValidNFCPushMessage(const NFCPushMessage& message) { | 446 bool IsValidNFCPushMessage(const NFCPushMessage& message) { |
| 447 // If NFCPushMessage of invalid type, reject promise with TypeError |
449 if (!message.isNFCMessage() && !message.isString() && | 448 if (!message.isNFCMessage() && !message.isString() && |
450 !message.isArrayBuffer()) | 449 !message.isArrayBuffer()) |
451 return DOMException::Create(kTypeMismatchError); | 450 return false; |
452 | 451 |
453 if (message.isNFCMessage()) { | 452 if (message.isNFCMessage()) { |
| 453 // https://w3c.github.io/web-nfc/#the-push-method |
| 454 // If NFCMessage.data is empty, reject promise with TypeError |
454 if (!message.getAsNFCMessage().hasData()) | 455 if (!message.getAsNFCMessage().hasData()) |
455 return DOMException::Create(kTypeMismatchError); | 456 return false; |
456 | 457 |
457 return IsValidNFCRecordArray(message.getAsNFCMessage().data()); | 458 return IsValidNFCRecordArray(message.getAsNFCMessage().data()); |
458 } | 459 } |
459 | 460 |
460 return nullptr; | 461 return true; |
461 } | 462 } |
462 | 463 |
463 bool SetURL(const String& origin, | 464 bool SetURL(const String& origin, |
464 device::nfc::mojom::blink::NFCMessagePtr& message) { | 465 device::nfc::mojom::blink::NFCMessagePtr& message) { |
465 KURL origin_url(kParsedURLString, origin); | 466 KURL origin_url(kParsedURLString, origin); |
466 | 467 |
467 if (!message->url.IsEmpty() && origin_url.CanSetPathname()) { | 468 if (!message->url.IsEmpty() && origin_url.CanSetPathname()) { |
468 origin_url.SetPath(message->url); | 469 origin_url.SetPath(message->url); |
469 } | 470 } |
470 | 471 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 | 608 |
608 // https://w3c.github.io/web-nfc/#writing-or-pushing-content | 609 // https://w3c.github.io/web-nfc/#writing-or-pushing-content |
609 // https://w3c.github.io/web-nfc/#dom-nfc-push | 610 // https://w3c.github.io/web-nfc/#dom-nfc-push |
610 ScriptPromise NFC::push(ScriptState* script_state, | 611 ScriptPromise NFC::push(ScriptState* script_state, |
611 const NFCPushMessage& push_message, | 612 const NFCPushMessage& push_message, |
612 const NFCPushOptions& options) { | 613 const NFCPushOptions& options) { |
613 ScriptPromise promise = RejectIfNotSupported(script_state); | 614 ScriptPromise promise = RejectIfNotSupported(script_state); |
614 if (!promise.IsEmpty()) | 615 if (!promise.IsEmpty()) |
615 return promise; | 616 return promise; |
616 | 617 |
617 DOMException* exception = IsValidNFCPushMessage(push_message); | 618 if (!IsValidNFCPushMessage(push_message)) { |
618 if (exception) | 619 return ScriptPromise::Reject( |
619 return ScriptPromise::RejectWithDOMException(script_state, exception); | 620 script_state, V8ThrowException::CreateTypeError( |
| 621 script_state->GetIsolate(), |
| 622 "Invalid NFCPushMessage type was provided.")); |
| 623 } |
620 | 624 |
621 device::nfc::mojom::blink::NFCMessagePtr message = | 625 device::nfc::mojom::blink::NFCMessagePtr message = |
622 device::nfc::mojom::blink::NFCMessage::From(push_message); | 626 device::nfc::mojom::blink::NFCMessage::From(push_message); |
623 if (!message) | 627 if (!message) |
624 return ScriptPromise::RejectWithDOMException( | 628 return ScriptPromise::RejectWithDOMException( |
625 script_state, DOMException::Create(kSyntaxError)); | 629 script_state, DOMException::Create(kSyntaxError)); |
626 | 630 |
627 if (!SetURL( | 631 if (!SetURL( |
628 ExecutionContext::From(script_state)->GetSecurityOrigin()->ToString(), | 632 ExecutionContext::From(script_state)->GetSecurityOrigin()->ToString(), |
629 message)) | 633 message)) |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 } | 829 } |
826 | 830 |
827 DEFINE_TRACE(NFC) { | 831 DEFINE_TRACE(NFC) { |
828 PageVisibilityObserver::Trace(visitor); | 832 PageVisibilityObserver::Trace(visitor); |
829 ContextLifecycleObserver::Trace(visitor); | 833 ContextLifecycleObserver::Trace(visitor); |
830 visitor->Trace(requests_); | 834 visitor->Trace(requests_); |
831 visitor->Trace(callbacks_); | 835 visitor->Trace(callbacks_); |
832 } | 836 } |
833 | 837 |
834 } // namespace blink | 838 } // namespace blink |
OLD | NEW |