Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/apps/system/js/captive_portal.js
blob: b23cb0d339562d2aab5aa4892cd45c3e968191cd (plain)
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
/* -*Mode: js; js-indent-level: 2; indent-tabs-mode: nil -**/
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';

var CaptivePortalLogin = (function() {
  var eventId;
  var isManualConnect = false;
  var settings = window.navigator.mozSettings;
  var notification = null;
  var wifiManager = window.navigator.mozWifiManager;
  var _ = window.navigator.mozL10n.get;
  var captiveNotification_onTap = null;

  function handleLogin(id, url) {
    //captive portal login needed
    eventId = id;
    var currentNetwork = wifiManager.connection.network;
    var networkName = (currentNetwork && currentNetwork.ssid) ?
        currentNetwork.ssid : '';
    var message = _('captive-wifi-available', { networkName: networkName});
    if (!isManualConnect) {
      notification = NotificationScreen.addNotification({
        id: id, title: '', text: message, icon: null
      });
      captiveNotification_onTap = function() {
        notification.removeEventListener('tap', captiveNotification_onTap);
        captiveNotification_onTap = null;
        NotificationScreen.removeNotification(id);
        new MozActivity({
          name: 'view',
          data: { type: 'url', url: url }
        });
      };
      notification.addEventListener('tap', captiveNotification_onTap);
    } else {
      settings.createLock().set({'wifi.connect_via_settings': false});
      new MozActivity({
        name: 'view',
        data: { type: 'url', url: url }
      });
    }
  }

  function handleLoginAbort(id) {
    if (id === eventId && notification) {
      if (notification.parentNode) {
        if (captiveNotification_onTap) {
          notification.removeEventListener('tap', captiveNotification_onTap);
          captiveNotification_onTap = null;
        }
        NotificationScreen.removeNotification(id);
        notification = null;
      }
    }
  }

  window.addEventListener('mozChromeEvent', function handleChromeEvent(e) {
    switch (e.detail.type) {
      case 'captive-portal-login':
        handleLogin(e.detail.id, e.detail.url);
        break;
      case 'captive-portal-login-abort':
        handleLoginAbort(e.detail.id);
        break;
    }
  });

  // Using settings API to know whether user is manually selecting
  // wifi AP from settings app.
  SettingsListener.observe('wifi.connect_via_settings', true, function(value) {
    isManualConnect = value;
  });
})();