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
202
203
204
205
206
207
208
209
210
211
212
213
|
// 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.
#include "device/nfc/nfc_ndef_record.h"
#include <map>
#include "base/logging.h"
using base::DictionaryValue;
using base::ListValue;
namespace device {
namespace {
typedef std::map<std::string, base::Value::Type> FieldValueMap;
bool CheckFieldsAreValid(
const FieldValueMap& required_fields,
const FieldValueMap& optional_fields,
const DictionaryValue* data) {
size_t required_count = 0;
for (DictionaryValue::Iterator iter(*data);
!iter.IsAtEnd(); iter.Advance()) {
FieldValueMap::const_iterator field_iter =
required_fields.find(iter.key());
if (field_iter == required_fields.end()) {
// Field wasn't one of the required fields. Check if optional.
field_iter = optional_fields.find(iter.key());
if (field_iter == optional_fields.end()) {
// If the field isn't one of the optional fields either, then it's
// invalid.
VLOG(1) << "Tried to populate record with invalid field: "
<< iter.key();
return false;
}
} else {
required_count++;
}
// The field is invalid, if the type of its value is incorrect.
if (field_iter->second != iter.value().GetType()) {
VLOG(1) << "Provided value for field \"" << iter.key() << "\" has type "
<< iter.value().GetType() << ", expected: "
<< field_iter->second;
return false;
}
}
// Check for required fields.
if (required_count != required_fields.size()) {
VLOG(1) << "Provided data did not contain all required fields for "
<< "requested NDEF type.";
return false;
}
return true;
}
// Verifies that the contents of |data| conform to the fields of NDEF type
// "Text".
bool HandleTypeText(const DictionaryValue* data) {
VLOG(1) << "Populating record with type \"Text\".";
FieldValueMap required_fields;
required_fields[NfcNdefRecord::kFieldText] = base::Value::TYPE_STRING;
FieldValueMap optional_fields;
optional_fields[NfcNdefRecord::kFieldEncoding] = base::Value::TYPE_STRING;
optional_fields[NfcNdefRecord::kFieldLanguageCode] = base::Value::TYPE_STRING;
if (!CheckFieldsAreValid(required_fields, optional_fields, data)) {
VLOG(1) << "Failed to populate record.";
return false;
}
return true;
}
// Verifies that the contents of |data| conform to the fields of NDEF type
// "SmartPoster".
bool HandleTypeSmartPoster(const DictionaryValue* data) {
VLOG(1) << "Populating record with type \"SmartPoster\".";
FieldValueMap required_fields;
required_fields[NfcNdefRecord::kFieldURI] = base::Value::TYPE_STRING;
FieldValueMap optional_fields;
optional_fields[NfcNdefRecord::kFieldAction] = base::Value::TYPE_STRING;
optional_fields[NfcNdefRecord::kFieldMimeType] = base::Value::TYPE_STRING;
// base::Value restricts the number types to BOOL, INTEGER, and DOUBLE only.
// uint32 will automatically get converted to a double. "target size" is
// really a uint32 but we define it as a double for this reason.
// (See dbus/values_util.h).
optional_fields[NfcNdefRecord::kFieldTargetSize] = base::Value::TYPE_DOUBLE;
optional_fields[NfcNdefRecord::kFieldTitles] = base::Value::TYPE_LIST;
if (!CheckFieldsAreValid(required_fields, optional_fields, data)) {
VLOG(1) << "Failed to populate record.";
return false;
}
// Verify that the "titles" field was formatted correctly, if it exists.
const ListValue* titles = NULL;
if (data->GetList(NfcNdefRecord::kFieldTitles, &titles)) {
if (titles->empty()) {
VLOG(1) << "\"titles\" field of SmartPoster is empty.";
return false;
}
for (ListValue::const_iterator iter = titles->begin();
iter != titles->end(); ++iter) {
const DictionaryValue* title_data = NULL;
if (!(*iter)->GetAsDictionary(&title_data)) {
VLOG(1) << "\"title\" entry for SmartPoster contains an invalid value "
<< "type";
return false;
}
if (!HandleTypeText(title_data)) {
VLOG(1) << "Badly formatted \"title\" entry for SmartPoster.";
return false;
}
}
}
return true;
}
// Verifies that the contents of |data| conform to the fields of NDEF type
// "URI".
bool HandleTypeUri(const DictionaryValue* data) {
VLOG(1) << "Populating record with type \"URI\".";
FieldValueMap required_fields;
required_fields[NfcNdefRecord::kFieldURI] = base::Value::TYPE_STRING;
FieldValueMap optional_fields;
optional_fields[NfcNdefRecord::kFieldMimeType] = base::Value::TYPE_STRING;
optional_fields[NfcNdefRecord::kFieldTargetSize] = base::Value::TYPE_DOUBLE;
if (!CheckFieldsAreValid(required_fields, optional_fields, data)) {
VLOG(1) << "Failed to populate record.";
return false;
}
return true;
}
} // namespace
// static
const char NfcNdefRecord::kFieldEncoding[] = "encoding";
// static
const char NfcNdefRecord::kFieldLanguageCode[] = "languageCode";
// static
const char NfcNdefRecord::kFieldText[] = "text";
// static
const char NfcNdefRecord::kFieldURI[] = "uri";
// static
const char NfcNdefRecord::kFieldMimeType[] = "mimeType";
// static
const char NfcNdefRecord::kFieldTargetSize[] = "targetSize";
// static
const char NfcNdefRecord::kFieldTitles[] = "titles";
// static
const char NfcNdefRecord::kFieldAction[] = "action";
// static
const char NfcNdefRecord::kEncodingUtf8[] = "UTF-8";
// static
const char NfcNdefRecord::kEncodingUtf16[] = "UTF-16";
// static
const char NfcNdefRecord::kSmartPosterActionDo[] = "do";
// static
const char NfcNdefRecord::kSmartPosterActionSave[] = "save";
// static
const char NfcNdefRecord::kSmartPosterActionOpen[] = "open";
NfcNdefRecord::NfcNdefRecord() : type_(kTypeUnknown) {
}
NfcNdefRecord::~NfcNdefRecord() {
}
bool NfcNdefRecord::IsPopulated() const {
return type_ != kTypeUnknown;
}
bool NfcNdefRecord::Populate(Type type, const DictionaryValue* data) {
if (IsPopulated())
return false;
DCHECK(data_.empty());
// At this time, only "Text", "URI", and "SmartPoster" are supported.
bool result = false;
switch (type) {
case kTypeText:
result = HandleTypeText(data);
break;
case kTypeSmartPoster:
result = HandleTypeSmartPoster(data);
break;
case kTypeURI:
result = HandleTypeUri(data);
break;
default:
VLOG(1) << "Unsupported NDEF type: " << type;
break;
}
if (!result)
return false;
type_ = type;
data_.MergeDictionary(data);
return true;
}
NfcNdefMessage::NfcNdefMessage() {
}
NfcNdefMessage::~NfcNdefMessage() {
}
void NfcNdefMessage::AddRecord(NfcNdefRecord* record) {
records_.push_back(record);
}
} // namespace device
|