Repository navigation
Expand file tree
/
Copy pathwifi.js
More file actions
1061 lines (937 loc) · 34.1 KB
/
Copy pathwifi.js
File metadata and controls
1061 lines (937 loc) · 34.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
// handle Wi-Fi settings
navigator.mozL10n.ready(function wifiSettings() {
var _ = navigator.mozL10n.get;
var settings = window.navigator.mozSettings;
if (!settings)
return;
var gWifiManager = WifiHelper.getWifiManager();
var gWifi = document.querySelector('#wifi');
var gWifiCheckBox = document.querySelector('#wifi-enabled input');
var gWifiInfoBlock = document.querySelector('#wifi-desc');
var gWpsInfoBlock = document.querySelector('#wps-column small');
var gWpsPbcLabelBlock = document.querySelector('#wps-column a');
var gCurrentNetwork = gWifiManager.connection.network;
// auto-scan networks when the Wi-Fi panel gets visible
var gScanPending = false;
var gWifiSectionVisible = false;
function updateVisibilityStatus() {
var computedStyle = window.getComputedStyle(gWifi);
gWifiSectionVisible = (!document.hidden &&
computedStyle.visibility != 'hidden');
if (gWifiSectionVisible && gScanPending) {
gNetworkList.scan();
gScanPending = false;
}
}
document.addEventListener('visibilitychange', updateVisibilityStatus);
gWifi.addEventListener('transitionend', function(evt) {
if (evt.target == gWifi) {
updateVisibilityStatus();
}
});
// toggle wifi on/off
gWifiCheckBox. toggleWifi() {
settings.createLock().set({
'wifi.enabled': this.checked
}). {
// Fail to write mozSettings, return toggle control to the user.
gWifiCheckBox.disabled = false;
};
this.disabled = true;
};
/**
* mozWifiManager status
* see dom/wifi/nsIWifi.idl -- the 4 possible statuses are:
* - connecting:
* fires when we start the process of connecting to a network.
* - associated:
* fires when we have connected to an access point but do not yet
* have an IP address.
* - connected:
* fires once we are fully connected to an access point.
* - connectingfailed:
* fires when we fail to connect to an access point.
* - disconnected:
* fires when we were connected to a network but have been
* disconnected.
*/
var gScanStates = new Set(['connected', 'connectingfailed', 'disconnected']);
Connectivity.wifiStatusChange = function(event) {
updateNetworkState();
if (gScanStates.has(event.status)) {
if (gWifiSectionVisible) {
gNetworkList.scan();
} else {
gScanPending = true;
}
}
};
Connectivity.wifiEnabled = function onWifiEnabled() {
// Re-enable UI toggle
gWifiCheckBox.disabled = false;
updateNetworkState();
gNetworkList.scan();
};
Connectivity.wifiDisabled = function onWifiDisabled() {
// Re-enable UI toggle
gWifiCheckBox.disabled = false;
gWifiInfoBlock.textContent = _('disabled');
gWifiInfoBlock.dataset.l10nId = 'disabled';
gNetworkList.clear(false);
gNetworkList.autoscan = false;
};
// Wi-Fi Protected Setup
var gWpsInProgress = false;
function wpsStatusReset() {
// The WPS process is done (connected, cancelled or error):
// - reset the title of the WPS item ("Connect with WPS") right now;
// - leave the current status for a moment, then reset it to the default
// message ("Automatic Wi-Fi setup").
localize(gWpsPbcLabelBlock, 'wpsMessage');
setTimeout(function resetWpsInfoBlock() {
localize(gWpsInfoBlock, 'wpsDescription2');
}, 1500);
}
document.getElementById('wps-column'). {
if (gWpsInProgress) {
var req = gWifiManager.wps({
method: 'cancel'
});
req. {
gWpsInProgress = false;
localize(gWpsInfoBlock, 'fullStatus-wps-canceled');
wpsStatusReset();
};
req. {
gWpsInfoBlock.textContent = _('wpsCancelFailedMessage') +
' [' + req.error.name + ']';
};
} else {
wpsDialog('wifi-wps', wpsCallback);
}
function wpsCallback(bssid, method, pin) {
var req;
if (method === 'pbc') {
req = gWifiManager.wps({
method: 'pbc'
});
} else if (method === 'myPin') {
req = gWifiManager.wps({
method: 'pin',
bssid: bssid
});
} else {
req = gWifiManager.wps({
method: 'pin',
bssid: bssid,
pin: pin
});
}
req. {
if (method === 'myPin') {
alert(_('wpsPinInput', { pin: req.result }));
}
gWpsInProgress = true;
localize(gWpsPbcLabelBlock, 'wpsCancelMessage');
localize(gWpsInfoBlock, 'fullStatus-wps-inprogress');
};
req. {
gWpsInfoBlock.textContent = _('fullStatus-wps-failed') +
' [' + req.error.name + ']';
};
}
function wpsDialog(dialogID, callback) {
// hide dialog box
function pinChecksum(pin) {
var accum = 0;
while (pin > 0) {
accum += 3 * (pin % 10);
pin = Math.floor(pin / 10);
accum += pin % 10;
pin = Math.floor(pin / 10);
}
return (10 - accum % 10) % 10;
}
function isValidWpsPin(pin) {
if (pin.match(/[^0-9]+/))
return false;
if (pin.length === 4)
return true;
if (pin.length !== 8)
return false;
var num = pin - 0;
return pinChecksum(Math.floor(num / 10)) === (num % 10);
}
var dialog = document.getElementById(dialogID);
if (!dialog)
return;
var apSelectionArea = dialog.querySelector('#wifi-wps-pin-aps');
var apSelect = apSelectionArea.querySelector('select');
for (var i = apSelect.childNodes.length - 1; i >= 0; i--) {
apSelect.removeChild(apSelect.childNodes[i]);
}
var option = document.createElement('option');
option.textContent = _('wpsAnyAp');
option.value = 'any';
apSelect.appendChild(option);
var wpsAvailableNetworks = gNetworkList.getWpsAvailableNetworks();
for (var i = 0; i < wpsAvailableNetworks.length; i++) {
option = document.createElement('option');
option.textContent = wpsAvailableNetworks[i].ssid;
option.value = wpsAvailableNetworks[i].bssid;
apSelect.appendChild(option);
}
var submitWpsButton = dialog.querySelector('button[type=submit]');
var pinItem = document.getElementById('wifi-wps-pin-area');
var pinDesc = pinItem.querySelector('p');
var pinInput = pinItem.querySelector('input');
pinInput. {
submitWpsButton.disabled = !isValidWpsPin(pinInput.value);
};
function onWpsMethodChange() {
var method =
dialog.querySelector("input[type='radio']:checked").value;
if (method === 'apPin') {
submitWpsButton.disabled = !isValidWpsPin(pinInput.value);
pinItem.hidden = false;
} else {
submitWpsButton.disabled = false;
pinItem.hidden = true;
}
apSelectionArea.hidden = method === 'pbc';
}
var radios = dialog.querySelectorAll('input[type="radio"]');
for (var i = 0; i < radios.length; i++) {
radios[i].>
}
onWpsMethodChange();
openDialog(dialogID, function submit() {
callback(apSelect.options[apSelect.selectedIndex].value,
dialog.querySelector("input[type='radio']:checked").value,
pinInput.value);
});
}
};
// create a network list item
function newListItem(network, callback) {
/**
* A Wi-Fi list item has the following HTML structure:
* <li>
* <aside class="pack-end wifi-icon level-[?] [secured]"></aside>
* <small> Network Security </small>
* <a> Network SSID </a>
* </li>
*/
// icon
var icon = document.createElement('aside');
icon.classList.add('pack-end');
icon.classList.add('wifi-icon');
var level = Math.min(Math.floor(network.relSignalStrength / 20), 4);
icon.classList.add('level-' + level);
// ssid
var ssid = document.createElement('a');
ssid.textContent = network.ssid;
// supported authentication methods
var small = document.createElement('small');
var keys = WifiHelper.getSecurity(network);
if (keys && keys.length) {
small.textContent = _('securedBy', { capabilities: keys.join(', ') });
icon.classList.add('secured');
} else {
small.textContent = _('securityOpen');
}
// create list item
var li = document.createElement('li');
li.appendChild(icon);
li.appendChild(small);
li.appendChild(ssid);
// Show connection status
icon.classList.add('wifi-signal');
if (WifiHelper.isConnected(network)) {
small.textContent = _('shortStatus-connected');
icon.classList.add('connected');
li.classList.add('active');
}
// bind connection callback
li. {
callback(network);
};
return li;
}
// create an explanatory list item
function newExplanationItem(message) {
var li = document.createElement('li');
li.className = 'explanation';
li.textContent = _(message);
return li;
}
// available network list
var gNetworkList = (function networkList(list) {
var scanning = false;
var autoscan = false;
var scanRate = 5000; // 5s after last scan results
var index = []; // index of all scanned networks
var networks = {};
// get the "Searching..." and "Search Again" items, respectively
var infoItem = list.querySelector('li[data-state="on"]');
var scanItem = list.querySelector('li[data-state="ready"]');
scanItem. {
clear(true);
scan();
};
// clear the network list
function clear(addScanningItem) {
index = [];
networks = {};
// remove all items except the text expl. and the "search again" button
var wifiItems = list.querySelectorAll('li:not([data-state])');
var len = wifiItems.length;
for (var i = len - 1; i >= 0; i--) {
list.removeChild(wifiItems[i]);
}
list.dataset.state = addScanningItem ? 'on' : 'off';
}
// scan wifi networks and display them in the list
function scan() {
if (scanning)
return;
// stop auto-scanning if wifi disabled or the app is hidden
if (!gWifiManager.enabled || document.hidden) {
scanning = false;
gScanPending = true;
return;
}
scanning = true;
var req = WifiHelper.getAvailableAndKnownNetworks();
req. onScanSuccess() {
clear(false);
var allNetworks = req.result;
for (var i = 0; i < allNetworks.length; ++i) {
var network = allNetworks[i];
// use ssid + security as a composited key
var key = network.ssid + '+' +
WifiHelper.getSecurity(network).join('+');
// keep connected network first, or select the highest strength
if (!networks[key] || network.connected) {
networks[key] = network;
} else {
if (!networks[key].connected &&
network.relSignalStrength > networks[key].relSignalStrength) {
networks[key] = network;
}
}
}
var networkKeys = Object.getOwnPropertyNames(networks);
// display network list
if (networkKeys.length) {
// sort networks by signal strength
networkKeys.sort(function(a, b) {
return networks[b].relSignalStrength -
networks[a].relSignalStrength;
});
// add detected networks
for (var i = 0; i < networkKeys.length; i++) {
var network = networks[networkKeys[i]];
var listItem = newListItem(network, toggleNetwork);
// put connected network on top of list
if (WifiHelper.isConnected(network)) {
list.insertBefore(listItem, infoItem.nextSibling);
} else {
list.insertBefore(listItem, scanItem);
}
index[networkKeys[i]] = listItem; // add composited key to index
}
} else {
// display a "no networks found" message if necessary
list.insertBefore(newExplanationItem('noNetworksFound'), scanItem);
}
// display the "Search Again" button
list.dataset.state = 'ready';
PerformanceTestingHelper.dispatch('settings-panel-wifi-ready');
// auto-rescan if requested
if (autoscan) {
window.setTimeout(scan, scanRate);
}
scanning = false;
};
req. onScanError(error) {
// always try again.
scanning = false;
PerformanceTestingHelper.dispatch('settings-panel-wifi-ready');
window.setTimeout(scan, scanRate);
};
}
// display a message on the network item matching the ssid
function display(network, networkStatus) {
if (!network) {
return;
}
var key = network.ssid + '+' +
WifiHelper.getSecurity(network).join('+');
var listItem = index[key];
var active = list.querySelector('.active');
if (active && active != listItem) {
active.classList.remove('active');
active.querySelector('small').textContent =
_('shortStatus-disconnected');
active.querySelector('aside').classList.remove('connecting');
active.querySelector('aside').classList.remove('connected');
}
if (listItem) {
listItem.classList.add('active');
listItem.querySelector('small').textContent =
_('shortStatus-' + networkStatus);
if (networkStatus === 'connecting') {
listItem.querySelector('aside').classList.add('connecting');
}
if (networkStatus === 'connected') {
listItem.querySelector('aside').classList.remove('connecting');
}
}
}
// get WPS available networks
function getWpsAvailableNetworks() {
var ssids = Object.getOwnPropertyNames(networks);
var wpsAvailableNetworks = [];
for (var i = 0; i < ssids.length; i++) {
var network = networks[ssids[i]];
if (WifiHelper.isWpsAvailable(network)) {
wpsAvailableNetworks.push(network);
}
}
return wpsAvailableNetworks;
}
// API
return {
get autoscan() { return autoscan; },
set autoscan(value) { autoscan = value; },
display: display,
clear: clear,
scan: scan,
get scanning() { return scanning; },
getWpsAvailableNetworks: getWpsAvailableNetworks
};
}) (document.getElementById('wifi-availableNetworks'));
// saved network list
var gKnownNetworkList = (function knownNetworkList(list) {
// clear the network list
function clear() {
while (list.hasChildNodes()) {
list.removeChild(list.lastChild);
}
}
// propose to forget a network
function forgetNetwork(network) {
var dialog = document.querySelector('#wifi-manageNetworks form');
dialog.hidden = false;
dialog. forget() {
gWifiManager.forget(network);
scan();
dialog.hidden = true;
return false;
};
dialog. cancel() {
dialog.hidden = true;
return false;
};
}
// list known networks
function scan() {
var req = gWifiManager.getKnownNetworks();
req. onSuccess() {
var allNetworks = req.result;
var networks = {};
for (var i = 0; i < allNetworks.length; ++i) {
var network = allNetworks[i];
// use ssid + capabilities as a composited key
var key = network.ssid + '+' +
WifiHelper.getSecurity(network).join('+');
networks[key] = network;
}
var networkKeys = Object.getOwnPropertyNames(networks);
clear();
// display network list
if (networkKeys.length) {
networkKeys.sort();
for (var i = 0; i < networkKeys.length; i++) {
var aItem = newListItem(networks[networkKeys[i]], forgetNetwork);
list.appendChild(aItem);
}
} else {
// display a "no known networks" message if necessary
list.appendChild(newExplanationItem('noKnownNetworks'));
}
};
req. onScanError(error) {
console.warn('wifi: could not retrieve any known network. ');
};
}
// API
return {
clear: clear,
scan: scan
};
}) (document.getElementById('wifi-knownNetworks'));
document.getElementById('manageNetworks'). knownNetworks() {
gKnownNetworkList.scan();
openDialog('wifi-manageNetworks');
};
// create a certificate list item
function newCertificateItem(caName) {
var label = document.createElement('label');
label.className = 'pack-checkbox';
var input = document.createElement('input');
input.type = 'checkbox';
input.name = caName;
input.checked = false;
var span = document.createElement('span');
span.textContent = caName;
label.appendChild(input);
label.appendChild(span);
var li = document.createElement('li');
li.appendChild(label);
return li;
}
function toggleDeleteCertificateBtn(enabled) {
document.getElementById('deleteCertificate').disabled = !enabled;
}
function toggleImportCertificateBtn(enabled) {
document.getElementById('importCertificate').disabled = !enabled;
}
// imported certificate list
var gCertificateList = (function certificateList(list) {
var certificateList = [];
// get list
function getList() {
return certificateList;
}
// return true: if more than one selected item
// return false: if no selected item
function isItemSelected() {
return list.querySelector('input[type=checkbox]:checked') != null;
}
// delete the selected certificate items in list
function deleteCertificate() {
var countItemDeleted = 0;
var checkedInputList =
list.querySelectorAll('input[type=checkbox]:checked');
for (var i = 0; i < checkedInputList.length; i++) {
var nickname = checkedInputList[i].name;
var certRequest = gWifiManager.deleteCert(nickname);
certRequest. {
if (++countItemDeleted == checkedInputList.length) {
// refresh certificate list
scan();
}
};
certRequest. {
if (++countItemDeleted == checkedInputList.length) {
// refresh certificate list
scan();
}
// Pop out alert message for certificate deletion failed
var dialog = document.getElementById('certificate-deletion-failed');
dialog.hidden = false;
dialog. confirm() {
dialog.hidden = true;
};
};
}
}
// clear the certificate list
function clear() {
while (list.hasChildNodes()) {
list.removeChild(list.lastChild);
}
}
// scan and list imported certificates
function scan() {
clear();
var certRequest = gWifiManager.getImportedCerts();
certRequest. {
var certList = certRequest.result;
// save the imported server certificates
certificateList = certList.ServerCert;
// display certificate list
if (certificateList.length) {
for (var i = 0; i < certificateList.length; i++) {
list.appendChild(newCertificateItem(certificateList[i]));
}
// add event listener for update toggle delete/import cert. buttons
var inputItems = list.querySelectorAll('input');
for (var i = 0; i < inputItems.length; i++) {
inputItems[i]. {
// To enable/disable delete, import certitifate buttons
// via items selected or not.
var option = isItemSelected();
toggleDeleteCertificateBtn(option);
toggleImportCertificateBtn(!option);
};
}
} else {
// display "no certificate" message while no any imported certificate
list.appendChild(newExplanationItem('noCertificate'));
}
};
certRequest. {
console.warn('getImportedCerts failed');
};
toggleDeleteCertificateBtn(false);
toggleImportCertificateBtn(true);
}
// Once Bug 917102 landed, we can remove the detection.
// Detect platform is supporting certificate or not..
if (gWifiManager.getImportedCerts) {
// API is ready.
scan();
} else {
// API is not ready yet.
// Hide the "Manage certificates" button since API is not ready yet.
var manageCertificatesBtn = document.getElementById('manageCertificates');
manageCertificatesBtn.parentNode.parentNode.hidden = true;
console.warn('Import certificate API is not ready yet!');
}
// API
return {
getList: getList,
scan: scan,
deleteCertificate: deleteCertificate
};
}) (document.getElementById('wifi-certificateList'));
// when certificate file imported, update the imported certificate list
window.addEventListener('certificate-imported', function() {
gCertificateList.scan();
});
document.getElementById('manageCertificates').>
function knownCertificates() {
gCertificateList.scan();
openDialog('wifi-manageCertificates');
};
document.getElementById('deleteCertificate').>
function deleteCertificate() {
gCertificateList.deleteCertificate();
};
document.getElementById('importCertificate').>
function importCertificate() {
// dispatch event for gSelectCertificateFiles.scan();
dispatchEvent(new CustomEvent('scan-certificate-file'));
openDialog('wifi-selectCertificateFile');
};
// join hidden network
document.getElementById('joinHidden'). joinHiddenNetwork() {
toggleNetwork();
};
// load the imported certificates into select options
function loadImportedCertificateOptions(select) {
// reset the option to be <option value="none">--</option> only
for (var i = 0; i < select.options.length - 1; i++) {
select.remove(1);
}
var certificateList = gCertificateList.getList();
for (var i = 0; i < certificateList.length; i++) {
var option = document.createElement('option');
option.text = certificateList[i];
option.value = certificateList[i];
select.add(option, null);
}
}
// UI to connect/disconnect
function toggleNetwork(network) {
if (!network) {
// offline, hidden SSID
network = {};
wifiDialog('wifi-joinHidden', wifiConnect);
} else if (WifiHelper.isConnected(network)) {
// online: show status + offer to disconnect
wifiDialog('wifi-status', wifiDisconnect);
} else if (network.password && (network.password == '*')) {
// offline, known network (hence the '*' password value):
// no further authentication required.
WifiHelper.setPassword(network);
wifiConnect();
} else {
// offline, unknown network: propose to connect
var key = WifiHelper.getKeyManagement(network);
switch (key) {
case 'WEP':
case 'WPA-PSK':
case 'WPA-EAP':
wifiDialog('wifi-auth', wifiConnect, key);
break;
default:
wifiConnect();
}
}
function wifiConnect() {
gCurrentNetwork = network;
gWifiManager.associate(network);
settings.createLock().set({'wifi.connect_via_settings': true});
}
function wifiDisconnect() {
settings.createLock().set({'wifi.connect_via_settings': false});
gWifiManager.forget(network);
// get available network list
gNetworkList.scan();
gCurrentNetwork = null;
}
// generic wifi property dialog
function wifiDialog(dialogID, callback, key) {
var dialog = document.getElementById(dialogID);
// authentication fields
var identity, password, showPassword, eap,
authPhase2, certificate, description;
if (dialogID != 'wifi-status') {
identity = dialog.querySelector('input[name=identity]');
identity.value = network.identity || '';
password = dialog.querySelector('input[name=password]');
password.type = 'password';
password.value = network.password || '';
showPassword = dialog.querySelector('input[name=show-pwd]');
showPassword.checked = false;
showPassword. {
password.type = this.checked ? 'text' : 'password';
};
eap = dialog.querySelector('li.eap select');
// Once Bug 917102 landed, we could remove the detection.
// Detect platform is supporting certificate or not..
if (!gWifiManager.getImportedCerts) {
console.warn('Import certificate API is not ready yet!');
// API is not ready yet.
// Remove the EAP methods(PEAP, TLS, TTLS) which are not supported.
while (eap.options.length != 1) {
eap.remove(1);
}
}
authPhase2 = dialog.querySelector('li.auth-phase2 select');
certificate = dialog.querySelector('li.server-certificate select');
loadImportedCertificateOptions(certificate);
description = dialog.querySelector('li.server-certificate-description');
}
if (dialogID === 'wifi-joinHidden') {
network.hidden = true;
// Make sure ssid length is less then 32 bytes.
var ssid = dialog.querySelector('input[name=ssid]');
ssid. {
var ssidStr = ssid.value;
// Non-ASCII chars in SSID will be encoded by UTF-8, and length of
// each char might be longer than 1 byte.
// Use encodeURIComponent() to encode ssid, then calculate correct
// length.
if (encodeURIComponent(ssidStr).replace(/%[\w\d]{2}/g, '1')
.length > 32) {
ssid.value = ssidStr.substring(0, ssidStr.length - 1);
}
};
}
// disable the "OK" button if the password is too short
if (password) {
var checkPassword = function checkPassword() {
dialog.querySelector('button[type=submit]').disabled =
!WifiHelper.isValidInput(key,
password.value,
identity.value,
eap.value);
};
eap. {
checkPassword();
changeDisplay(key);
};
password.>
identity.>
checkPassword();
}
// initialisation
var keys = WifiHelper.getSecurity(network);
var security = (keys && keys.length) ? keys.join(', ') : '';
var sl = Math.min(Math.floor(network.relSignalStrength / 20), 4);
var updateBaseStationInfo = function update_base_station_info() {
dialog.querySelector('[data-ssid]').textContent = network.ssid;
dialog.querySelector('[data-signal]').textContent =
_('signalLevel' + sl);
dialog.querySelector('[data-security]').textContent =
security || _('securityNone');
dialog.dataset.security = security;
};
switch (dialogID) {
case 'wifi-status':
// we're connected, let's display some connection info
var ipAddress = dialog.querySelector('[data-ip]'); // IP address
var speed = dialog.querySelector('[data-speed]'); // link speed
var updateNetInfo = function() {
var info = gWifiManager.connectionInformation;
ipAddress.textContent = info.ipAddress || '';
speed.textContent =
_('linkSpeedMbs'
AF0E
;, { linkSpeed: info.linkSpeed });
};
gWifiManager.connectionInfoUpdate = updateNetInfo;
updateNetInfo();
updateBaseStationInfo();
break;
case 'wifi-auth':
// network info -- #wifi-auth
updateBaseStationInfo();
changeDisplay(security);
break;
case 'wifi-joinHidden':
var security = dialog.querySelector('select');
var {
key = security.selectedIndex ? security.value : '';
WifiHelper.setSecurity(network, [key]);
dialog.dataset.security = key;
checkPassword();
changeDisplay(key);
};
security.>
onSecurityChange();
break;
}
// change element display
function changeDisplay(security) {
if (dialogID !== 'wifi-status') {
if (security === 'WEP' || security === 'WPA-PSK') {
identity.parentNode.style.display = 'none';
password.parentNode.style.display = 'block';
authPhase2.parentNode.parentNode.style.display = 'none';
certificate.parentNode.parentNode.style.display = 'none';
description.style.display = 'none';
} else if (security === 'WPA-EAP') {
if (eap) {
switch (eap.value) {
case 'SIM':
identity.parentNode.style.display = 'none';
password.parentNode.style.display = 'none';
authPhase2.parentNode.parentNode.style.display = 'none';
certificate.parentNode.parentNode.style.display = 'none';
description.style.display = 'none';
break;
case 'PEAP':
case 'TLS':
case 'TTLS':
identity.parentNode.style.display = 'block';
password.parentNode.style.display = 'block';
authPhase2.parentNode.parentNode.style.display = 'block';
certificate.parentNode.parentNode.style.display = 'block';
description.style.display = 'block';
break;
default:
break;
}
}
} else {
identity.parentNode.style.display = 'none';
password.parentNode.style.display = 'none';
}
}
}
// reset dialog box
function reset() {
if (speed) {
gWifiManager.connectionInfoUpdate = null;
}
if (dialogID != 'wifi-status') {
identity.value = '';
password.value = '';
showPassword.checked = false;
}
}
// OK|Cancel buttons
function submit() {
if (dialogID === 'wifi-joinHidden') {
network.ssid = dialog.querySelector('input[name=ssid]').value;
}
if (key) {
WifiHelper.setPassword(network,
password.value,
identity.value,
eap.value,
authPhase2.value,
certificate.value);
}
if (callback) {
callback();
if (dialogID === 'wifi-joinHidden') {
gKnownNetworkList.scan();
}
}
reset();
};
// show dialog box
openDialog(dialogID, submit, reset);
}
}
// update network state, called only when wifi enabled.
function updateNetworkState() {
var networkStatus = gWifiManager.connection.status;
// networkStatus has one of the following values:
// connecting, associated, connected, connectingfailed, disconnected.
gNetworkList.display(gCurrentNetwork, networkStatus);
gWifiInfoBlock.textContent =
_('fullStatus-' + networkStatus, gWifiManager.connection.network);
if (networkStatus === 'connectingfailed' && gCurrentNetwork) {
settings.createLock().set({'wifi.connect_via_settings': false});
// connection has failed, probably an authentication issue...
delete(gCurrentNetwork.password);
gWifiManager.forget(gCurrentNetwork); // force a new authentication dialog
gCurrentNetwork = null;
}
if (gWpsInProgress) {
if (networkStatus !== 'disconnected') {
gWpsInfoBlock.textContent = gWifiInfoBlock.textContent;
You can’t perform that action at this time.