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/JSONValuesForV8.h" | 7 #include "bindings/core/v8/JSONValuesForV8.h" |
8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
9 #include "bindings/core/v8/V8ArrayBuffer.h" | 9 #include "bindings/core/v8/V8ArrayBuffer.h" |
10 #include "core/dom/DOMArrayBuffer.h" | 10 #include "core/dom/DOMArrayBuffer.h" |
11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
12 #include "core/dom/Document.h" | 12 #include "core/dom/Document.h" |
13 #include "core/dom/ExceptionCode.h" | 13 #include "core/dom/ExceptionCode.h" |
14 #include "core/frame/LocalDOMWindow.h" | 14 #include "core/frame/LocalDOMWindow.h" |
15 #include "modules/nfc/NFCError.h" | 15 #include "modules/nfc/NFCError.h" |
16 #include "modules/nfc/NFCMessage.h" | 16 #include "modules/nfc/NFCMessage.h" |
17 #include "modules/nfc/NFCPushOptions.h" | 17 #include "modules/nfc/NFCPushOptions.h" |
18 #include "platform/mojo/MojoHelper.h" | 18 #include "platform/mojo/MojoHelper.h" |
19 #include "public/platform/InterfaceProvider.h" | 19 #include "public/platform/InterfaceProvider.h" |
20 | 20 |
21 namespace nfc = device::nfc::blink; | 21 namespace mojom = device::nfc::mojom::blink; |
22 | 22 |
23 namespace { | 23 namespace { |
24 const char kJsonMimePrefix[] = "application/"; | 24 const char kJsonMimePrefix[] = "application/"; |
25 const char kJsonMimeType[] = "application/json"; | 25 const char kJsonMimeType[] = "application/json"; |
26 const char kOpaqueMimeType[] = "application/octet-stream"; | 26 const char kOpaqueMimeType[] = "application/octet-stream"; |
27 const char kPlainTextMimeType[] = "text/plain"; | 27 const char kPlainTextMimeType[] = "text/plain"; |
28 const char kPlainTextMimePrefix[] = "text/"; | 28 const char kPlainTextMimePrefix[] = "text/"; |
29 const char kCharSetUTF8[] = ";charset=UTF-8"; | 29 const char kCharSetUTF8[] = ";charset=UTF-8"; |
30 } // anonymous namespace | 30 } // anonymous namespace |
31 | 31 |
32 // Mojo type converters | 32 // Mojo type converters |
33 namespace mojo { | 33 namespace mojo { |
34 | 34 |
35 using nfc::NFCMessage; | 35 using mojom::NFCMessage; |
36 using nfc::NFCMessagePtr; | 36 using mojom::NFCMessagePtr; |
37 using nfc::NFCRecord; | 37 using mojom::NFCRecord; |
38 using nfc::NFCRecordPtr; | 38 using mojom::NFCRecordPtr; |
39 using nfc::NFCRecordType; | 39 using mojom::NFCRecordType; |
40 using nfc::NFCPushOptions; | 40 using mojom::NFCPushOptions; |
41 using nfc::NFCPushOptionsPtr; | 41 using mojom::NFCPushOptionsPtr; |
42 using nfc::NFCPushTarget; | 42 using mojom::NFCPushTarget; |
43 | 43 |
44 NFCPushTarget toNFCPushTarget(const WTF::String& target) | 44 NFCPushTarget toNFCPushTarget(const WTF::String& target) |
45 { | 45 { |
46 if (target == "tag") | 46 if (target == "tag") |
47 return NFCPushTarget::TAG; | 47 return NFCPushTarget::TAG; |
48 | 48 |
49 if (target == "peer") | 49 if (target == "peer") |
50 return NFCPushTarget::PEER; | 50 return NFCPushTarget::PEER; |
51 | 51 |
52 return NFCPushTarget::ANY; | 52 return NFCPushTarget::ANY; |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 return true; | 352 return true; |
353 } | 353 } |
354 | 354 |
355 bool isValidOpaqueRecord(const NFCRecord& record) | 355 bool isValidOpaqueRecord(const NFCRecord& record) |
356 { | 356 { |
357 return record.data().v8Value()->IsArrayBuffer(); | 357 return record.data().v8Value()->IsArrayBuffer(); |
358 } | 358 } |
359 | 359 |
360 bool isValidNFCRecord(const NFCRecord& record) | 360 bool isValidNFCRecord(const NFCRecord& record) |
361 { | 361 { |
362 nfc::NFCRecordType type; | 362 mojom::NFCRecordType type; |
363 if (record.hasRecordType()) { | 363 if (record.hasRecordType()) { |
364 type = mojo::toNFCRecordType(record.recordType()); | 364 type = mojo::toNFCRecordType(record.recordType()); |
365 } else { | 365 } else { |
366 type = mojo::deduceRecordTypeFromDataType(record); | 366 type = mojo::deduceRecordTypeFromDataType(record); |
367 | 367 |
368 // https://w3c.github.io/web-nfc/#creating-web-nfc-message | 368 // https://w3c.github.io/web-nfc/#creating-web-nfc-message |
369 // If NFCRecord.recordType is not set and record type cannot be deduced | 369 // If NFCRecord.recordType is not set and record type cannot be deduced |
370 // from NFCRecord.data, reject promise with SyntaxError. | 370 // from NFCRecord.data, reject promise with SyntaxError. |
371 if (type == nfc::NFCRecordType::EMPTY) | 371 if (type == mojom::NFCRecordType::EMPTY) |
372 return false; | 372 return false; |
373 } | 373 } |
374 | 374 |
375 // Non-empty records must have data. | 375 // Non-empty records must have data. |
376 if (!record.hasData() && (type != nfc::NFCRecordType::EMPTY)) | 376 if (!record.hasData() && (type != mojom::NFCRecordType::EMPTY)) |
377 return false; | 377 return false; |
378 | 378 |
379 switch (type) { | 379 switch (type) { |
380 case nfc::NFCRecordType::TEXT: | 380 case mojom::NFCRecordType::TEXT: |
381 return isValidTextRecord(record); | 381 return isValidTextRecord(record); |
382 case nfc::NFCRecordType::URL: | 382 case mojom::NFCRecordType::URL: |
383 return isValidURLRecord(record); | 383 return isValidURLRecord(record); |
384 case nfc::NFCRecordType::JSON: | 384 case mojom::NFCRecordType::JSON: |
385 return isValidJSONRecord(record); | 385 return isValidJSONRecord(record); |
386 case nfc::NFCRecordType::OPAQUE_RECORD: | 386 case mojom::NFCRecordType::OPAQUE_RECORD: |
387 return isValidOpaqueRecord(record); | 387 return isValidOpaqueRecord(record); |
388 case nfc::NFCRecordType::EMPTY: | 388 case mojom::NFCRecordType::EMPTY: |
389 return !record.hasData() && record.mediaType().isEmpty(); | 389 return !record.hasData() && record.mediaType().isEmpty(); |
390 } | 390 } |
391 | 391 |
392 NOTREACHED(); | 392 NOTREACHED(); |
393 return false; | 393 return false; |
394 } | 394 } |
395 | 395 |
396 DOMException* isValidNFCRecordArray(const HeapVector<NFCRecord>& records) | 396 DOMException* isValidNFCRecordArray(const HeapVector<NFCRecord>& records) |
397 { | 397 { |
398 // https://w3c.github.io/web-nfc/#the-push-method | 398 // https://w3c.github.io/web-nfc/#the-push-method |
(...skipping 17 matching lines...) Expand all Loading... |
416 if (message.isNFCMessage()) { | 416 if (message.isNFCMessage()) { |
417 if (!message.getAsNFCMessage().hasData()) | 417 if (!message.getAsNFCMessage().hasData()) |
418 return DOMException::create(TypeMismatchError); | 418 return DOMException::create(TypeMismatchError); |
419 | 419 |
420 return isValidNFCRecordArray(message.getAsNFCMessage().data()); | 420 return isValidNFCRecordArray(message.getAsNFCMessage().data()); |
421 } | 421 } |
422 | 422 |
423 return nullptr; | 423 return nullptr; |
424 } | 424 } |
425 | 425 |
426 bool setURL(const String& origin, nfc::NFCMessagePtr& message) | 426 bool setURL(const String& origin, mojom::NFCMessagePtr& message) |
427 { | 427 { |
428 KURL originURL(ParsedURLString, origin); | 428 KURL originURL(ParsedURLString, origin); |
429 | 429 |
430 if (!message->url.isEmpty() && originURL.canSetPathname()) { | 430 if (!message->url.isEmpty() && originURL.canSetPathname()) { |
431 originURL.setPath(message->url); | 431 originURL.setPath(message->url); |
432 } | 432 } |
433 | 433 |
434 message->url = originURL; | 434 message->url = originURL; |
435 return originURL.isValid(); | 435 return originURL.isValid(); |
436 } | 436 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) | 479 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) |
480 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, errorMessage)); | 480 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, errorMessage)); |
481 | 481 |
482 DOMException* exception = isValidNFCPushMessage(pushMessage); | 482 DOMException* exception = isValidNFCPushMessage(pushMessage); |
483 if (exception) | 483 if (exception) |
484 return ScriptPromise::rejectWithDOMException(scriptState, exception); | 484 return ScriptPromise::rejectWithDOMException(scriptState, exception); |
485 | 485 |
486 if (!m_nfc) | 486 if (!m_nfc) |
487 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | 487 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
488 | 488 |
489 nfc::NFCMessagePtr message = nfc::NFCMessage::From(pushMessage); | 489 mojom::NFCMessagePtr message = mojom::NFCMessage::From(pushMessage); |
490 if (!message) | 490 if (!message) |
491 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SyntaxError)); | 491 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SyntaxError)); |
492 | 492 |
493 if (!setURL(scriptState->getExecutionContext()->getSecurityOrigin()->toStrin
g(), message)) | 493 if (!setURL(scriptState->getExecutionContext()->getSecurityOrigin()->toStrin
g(), message)) |
494 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SyntaxError)); | 494 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SyntaxError)); |
495 | 495 |
496 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 496 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
497 m_requests.add(resolver); | 497 m_requests.add(resolver); |
498 auto callback = convertToBaseCallback(WTF::bind(&NFC::OnRequestCompleted, wr
apPersistent(this), wrapPersistent(resolver))); | 498 auto callback = convertToBaseCallback(WTF::bind(&NFC::OnRequestCompleted, wr
apPersistent(this), wrapPersistent(resolver))); |
499 m_nfc->Push(std::move(message), nfc::NFCPushOptions::From(options), callback
); | 499 m_nfc->Push(std::move(message), mojom::NFCPushOptions::From(options), callba
ck); |
500 | 500 |
501 return resolver->promise(); | 501 return resolver->promise(); |
502 } | 502 } |
503 | 503 |
504 ScriptPromise NFC::cancelPush(ScriptState* scriptState, const String& target) | 504 ScriptPromise NFC::cancelPush(ScriptState* scriptState, const String& target) |
505 { | 505 { |
506 String errorMessage; | 506 String errorMessage; |
507 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) | 507 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) |
508 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, errorMessage)); | 508 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, errorMessage)); |
509 | 509 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 return; | 543 return; |
544 | 544 |
545 // NFC operations should be suspended. | 545 // NFC operations should be suspended. |
546 // https://w3c.github.io/web-nfc/#nfc-suspended | 546 // https://w3c.github.io/web-nfc/#nfc-suspended |
547 if (page()->visibilityState() == PageVisibilityStateVisible) | 547 if (page()->visibilityState() == PageVisibilityStateVisible) |
548 m_nfc->ResumeNFCOperations(); | 548 m_nfc->ResumeNFCOperations(); |
549 else | 549 else |
550 m_nfc->SuspendNFCOperations(); | 550 m_nfc->SuspendNFCOperations(); |
551 } | 551 } |
552 | 552 |
553 void NFC::OnRequestCompleted(ScriptPromiseResolver* resolver, nfc::NFCErrorPtr e
rror) | 553 void NFC::OnRequestCompleted(ScriptPromiseResolver* resolver, mojom::NFCErrorPtr
error) |
554 { | 554 { |
555 if (!m_requests.contains(resolver)) | 555 if (!m_requests.contains(resolver)) |
556 return; | 556 return; |
557 | 557 |
558 m_requests.remove(resolver); | 558 m_requests.remove(resolver); |
559 if (error.is_null()) | 559 if (error.is_null()) |
560 resolver->resolve(); | 560 resolver->resolve(); |
561 else | 561 else |
562 resolver->reject(NFCError::take(resolver, error->error_type)); | 562 resolver->reject(NFCError::take(resolver, error->error_type)); |
563 } | 563 } |
564 | 564 |
565 void NFC::OnConnectionError() | 565 void NFC::OnConnectionError() |
566 { | 566 { |
567 m_nfc.reset(); | 567 m_nfc.reset(); |
568 | 568 |
569 // If NFCService is not available or disappears when NFC hardware is | 569 // If NFCService is not available or disappears when NFC hardware is |
570 // disabled, reject promise with NotSupportedError exception. | 570 // disabled, reject promise with NotSupportedError exception. |
571 for (ScriptPromiseResolver* resolver : m_requests) | 571 for (ScriptPromiseResolver* resolver : m_requests) |
572 resolver->reject(NFCError::take(resolver, nfc::NFCErrorType::NOT_SUPPORT
ED)); | 572 resolver->reject(NFCError::take(resolver, mojom::NFCErrorType::NOT_SUPPO
RTED)); |
573 | 573 |
574 m_requests.clear(); | 574 m_requests.clear(); |
575 } | 575 } |
576 | 576 |
577 void NFC::OnWatch(mojo::WTFArray<uint32_t> ids, nfc::NFCMessagePtr) | 577 void NFC::OnWatch(mojo::WTFArray<uint32_t> ids, mojom::NFCMessagePtr) |
578 { | 578 { |
579 // TODO(shalamov): Not implemented. | 579 // TODO(shalamov): Not implemented. |
580 } | 580 } |
581 | 581 |
582 DEFINE_TRACE(NFC) | 582 DEFINE_TRACE(NFC) |
583 { | 583 { |
584 PageVisibilityObserver::trace(visitor); | 584 PageVisibilityObserver::trace(visitor); |
585 ContextLifecycleObserver::trace(visitor); | 585 ContextLifecycleObserver::trace(visitor); |
586 visitor->trace(m_requests); | 586 visitor->trace(m_requests); |
587 } | 587 } |
588 | 588 |
589 } // namespace blink | 589 } // namespace blink |
OLD | NEW |