summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/nfc/nfc_type_converters.cc
blob: 37e932d719592901e76df2146c587062dc2090ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/nfc/nfc_type_converters.h"

#include <limits>
#include <utility>

#include "services/device/public/mojom/nfc.mojom-blink.h"
#include "third_party/blink/renderer/modules/nfc/ndef_message.h"
#include "third_party/blink/renderer/modules/nfc/ndef_record.h"
#include "third_party/blink/renderer/modules/nfc/nfc_push_options.h"
#include "third_party/blink/renderer/modules/nfc/nfc_scan_options.h"
#include "third_party/blink/renderer/modules/nfc/nfc_utils.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

using device::mojom::blink::NDEFMessage;
using device::mojom::blink::NDEFMessagePtr;
using device::mojom::blink::NDEFRecord;
using device::mojom::blink::NDEFRecordPtr;
using device::mojom::blink::NDEFRecordTypeFilter;
using device::mojom::blink::NFCPushOptions;
using device::mojom::blink::NFCPushOptionsPtr;
using device::mojom::blink::NFCScanOptions;
using device::mojom::blink::NFCScanOptionsPtr;

// Mojo type converters
namespace mojo {

NDEFRecordPtr TypeConverter<NDEFRecordPtr, ::blink::NDEFRecord*>::Convert(
    const ::blink::NDEFRecord* record) {
  return NDEFRecord::New(blink::StringToNDEFRecordType(record->recordType()),
                         record->mediaType(), record->data());
}

NDEFMessagePtr TypeConverter<NDEFMessagePtr, ::blink::NDEFMessage*>::Convert(
    const ::blink::NDEFMessage* message) {
  NDEFMessagePtr messagePtr = NDEFMessage::New();
  messagePtr->url = message->url();
  messagePtr->data.resize(message->records().size());
  for (wtf_size_t i = 0; i < message->records().size(); ++i) {
    NDEFRecordPtr record = NDEFRecord::From(message->records()[i].Get());
    DCHECK(record);
    messagePtr->data[i] = std::move(record);
  }
  return messagePtr;
}

NFCPushOptionsPtr
TypeConverter<NFCPushOptionsPtr, const blink::NFCPushOptions*>::Convert(
    const blink::NFCPushOptions* pushOptions) {
  // https://w3c.github.io/web-nfc/#the-nfcpushoptions-dictionary
  // Default values for NFCPushOptions dictionary are:
  // target = 'any', timeout = Infinity, ignoreRead = true,
  // compatibility = "nfc-forum"
  NFCPushOptionsPtr pushOptionsPtr = NFCPushOptions::New();
  pushOptionsPtr->target = blink::StringToNFCPushTarget(pushOptions->target());
  pushOptionsPtr->ignore_read = pushOptions->ignoreRead();
  pushOptionsPtr->compatibility =
      blink::StringToNDEFCompatibility(pushOptions->compatibility());

  if (pushOptions->hasTimeout())
    pushOptionsPtr->timeout = pushOptions->timeout();
  else
    pushOptionsPtr->timeout = std::numeric_limits<double>::infinity();

  return pushOptionsPtr;
}

NFCScanOptionsPtr
TypeConverter<NFCScanOptionsPtr, const blink::NFCScanOptions*>::Convert(
    const blink::NFCScanOptions* scanOptions) {
  // https://w3c.github.io/web-nfc/#dom-nfcscanoptions
  // Default values for NFCScanOptions dictionary are:
  // url = "", recordType = null, mediaType = "", compatibility = "nfc-forum"
  NFCScanOptionsPtr scanOptionsPtr = NFCScanOptions::New();
  scanOptionsPtr->url = scanOptions->url();
  scanOptionsPtr->media_type = scanOptions->mediaType();
  scanOptionsPtr->compatibility =
      blink::StringToNDEFCompatibility(scanOptions->compatibility());

  if (scanOptions->hasRecordType()) {
    scanOptionsPtr->record_filter = NDEFRecordTypeFilter::New();
    scanOptionsPtr->record_filter->record_type =
        blink::StringToNDEFRecordType(scanOptions->recordType());
  }

  return scanOptionsPtr;
}

}  // namespace mojo