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

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

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

Powered by Google App Engine
This is Rietveld 408576698