summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/nfc/ndef_message.cc
blob: 6d6154d4242480ef6672449de31bae060766d4e2 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// 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/ndef_message.h"

#include "services/device/public/mojom/nfc.mojom-blink.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_ndef_message_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_ndef_record_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_arraybuffer_arraybufferview_ndefmessageinit_string.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/nfc/ndef_record.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"

namespace blink {

// static
NDEFMessage* NDEFMessage::Create(const ExecutionContext* execution_context,
                                 const NDEFMessageInit* init,
                                 ExceptionState& exception_state,
                                 bool is_embedded) {
  // https://w3c.github.io/web-nfc/#creating-ndef-message

  // NDEFMessageInit#records is a required field.
  DCHECK(init->hasRecords());
  if (init->records().IsEmpty()) {
    exception_state.ThrowTypeError(
        "NDEFMessageInit#records being empty makes no sense.");
    return nullptr;
  }

  NDEFMessage* message = MakeGarbageCollected<NDEFMessage>();
  for (const NDEFRecordInit* record_init : init->records()) {
    NDEFRecord* record = NDEFRecord::Create(execution_context, record_init,
                                            exception_state, is_embedded);
    if (exception_state.HadException())
      return nullptr;
    DCHECK(record);
    message->records_.push_back(record);
  }
  return message;
}

// static
NDEFMessage* NDEFMessage::Create(const ExecutionContext* execution_context,
                                 const V8NDEFMessageSource* source,
                                 ExceptionState& exception_state) {
  DCHECK(source);

  // https://w3c.github.io/web-nfc/#creating-ndef-message
  switch (source->GetContentType()) {
    case V8NDEFMessageSource::ContentType::kArrayBuffer: {
      WTF::Vector<uint8_t> payload_data;
      size_t byte_length = source->GetAsArrayBuffer()->ByteLength();
      if (byte_length > std::numeric_limits<wtf_size_t>::max()) {
        exception_state.ThrowRangeError(
            "Buffer size exceeds maximum heap object size.");
        return nullptr;
      }
      payload_data.Append(
          static_cast<uint8_t*>(source->GetAsArrayBuffer()->Data()),
          static_cast<wtf_size_t>(byte_length));
      NDEFMessage* message = MakeGarbageCollected<NDEFMessage>();
      message->records_.push_back(MakeGarbageCollected<NDEFRecord>(
          String() /* id */, "application/octet-stream",
          std::move(payload_data)));
      return message;
    }
    case V8NDEFMessageSource::ContentType::kArrayBufferView: {
      size_t byte_length = source->GetAsArrayBufferView()->byteLength();
      if (byte_length > std::numeric_limits<wtf_size_t>::max()) {
        exception_state.ThrowRangeError(
            "Buffer size exceeds maximum heap object size.");
        return nullptr;
      }
      WTF::Vector<uint8_t> payload_data;
      payload_data.Append(
          static_cast<uint8_t*>(source->GetAsArrayBufferView()->BaseAddress()),
          static_cast<wtf_size_t>(byte_length));
      NDEFMessage* message = MakeGarbageCollected<NDEFMessage>();
      message->records_.push_back(MakeGarbageCollected<NDEFRecord>(
          String() /* id */, "application/octet-stream",
          std::move(payload_data)));
      return message;
    }
    case V8NDEFMessageSource::ContentType::kNDEFMessageInit: {
      return Create(execution_context, source->GetAsNDEFMessageInit(),
                    exception_state);
    }
    case V8NDEFMessageSource::ContentType::kString: {
      NDEFMessage* message = MakeGarbageCollected<NDEFMessage>();
      message->records_.push_back(MakeGarbageCollected<NDEFRecord>(
          execution_context, source->GetAsString()));
      return message;
    }
  }

  NOTREACHED();
  return nullptr;
}

// static
NDEFMessage* NDEFMessage::CreateAsPayloadOfSmartPoster(
    const ExecutionContext* execution_context,
    const NDEFMessageInit* init,
    ExceptionState& exception_state) {
  // NDEFMessageInit#records is a required field.
  DCHECK(init->hasRecords());

  NDEFMessage* payload_message = MakeGarbageCollected<NDEFMessage>();

  bool has_url_record = false;
  bool has_size_record = false;
  bool has_type_record = false;
  bool has_action_record = false;
  for (const NDEFRecordInit* record_init : init->records()) {
    const String& record_type = record_init->recordType();
    if (record_type == "url") {
      // The single mandatory url record.
      if (has_url_record) {
        exception_state.ThrowTypeError(
            "'smart-poster' NDEFRecord contains more than one url record.");
        return nullptr;
      }
      has_url_record = true;
    } else if (record_type == ":s") {
      // Zero or one size record.
      if (has_size_record) {
        exception_state.ThrowTypeError(
            "'smart-poster' NDEFRecord contains more than one size record.");
        return nullptr;
      }
      has_size_record = true;
    } else if (record_type == ":t") {
      // Zero or one type record.
      if (has_type_record) {
        exception_state.ThrowTypeError(
            "'smart-poster' NDEFRecord contains more than one type record.");
        return nullptr;
      }
      has_type_record = true;
    } else if (record_type == ":act") {
      // Zero or one action record.
      if (has_action_record) {
        exception_state.ThrowTypeError(
            "'smart-poster' NDEFRecord contains more than one action record.");
        return nullptr;
      }
      has_action_record = true;
    } else {
      // No restriction on other record types.
    }
    NDEFRecord* record = NDEFRecord::Create(
        execution_context, record_init, exception_state, /*is_embedded=*/true);
    if (exception_state.HadException())
      return nullptr;
    DCHECK(record);

    if (record->recordType() == ":s" && record->payloadData().size() != 4) {
      exception_state.ThrowTypeError(
          "Size record of smart-poster must contain a 4-byte 32 bit unsigned "
          "integer.");
      return nullptr;
    }
    if (record->recordType() == ":act" && record->payloadData().size() != 1) {
      exception_state.ThrowTypeError(
          "Action record of smart-poster must contain only a single byte.");
      return nullptr;
    }

    payload_message->records_.push_back(record);
  }

  if (!has_url_record) {
    exception_state.ThrowTypeError(
        "'smart-poster' NDEFRecord is missing the single mandatory url "
        "record.");
    return nullptr;
  }

  return payload_message;
}

NDEFMessage::NDEFMessage() = default;

NDEFMessage::NDEFMessage(const device::mojom::blink::NDEFMessage& message) {
  for (wtf_size_t i = 0; i < message.data.size(); ++i) {
    records_.push_back(MakeGarbageCollected<NDEFRecord>(*message.data[i]));
  }
}

const HeapVector<Member<NDEFRecord>>& NDEFMessage::records() const {
  return records_;
}

void NDEFMessage::Trace(Visitor* visitor) const {
  visitor->Trace(records_);
  ScriptWrappable::Trace(visitor);
}

}  // namespace blink