OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 #include "chrome/browser/ui/webui/chromeos/nfc_debug_ui.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/values.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/common/url_constants.h" | |
12 #include "content/public/browser/web_ui.h" | |
13 #include "content/public/browser/web_ui_data_source.h" | |
14 #include "content/public/browser/web_ui_message_handler.h" | |
15 #include "device/nfc/nfc_adapter.h" | |
16 #include "device/nfc/nfc_adapter_factory.h" | |
17 #include "device/nfc/nfc_peer.h" | |
18 #include "device/nfc/nfc_tag.h" | |
19 #include "grit/browser_resources.h" | |
20 #include "grit/generated_resources.h" | |
21 | |
22 using device::NfcAdapter; | |
23 using device::NfcAdapterFactory; | |
24 using device::NfcNdefMessage; | |
25 using device::NfcNdefRecord; | |
26 using device::NfcNdefTagTechnology; | |
27 using device::NfcPeer; | |
28 using device::NfcTag; | |
29 using device::NfcTagTechnology; | |
30 | |
31 namespace chromeos { | |
32 | |
33 namespace { | |
34 | |
35 // Keys for various NFC properties that are used by JS. | |
36 const char kAdapterPeersProperty[] = "peers"; | |
37 const char kAdapterPollingProperty[] = "polling"; | |
38 const char kAdapterPoweredProperty[] = "powered"; | |
39 const char kAdapterPresentProperty[] = "present"; | |
40 const char kAdapterTagsProperty[] = "tags"; | |
41 | |
42 const char kPeerIdentifierProperty[] = "identifier"; | |
43 const char kPeerRecordsProperty[] = "records"; | |
44 | |
45 const char kTagIdentifierProperty[] = "identifier"; | |
46 const char kTagTypeProperty[] = "type"; | |
47 const char kTagReadOnlyProperty[] = "readOnly"; | |
48 const char kTagRecordsProperty[] = "records"; | |
49 const char kTagSupportedProtocolProperty[] = "supportedProtocol"; | |
50 const char kTagSupportedTechnologiesProperty[] = "supportedTechnologies"; | |
51 const char kRecordTypeProperty[] = "type"; | |
52 | |
53 // Tag types. | |
54 const char kTagType1[] = "TYPE-1"; | |
55 const char kTagType2[] = "TYPE-2"; | |
56 const char kTagType3[] = "TYPE-3"; | |
57 const char kTagType4[] = "TYPE-4"; | |
58 const char kTagTypeUnknown[] = "UNKNOWN"; | |
59 | |
60 // NFC transfer protocols. | |
61 const char kTagProtocolFelica[] = "FELICA"; | |
62 const char kTagProtocolIsoDep[] = "ISO-DEP"; | |
63 const char kTagProtocolJewel[] = "JEWEL"; | |
64 const char kTagProtocolMifare[] = "MIFARE"; | |
65 const char kTagProtocolNfcDep[] = "NFC-DEP"; | |
66 const char kTagProtocolUnknown[] = "UNKNOWN"; | |
67 | |
68 // NFC tag technologies. | |
69 const char kTagTechnologyNfcA[] = "NFC-A"; | |
70 const char kTagTechnologyNfcB[] = "NFC-B"; | |
71 const char kTagTechnologyNfcF[] = "NFC-F"; | |
72 const char kTagTechnologyNfcV[] = "NFC-V"; | |
73 const char kTagTechnologyIsoDep[] = "ISO-DEP"; | |
74 const char kTagTechnologyNdef[] = "NDEF"; | |
75 | |
76 // NDEF types. | |
77 const char kTypeHandoverCarrier[] = "HANDOVER_CARRIER"; | |
78 const char kTypeHandoverRequest[] = "HANDOVER_REQUEST"; | |
79 const char kTypeHandoverSelect[] = "HANDOVER_SELECT"; | |
80 const char kTypeSmartPoster[] = "SMART_POSTER"; | |
81 const char kTypeText[] = "TEXT"; | |
82 const char kTypeURI[] = "URI"; | |
83 const char kTypeUnknown[] = "UNKNOWN"; | |
84 | |
85 // JavaScript callback constants. | |
86 const char kInitializeCallback[] = "initialize"; | |
87 const char kSetAdapterPowerCallback[] = "setAdapterPower"; | |
88 const char kSetAdapterPollingCallback[] = "setAdapterPolling"; | |
89 const char kSubmitRecordFormCallback[] = "submitRecordForm"; | |
90 | |
91 // Constants for JavaScript functions that we can call. | |
92 const char kOnNfcAdapterInfoChangedFunction[] = | |
93 "nfcDebug.NfcDebugUI.onNfcAdapterInfoChanged"; | |
94 const char kOnNfcAvailabilityDeterminedFunction[] = | |
95 "nfcDebug.NfcDebugUI.onNfcAvailabilityDetermined"; | |
96 const char kOnNfcPeerDeviceInfoChangedFunction[] = | |
97 "nfcDebug.NfcDebugUI.onNfcPeerDeviceInfoChanged"; | |
98 const char kOnNfcTagInfoChangedFunction[] = | |
99 "nfcDebug.NfcDebugUI.onNfcTagInfoChanged"; | |
100 const char kOnSetAdapterPollingFailedFunction[] = | |
101 "nfcDebug.NfcDebugUI.onSetAdapterPollingFailed"; | |
102 const char kOnSetAdapterPowerFailedFunction[] = | |
103 "nfcDebug.NfcDebugUI.onSetAdapterPowerFailed"; | |
104 const char kOnSubmitRecordFormFailedFunction[] = | |
105 "nfcDebug.NfcDebugUI.onSubmitRecordFormFailed"; | |
106 | |
107 std::string RecordTypeToString(NfcNdefRecord::Type type) { | |
108 switch (type) { | |
109 case NfcNdefRecord::kTypeHandoverCarrier: | |
110 return kTypeHandoverCarrier; | |
111 case NfcNdefRecord::kTypeHandoverRequest: | |
112 return kTypeHandoverRequest; | |
113 case NfcNdefRecord::kTypeHandoverSelect: | |
114 return kTypeHandoverSelect; | |
115 case NfcNdefRecord::kTypeSmartPoster: | |
116 return kTypeSmartPoster; | |
117 case NfcNdefRecord::kTypeText: | |
118 return kTypeText; | |
119 case NfcNdefRecord::kTypeURI: | |
120 return kTypeURI; | |
121 case NfcNdefRecord::kTypeUnknown: | |
122 return kTypeUnknown; | |
123 } | |
124 return kTypeUnknown; | |
125 } | |
126 | |
127 NfcNdefRecord::Type RecordTypeStringValueToEnum(const std::string& type) { | |
128 if (type == kTypeHandoverCarrier) | |
129 return NfcNdefRecord::kTypeHandoverCarrier; | |
130 if (type == kTypeHandoverRequest) | |
131 return NfcNdefRecord::kTypeHandoverRequest; | |
132 if (type == kTypeHandoverSelect) | |
133 return NfcNdefRecord::kTypeHandoverSelect; | |
134 if (type == kTypeSmartPoster) | |
135 return NfcNdefRecord::kTypeSmartPoster; | |
136 if (type == kTypeText) | |
137 return NfcNdefRecord::kTypeText; | |
138 if (type == kTypeURI) | |
139 return NfcNdefRecord::kTypeURI; | |
140 return NfcNdefRecord::kTypeUnknown; | |
141 } | |
142 | |
143 std::string TagTypeToString(NfcTag::TagType type) { | |
144 switch (type) { | |
145 case NfcTag::kTagType1: | |
146 return kTagType1; | |
147 case NfcTag::kTagType2: | |
148 return kTagType2; | |
149 case NfcTag::kTagType3: | |
150 return kTagType3; | |
151 case NfcTag::kTagType4: | |
152 return kTagType4; | |
153 case NfcTag::kTagTypeUnknown: | |
154 return kTagTypeUnknown; | |
155 } | |
156 return kTagTypeUnknown; | |
157 } | |
158 | |
159 std::string TagProtocolToString(NfcTag::Protocol protocol) { | |
160 switch (protocol) { | |
161 case NfcTag::kProtocolFelica: | |
162 return kTagProtocolFelica; | |
163 case NfcTag::kProtocolIsoDep: | |
164 return kTagProtocolIsoDep; | |
165 case NfcTag::kProtocolJewel: | |
166 return kTagProtocolJewel; | |
167 case NfcTag::kProtocolMifare: | |
168 return kTagProtocolMifare; | |
169 case NfcTag::kProtocolNfcDep: | |
170 return kTagProtocolNfcDep; | |
171 case NfcTag::kProtocolUnknown: | |
172 return kTagProtocolUnknown; | |
173 } | |
174 return kTagProtocolUnknown; | |
175 } | |
176 | |
177 // content::WebUIMessageHandler implementation for this page. | |
178 class NfcDebugMessageHandler : public content::WebUIMessageHandler, | |
179 public NfcAdapter::Observer, | |
180 public NfcNdefTagTechnology::Observer, | |
181 public NfcPeer::Observer, | |
182 public NfcTag::Observer { | |
183 public: | |
184 NfcDebugMessageHandler(); | |
185 virtual ~NfcDebugMessageHandler(); | |
186 | |
187 // WebUIMessageHandler implementation. | |
188 virtual void RegisterMessages() OVERRIDE; | |
189 | |
190 // NfcAdapter::Observer overrides. | |
191 virtual void AdapterPresentChanged( | |
192 NfcAdapter* adapter, | |
193 bool present) OVERRIDE; | |
194 virtual void AdapterPoweredChanged( | |
195 NfcAdapter* adapter, | |
196 bool powered) OVERRIDE; | |
197 virtual void AdapterPollingChanged( | |
198 NfcAdapter* adapter, | |
199 bool polling) OVERRIDE; | |
200 virtual void TagFound(NfcAdapter* adapter, NfcTag* tag) OVERRIDE; | |
201 virtual void TagLost(NfcAdapter*adapter, NfcTag* tag) OVERRIDE; | |
202 virtual void PeerFound(NfcAdapter* adaper, NfcPeer* peer) OVERRIDE; | |
203 virtual void PeerLost(NfcAdapter* adapter, NfcPeer* peer) OVERRIDE; | |
204 | |
205 // NfcNdefTagTechnology::Observer override. | |
206 virtual void RecordReceived( | |
207 NfcTag* tag, | |
208 const NfcNdefRecord* record) OVERRIDE; | |
209 | |
210 // NfcPeer::Observer override. | |
211 virtual void RecordReceived( | |
212 NfcPeer* peer, | |
213 const NfcNdefRecord* record) OVERRIDE; | |
214 | |
215 // NfcTag::Observer override. | |
216 virtual void TagReady(NfcTag* tag) OVERRIDE; | |
217 | |
218 private: | |
219 // Called by the UI when the page loads. This method requests information | |
220 // about NFC availability on the current platform and requests the underlying | |
221 // Adapter object. The UI is notified once the information is available. | |
222 void Initialize(const base::ListValue* args); | |
223 | |
224 // Called by the UI to toggle the adapter power. |args| will contain one | |
225 // boolean that indicates whether the power should be set to ON or OFF. | |
226 void SetAdapterPower(const base::ListValue* args); | |
227 void OnSetAdapterPowerError(); | |
228 | |
229 // Called by the UI set the adapter's poll status. |args| will contain one | |
230 // boolean that indicates whether polling should be started or stopped. | |
231 void SetAdapterPolling(const base::ListValue* args); | |
232 void OnSetAdapterPollingError(); | |
233 | |
234 // Called by the UI to push an NDEF record to a remote device or tag. |args| | |
235 // will contain one dictionary that contains the record data. | |
236 void SubmitRecordForm(const base::ListValue* args); | |
237 void OnSubmitRecordFormFailed(const std::string& error_message); | |
238 | |
239 // Callback passed to NfcAdapterFactory::GetAdapter. | |
240 void OnGetAdapter(scoped_refptr<NfcAdapter> adapter); | |
241 | |
242 // Stores the properties of the NFC adapter in |out|, mapping these to keys | |
243 // that will be read by JS. | |
244 void GetAdapterProperties(base::DictionaryValue* out); | |
245 | |
246 // Stores the properties of the NFC peer in |out|, mapping these to keys | |
247 // that will be read by JS. |out| will not be modified, if no peer is known. | |
248 void GetPeerProperties(base::DictionaryValue* out); | |
249 | |
250 // Stores the properties of the NFC tag in |out|, mapping these to keys | |
251 // that will be read by JS. |out| will not be modified, if no tag is known. | |
252 void GetTagProperties(base::DictionaryValue* out); | |
253 | |
254 // Returns the records in |message| by populating |out|, in which | |
255 // they have been converted to a JS friendly format. | |
256 void GetRecordList(const NfcNdefMessage& message, base::ListValue* out); | |
257 | |
258 // Updates the data displayed in the UI for the current adapter. | |
259 void UpdateAdapterInfo(); | |
260 | |
261 // Updates the data displayed in the UI for the current peer. | |
262 void UpdatePeerInfo(); | |
263 | |
264 // Updates the data displayed in the UI for the current tag. | |
265 void UpdateTagInfo(); | |
266 | |
267 // The NfcAdapter object. | |
268 scoped_refptr<NfcAdapter> nfc_adapter_; | |
269 | |
270 // The cached identifier of the most recent NFC peer device found. | |
271 std::string peer_identifier_; | |
272 | |
273 // The cached identifier of the most recent NFC tag found. | |
274 std::string tag_identifier_; | |
275 | |
276 DISALLOW_COPY_AND_ASSIGN(NfcDebugMessageHandler); | |
277 }; | |
278 | |
279 NfcDebugMessageHandler::NfcDebugMessageHandler() { | |
280 } | |
281 | |
282 NfcDebugMessageHandler::~NfcDebugMessageHandler() { | |
283 } | |
284 | |
285 void NfcDebugMessageHandler::AdapterPresentChanged( | |
286 NfcAdapter* adapter, | |
287 bool present) { | |
288 UpdateAdapterInfo(); | |
289 } | |
290 | |
291 void NfcDebugMessageHandler::AdapterPoweredChanged( | |
292 NfcAdapter* adapter, | |
293 bool powered) { | |
294 UpdateAdapterInfo(); | |
295 } | |
296 | |
297 void NfcDebugMessageHandler::AdapterPollingChanged( | |
298 NfcAdapter* adapter, | |
299 bool polling) { | |
300 UpdateAdapterInfo(); | |
301 } | |
302 | |
303 void NfcDebugMessageHandler::TagFound(NfcAdapter* adapter, NfcTag* tag) { | |
304 VLOG(1) << "Found NFC tag: " << tag->GetIdentifier(); | |
305 tag->AddObserver(this); | |
306 tag_identifier_ = tag->GetIdentifier(); | |
307 tag->GetNdefTagTechnology()->AddObserver(this); | |
308 UpdateAdapterInfo(); | |
309 } | |
310 | |
311 void NfcDebugMessageHandler::TagLost(NfcAdapter*adapter, NfcTag* tag) { | |
312 VLOG(1) << "Lost NFC tag: " << tag->GetIdentifier(); | |
313 tag->RemoveObserver(this); | |
314 tag->GetNdefTagTechnology()->RemoveObserver(this); | |
315 tag_identifier_.clear(); | |
316 UpdateAdapterInfo(); | |
317 UpdateTagInfo(); | |
318 } | |
319 | |
320 void NfcDebugMessageHandler::PeerFound(NfcAdapter* adaper, NfcPeer* peer) { | |
321 VLOG(1) << "Found NFC peer device: " << peer->GetIdentifier(); | |
322 peer->AddObserver(this); | |
323 peer_identifier_ = peer->GetIdentifier(); | |
324 UpdateAdapterInfo(); | |
325 UpdatePeerInfo(); | |
326 } | |
327 | |
328 void NfcDebugMessageHandler::PeerLost(NfcAdapter* adapter, NfcPeer* peer) { | |
329 VLOG(1) << "Lost NFC peer device: " << peer->GetIdentifier(); | |
330 peer->RemoveObserver(this); | |
331 peer_identifier_.clear(); | |
332 UpdateAdapterInfo(); | |
333 UpdatePeerInfo(); | |
334 } | |
335 | |
336 void NfcDebugMessageHandler::RecordReceived( | |
337 NfcTag* tag, | |
338 const NfcNdefRecord* record) { | |
339 if (tag->GetIdentifier() != tag_identifier_) { | |
340 LOG(WARNING) << "Records received from unknown tag: " | |
341 << tag->GetIdentifier(); | |
342 return; | |
343 } | |
344 UpdateTagInfo(); | |
345 } | |
346 | |
347 void NfcDebugMessageHandler::RecordReceived( | |
348 NfcPeer* peer, | |
349 const NfcNdefRecord* record) { | |
350 if (peer->GetIdentifier() != peer_identifier_) { | |
351 LOG(WARNING) << "Records received from unknown peer: " | |
352 << peer->GetIdentifier(); | |
353 return; | |
354 } | |
355 UpdatePeerInfo(); | |
356 } | |
357 | |
358 void NfcDebugMessageHandler::TagReady(NfcTag* tag) { | |
359 if (tag_identifier_ != tag->GetIdentifier()) { | |
360 LOG(WARNING) << "Unknown tag became ready: " << tag->GetIdentifier(); | |
361 return; | |
362 } | |
363 VLOG(1) << "Tag ready: " << tag->GetIdentifier(); | |
364 UpdateTagInfo(); | |
365 } | |
366 | |
367 void NfcDebugMessageHandler::RegisterMessages() { | |
368 web_ui()->RegisterMessageCallback( | |
369 kInitializeCallback, | |
370 base::Bind(&NfcDebugMessageHandler::Initialize, | |
371 base::Unretained(this))); | |
372 web_ui()->RegisterMessageCallback( | |
373 kSetAdapterPowerCallback, | |
374 base::Bind(&NfcDebugMessageHandler::SetAdapterPower, | |
375 base::Unretained(this))); | |
376 web_ui()->RegisterMessageCallback( | |
377 kSetAdapterPollingCallback, | |
378 base::Bind(&NfcDebugMessageHandler::SetAdapterPolling, | |
379 base::Unretained(this))); | |
380 web_ui()->RegisterMessageCallback( | |
381 kSubmitRecordFormCallback, | |
382 base::Bind(&NfcDebugMessageHandler::SubmitRecordForm, | |
383 base::Unretained(this))); | |
384 } | |
385 | |
386 void NfcDebugMessageHandler::Initialize(const base::ListValue* args) { | |
387 bool nfc_available = NfcAdapterFactory::IsNfcAvailable(); | |
388 base::FundamentalValue available(nfc_available); | |
389 web_ui()->CallJavascriptFunction(kOnNfcAvailabilityDeterminedFunction, | |
390 available); | |
391 if (!nfc_available) { | |
392 LOG(WARNING) << "NFC is not available on current platform."; | |
393 return; | |
394 } | |
395 NfcAdapterFactory::GetAdapter( | |
396 base::Bind(&NfcDebugMessageHandler::OnGetAdapter, | |
397 base::Unretained(this))); | |
398 } | |
399 | |
400 void NfcDebugMessageHandler::SetAdapterPower(const base::ListValue* args) { | |
401 DCHECK(1 == args->GetSize()); | |
402 DCHECK(nfc_adapter_.get()); | |
403 bool powered; | |
404 args->GetBoolean(0, &powered); | |
405 VLOG(1) << "Setting adapter power: " << powered; | |
406 nfc_adapter_->SetPowered( | |
407 powered, base::Bind(&base::DoNothing), | |
408 base::Bind(&NfcDebugMessageHandler::OnSetAdapterPowerError, | |
409 base::Unretained(this))); | |
410 } | |
411 | |
412 void NfcDebugMessageHandler::OnSetAdapterPowerError() { | |
413 LOG(ERROR) << "Failed to set NFC adapter power."; | |
414 web_ui()->CallJavascriptFunction(kOnSetAdapterPowerFailedFunction); | |
415 } | |
416 | |
417 void NfcDebugMessageHandler::SetAdapterPolling(const base::ListValue* args) { | |
418 DCHECK(1 == args->GetSize()); | |
419 DCHECK(nfc_adapter_.get()); | |
420 bool start = false; | |
421 bool result = args->GetBoolean(0, &start); | |
422 DCHECK(result); | |
stevenjb
2014/01/24 23:33:53
nit: If the source of |args| is a DBus service, I
armansito
2014/01/24 23:40:41
Actually this function is invoked from the JS and
xiyuan
2014/01/24 23:53:24
Usually, we CHECK(args->GetBoolean(0, &start)) in
| |
423 if (start) { | |
424 VLOG(1) << "Starting NFC poll loop."; | |
425 nfc_adapter_->StartPolling( | |
426 base::Bind(&base::DoNothing), | |
427 base::Bind(&NfcDebugMessageHandler::OnSetAdapterPollingError, | |
428 base::Unretained(this))); | |
429 } else { | |
430 VLOG(1) << "Stopping NFC poll loop."; | |
431 nfc_adapter_->StopPolling( | |
432 base::Bind(&base::DoNothing), | |
433 base::Bind(&NfcDebugMessageHandler::OnSetAdapterPollingError, | |
434 base::Unretained(this))); | |
435 } | |
436 } | |
437 | |
438 void NfcDebugMessageHandler::OnSetAdapterPollingError() { | |
439 LOG(ERROR) << "Failed to start/stop polling."; | |
440 web_ui()->CallJavascriptFunction(kOnSetAdapterPollingFailedFunction); | |
441 } | |
442 | |
443 void NfcDebugMessageHandler::SubmitRecordForm(const base::ListValue* args) { | |
444 DCHECK(1 == args->GetSize()); | |
445 DCHECK(nfc_adapter_.get()); | |
446 const base::DictionaryValue* record_data_const = NULL; | |
447 if (!args->GetDictionary(0, &record_data_const)) { | |
448 NOTREACHED(); | |
449 return; | |
450 } | |
451 | |
452 if (peer_identifier_.empty() && tag_identifier_.empty()) { | |
453 OnSubmitRecordFormFailed("No peer or tag present."); | |
454 return; | |
455 } | |
456 | |
457 std::string type; | |
458 if (!record_data_const->GetString(kRecordTypeProperty, &type)) { | |
459 OnSubmitRecordFormFailed("Record type not provided."); | |
460 return; | |
461 } | |
462 | |
463 base::DictionaryValue* record_data = record_data_const->DeepCopy(); | |
464 record_data->Remove(kRecordTypeProperty, NULL); | |
465 | |
466 // Convert the "targetSize" field to a double, in case JS stored it as an | |
467 // integer. | |
468 int target_size; | |
469 if (record_data->GetInteger(NfcNdefRecord::kFieldTargetSize, &target_size)) { | |
470 record_data->SetDouble(NfcNdefRecord::kFieldTargetSize, | |
471 static_cast<double>(target_size)); | |
472 } | |
473 | |
474 NfcNdefRecord record; | |
475 if (!record.Populate(RecordTypeStringValueToEnum(type), record_data)) { | |
476 OnSubmitRecordFormFailed("Invalid record data provided. Missing required " | |
477 "fields?"); | |
478 return; | |
479 } | |
480 | |
481 if (!peer_identifier_.empty()) { | |
482 NfcPeer* peer = nfc_adapter_->GetPeer(peer_identifier_); | |
483 if (!peer) { | |
484 OnSubmitRecordFormFailed("The current NFC adapter doesn't seem to know " | |
485 "about peer: " + peer_identifier_); | |
486 return; | |
487 } | |
488 NfcNdefMessage message; | |
489 message.AddRecord(&record); | |
490 peer->PushNdef(message, | |
491 base::Bind(&base::DoNothing), | |
492 base::Bind(&NfcDebugMessageHandler::OnSubmitRecordFormFailed, | |
493 base::Unretained(this), | |
494 "Failed to push NDEF record.")); | |
495 return; | |
496 } | |
497 NfcTag* tag = nfc_adapter_->GetTag(tag_identifier_); | |
498 if (!tag) { | |
499 OnSubmitRecordFormFailed("The current NFC tag doesn't seem to known about " | |
500 "tag: " + tag_identifier_); | |
501 return; | |
502 } | |
503 NfcNdefMessage message; | |
504 message.AddRecord(&record); | |
505 tag->GetNdefTagTechnology()->WriteNdef( | |
506 message, | |
507 base::Bind(&base::DoNothing), | |
508 base::Bind(&NfcDebugMessageHandler::OnSubmitRecordFormFailed, | |
509 base::Unretained(this), | |
510 "Failed to write NDEF record.")); | |
511 } | |
512 | |
513 void NfcDebugMessageHandler::OnSubmitRecordFormFailed( | |
514 const std::string& error_message) { | |
515 LOG(ERROR) << "SubmitRecordForm failed: " << error_message; | |
516 web_ui()->CallJavascriptFunction(kOnSubmitRecordFormFailedFunction, | |
517 base::StringValue(error_message)); | |
518 } | |
519 | |
520 void NfcDebugMessageHandler::OnGetAdapter( | |
521 scoped_refptr<NfcAdapter> adapter) { | |
522 if (nfc_adapter_.get()) | |
523 return; | |
524 nfc_adapter_ = adapter; | |
525 nfc_adapter_->AddObserver(this); | |
526 UpdateAdapterInfo(); | |
527 | |
528 NfcAdapter::PeerList peers; | |
529 nfc_adapter_->GetPeers(&peers); | |
530 for (NfcAdapter::PeerList::const_iterator iter = peers.begin(); | |
531 iter != peers.end(); ++iter) { | |
532 PeerFound(nfc_adapter_.get(), *iter); | |
533 } | |
534 } | |
535 | |
536 void NfcDebugMessageHandler::GetAdapterProperties( | |
537 base::DictionaryValue* out) { | |
538 if (!nfc_adapter_.get()) { | |
539 VLOG(1) << "NFC adapter hasn't been received yet."; | |
540 return; | |
541 } | |
542 out->SetBoolean(kAdapterPresentProperty, nfc_adapter_->IsPresent()); | |
543 out->SetBoolean(kAdapterPollingProperty, nfc_adapter_->IsPolling()); | |
544 out->SetBoolean(kAdapterPoweredProperty, nfc_adapter_->IsPowered()); | |
545 | |
546 NfcAdapter::PeerList peers; | |
547 nfc_adapter_->GetPeers(&peers); | |
548 out->SetInteger(kAdapterPeersProperty, static_cast<int>(peers.size())); | |
549 | |
550 NfcAdapter::TagList tags; | |
551 nfc_adapter_->GetTags(&tags); | |
552 out->SetInteger(kAdapterTagsProperty, static_cast<int>(tags.size())); | |
553 } | |
554 | |
555 void NfcDebugMessageHandler::GetPeerProperties(base::DictionaryValue* out) { | |
556 if (peer_identifier_.empty()) { | |
557 VLOG(1) << "No known peer exists."; | |
558 return; | |
559 } | |
560 if (!nfc_adapter_.get()) { | |
561 VLOG(1) << "NFC adapter hasn't been received yet."; | |
562 return; | |
563 } | |
564 NfcPeer* peer = nfc_adapter_->GetPeer(peer_identifier_); | |
565 if (!peer) { | |
566 LOG(ERROR) << "The current NFC adapter doesn't seem to know about peer: " | |
567 << peer_identifier_; | |
568 return; | |
569 } | |
570 out->SetString(kPeerIdentifierProperty, peer_identifier_); | |
571 | |
572 base::ListValue* records = new base::ListValue(); | |
573 GetRecordList(peer->GetNdefMessage(), records); | |
574 out->Set(kPeerRecordsProperty, records); | |
575 } | |
576 | |
577 void NfcDebugMessageHandler::GetTagProperties(base::DictionaryValue* out) { | |
578 if (tag_identifier_.empty()) { | |
579 VLOG(1) << "No known tag exists."; | |
580 return; | |
581 } | |
582 if (!nfc_adapter_.get()) { | |
583 VLOG(1) << "NFC adapter hasn't been received yet."; | |
584 return; | |
585 } | |
586 NfcTag* tag = nfc_adapter_->GetTag(tag_identifier_); | |
587 if (!tag) { | |
588 LOG(ERROR) << "The current NFC adapter doesn't seem to know about tag: " | |
589 << tag_identifier_; | |
590 return; | |
591 } | |
592 out->SetString(kTagIdentifierProperty, tag_identifier_); | |
593 out->SetString(kTagTypeProperty, TagTypeToString(tag->GetType())); | |
594 out->SetBoolean(kTagReadOnlyProperty, tag->IsReadOnly()); | |
595 out->SetString(kTagSupportedProtocolProperty, | |
596 TagProtocolToString(tag->GetSupportedProtocol())); | |
597 | |
598 base::ListValue* technologies = new base::ListValue(); | |
599 NfcTagTechnology::TechnologyTypeMask technology_mask = | |
600 tag->GetSupportedTechnologies(); | |
601 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcA) | |
602 technologies->AppendString(kTagTechnologyNfcA); | |
603 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcB) | |
604 technologies->AppendString(kTagTechnologyNfcB); | |
605 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcF) | |
606 technologies->AppendString(kTagTechnologyNfcF); | |
607 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcV) | |
608 technologies->AppendString(kTagTechnologyNfcV); | |
609 if (technology_mask & NfcTagTechnology::kTechnologyTypeIsoDep) | |
610 technologies->AppendString(kTagTechnologyIsoDep); | |
611 if (technology_mask & NfcTagTechnology::kTechnologyTypeNdef) | |
612 technologies->AppendString(kTagTechnologyNdef); | |
613 out->Set(kTagSupportedTechnologiesProperty, technologies); | |
614 | |
615 base::ListValue* records = new base::ListValue(); | |
616 GetRecordList(tag->GetNdefTagTechnology()->GetNdefMessage(), records); | |
617 out->Set(kTagRecordsProperty, records); | |
618 } | |
619 | |
620 void NfcDebugMessageHandler::GetRecordList(const NfcNdefMessage& message, | |
621 base::ListValue* out) { | |
622 for (NfcNdefMessage::RecordList::const_iterator iter = | |
623 message.records().begin(); | |
624 iter != message.records().end(); ++iter) { | |
625 const NfcNdefRecord* record = (*iter); | |
626 base::DictionaryValue* record_data = record->data().DeepCopy(); | |
627 record_data->SetString(kRecordTypeProperty, | |
628 RecordTypeToString(record->type())); | |
629 out->Append(record_data); | |
630 } | |
631 } | |
632 | |
633 void NfcDebugMessageHandler::UpdateAdapterInfo() { | |
634 base::DictionaryValue data; | |
635 GetAdapterProperties(&data); | |
636 web_ui()->CallJavascriptFunction(kOnNfcAdapterInfoChangedFunction, data); | |
637 } | |
638 | |
639 void NfcDebugMessageHandler::UpdatePeerInfo() { | |
640 base::DictionaryValue data; | |
641 GetPeerProperties(&data); | |
642 web_ui()->CallJavascriptFunction(kOnNfcPeerDeviceInfoChangedFunction, data); | |
643 } | |
644 | |
645 void NfcDebugMessageHandler::UpdateTagInfo() { | |
646 base::DictionaryValue data; | |
647 GetTagProperties(&data); | |
648 web_ui()->CallJavascriptFunction(kOnNfcTagInfoChangedFunction, data); | |
649 } | |
650 | |
651 } // namespace | |
652 | |
653 NfcDebugUI::NfcDebugUI(content::WebUI* web_ui) | |
654 : content::WebUIController(web_ui) { | |
655 web_ui->AddMessageHandler(new NfcDebugMessageHandler()); | |
656 | |
657 content::WebUIDataSource* html_source = | |
658 content::WebUIDataSource::Create(chrome::kChromeUINfcDebugHost); | |
659 html_source->SetUseJsonJSFormatV2(); | |
660 | |
661 html_source->AddLocalizedString("titleText", IDS_NFC_DEBUG_TITLE); | |
662 html_source->AddLocalizedString("notSupportedText", | |
663 IDS_NFC_DEBUG_NOT_SUPPORTED); | |
664 html_source->AddLocalizedString("adapterHeaderText", | |
665 IDS_NFC_DEBUG_ADAPTER_HEADER); | |
666 html_source->AddLocalizedString("adapterPowerOnText", | |
667 IDS_NFC_DEBUG_ADAPTER_POWER_ON); | |
668 html_source->AddLocalizedString("adapterPowerOffText", | |
669 IDS_NFC_DEBUG_ADAPTER_POWER_OFF); | |
670 html_source->AddLocalizedString("adapterStartPollText", | |
671 IDS_NFC_DEBUG_ADAPTER_START_POLL); | |
672 html_source->AddLocalizedString("adapterStopPollText", | |
673 IDS_NFC_DEBUG_ADAPTER_STOP_POLL); | |
674 html_source->AddLocalizedString("ndefFormHeaderText", | |
675 IDS_NFC_DEBUG_NDEF_FORM_HEADER); | |
676 html_source->AddLocalizedString("ndefFormTypeTextText", | |
677 IDS_NFC_DEBUG_NDEF_FORM_TYPE_TEXT); | |
678 html_source->AddLocalizedString("ndefFormTypeUriText", | |
679 IDS_NFC_DEBUG_NDEF_FORM_TYPE_URI); | |
680 html_source->AddLocalizedString("ndefFormTypeSmartPosterText", | |
681 IDS_NFC_DEBUG_NDEF_FORM_TYPE_SMART_POSTER); | |
682 html_source->AddLocalizedString("ndefFormWriteButtonText", | |
683 IDS_NFC_DEBUG_NDEF_FORM_WRITE_BUTTON); | |
684 html_source->AddLocalizedString("ndefFormFieldTextText", | |
685 IDS_NFC_DEBUG_NDEF_FORM_FIELD_TEXT); | |
686 html_source->AddLocalizedString("ndefFormFieldEncodingText", | |
687 IDS_NFC_DEBUG_NDEF_FORM_FIELD_ENCODING); | |
688 html_source->AddLocalizedString("ndefFormFieldLanguageCodeText", | |
689 IDS_NFC_DEBUG_NDEF_FORM_FIELD_LANGUAGE_CODE); | |
690 html_source->AddLocalizedString("ndefFormFieldUriText", | |
691 IDS_NFC_DEBUG_NDEF_FORM_FIELD_URI); | |
692 html_source->AddLocalizedString("ndefFormFieldMimeTypeText", | |
693 IDS_NFC_DEBUG_NDEF_FORM_FIELD_MIME_TYPE); | |
694 html_source->AddLocalizedString("ndefFormFieldTargetSizeText", | |
695 IDS_NFC_DEBUG_NDEF_FORM_FIELD_TARGET_SIZE); | |
696 html_source->AddLocalizedString("ndefFormFieldTitleTextText", | |
697 IDS_NFC_DEBUG_NDEF_FORM_FIELD_TITLE_TEXT); | |
698 html_source->AddLocalizedString("ndefFormFieldTitleEncodingText", | |
699 IDS_NFC_DEBUG_NDEF_FORM_FIELD_TITLE_ENCODING); | |
700 html_source->AddLocalizedString( | |
701 "ndefFormFieldTitleLanguageCodeText", | |
702 IDS_NFC_DEBUG_NDEF_FORM_FIELD_TITLE_LANGUAGE_CODE); | |
703 html_source->AddLocalizedString("ndefFormPushButtonText", | |
704 IDS_NFC_DEBUG_NDEF_FORM_PUSH_BUTTON); | |
705 html_source->AddLocalizedString("nfcPeerHeaderText", | |
706 IDS_NFC_DEBUG_NFC_PEER_HEADER); | |
707 html_source->AddLocalizedString("nfcTagHeaderText", | |
708 IDS_NFC_DEBUG_NFC_TAG_HEADER); | |
709 html_source->AddLocalizedString("recordsHeaderText", | |
710 IDS_NFC_DEBUG_RECORDS_HEADER); | |
711 html_source->AddLocalizedString("errorFailedToSetPowerText", | |
712 IDS_NFC_DEBUG_ERROR_FAILED_TO_SET_POWER); | |
713 html_source->AddLocalizedString("errorFailedToSetPollingText", | |
714 IDS_NFC_DEBUG_ERROR_FAILED_TO_SET_POLLING); | |
715 html_source->AddLocalizedString("errorFailedToSubmitPrefixText", | |
716 IDS_NFC_DEBUG_ERROR_FAILED_TO_SUBMIT_PREFIX); | |
717 html_source->SetJsonPath("strings.js"); | |
718 | |
719 // Add required resources. | |
720 html_source->AddResourcePath("nfc_debug.css", IDR_NFC_DEBUG_CSS); | |
721 html_source->AddResourcePath("nfc_debug.js", IDR_NFC_DEBUG_JS); | |
722 html_source->SetDefaultResource(IDR_NFC_DEBUG_HTML); | |
723 | |
724 Profile* profile = Profile::FromWebUI(web_ui); | |
725 content::WebUIDataSource::Add(profile, html_source); | |
726 } | |
727 | |
728 NfcDebugUI::~NfcDebugUI() { | |
729 } | |
730 | |
731 } // namespace chromeos | |
OLD | NEW |