summaryrefslogtreecommitdiffstats
path: root/device/nfc/nfc_ndef_record.h
blob: 24cbc12090c2ca26a7d162b942726a96e51b8adb (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
// Copyright 2013 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.

#ifndef DEVICE_NFC_NFC_NDEF_RECORD_H_
#define DEVICE_NFC_NFC_NDEF_RECORD_H_

#include <string>
#include <vector>

#include "base/values.h"

namespace device {

// NfcNdefRecord represents an NDEF (NFC Data Exchange Format) record. NDEF is
// a light-weight binary format specified by the NFC Forum for transmission and
// storage of typed data over NFC. NDEF defines two constructs: NDEF records and
// messages. An NDEF record contains typed data, such as MIME-type media, a URI,
// or a custom application payload, whereas an NDEF message is a container for
// one or more NDEF records.
class NfcNdefRecord {
 public:
  // NDEF record types that define the payload of the NDEF record.
  enum Type {
    kTypeHandoverCarrier,
    kTypeHandoverRequest,
    kTypeHandoverSelect,
    kTypeSmartPoster,
    kTypeText,
    kTypeURI,
    kTypeUnknown
  };

  // The following are strings that define a possible field of an NDEF record.
  // These strings are used as the keys in the dictionary returned by |data|.
  // Not all fields are always present in an NDEF record, where the presence
  // of a field depends on the type of the record. While some fields are
  // required for a specific record type, others can be optional and won't
  // always be present.

  // Fields for type "Text".

  // The character encoding. When present, the value is one of |kEncodingUtf8|
  // and |kEncodingUtf16|. Otherwise, this field is optional.
  static const char kFieldEncoding[];

  // The ISO/IANA language code (e.g. "en" or "jp"). This field is optional.
  static const char kFieldLanguageCode[];

  // The human readable representation of a text. This field is mandatory.
  static const char kFieldText[];

  // Fields for type "URI".

  // The complete URI, including the scheme and the resource. This field is
  // required.
  static const char kFieldURI[];

  // The URI object MIME type. This is a description of the MIME type of the
  // object the URI points at. This field is optional.
  static const char kFieldMimeType[];

  // The size of the object the URI points at. This field is optional.
  // If present, the value is an unsigned integer. Since base/values.h does not
  // define an unsigned integer type, use a base::DoubleValue to store this.
  static const char kFieldTargetSize[];

  // Fields for type "SmartPoster". A SmartPoster can contain all possible
  // fields of a "URI" record, in addition to the following:

  // The "title" of the SmartPoster. This is an optional field. If present, the
  // value of this field is a list of dictionaries, where each dictionary
  // contains the possible fields of a "Text" record. If the list contains
  // more than one element, each element usually represents the same "title"
  // text in a different language.
  static const char kFieldTitles[];

  // The suggested course of action. The value of this field is one of
  // |kSmartPosterAction*|. This field is optional.
  static const char kFieldAction[];

  // Possible values for character encoding.
  static const char kEncodingUtf8[];
  static const char kEncodingUtf16[];

  // Possible actions defined by the NFC forum SmartPoster record type. Each
  // action is a suggestion to the application indicating the action it should
  // take with the contents of the record.

  // Do the action. e.g. open a URI, send an SMS, dial a phone number.
  static const char kSmartPosterActionDo[];

  // Store data, e.g. store an SMS, bookmark a URI, etc.
  static const char kSmartPosterActionSave[];

  // Open the data for editing.
  static const char kSmartPosterActionOpen[];

  NfcNdefRecord();
  virtual ~NfcNdefRecord();

  // Returns the type that defines the payload of this NDEF record.
  Type type() const { return type_; }

  // Returns the contents of this record in the form of a mapping from keys
  // declared above to their stored values.
  const base::DictionaryValue& data() const { return data_; }

  // Returns true, if this record has been populated via a call to "Populate".
  bool IsPopulated() const;

  // Populates the record with the contents of |data| and sets its type to
  // |type|. Returns true, if the record was successfully populated. If a
  // failure occurs, e.g. |data| contains values that are not allowed in
  // records of type |type| or if |data| does not contain mandatory fields of
  // |type|, this method returns false. Populating an instance of an
  // NfcNdefRecord is allowed only once and after a successful call to this
  // method, all subsequent calls to this method will fail. Use IsPopulated()
  // to determine if this record can be populated.
  bool Populate(Type type, const base::DictionaryValue* data);

 private:
  // The type of this record.
  Type type_;

  // The contents of the record.
  base::DictionaryValue data_;

  DISALLOW_COPY_AND_ASSIGN(NfcNdefRecord);
};

// NfcNdefMessage represent an NDEF message. An NDEF message, contains one or
// more NDEF records and the order in which the records are stored dictates the
// order in which applications are meant to interpret them. For example, a
// client may decide to dispatch to applications based on the first record in
// the sequence.
class NfcNdefMessage {
 public:
  // Typedef for a list of NDEF records.
  typedef std::vector<NfcNdefRecord*> RecordList;

  NfcNdefMessage();
  virtual ~NfcNdefMessage();

  // The NDEF records that are contained in this message.
  const RecordList& records() const { return records_; }

  // Adds the NDEF record |record| to the sequence of records that this
  // NfcNdefMessage contains. This method simply adds the record to this message
  // and does NOT take ownership of it.
  void AddRecord(NfcNdefRecord* record);

  // Removes the NDEF record |record| from this message. Returns true, if the
  // record was removed, otherwise returns false if |record| was not contained
  // in this message.
  bool RemoveRecord(NfcNdefRecord* record);

 private:
  // The NDEF records that are contained by this message.
  RecordList records_;

  DISALLOW_COPY_AND_ASSIGN(NfcNdefMessage);
};

}  // namespace device

#endif  // DEVICE_NFC_NFC_NDEF_RECORD_H_