Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / nfc_debug_ui.cc
blob9f3244e74a0058619ec3b1569bf79fefdd7e937e
1 // Copyright 2014 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.
5 #include "chrome/browser/ui/webui/chromeos/nfc_debug_ui.h"
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 "chrome/grit/generated_resources.h"
13 #include "content/public/browser/web_ui.h"
14 #include "content/public/browser/web_ui_data_source.h"
15 #include "content/public/browser/web_ui_message_handler.h"
16 #include "device/nfc/nfc_adapter.h"
17 #include "device/nfc/nfc_adapter_factory.h"
18 #include "device/nfc/nfc_peer.h"
19 #include "device/nfc/nfc_tag.h"
20 #include "grit/browser_resources.h"
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;
31 namespace chromeos {
33 namespace {
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";
42 const char kPeerIdentifierProperty[] = "identifier";
43 const char kPeerRecordsProperty[] = "records";
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";
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";
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";
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";
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";
85 // JavaScript callback constants.
86 const char kInitializeCallback[] = "initialize";
87 const char kSetAdapterPowerCallback[] = "setAdapterPower";
88 const char kSetAdapterPollingCallback[] = "setAdapterPolling";
89 const char kSubmitRecordFormCallback[] = "submitRecordForm";
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";
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;
124 return kTypeUnknown;
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;
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;
156 return kTagTypeUnknown;
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;
174 return kTagProtocolUnknown;
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 ~NfcDebugMessageHandler() override;
187 // WebUIMessageHandler implementation.
188 void RegisterMessages() override;
190 // NfcAdapter::Observer overrides.
191 void AdapterPresentChanged(NfcAdapter* adapter, bool present) override;
192 void AdapterPoweredChanged(NfcAdapter* adapter, bool powered) override;
193 void AdapterPollingChanged(NfcAdapter* adapter, bool polling) override;
194 void TagFound(NfcAdapter* adapter, NfcTag* tag) override;
195 void TagLost(NfcAdapter* adapter, NfcTag* tag) override;
196 void PeerFound(NfcAdapter* adaper, NfcPeer* peer) override;
197 void PeerLost(NfcAdapter* adapter, NfcPeer* peer) override;
199 // NfcNdefTagTechnology::Observer override.
200 void RecordReceived(NfcTag* tag, const NfcNdefRecord* record) override;
202 // NfcPeer::Observer override.
203 void RecordReceived(NfcPeer* peer, const NfcNdefRecord* record) override;
205 // NfcTag::Observer override.
206 void TagReady(NfcTag* tag) override;
208 private:
209 // Called by the UI when the page loads. This method requests information
210 // about NFC availability on the current platform and requests the underlying
211 // Adapter object. The UI is notified once the information is available.
212 void Initialize(const base::ListValue* args);
214 // Called by the UI to toggle the adapter power. |args| will contain one
215 // boolean that indicates whether the power should be set to ON or OFF.
216 void SetAdapterPower(const base::ListValue* args);
217 void OnSetAdapterPowerError();
219 // Called by the UI set the adapter's poll status. |args| will contain one
220 // boolean that indicates whether polling should be started or stopped.
221 void SetAdapterPolling(const base::ListValue* args);
222 void OnSetAdapterPollingError();
224 // Called by the UI to push an NDEF record to a remote device or tag. |args|
225 // will contain one dictionary that contains the record data.
226 void SubmitRecordForm(const base::ListValue* args);
227 void OnSubmitRecordFormFailed(const std::string& error_message);
229 // Callback passed to NfcAdapterFactory::GetAdapter.
230 void OnGetAdapter(scoped_refptr<NfcAdapter> adapter);
232 // Stores the properties of the NFC adapter in |out|, mapping these to keys
233 // that will be read by JS.
234 void GetAdapterProperties(base::DictionaryValue* out);
236 // Stores the properties of the NFC peer in |out|, mapping these to keys
237 // that will be read by JS. |out| will not be modified, if no peer is known.
238 void GetPeerProperties(base::DictionaryValue* out);
240 // Stores the properties of the NFC tag in |out|, mapping these to keys
241 // that will be read by JS. |out| will not be modified, if no tag is known.
242 void GetTagProperties(base::DictionaryValue* out);
244 // Returns the records in |message| by populating |out|, in which
245 // they have been converted to a JS friendly format.
246 void GetRecordList(const NfcNdefMessage& message, base::ListValue* out);
248 // Updates the data displayed in the UI for the current adapter.
249 void UpdateAdapterInfo();
251 // Updates the data displayed in the UI for the current peer.
252 void UpdatePeerInfo();
254 // Updates the data displayed in the UI for the current tag.
255 void UpdateTagInfo();
257 // The NfcAdapter object.
258 scoped_refptr<NfcAdapter> nfc_adapter_;
260 // The cached identifier of the most recent NFC peer device found.
261 std::string peer_identifier_;
263 // The cached identifier of the most recent NFC tag found.
264 std::string tag_identifier_;
266 DISALLOW_COPY_AND_ASSIGN(NfcDebugMessageHandler);
269 NfcDebugMessageHandler::NfcDebugMessageHandler() {
272 NfcDebugMessageHandler::~NfcDebugMessageHandler() {
273 if (!nfc_adapter_.get())
274 return;
275 nfc_adapter_->RemoveObserver(this);
276 if (!peer_identifier_.empty()) {
277 NfcPeer* peer = nfc_adapter_->GetPeer(peer_identifier_);
278 if (peer)
279 peer->RemoveObserver(this);
281 if (!tag_identifier_.empty()) {
282 NfcTag* tag = nfc_adapter_->GetTag(tag_identifier_);
283 if (tag)
284 tag->RemoveObserver(this);
288 void NfcDebugMessageHandler::AdapterPresentChanged(
289 NfcAdapter* adapter,
290 bool present) {
291 UpdateAdapterInfo();
294 void NfcDebugMessageHandler::AdapterPoweredChanged(
295 NfcAdapter* adapter,
296 bool powered) {
297 UpdateAdapterInfo();
300 void NfcDebugMessageHandler::AdapterPollingChanged(
301 NfcAdapter* adapter,
302 bool polling) {
303 UpdateAdapterInfo();
306 void NfcDebugMessageHandler::TagFound(NfcAdapter* adapter, NfcTag* tag) {
307 VLOG(1) << "Found NFC tag: " << tag->GetIdentifier();
308 tag->AddObserver(this);
309 tag_identifier_ = tag->GetIdentifier();
310 tag->GetNdefTagTechnology()->AddObserver(this);
311 UpdateAdapterInfo();
312 UpdateTagInfo();
315 void NfcDebugMessageHandler::TagLost(NfcAdapter*adapter, NfcTag* tag) {
316 VLOG(1) << "Lost NFC tag: " << tag->GetIdentifier();
317 tag->RemoveObserver(this);
318 tag->GetNdefTagTechnology()->RemoveObserver(this);
319 tag_identifier_.clear();
320 UpdateAdapterInfo();
321 UpdateTagInfo();
324 void NfcDebugMessageHandler::PeerFound(NfcAdapter* adaper, NfcPeer* peer) {
325 VLOG(1) << "Found NFC peer device: " << peer->GetIdentifier();
326 peer->AddObserver(this);
327 peer_identifier_ = peer->GetIdentifier();
328 UpdateAdapterInfo();
329 UpdatePeerInfo();
332 void NfcDebugMessageHandler::PeerLost(NfcAdapter* adapter, NfcPeer* peer) {
333 VLOG(1) << "Lost NFC peer device: " << peer->GetIdentifier();
334 peer->RemoveObserver(this);
335 peer_identifier_.clear();
336 UpdateAdapterInfo();
337 UpdatePeerInfo();
340 void NfcDebugMessageHandler::RecordReceived(
341 NfcTag* tag,
342 const NfcNdefRecord* record) {
343 if (tag->GetIdentifier() != tag_identifier_) {
344 LOG(WARNING) << "Records received from unknown tag: "
345 << tag->GetIdentifier();
346 return;
348 UpdateTagInfo();
351 void NfcDebugMessageHandler::RecordReceived(
352 NfcPeer* peer,
353 const NfcNdefRecord* record) {
354 if (peer->GetIdentifier() != peer_identifier_) {
355 LOG(WARNING) << "Records received from unknown peer: "
356 << peer->GetIdentifier();
357 return;
359 UpdatePeerInfo();
362 void NfcDebugMessageHandler::TagReady(NfcTag* tag) {
363 if (tag_identifier_ != tag->GetIdentifier()) {
364 LOG(WARNING) << "Unknown tag became ready: " << tag->GetIdentifier();
365 return;
367 VLOG(1) << "Tag ready: " << tag->GetIdentifier();
368 UpdateTagInfo();
371 void NfcDebugMessageHandler::RegisterMessages() {
372 web_ui()->RegisterMessageCallback(
373 kInitializeCallback,
374 base::Bind(&NfcDebugMessageHandler::Initialize,
375 base::Unretained(this)));
376 web_ui()->RegisterMessageCallback(
377 kSetAdapterPowerCallback,
378 base::Bind(&NfcDebugMessageHandler::SetAdapterPower,
379 base::Unretained(this)));
380 web_ui()->RegisterMessageCallback(
381 kSetAdapterPollingCallback,
382 base::Bind(&NfcDebugMessageHandler::SetAdapterPolling,
383 base::Unretained(this)));
384 web_ui()->RegisterMessageCallback(
385 kSubmitRecordFormCallback,
386 base::Bind(&NfcDebugMessageHandler::SubmitRecordForm,
387 base::Unretained(this)));
390 void NfcDebugMessageHandler::Initialize(const base::ListValue* args) {
391 bool nfc_available = NfcAdapterFactory::IsNfcAvailable();
392 base::FundamentalValue available(nfc_available);
393 web_ui()->CallJavascriptFunction(kOnNfcAvailabilityDeterminedFunction,
394 available);
395 if (!nfc_available) {
396 LOG(WARNING) << "NFC is not available on current platform.";
397 return;
399 NfcAdapterFactory::GetAdapter(
400 base::Bind(&NfcDebugMessageHandler::OnGetAdapter,
401 base::Unretained(this)));
404 void NfcDebugMessageHandler::SetAdapterPower(const base::ListValue* args) {
405 DCHECK(1 == args->GetSize());
406 DCHECK(nfc_adapter_.get());
407 bool powered;
408 args->GetBoolean(0, &powered);
409 VLOG(1) << "Setting adapter power: " << powered;
410 nfc_adapter_->SetPowered(
411 powered, base::Bind(&base::DoNothing),
412 base::Bind(&NfcDebugMessageHandler::OnSetAdapterPowerError,
413 base::Unretained(this)));
416 void NfcDebugMessageHandler::OnSetAdapterPowerError() {
417 LOG(ERROR) << "Failed to set NFC adapter power.";
418 web_ui()->CallJavascriptFunction(kOnSetAdapterPowerFailedFunction);
421 void NfcDebugMessageHandler::SetAdapterPolling(const base::ListValue* args) {
422 DCHECK(1 == args->GetSize());
423 DCHECK(nfc_adapter_.get());
424 bool start = false;
425 bool result = args->GetBoolean(0, &start);
426 DCHECK(result);
427 if (start) {
428 VLOG(1) << "Starting NFC poll loop.";
429 nfc_adapter_->StartPolling(
430 base::Bind(&base::DoNothing),
431 base::Bind(&NfcDebugMessageHandler::OnSetAdapterPollingError,
432 base::Unretained(this)));
433 } else {
434 VLOG(1) << "Stopping NFC poll loop.";
435 nfc_adapter_->StopPolling(
436 base::Bind(&base::DoNothing),
437 base::Bind(&NfcDebugMessageHandler::OnSetAdapterPollingError,
438 base::Unretained(this)));
442 void NfcDebugMessageHandler::OnSetAdapterPollingError() {
443 LOG(ERROR) << "Failed to start/stop polling.";
444 web_ui()->CallJavascriptFunction(kOnSetAdapterPollingFailedFunction);
447 void NfcDebugMessageHandler::SubmitRecordForm(const base::ListValue* args) {
448 DCHECK(1 == args->GetSize());
449 DCHECK(nfc_adapter_.get());
450 const base::DictionaryValue* record_data_const = NULL;
451 if (!args->GetDictionary(0, &record_data_const)) {
452 NOTREACHED();
453 return;
456 if (peer_identifier_.empty() && tag_identifier_.empty()) {
457 OnSubmitRecordFormFailed("No peer or tag present.");
458 return;
461 std::string type;
462 if (!record_data_const->GetString(kRecordTypeProperty, &type)) {
463 OnSubmitRecordFormFailed("Record type not provided.");
464 return;
467 base::DictionaryValue* record_data = record_data_const->DeepCopy();
468 record_data->Remove(kRecordTypeProperty, NULL);
470 // Convert the "targetSize" field to a double, in case JS stored it as an
471 // integer.
472 int target_size;
473 if (record_data->GetInteger(NfcNdefRecord::kFieldTargetSize, &target_size)) {
474 record_data->SetDouble(NfcNdefRecord::kFieldTargetSize,
475 static_cast<double>(target_size));
478 NfcNdefRecord record;
479 if (!record.Populate(RecordTypeStringValueToEnum(type), record_data)) {
480 OnSubmitRecordFormFailed("Invalid record data provided. Missing required "
481 "fields?");
482 return;
485 if (!peer_identifier_.empty()) {
486 NfcPeer* peer = nfc_adapter_->GetPeer(peer_identifier_);
487 if (!peer) {
488 OnSubmitRecordFormFailed("The current NFC adapter doesn't seem to know "
489 "about peer: " + peer_identifier_);
490 return;
492 NfcNdefMessage message;
493 message.AddRecord(&record);
494 peer->PushNdef(message,
495 base::Bind(&base::DoNothing),
496 base::Bind(&NfcDebugMessageHandler::OnSubmitRecordFormFailed,
497 base::Unretained(this),
498 "Failed to push NDEF record."));
499 return;
501 NfcTag* tag = nfc_adapter_->GetTag(tag_identifier_);
502 if (!tag) {
503 OnSubmitRecordFormFailed("The current NFC tag doesn't seem to known about "
504 "tag: " + tag_identifier_);
505 return;
507 NfcNdefMessage message;
508 message.AddRecord(&record);
509 tag->GetNdefTagTechnology()->WriteNdef(
510 message,
511 base::Bind(&base::DoNothing),
512 base::Bind(&NfcDebugMessageHandler::OnSubmitRecordFormFailed,
513 base::Unretained(this),
514 "Failed to write NDEF record."));
517 void NfcDebugMessageHandler::OnSubmitRecordFormFailed(
518 const std::string& error_message) {
519 LOG(ERROR) << "SubmitRecordForm failed: " << error_message;
520 web_ui()->CallJavascriptFunction(kOnSubmitRecordFormFailedFunction,
521 base::StringValue(error_message));
524 void NfcDebugMessageHandler::OnGetAdapter(
525 scoped_refptr<NfcAdapter> adapter) {
526 if (nfc_adapter_.get())
527 return;
528 nfc_adapter_ = adapter;
529 nfc_adapter_->AddObserver(this);
530 UpdateAdapterInfo();
532 NfcAdapter::PeerList peers;
533 nfc_adapter_->GetPeers(&peers);
534 for (NfcAdapter::PeerList::const_iterator iter = peers.begin();
535 iter != peers.end(); ++iter) {
536 PeerFound(nfc_adapter_.get(), *iter);
539 NfcAdapter::TagList tags;
540 nfc_adapter_->GetTags(&tags);
541 for (NfcAdapter::TagList::const_iterator iter = tags.begin();
542 iter != tags.end(); ++iter) {
543 TagFound(nfc_adapter_.get(), *iter);
547 void NfcDebugMessageHandler::GetAdapterProperties(
548 base::DictionaryValue* out) {
549 if (!nfc_adapter_.get()) {
550 VLOG(1) << "NFC adapter hasn't been received yet.";
551 return;
553 out->SetBoolean(kAdapterPresentProperty, nfc_adapter_->IsPresent());
554 out->SetBoolean(kAdapterPollingProperty, nfc_adapter_->IsPolling());
555 out->SetBoolean(kAdapterPoweredProperty, nfc_adapter_->IsPowered());
557 NfcAdapter::PeerList peers;
558 nfc_adapter_->GetPeers(&peers);
559 out->SetInteger(kAdapterPeersProperty, static_cast<int>(peers.size()));
561 NfcAdapter::TagList tags;
562 nfc_adapter_->GetTags(&tags);
563 out->SetInteger(kAdapterTagsProperty, static_cast<int>(tags.size()));
566 void NfcDebugMessageHandler::GetPeerProperties(base::DictionaryValue* out) {
567 if (peer_identifier_.empty()) {
568 VLOG(1) << "No known peer exists.";
569 return;
571 if (!nfc_adapter_.get()) {
572 VLOG(1) << "NFC adapter hasn't been received yet.";
573 return;
575 NfcPeer* peer = nfc_adapter_->GetPeer(peer_identifier_);
576 if (!peer) {
577 LOG(ERROR) << "The current NFC adapter doesn't seem to know about peer: "
578 << peer_identifier_;
579 return;
581 out->SetString(kPeerIdentifierProperty, peer_identifier_);
583 base::ListValue* records = new base::ListValue();
584 GetRecordList(peer->GetNdefMessage(), records);
585 out->Set(kPeerRecordsProperty, records);
588 void NfcDebugMessageHandler::GetTagProperties(base::DictionaryValue* out) {
589 if (tag_identifier_.empty()) {
590 VLOG(1) << "No known tag exists.";
591 return;
593 if (!nfc_adapter_.get()) {
594 VLOG(1) << "NFC adapter hasn't been received yet.";
595 return;
597 NfcTag* tag = nfc_adapter_->GetTag(tag_identifier_);
598 if (!tag) {
599 LOG(ERROR) << "The current NFC adapter doesn't seem to know about tag: "
600 << tag_identifier_;
601 return;
603 out->SetString(kTagIdentifierProperty, tag_identifier_);
604 out->SetString(kTagTypeProperty, TagTypeToString(tag->GetType()));
605 out->SetBoolean(kTagReadOnlyProperty, tag->IsReadOnly());
606 out->SetString(kTagSupportedProtocolProperty,
607 TagProtocolToString(tag->GetSupportedProtocol()));
609 base::ListValue* technologies = new base::ListValue();
610 NfcTagTechnology::TechnologyTypeMask technology_mask =
611 tag->GetSupportedTechnologies();
612 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcA)
613 technologies->AppendString(kTagTechnologyNfcA);
614 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcB)
615 technologies->AppendString(kTagTechnologyNfcB);
616 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcF)
617 technologies->AppendString(kTagTechnologyNfcF);
618 if (technology_mask & NfcTagTechnology::kTechnologyTypeNfcV)
619 technologies->AppendString(kTagTechnologyNfcV);
620 if (technology_mask & NfcTagTechnology::kTechnologyTypeIsoDep)
621 technologies->AppendString(kTagTechnologyIsoDep);
622 if (technology_mask & NfcTagTechnology::kTechnologyTypeNdef)
623 technologies->AppendString(kTagTechnologyNdef);
624 out->Set(kTagSupportedTechnologiesProperty, technologies);
626 base::ListValue* records = new base::ListValue();
627 GetRecordList(tag->GetNdefTagTechnology()->GetNdefMessage(), records);
628 out->Set(kTagRecordsProperty, records);
631 void NfcDebugMessageHandler::GetRecordList(const NfcNdefMessage& message,
632 base::ListValue* out) {
633 for (NfcNdefMessage::RecordList::const_iterator iter =
634 message.records().begin();
635 iter != message.records().end(); ++iter) {
636 const NfcNdefRecord* record = (*iter);
637 base::DictionaryValue* record_data = record->data().DeepCopy();
638 record_data->SetString(kRecordTypeProperty,
639 RecordTypeToString(record->type()));
640 out->Append(record_data);
644 void NfcDebugMessageHandler::UpdateAdapterInfo() {
645 base::DictionaryValue data;
646 GetAdapterProperties(&data);
647 web_ui()->CallJavascriptFunction(kOnNfcAdapterInfoChangedFunction, data);
650 void NfcDebugMessageHandler::UpdatePeerInfo() {
651 base::DictionaryValue data;
652 GetPeerProperties(&data);
653 web_ui()->CallJavascriptFunction(kOnNfcPeerDeviceInfoChangedFunction, data);
656 void NfcDebugMessageHandler::UpdateTagInfo() {
657 base::DictionaryValue data;
658 GetTagProperties(&data);
659 web_ui()->CallJavascriptFunction(kOnNfcTagInfoChangedFunction, data);
662 } // namespace
664 NfcDebugUI::NfcDebugUI(content::WebUI* web_ui)
665 : content::WebUIController(web_ui) {
666 web_ui->AddMessageHandler(new NfcDebugMessageHandler());
668 content::WebUIDataSource* html_source =
669 content::WebUIDataSource::Create(chrome::kChromeUINfcDebugHost);
671 html_source->AddLocalizedString("titleText", IDS_NFC_DEBUG_TITLE);
672 html_source->AddLocalizedString("notSupportedText",
673 IDS_NFC_DEBUG_NOT_SUPPORTED);
674 html_source->AddLocalizedString("adapterHeaderText",
675 IDS_NFC_DEBUG_ADAPTER_HEADER);
676 html_source->AddLocalizedString("adapterPowerOnText",
677 IDS_NFC_DEBUG_ADAPTER_POWER_ON);
678 html_source->AddLocalizedString("adapterPowerOffText",
679 IDS_NFC_DEBUG_ADAPTER_POWER_OFF);
680 html_source->AddLocalizedString("adapterStartPollText",
681 IDS_NFC_DEBUG_ADAPTER_START_POLL);
682 html_source->AddLocalizedString("adapterStopPollText",
683 IDS_NFC_DEBUG_ADAPTER_STOP_POLL);
684 html_source->AddLocalizedString("ndefFormHeaderText",
685 IDS_NFC_DEBUG_NDEF_FORM_HEADER);
686 html_source->AddLocalizedString("ndefFormTypeTextText",
687 IDS_NFC_DEBUG_NDEF_FORM_TYPE_TEXT);
688 html_source->AddLocalizedString("ndefFormTypeUriText",
689 IDS_NFC_DEBUG_NDEF_FORM_TYPE_URI);
690 html_source->AddLocalizedString("ndefFormTypeSmartPosterText",
691 IDS_NFC_DEBUG_NDEF_FORM_TYPE_SMART_POSTER);
692 html_source->AddLocalizedString("ndefFormWriteButtonText",
693 IDS_NFC_DEBUG_NDEF_FORM_WRITE_BUTTON);
694 html_source->AddLocalizedString("ndefFormFieldTextText",
695 IDS_NFC_DEBUG_NDEF_FORM_FIELD_TEXT);
696 html_source->AddLocalizedString("ndefFormFieldEncodingText",
697 IDS_NFC_DEBUG_NDEF_FORM_FIELD_ENCODING);
698 html_source->AddLocalizedString("ndefFormFieldLanguageCodeText",
699 IDS_NFC_DEBUG_NDEF_FORM_FIELD_LANGUAGE_CODE);
700 html_source->AddLocalizedString("ndefFormFieldUriText",
701 IDS_NFC_DEBUG_NDEF_FORM_FIELD_URI);
702 html_source->AddLocalizedString("ndefFormFieldMimeTypeText",
703 IDS_NFC_DEBUG_NDEF_FORM_FIELD_MIME_TYPE);
704 html_source->AddLocalizedString("ndefFormFieldTargetSizeText",
705 IDS_NFC_DEBUG_NDEF_FORM_FIELD_TARGET_SIZE);
706 html_source->AddLocalizedString("ndefFormFieldTitleTextText",
707 IDS_NFC_DEBUG_NDEF_FORM_FIELD_TITLE_TEXT);
708 html_source->AddLocalizedString("ndefFormFieldTitleEncodingText",
709 IDS_NFC_DEBUG_NDEF_FORM_FIELD_TITLE_ENCODING);
710 html_source->AddLocalizedString(
711 "ndefFormFieldTitleLanguageCodeText",
712 IDS_NFC_DEBUG_NDEF_FORM_FIELD_TITLE_LANGUAGE_CODE);
713 html_source->AddLocalizedString("ndefFormPushButtonText",
714 IDS_NFC_DEBUG_NDEF_FORM_PUSH_BUTTON);
715 html_source->AddLocalizedString("nfcPeerHeaderText",
716 IDS_NFC_DEBUG_NFC_PEER_HEADER);
717 html_source->AddLocalizedString("nfcTagHeaderText",
718 IDS_NFC_DEBUG_NFC_TAG_HEADER);
719 html_source->AddLocalizedString("recordsHeaderText",
720 IDS_NFC_DEBUG_RECORDS_HEADER);
721 html_source->AddLocalizedString("errorFailedToSetPowerText",
722 IDS_NFC_DEBUG_ERROR_FAILED_TO_SET_POWER);
723 html_source->AddLocalizedString("errorFailedToSetPollingText",
724 IDS_NFC_DEBUG_ERROR_FAILED_TO_SET_POLLING);
725 html_source->AddLocalizedString("errorFailedToSubmitPrefixText",
726 IDS_NFC_DEBUG_ERROR_FAILED_TO_SUBMIT_PREFIX);
727 html_source->SetJsonPath("strings.js");
729 // Add required resources.
730 html_source->AddResourcePath("nfc_debug.css", IDR_NFC_DEBUG_CSS);
731 html_source->AddResourcePath("nfc_debug.js", IDR_NFC_DEBUG_JS);
732 html_source->SetDefaultResource(IDR_NFC_DEBUG_HTML);
734 Profile* profile = Profile::FromWebUI(web_ui);
735 content::WebUIDataSource::Add(profile, html_source);
738 NfcDebugUI::~NfcDebugUI() {
741 } // namespace chromeos