OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef DEVICE_NFC_NFC_NDEF_RECORD_H_ | |
6 #define DEVICE_NFC_NFC_NDEF_RECORD_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/values.h" | |
13 | |
14 namespace device { | |
15 | |
16 // NfcNdefRecord represents an NDEF (NFC Data Exchange Format) record. NDEF is | |
17 // a light-weight binary format specified by the NFC Forum for transmission and | |
18 // storage of typed data over NFC. NDEF defines two constructs: NDEF records and | |
19 // messages. An NDEF record contains typed data, such as MIME-type media, a URI, | |
20 // or a custom application payload, whereas an NDEF message is a container for | |
21 // one or more NDEF records. | |
22 class NfcNdefRecord { | |
23 public: | |
24 // NDEF record types that define the payload of the NDEF record. | |
25 enum Type { | |
26 kTypeHandoverCarrier, | |
27 kTypeHandoverRequest, | |
28 kTypeHandoverSelect, | |
29 kTypeSmartPoster, | |
30 kTypeText, | |
31 kTypeURI, | |
32 kTypeUnknown | |
33 }; | |
34 | |
35 // The following are strings that define a possible field of an NDEF record. | |
36 // These strings are used as the keys in the dictionary returned by |data|. | |
37 // Not all fields are always present in an NDEF record, where the presence | |
38 // of a field depends on the type of the record. While some fields are | |
39 // required for a specific record type, others can be optional and won't | |
40 // always be present. | |
41 | |
42 // Fields for type "Text". | |
43 | |
44 // The character encoding. When present, the value is one of |kEncodingUtf8| | |
45 // and |kEncodingUtf16|. Otherwise, this field is optional. | |
46 static const char kFieldEncoding[]; | |
47 | |
48 // The ISO/IANA language code (e.g. "en" or "jp"). This field is optional. | |
49 static const char kFieldLanguageCode[]; | |
50 | |
51 // The human readable representation of a text. This field is mandatory. | |
52 static const char kFieldText[]; | |
53 | |
54 // Fields for type "URI". | |
55 | |
56 // The complete URI, including the scheme and the resource. This field is | |
57 // required. | |
58 static const char kFieldURI[]; | |
59 | |
60 // The URI object MIME type. This is a description of the MIME type of the | |
61 // object the URI points at. This field is optional. | |
62 static const char kFieldMimeType[]; | |
63 | |
64 // The size of the object the URI points at. This field is optional. | |
65 // If present, the value is an unsigned integer. Since base/values.h does not | |
66 // define an unsigned integer type, use a base::DoubleValue to store this. | |
67 static const char kFieldTargetSize[]; | |
68 | |
69 // Fields for type "SmartPoster". A SmartPoster can contain all possible | |
70 // fields of a "URI" record, in addition to the following: | |
71 | |
72 // The "title" of the SmartPoster. This is an optional field. If present, the | |
73 // value of this field is a list of dictionaries, where each dictionary | |
74 // contains the possible fields of a "Text" record. If the list contains | |
75 // more than one element, each element usually represents the same "title" | |
76 // text in a different language. | |
77 static const char kFieldTitles[]; | |
78 | |
79 // The suggested course of action. The value of this field is one of | |
80 // |kSmartPosterAction*|. This field is optional. | |
81 static const char kFieldAction[]; | |
82 | |
83 // Possible values for character encoding. | |
84 static const char kEncodingUtf8[]; | |
85 static const char kEncodingUtf16[]; | |
86 | |
87 // Possible actions defined by the NFC forum SmartPoster record type. Each | |
88 // action is a suggestion to the application indicating the action it should | |
89 // take with the contents of the record. | |
90 | |
91 // Do the action. e.g. open a URI, send an SMS, dial a phone number. | |
92 static const char kSmartPosterActionDo[]; | |
93 | |
94 // Store data, e.g. store an SMS, bookmark a URI, etc. | |
95 static const char kSmartPosterActionSave[]; | |
96 | |
97 // Open the data for editing. | |
98 static const char kSmartPosterActionOpen[]; | |
99 | |
100 NfcNdefRecord(); | |
101 virtual ~NfcNdefRecord(); | |
102 | |
103 // Returns the type that defines the payload of this NDEF record. | |
104 Type type() const { return type_; } | |
105 | |
106 // Returns the contents of this record in the form of a mapping from keys | |
107 // declared above to their stored values. | |
108 const base::DictionaryValue& data() const { return data_; } | |
109 | |
110 // Returns true, if this record has been populated via a call to "Populate". | |
111 bool IsPopulated() const; | |
112 | |
113 // Populates the record with the contents of |data| and sets its type to | |
114 // |type|. Returns true, if the record was successfully populated. If a | |
115 // failure occurs, e.g. |data| contains values that are not allowed in | |
116 // records of type |type| or if |data| does not contain mandatory fields of | |
117 // |type|, this method returns false. Populating an instance of an | |
118 // NfcNdefRecord is allowed only once and after a successful call to this | |
119 // method, all subsequent calls to this method will fail. Use IsPopulated() | |
120 // to determine if this record can be populated. | |
121 bool Populate(Type type, const base::DictionaryValue* data); | |
122 | |
123 private: | |
124 // The type of this record. | |
125 Type type_; | |
126 | |
127 // The contents of the record. | |
128 base::DictionaryValue data_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(NfcNdefRecord); | |
131 }; | |
132 | |
133 // NfcNdefMessage represent an NDEF message. An NDEF message, contains one or | |
134 // more NDEF records and the order in which the records are stored dictates the | |
135 // order in which applications are meant to interpret them. For example, a | |
136 // client may decide to dispatch to applications based on the first record in | |
137 // the sequence. | |
138 class NfcNdefMessage { | |
139 public: | |
140 // Typedef for a list of NDEF records. | |
141 typedef std::vector<NfcNdefRecord*> RecordList; | |
142 | |
143 NfcNdefMessage(); | |
144 virtual ~NfcNdefMessage(); | |
145 | |
146 // The NDEF records that are contained in this message. | |
147 const RecordList& records() const { return records_; } | |
148 | |
149 // Adds the NDEF record |record| to the sequence of records that this | |
150 // NfcNdefMessage contains. This method simply adds the record to this message | |
151 // and does NOT take ownership of it. | |
152 void AddRecord(NfcNdefRecord* record); | |
153 | |
154 // Removes the NDEF record |record| from this message. Returns true, if the | |
155 // record was removed, otherwise returns false if |record| was not contained | |
156 // in this message. | |
157 bool RemoveRecord(NfcNdefRecord* record); | |
158 | |
159 private: | |
160 // The NDEF records that are contained by this message. | |
161 RecordList records_; | |
162 | |
163 DISALLOW_COPY_AND_ASSIGN(NfcNdefMessage); | |
164 }; | |
165 | |
166 } // namespace device | |
167 | |
168 #endif // DEVICE_NFC_NFC_NDEF_RECORD_H_ | |
OLD | NEW |