Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: chrome/browser/ui/webui/chromeos/nfc_debug_ui.cc

Issue 131103007: Add chrome://nfc-debug for debugging NFC on Chrome OS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reupload. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "nfc_debug.NfcDebugUI.onNfcAdapterInfoChanged";
94 const char kOnNfcAvailabilityDeterminedFunction[] =
95 "nfc_debug.NfcDebugUI.onNfcAvailabilityDetermined";
96 const char kOnNfcPeerDeviceInfoChangedFunction[] =
97 "nfc_debug.NfcDebugUI.onNfcPeerDeviceInfoChanged";
98 const char kOnNfcTagInfoChangedFunction[] =
99 "nfc_debug.NfcDebugUI.onNfcTagInfoChanged";
100 const char kOnSetAdapterPollingFailedFunction[] =
101 "nfc_debug.NfcDebugUI.onSetAdapterPollingFailed";
102 const char kOnSetAdapterPowerFailedFunction[] =
103 "nfc_debug.NfcDebugUI.onSetAdapterPowerFailed";
104 const char kOnSubmitRecordFormFailedFunction[] =
105 "nfc_debug.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 default:
stevenjb 2014/01/23 21:57:57 remove default so that compiler will catch additio
armansito 2014/01/24 20:50:33 Done.
122 return kTypeUnknown;
123 }
124 }
125
126 NfcNdefRecord::Type RecordTypeStringValueToEnum(const std::string& type) {
127 if (type == kTypeHandoverCarrier)
128 return NfcNdefRecord::kTypeHandoverCarrier;
129 if (type == kTypeHandoverRequest)
130 return NfcNdefRecord::kTypeHandoverRequest;
131 if (type == kTypeHandoverSelect)
132 return NfcNdefRecord::kTypeHandoverSelect;
133 if (type == kTypeSmartPoster)
134 return NfcNdefRecord::kTypeSmartPoster;
135 if (type == kTypeText)
136 return NfcNdefRecord::kTypeText;
137 if (type == kTypeURI)
138 return NfcNdefRecord::kTypeURI;
139 return NfcNdefRecord::kTypeUnknown;
140 }
141
142 std::string TagTypeToString(NfcTag::TagType type) {
143 switch (type) {
144 case NfcTag::kTagType1:
145 return kTagType1;
146 case NfcTag::kTagType2:
147 return kTagType2;
148 case NfcTag::kTagType3:
149 return kTagType3;
150 case NfcTag::kTagType4:
151 return kTagType4;
152 default:
153 return kTagTypeUnknown;
stevenjb 2014/01/23 21:57:57 Same here.
armansito 2014/01/24 20:50:33 Done.
154 }
155 }
156
157 std::string TagProtocolToString(NfcTag::Protocol protocol) {
158 switch (protocol) {
159 case NfcTag::kProtocolFelica:
160 return kTagProtocolFelica;
161 case NfcTag::kProtocolIsoDep:
162 return kTagProtocolIsoDep;
163 case NfcTag::kProtocolJewel:
164 return kTagProtocolJewel;
165 case NfcTag::kProtocolMifare:
166 return kTagProtocolMifare;
167 case NfcTag::kProtocolNfcDep:
168 return kTagProtocolNfcDep;
169 default:
170 return kTagProtocolUnknown;
stevenjb 2014/01/23 21:57:57 Same here.
armansito 2014/01/24 20:50:33 Done.
171 }
172 }
173
174 // content::WebUIMessageHandler implementation for this page.
175 class NfcDebugMessageHandler : public content::WebUIMessageHandler,
176 public NfcAdapter::Observer,
177 public NfcNdefTagTechnology::Observer,
178 public NfcPeer::Observer,
179 public NfcTag::Observer {
180 public:
181 NfcDebugMessageHandler();
182 virtual ~NfcDebugMessageHandler();
183
184 // WebUIMessageHandler implementation.
185 virtual void RegisterMessages() OVERRIDE;
186
187 // NfcAdapter::Observer overrides.
188 virtual void AdapterPresentChanged(
189 NfcAdapter* adapter, bool present) OVERRIDE;
stevenjb 2014/01/23 21:57:57 one line per arg unless entire declaration fits on
armansito 2014/01/24 20:50:33 Done.
190 virtual void AdapterPoweredChanged(
191 NfcAdapter* adapter, bool powered) OVERRIDE;
192 virtual void AdapterPollingChanged(
193 NfcAdapter* adapter, bool polling) OVERRIDE;
194 virtual void TagFound(NfcAdapter* adapter, NfcTag* tag) OVERRIDE;
195 virtual void TagLost(NfcAdapter*adapter, NfcTag* tag) OVERRIDE;
196 virtual void PeerFound(NfcAdapter* adaper, NfcPeer* peer) OVERRIDE;
197 virtual void PeerLost(NfcAdapter* adapter, NfcPeer* peer) OVERRIDE;
198
199 // NfcNdefTagTechnology::Observer override.
200 virtual void RecordReceived(
201 NfcTag* tag, const NfcNdefRecord* record) OVERRIDE;
202
203 // NfcPeer::Observer override.
204 virtual void RecordReceived(
205 NfcPeer* peer, const NfcNdefRecord* record) OVERRIDE;
206
207 // NfcTag::Observer override.
208 virtual void TagReady(NfcTag* tag) OVERRIDE;
209
210 private:
211 // Called by the UI when the page loads. This method requests information
212 // about NFC availability on the current platform and requests the underlying
213 // Adapter object. The UI is notified once the information is available.
214 void Initialize(const base::ListValue* args);
215
216 // Called by the UI to toggle the adapter power. |args| will contain one
217 // boolean that indicates whether the power should be set to ON or OFF.
218 void SetAdapterPower(const base::ListValue* args);
219 void OnSetAdapterPowerError();
220
221 // Called by the UI set the adapter's poll status. |args| will contain one
222 // boolean that indicates whether polling should be started or stopped.
223 void SetAdapterPolling(const base::ListValue* args);
224 void OnSetAdapterPollingError();
225
226 // Called by the UI to push an NDEF record to a remote device or tag. |args|
227 // will contain one dictionary that contains the record data.
228 void SubmitRecordForm(const base::ListValue* args);
229 void OnSubmitRecordFormFailed(const std::string& error_message);
230
231 // Callback passed to NfcAdapterFactory::GetAdapter.
232 void OnGetAdapter(scoped_refptr<NfcAdapter> adapter);
233
234 // Stores the properties of the NFC adapter in |out|, mapping these to keys
235 // that will be read by JS.
236 void GetAdapterProperties(base::DictionaryValue* out);
237
238 // Stores the properties of the NFC peer in |out|, mapping these to keys
239 // that will be read by JS. |out| will not be modified, if no peer is known.
240 void GetPeerProperties(base::DictionaryValue* out);
241
242 // Stores the properties of the NFC tag in |out|, mapping these to keys
243 // that will be read by JS. |out| will not be modified, if no tag is known.
244 void GetTagProperties(base::DictionaryValue* out);
245
246 // Returns the records in |message| by populating |out|, in which
247 // they have been converted to a JS friendly format.
248 void GetRecordList(const NfcNdefMessage& message, base::ListValue* out);
249
250 // Updates the data displayed in the UI for the current adapter.
251 void UpdateAdapterInfo();
252
253 // Updates the data displayed in the UI for the current peer.
254 void UpdatePeerInfo();
255
256 // Updates the data displayed in the UI for the current tag.
257 void UpdateTagInfo();
258
259 // The NfcAdapter object.
260 scoped_refptr<NfcAdapter> nfc_adapter_;
261
262 // The cached identifier of the most recent NFC peer device found.
263 std::string peer_identifier_;
264
265 // The cached identifier of the most recent NFC tag found.
266 std::string tag_identifier_;
267
268 DISALLOW_COPY_AND_ASSIGN(NfcDebugMessageHandler);
269 };
270
271 NfcDebugMessageHandler::NfcDebugMessageHandler() {
272 }
273
274 NfcDebugMessageHandler::~NfcDebugMessageHandler() {
275 }
276
277 void NfcDebugMessageHandler::AdapterPresentChanged(
278 NfcAdapter* adapter, bool present) {
279 UpdateAdapterInfo();
280 }
281
282 void NfcDebugMessageHandler::AdapterPoweredChanged(
283 NfcAdapter* adapter, bool powered) {
284 UpdateAdapterInfo();
285 }
286
287 void NfcDebugMessageHandler::AdapterPollingChanged(
288 NfcAdapter* adapter, bool polling) {
289 UpdateAdapterInfo();
290 }
291
292 void NfcDebugMessageHandler::TagFound(NfcAdapter* adapter, NfcTag* tag) {
293 VLOG(1) << "Found NFC tag: " << tag->GetIdentifier();
294 tag->AddObserver(this);
295 tag_identifier_ = tag->GetIdentifier();
296 tag->GetNdefTagTechnology()->AddObserver(this);
297 UpdateAdapterInfo();
298 }
299
300 void NfcDebugMessageHandler::TagLost(NfcAdapter*adapter, NfcTag* tag) {
301 VLOG(1) << "Lost NFC tag: " << tag->GetIdentifier();
302 tag->RemoveObserver(this);
303 tag->GetNdefTagTechnology()->RemoveObserver(this);
304 tag_identifier_.clear();
305 UpdateAdapterInfo();
306 UpdateTagInfo();
307 }
308
309 void NfcDebugMessageHandler::PeerFound(NfcAdapter* adaper, NfcPeer* peer) {
310 VLOG(1) << "Found NFC peer device: " << peer->GetIdentifier();
311 peer->AddObserver(this);
312 peer_identifier_ = peer->GetIdentifier();
313 UpdateAdapterInfo();
314 UpdatePeerInfo();
315 }
316
317 void NfcDebugMessageHandler::PeerLost(NfcAdapter* adapter, NfcPeer* peer) {
318 VLOG(1) << "Lost NFC peer device: " << peer->GetIdentifier();
319 peer->RemoveObserver(this);
320 peer_identifier_.clear();
321 UpdateAdapterInfo();
322 UpdatePeerInfo();
323 }
324
325 void NfcDebugMessageHandler::RecordReceived(
326 NfcTag* tag, const NfcNdefRecord* record) {
stevenjb 2014/01/23 21:57:57 one line per arg, here and below
armansito 2014/01/24 20:50:33 Done.
327 if (tag->GetIdentifier() != tag_identifier_) {
328 LOG(WARNING) << "Records received from unknown tag: "
329 << tag->GetIdentifier();
330 return;
331 }
332 UpdateTagInfo();
333 }
334
335 void NfcDebugMessageHandler::RecordReceived(
336 NfcPeer* peer, const NfcNdefRecord* record) {
337 if (peer->GetIdentifier() != peer_identifier_) {
338 LOG(WARNING) << "Records received from unknown peer: "
339 << peer->GetIdentifier();
340 return;
341 }
342 UpdatePeerInfo();
343 }
344
345 void NfcDebugMessageHandler::TagReady(NfcTag* tag) {
346 if (tag_identifier_ != tag->GetIdentifier()) {
347 LOG(WARNING) << "Unknown tag became ready: " << tag->GetIdentifier();
348 return;
349 }
350 VLOG(1) << "Tag ready: " << tag->GetIdentifier();
351 UpdateTagInfo();
352 }
353
354 void NfcDebugMessageHandler::RegisterMessages() {
355 web_ui()->RegisterMessageCallback(
356 kInitializeCallback,
357 base::Bind(&NfcDebugMessageHandler::Initialize,
358 base::Unretained(this)));
359 web_ui()->RegisterMessageCallback(
360 kSetAdapterPowerCallback,
361 base::Bind(&NfcDebugMessageHandler::SetAdapterPower,
362 base::Unretained(this)));
363 web_ui()->RegisterMessageCallback(
364 kSetAdapterPollingCallback,
365 base::Bind(&NfcDebugMessageHandler::SetAdapterPolling,
366 base::Unretained(this)));
367 web_ui()->RegisterMessageCallback(
368 kSubmitRecordFormCallback,
369 base::Bind(&NfcDebugMessageHandler::SubmitRecordForm,
370 base::Unretained(this)));
371 }
372
373 void NfcDebugMessageHandler::Initialize(const base::ListValue* args) {
374 bool nfc_available = NfcAdapterFactory::IsNfcAvailable();
375 base::FundamentalValue available(nfc_available);
376 web_ui()->CallJavascriptFunction(kOnNfcAvailabilityDeterminedFunction,
377 available);
378 if (!nfc_available) {
379 LOG(WARNING) << "NFC is not available on current platform.";
380 return;
381 }
382 NfcAdapterFactory::GetAdapter(
383 base::Bind(&NfcDebugMessageHandler::OnGetAdapter,
384 base::Unretained(this)));
385 }
386
387 void NfcDebugMessageHandler::SetAdapterPower(const base::ListValue* args) {
388 DCHECK(1 == args->GetSize());
389 DCHECK(nfc_adapter_.get());
390 bool powered;
391 args->GetBoolean(0, &powered);
392 VLOG(1) << "Setting adapter power: " << powered;
393 nfc_adapter_->SetPowered(
394 powered, base::Bind(&base::DoNothing),
395 base::Bind(&NfcDebugMessageHandler::OnSetAdapterPowerError,
396 base::Unretained(this)));
397 }
398
399 void NfcDebugMessageHandler::OnSetAdapterPowerError() {
400 LOG(ERROR) << "Failed to set NFC adapter power.";
401 web_ui()->CallJavascriptFunction(kOnSetAdapterPowerFailedFunction);
402 }
403
404 void NfcDebugMessageHandler:: SetAdapterPolling(const base::ListValue* args) {
405 DCHECK(1 == args->GetSize());
406 DCHECK(nfc_adapter_.get());
407 bool start;
stevenjb 2014/01/23 21:57:57 Initialize start or check result of GetBoolean.
armansito 2014/01/24 20:50:33 Done.
408 args->GetBoolean(0, &start);
409 if (start) {
410 VLOG(1) << "Starting NFC poll loop.";
411 nfc_adapter_->StartPolling(
412 base::Bind(&base::DoNothing),
413 base::Bind(&NfcDebugMessageHandler::OnSetAdapterPollingError,
414 base::Unretained(this)));
415 } else {
416 VLOG(1) << "Stopping NFC poll loop.";
417 nfc_adapter_->StopPolling(
418 base::Bind(&base::DoNothing),
419 base::Bind(&NfcDebugMessageHandler::OnSetAdapterPollingError,
420 base::Unretained(this)));
421 }
422 }
423
424 void NfcDebugMessageHandler::OnSetAdapterPollingError() {
425 LOG(ERROR) << "Failed to start/stop polling.";
426 web_ui()->CallJavascriptFunction(kOnSetAdapterPollingFailedFunction);
427 }
428
429 void NfcDebugMessageHandler::SubmitRecordForm(const base::ListValue* args) {
430 DCHECK(1 == args->GetSize());
431 DCHECK(nfc_adapter_.get());
432 const base::DictionaryValue* record_data_const = NULL;
433 if (!args->GetDictionary(0, &record_data_const)) {
434 NOTREACHED();
435 return;
436 }
437
438 if (peer_identifier_.empty() && tag_identifier_.empty()) {
439 OnSubmitRecordFormFailed("No peer or tag present.");
440 return;
441 }
442
443 std::string type;
444 if (!record_data_const->GetString(kRecordTypeProperty, &type)) {
445 OnSubmitRecordFormFailed("Record type not provided.");
446 return;
447 }
448
449 base::DictionaryValue* record_data = record_data_const->DeepCopy();
450 record_data->Remove(kRecordTypeProperty, NULL);
451
452 // Convert the "targetSize" field to a double, in case JS stored it as an
453 // integer.
454 int target_size;
455 if (record_data->GetInteger(NfcNdefRecord::kFieldTargetSize, &target_size)) {
456 record_data->SetDouble(NfcNdefRecord::kFieldTargetSize,
457 static_cast<double>(target_size));
458 }
459
460 NfcNdefRecord record;
461 if (!record.Populate(RecordTypeStringValueToEnum(type), record_data)) {
462 OnSubmitRecordFormFailed("Invalid record data provided. Missing required "
463 "fields?");
464 return;
465 }
466
467 if (!peer_identifier_.empty()) {
468 NfcPeer* peer = nfc_adapter_->GetPeer(peer_identifier_);
469 if (!peer) {
470 OnSubmitRecordFormFailed("The current NFC adapter doesn't seem to know "
471 "about peer: " + peer_identifier_);
472 return;
473 }
474 NfcNdefMessage message;
475 message.AddRecord(&record);
476 peer->PushNdef(message,
477 base::Bind(&base::DoNothing),
478 base::Bind(&NfcDebugMessageHandler::OnSubmitRecordFormFailed,
479 base::Unretained(this),
480 "Failed to push NDEF record."));
481 return;
482 }
483 NfcTag* tag = nfc_adapter_->GetTag(tag_identifier_);
484 if (!tag) {
485 OnSubmitRecordFormFailed("The current NFC tag doesn't seem to known about "
486 "tag: " + tag_identifier_);
487 return;
488 }
489 NfcNdefMessage message;
490 message.AddRecord(&record);
491 tag->GetNdefTagTechnology()->WriteNdef(
492 message,
493 base::Bind(&base::DoNothing),
494 base::Bind(&NfcDebugMessageHandler::OnSubmitRecordFormFailed,
495 base::Unretained(this),
496 "Failed to write NDEF record."));
497 }
498
499 void NfcDebugMessageHandler::OnSubmitRecordFormFailed(
500 const std::string& error_message) {
501 LOG(ERROR) << "SubmitRecordForm failed: " << error_message;
502 web_ui()->CallJavascriptFunction(kOnSubmitRecordFormFailedFunction,
503 base::StringValue(error_message));
504 }
505
506 void NfcDebugMessageHandler::OnGetAdapter(
507 scoped_refptr<NfcAdapter> adapter) {
508 if (nfc_adapter_.get())
509 return;
510 nfc_adapter_ = adapter;
511 nfc_adapter_->AddObserver(this);
512 UpdateAdapterInfo();
513
514 NfcAdapter::PeerList peers;
515 nfc_adapter_->GetPeers(&peers);
516 for (NfcAdapter::PeerList::const_iterator iter = peers.begin();
517 iter != peers.end(); ++iter) {
518 PeerFound(nfc_adapter_.get(), *iter);
519 }
520 }
521
522 void NfcDebugMessageHandler::GetAdapterProperties(
523 base::DictionaryValue* out) {
524 if (!nfc_adapter_.get()) {
525 VLOG(1) << "NFC adapter hasn't been received yet.";
526 return;
527 }
528 out->SetBoolean(kAdapterPresentProperty, nfc_adapter_->IsPresent());
529 out->SetBoolean(kAdapterPollingProperty, nfc_adapter_->IsPolling());
530 out->SetBoolean(kAdapterPoweredProperty, nfc_adapter_->IsPowered());
531
532 NfcAdapter::PeerList peers;
533 nfc_adapter_->GetPeers(&peers);
534 out->SetInteger(kAdapterPeersProperty, static_cast<int>(peers.size()));
535
536 NfcAdapter::TagList tags;
537 nfc_adapter_->GetTags(&tags);
538 out->SetInteger(kAdapterTagsProperty, static_cast<int>(tags.size()));
539 }
540
541 void NfcDebugMessageHandler::GetPeerProperties(base::DictionaryValue* out) {
542 if (peer_identifier_.empty()) {
543 VLOG(1) << "No known peer exists.";
544 return;
545 }
546 if (!nfc_adapter_.get()) {
547 VLOG(1) << "NFC adapter hasn't been received yet.";
548 return;
549 }
550 NfcPeer* peer = nfc_adapter_->GetPeer(peer_identifier_);
551 if (!peer) {
552 LOG(ERROR) << "The current NFC adapter doesn't seem to know about peer: "
553 << peer_identifier_;
554 return;
555 }
556 out->SetString(kPeerIdentifierProperty, peer_identifier_);
557
558 base::ListValue* records = new base::ListValue();
559 GetRecordList(peer->GetNdefMessage(), records);
560 out->Set(kPeerRecordsProperty, records);
561 }
562
563 void NfcDebugMessageHandler::GetTagProperties(base::DictionaryValue* out) {
564 if (tag_identifier_.empty()) {
565 VLOG(1) << "No known tag exists.";
566 return;
567 }
568 if (!nfc_adapter_.get()) {
569 VLOG(1) << "NFC adapter hasn't been received yet.";
570 return;
571 }
572 NfcTag* tag = nfc_adapter_->GetTag(tag_identifier_);
573 if (!tag) {
574 LOG(ERROR) << "The current NFC adapter doesn't seem to know about tag: "
575 << tag_identifier_;
576 return;
577 }
578 out->SetString(kTagIdentifierProperty, tag_identifier_);
579 out->SetString(kTagTypeProperty, TagTypeToString(tag->GetType()));
580 out->SetBoolean(kTagReadOnlyProperty, tag->IsReadOnly());
581 out->SetString(kTagSupportedProtocolProperty,
582 TagProtocolToString(tag->GetSupportedProtocol()));
583
584 base::ListValue* technologies = new base::ListValue();
585 NfcTagTechnology::TechnologyTypeMask technology_mask =
586 tag->GetSupportedTechnologies();
587 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcA)
588 technologies->AppendString(kTagTechnologyNfcA);
589 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcB)
590 technologies->AppendString(kTagTechnologyNfcB);
591 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcF)
592 technologies->AppendString(kTagTechnologyNfcF);
593 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcV)
594 technologies->AppendString(kTagTechnologyNfcV);
595 if (technology_mask & NfcTagTechnology::kTechnologyTypeIsoDep)
596 technologies->AppendString(kTagTechnologyIsoDep);
597 if (technology_mask & NfcTagTechnology::kTechnologyTypeNdef)
598 technologies->AppendString(kTagTechnologyNdef);
599 out->Set(kTagSupportedTechnologiesProperty, technologies);
600
601 base::ListValue* records = new base::ListValue();
602 GetRecordList(tag->GetNdefTagTechnology()->GetNdefMessage(), records);
603 out->Set(kTagRecordsProperty, records);
604 }
605
606 void NfcDebugMessageHandler::GetRecordList(const NfcNdefMessage& message,
607 base::ListValue* out) {
608 for (NfcNdefMessage::RecordList::const_iterator iter =
609 message.records().begin();
610 iter != message.records().end(); ++iter) {
611 const NfcNdefRecord* record = (*iter);
612 base::DictionaryValue* record_data = record->data().DeepCopy();
613 record_data->SetString(kRecordTypeProperty,
614 RecordTypeToString(record->type()));
615 out->Append(record_data);
616 }
617 }
618
619 void NfcDebugMessageHandler::UpdateAdapterInfo() {
620 base::DictionaryValue data;
621 GetAdapterProperties(&data);
622 web_ui()->CallJavascriptFunction(kOnNfcAdapterInfoChangedFunction, data);
623 }
624
625 void NfcDebugMessageHandler::UpdatePeerInfo() {
626 base::DictionaryValue data;
627 GetPeerProperties(&data);
628 web_ui()->CallJavascriptFunction(kOnNfcPeerDeviceInfoChangedFunction, data);
629 }
630
631 void NfcDebugMessageHandler::UpdateTagInfo() {
632 base::DictionaryValue data;
633 GetTagProperties(&data);
634 web_ui()->CallJavascriptFunction(kOnNfcTagInfoChangedFunction, data);
635 }
636
637 } // namespace
638
639 NfcDebugUI::NfcDebugUI(content::WebUI* web_ui)
640 : content::WebUIController(web_ui) {
641 web_ui->AddMessageHandler(new NfcDebugMessageHandler());
642
643 content::WebUIDataSource* html_source =
644 content::WebUIDataSource::Create(chrome::kChromeUINfcDebugHost);
645 html_source->SetUseJsonJSFormatV2();
646
647 html_source->SetJsonPath("strings.js");
648
649 // Add required resources.
650 html_source->AddResourcePath("nfc_debug.css", IDR_NFC_DEBUG_CSS);
651 html_source->AddResourcePath("nfc_debug.js", IDR_NFC_DEBUG_JS);
652 html_source->SetDefaultResource(IDR_NFC_DEBUG_HTML);
653
654 Profile* profile = Profile::FromWebUI(web_ui);
655 content::WebUIDataSource::Add(profile, html_source);
656 }
657
658 NfcDebugUI::~NfcDebugUI() {
659 }
660
661 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698