2012-02-14 12:12:11 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2013-04-30 04:04:36 -07:00
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
2012-02-14 12:12:11 -08:00
|
|
|
|
|
|
|
const NETWORKMANAGER_CONTRACTID = "@mozilla.org/network/manager;1";
|
|
|
|
const NETWORKMANAGER_CID =
|
|
|
|
Components.ID("{33901e46-33b8-11e1-9869-f46d04d25bcc}");
|
|
|
|
const NETWORKINTERFACE_CONTRACTID = "@mozilla.org/network/interface;1";
|
|
|
|
const NETWORKINTERFACE_CID =
|
|
|
|
Components.ID("{266c3edd-78f0-4512-8178-2d6fee2d35ee}");
|
|
|
|
|
|
|
|
const DEFAULT_PREFERRED_NETWORK_TYPE = Ci.nsINetworkInterface.NETWORK_TYPE_WIFI;
|
|
|
|
|
2012-08-29 12:27:08 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gSettingsService",
|
|
|
|
"@mozilla.org/settingsService;1",
|
|
|
|
"nsISettingsService");
|
2013-05-02 04:41:20 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "ppmm", function() {
|
|
|
|
return Cc["@mozilla.org/parentprocessmessagemanager;1"]
|
|
|
|
.getService(Ci.nsIMessageBroadcaster);
|
|
|
|
});
|
2012-08-29 12:27:08 -07:00
|
|
|
|
2013-05-31 01:14:45 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gDNSService",
|
|
|
|
"@mozilla.org/network/dns-service;1",
|
|
|
|
"nsIDNSService");
|
|
|
|
|
2012-02-14 12:12:11 -08:00
|
|
|
const TOPIC_INTERFACE_STATE_CHANGED = "network-interface-state-changed";
|
|
|
|
const TOPIC_INTERFACE_REGISTERED = "network-interface-registered";
|
|
|
|
const TOPIC_INTERFACE_UNREGISTERED = "network-interface-unregistered";
|
2013-02-07 00:21:28 -08:00
|
|
|
const TOPIC_ACTIVE_CHANGED = "network-active-changed";
|
2012-08-29 12:27:08 -07:00
|
|
|
const TOPIC_MOZSETTINGS_CHANGED = "mozsettings-changed";
|
2012-12-29 10:11:04 -08:00
|
|
|
const TOPIC_PREF_CHANGED = "nsPref:changed";
|
2012-08-29 12:27:08 -07:00
|
|
|
const TOPIC_XPCOM_SHUTDOWN = "xpcom-shutdown";
|
2012-12-29 10:11:04 -08:00
|
|
|
const PREF_MANAGE_OFFLINE_STATUS = "network.gonk.manage-offline-status";
|
2012-08-29 12:27:08 -07:00
|
|
|
|
2013-04-30 04:04:36 -07:00
|
|
|
const POSSIBLE_USB_INTERFACE_NAME = "rndis0,usb0";
|
2012-08-29 12:27:08 -07:00
|
|
|
const DEFAULT_USB_INTERFACE_NAME = "rndis0";
|
|
|
|
const DEFAULT_3G_INTERFACE_NAME = "rmnet0";
|
|
|
|
const DEFAULT_WIFI_INTERFACE_NAME = "wlan0";
|
|
|
|
|
2013-04-30 04:04:36 -07:00
|
|
|
// The kernel's proc entry for network lists.
|
|
|
|
const KERNEL_NETWORK_ENTRY = "/sys/class/net";
|
|
|
|
|
2012-08-29 12:27:08 -07:00
|
|
|
const TETHERING_TYPE_WIFI = "WiFi";
|
|
|
|
const TETHERING_TYPE_USB = "USB";
|
|
|
|
|
2012-09-05 02:30:48 -07:00
|
|
|
// 1xx - Requested action is proceeding
|
|
|
|
const NETD_COMMAND_PROCEEDING = 100;
|
|
|
|
// 2xx - Requested action has been successfully completed
|
|
|
|
const NETD_COMMAND_OKAY = 200;
|
|
|
|
// 4xx - The command is accepted but the requested action didn't
|
|
|
|
// take place.
|
|
|
|
const NETD_COMMAND_FAIL = 400;
|
|
|
|
// 5xx - The command syntax or parameters error
|
|
|
|
const NETD_COMMAND_ERROR = 500;
|
|
|
|
// 6xx - Unsolicited broadcasts
|
|
|
|
const NETD_COMMAND_UNSOLICITED = 600;
|
2012-08-29 12:27:08 -07:00
|
|
|
|
|
|
|
const WIFI_FIRMWARE_AP = "AP";
|
|
|
|
const WIFI_FIRMWARE_STATION = "STA";
|
|
|
|
const WIFI_SECURITY_TYPE_NONE = "open";
|
|
|
|
const WIFI_SECURITY_TYPE_WPA_PSK = "wpa-psk";
|
|
|
|
const WIFI_SECURITY_TYPE_WPA2_PSK = "wpa2-psk";
|
|
|
|
const WIFI_CTRL_INTERFACE = "wl0.1";
|
|
|
|
|
|
|
|
const NETWORK_INTERFACE_UP = "up";
|
|
|
|
const NETWORK_INTERFACE_DOWN = "down";
|
|
|
|
|
2013-05-16 04:39:58 -07:00
|
|
|
const TETHERING_STATE_ONGOING = "ongoing";
|
|
|
|
const TETHERING_STATE_IDLE = "idle";
|
2012-08-29 12:27:08 -07:00
|
|
|
|
|
|
|
// Settings DB path for USB tethering.
|
|
|
|
const SETTINGS_USB_ENABLED = "tethering.usb.enabled";
|
|
|
|
const SETTINGS_USB_IP = "tethering.usb.ip";
|
|
|
|
const SETTINGS_USB_PREFIX = "tethering.usb.prefix";
|
|
|
|
const SETTINGS_USB_DHCPSERVER_STARTIP = "tethering.usb.dhcpserver.startip";
|
|
|
|
const SETTINGS_USB_DHCPSERVER_ENDIP = "tethering.usb.dhcpserver.endip";
|
2013-02-27 23:22:56 -08:00
|
|
|
const SETTINGS_USB_DNS1 = "tethering.usb.dns1";
|
|
|
|
const SETTINGS_USB_DNS2 = "tethering.usb.dns2";
|
|
|
|
|
2013-06-19 03:57:58 -07:00
|
|
|
// Settings DB path for WIFI tethering.
|
|
|
|
const SETTINGS_WIFI_DHCPSERVER_STARTIP = "tethering.wifi.dhcpserver.startip";
|
|
|
|
const SETTINGS_WIFI_DHCPSERVER_ENDIP = "tethering.wifi.dhcpserver.endip";
|
|
|
|
|
2013-02-27 23:22:56 -08:00
|
|
|
// Default value for USB tethering.
|
|
|
|
const DEFAULT_USB_IP = "192.168.0.1";
|
|
|
|
const DEFAULT_USB_PREFIX = "24";
|
|
|
|
const DEFAULT_USB_DHCPSERVER_STARTIP = "192.168.0.10";
|
|
|
|
const DEFAULT_USB_DHCPSERVER_ENDIP = "192.168.0.30";
|
|
|
|
|
|
|
|
const DEFAULT_DNS1 = "8.8.8.8";
|
|
|
|
const DEFAULT_DNS2 = "8.8.4.4";
|
2012-02-14 12:12:11 -08:00
|
|
|
|
2013-06-19 03:57:58 -07:00
|
|
|
const DEFAULT_WIFI_DHCPSERVER_STARTIP = "192.168.1.10";
|
|
|
|
const DEFAULT_WIFI_DHCPSERVER_ENDIP = "192.168.1.30";
|
|
|
|
|
2012-08-01 07:55:43 -07:00
|
|
|
const MANUAL_PROXY_CONFIGURATION = 1;
|
|
|
|
|
2012-02-14 12:12:11 -08:00
|
|
|
const DEBUG = false;
|
|
|
|
|
2012-09-05 02:30:48 -07:00
|
|
|
function netdResponseType(code) {
|
|
|
|
return Math.floor(code/100)*100;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isError(code) {
|
|
|
|
let type = netdResponseType(code);
|
|
|
|
return (type != NETD_COMMAND_PROCEEDING && type != NETD_COMMAND_OKAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isComplete(code) {
|
|
|
|
let type = netdResponseType(code);
|
|
|
|
return (type != NETD_COMMAND_PROCEEDING);
|
|
|
|
}
|
|
|
|
|
2013-06-05 02:20:50 -07:00
|
|
|
function defineLazyRegExp(obj, name, pattern) {
|
|
|
|
obj.__defineGetter__(name, function() {
|
|
|
|
delete obj[name];
|
|
|
|
return obj[name] = new RegExp(pattern);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-02-14 12:12:11 -08:00
|
|
|
/**
|
|
|
|
* This component watches for network interfaces changing state and then
|
|
|
|
* adjusts routes etc. accordingly.
|
|
|
|
*/
|
|
|
|
function NetworkManager() {
|
|
|
|
this.networkInterfaces = {};
|
|
|
|
Services.obs.addObserver(this, TOPIC_INTERFACE_STATE_CHANGED, true);
|
2013-05-31 01:14:45 -07:00
|
|
|
Services.obs.addObserver(this, TOPIC_INTERFACE_REGISTERED, true);
|
|
|
|
Services.obs.addObserver(this, TOPIC_INTERFACE_UNREGISTERED, true);
|
2012-08-29 12:27:08 -07:00
|
|
|
Services.obs.addObserver(this, TOPIC_XPCOM_SHUTDOWN, false);
|
|
|
|
Services.obs.addObserver(this, TOPIC_MOZSETTINGS_CHANGED, false);
|
2012-02-14 12:12:11 -08:00
|
|
|
|
|
|
|
debug("Starting worker.");
|
|
|
|
this.worker = new ChromeWorker("resource://gre/modules/net_worker.js");
|
2012-12-29 10:11:04 -08:00
|
|
|
this.worker.onmessage = this.handleWorkerMessage.bind(this);
|
2012-02-14 12:12:11 -08:00
|
|
|
this.worker.onerror = function onerror(event) {
|
|
|
|
debug("Received error from worker: " + event.filename +
|
|
|
|
":" + event.lineno + ": " + event.message + "\n");
|
|
|
|
// Prevent the event from bubbling any further.
|
|
|
|
event.preventDefault();
|
|
|
|
};
|
2012-08-29 12:27:08 -07:00
|
|
|
|
|
|
|
// Callbacks to invoke when a reply arrives from the net_worker.
|
|
|
|
this.controlCallbacks = Object.create(null);
|
|
|
|
|
2012-12-29 10:11:04 -08:00
|
|
|
try {
|
|
|
|
this._manageOfflineStatus =
|
|
|
|
Services.prefs.getBoolPref(PREF_MANAGE_OFFLINE_STATUS);
|
|
|
|
} catch(ex) {
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
Services.prefs.addObserver(PREF_MANAGE_OFFLINE_STATUS, this, false);
|
|
|
|
|
2013-04-30 04:04:36 -07:00
|
|
|
// Possible usb tethering interfaces for different gonk platform.
|
|
|
|
this.possibleInterface = POSSIBLE_USB_INTERFACE_NAME.split(",");
|
|
|
|
|
2012-08-29 12:27:08 -07:00
|
|
|
// Default values for internal and external interfaces.
|
|
|
|
this._tetheringInterface = Object.create(null);
|
|
|
|
this._tetheringInterface[TETHERING_TYPE_USB] = {externalInterface: DEFAULT_3G_INTERFACE_NAME,
|
|
|
|
internalInterface: DEFAULT_USB_INTERFACE_NAME};
|
|
|
|
this._tetheringInterface[TETHERING_TYPE_WIFI] = {externalInterface: DEFAULT_3G_INTERFACE_NAME,
|
|
|
|
internalInterface: DEFAULT_WIFI_INTERFACE_NAME};
|
|
|
|
|
2013-02-27 23:22:56 -08:00
|
|
|
this.initTetheringSettings();
|
2012-08-29 12:27:08 -07:00
|
|
|
|
2012-09-05 17:33:03 -07:00
|
|
|
let settingsLock = gSettingsService.createLock();
|
2012-08-29 12:27:08 -07:00
|
|
|
// Read usb tethering data from settings DB.
|
|
|
|
settingsLock.get(SETTINGS_USB_IP, this);
|
|
|
|
settingsLock.get(SETTINGS_USB_PREFIX, this);
|
|
|
|
settingsLock.get(SETTINGS_USB_DHCPSERVER_STARTIP, this);
|
|
|
|
settingsLock.get(SETTINGS_USB_DHCPSERVER_ENDIP, this);
|
2013-02-27 23:22:56 -08:00
|
|
|
settingsLock.get(SETTINGS_USB_DNS1, this);
|
|
|
|
settingsLock.get(SETTINGS_USB_DNS2, this);
|
2013-05-16 04:39:58 -07:00
|
|
|
settingsLock.get(SETTINGS_USB_ENABLED, this);
|
2012-09-26 04:08:50 -07:00
|
|
|
|
2013-06-19 03:57:58 -07:00
|
|
|
// Read wifi tethering data from settings DB.
|
|
|
|
settingsLock.get(SETTINGS_WIFI_DHCPSERVER_STARTIP, this);
|
|
|
|
settingsLock.get(SETTINGS_WIFI_DHCPSERVER_ENDIP, this);
|
|
|
|
|
2013-05-16 04:39:58 -07:00
|
|
|
this._usbTetheringSettingsToRead = [SETTINGS_USB_IP,
|
|
|
|
SETTINGS_USB_PREFIX,
|
|
|
|
SETTINGS_USB_DHCPSERVER_STARTIP,
|
|
|
|
SETTINGS_USB_DHCPSERVER_ENDIP,
|
|
|
|
SETTINGS_USB_DNS1,
|
|
|
|
SETTINGS_USB_DNS2,
|
2013-06-19 03:57:58 -07:00
|
|
|
SETTINGS_USB_ENABLED,
|
|
|
|
SETTINGS_WIFI_DHCPSERVER_STARTIP,
|
|
|
|
SETTINGS_WIFI_DHCPSERVER_ENDIP];
|
2012-11-30 03:37:34 -08:00
|
|
|
|
2013-05-16 04:39:58 -07:00
|
|
|
this.wantConnectionEvent = null;
|
|
|
|
this.setAndConfigureActive();
|
2013-05-02 04:41:20 -07:00
|
|
|
|
|
|
|
ppmm.addMessageListener('NetworkInterfaceList:ListInterface', this);
|
2013-06-05 02:20:50 -07:00
|
|
|
|
|
|
|
// Used in resolveHostname().
|
|
|
|
defineLazyRegExp(this, "REGEXP_IPV4", "^\\d{1,3}(?:\\.\\d{1,3}){3}$");
|
|
|
|
defineLazyRegExp(this, "REGEXP_IPV6", "^[\\da-fA-F]{4}(?::[\\da-fA-F]{4}){7}$");
|
2012-02-14 12:12:11 -08:00
|
|
|
}
|
|
|
|
NetworkManager.prototype = {
|
|
|
|
classID: NETWORKMANAGER_CID,
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: NETWORKMANAGER_CID,
|
|
|
|
contractID: NETWORKMANAGER_CONTRACTID,
|
|
|
|
classDescription: "Network Manager",
|
|
|
|
interfaces: [Ci.nsINetworkManager]}),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsINetworkManager,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsIObserver,
|
2012-08-29 12:27:08 -07:00
|
|
|
Ci.nsIWorkerHolder,
|
|
|
|
Ci.nsISettingsServiceCallback]),
|
2012-02-14 12:12:11 -08:00
|
|
|
|
|
|
|
// nsIWorkerHolder
|
|
|
|
|
|
|
|
worker: null,
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
|
|
|
|
observe: function observe(subject, topic, data) {
|
2012-08-29 12:27:08 -07:00
|
|
|
switch (topic) {
|
|
|
|
case TOPIC_INTERFACE_STATE_CHANGED:
|
|
|
|
let network = subject.QueryInterface(Ci.nsINetworkInterface);
|
|
|
|
debug("Network " + network.name + " changed state to " + network.state);
|
|
|
|
switch (network.state) {
|
|
|
|
case Ci.nsINetworkInterface.NETWORK_STATE_CONNECTED:
|
2012-09-26 05:57:37 -07:00
|
|
|
// Add host route for data calls
|
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE ||
|
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS ||
|
2012-08-30 09:57:33 -07:00
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_SUPL) {
|
|
|
|
this.addHostRoute(network);
|
|
|
|
}
|
2013-07-11 22:01:37 -07:00
|
|
|
// Add extra host route. For example, mms proxy or mmsc.
|
|
|
|
this.setExtraHostRoute(network);
|
2012-09-04 22:29:48 -07:00
|
|
|
// Remove pre-created default route and let setAndConfigureActive()
|
|
|
|
// to set default route only on preferred network
|
|
|
|
this.removeDefaultRoute(network.name);
|
2012-08-30 09:57:33 -07:00
|
|
|
this.setAndConfigureActive();
|
2013-01-22 20:05:34 -08:00
|
|
|
// Update data connection when Wifi connected/disconnected
|
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_WIFI) {
|
2013-07-02 02:36:51 -07:00
|
|
|
this.mRIL.getRadioInterface(0).updateRILNetworkInterface();
|
2013-01-22 20:05:34 -08:00
|
|
|
}
|
2013-05-16 04:39:58 -07:00
|
|
|
|
|
|
|
this.onConnectionChanged(network);
|
|
|
|
|
2012-11-14 19:26:43 -08:00
|
|
|
// Probing the public network accessibility after routing table is ready
|
|
|
|
CaptivePortalDetectionHelper.notify(CaptivePortalDetectionHelper.EVENT_CONNECT, this.active);
|
2012-08-30 09:57:33 -07:00
|
|
|
break;
|
2012-08-29 12:27:08 -07:00
|
|
|
case Ci.nsINetworkInterface.NETWORK_STATE_DISCONNECTED:
|
2012-09-26 05:57:37 -07:00
|
|
|
// Remove host route for data calls
|
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE ||
|
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS ||
|
2012-08-30 09:57:33 -07:00
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_SUPL) {
|
|
|
|
this.removeHostRoute(network);
|
|
|
|
}
|
2013-07-11 22:01:37 -07:00
|
|
|
// Remove extra host route. For example, mms proxy or mmsc.
|
|
|
|
this.removeExtraHostRoute(network);
|
2013-05-02 03:02:33 -07:00
|
|
|
// Remove routing table in /proc/net/route
|
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_WIFI) {
|
2013-07-01 20:50:50 -07:00
|
|
|
this.resetRoutingTable(network);
|
2013-07-11 22:01:37 -07:00
|
|
|
} else if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE) {
|
|
|
|
this.removeDefaultRoute(network.name);
|
2013-05-02 03:02:33 -07:00
|
|
|
}
|
2012-11-14 19:26:43 -08:00
|
|
|
// Abort ongoing captive portal detection on the wifi interface
|
|
|
|
CaptivePortalDetectionHelper.notify(CaptivePortalDetectionHelper.EVENT_DISCONNECT, network);
|
2012-08-29 12:27:08 -07:00
|
|
|
this.setAndConfigureActive();
|
2013-01-22 20:05:34 -08:00
|
|
|
// Update data connection when Wifi connected/disconnected
|
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_WIFI) {
|
2013-07-02 02:36:51 -07:00
|
|
|
this.mRIL.getRadioInterface(0).updateRILNetworkInterface();
|
2013-01-22 20:05:34 -08:00
|
|
|
}
|
2012-08-29 12:27:08 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-05-31 01:14:45 -07:00
|
|
|
case TOPIC_INTERFACE_REGISTERED:
|
|
|
|
let regNetwork = subject.QueryInterface(Ci.nsINetworkInterface);
|
2013-07-11 22:01:37 -07:00
|
|
|
// Add extra host route. For example, mms proxy or mmsc.
|
|
|
|
this.setExtraHostRoute(regNetwork);
|
2013-05-31 01:14:45 -07:00
|
|
|
break;
|
|
|
|
case TOPIC_INTERFACE_UNREGISTERED:
|
|
|
|
let unregNetwork = subject.QueryInterface(Ci.nsINetworkInterface);
|
2013-07-11 22:01:37 -07:00
|
|
|
// Remove extra host route. For example, mms proxy or mmsc.
|
|
|
|
this.removeExtraHostRoute(unregNetwork);
|
2013-05-31 01:14:45 -07:00
|
|
|
break;
|
2012-08-29 12:27:08 -07:00
|
|
|
case TOPIC_MOZSETTINGS_CHANGED:
|
|
|
|
let setting = JSON.parse(data);
|
|
|
|
this.handle(setting.key, setting.value);
|
|
|
|
break;
|
2012-12-29 10:11:04 -08:00
|
|
|
case TOPIC_PREF_CHANGED:
|
|
|
|
this._manageOfflineStatus =
|
|
|
|
Services.prefs.getBoolPref(PREF_MANAGE_OFFLINE_STATUS);
|
|
|
|
debug(PREF_MANAGE_OFFLINE_STATUS + " has changed to " + this._manageOfflineStatus);
|
|
|
|
break;
|
2012-08-29 12:27:08 -07:00
|
|
|
case TOPIC_XPCOM_SHUTDOWN:
|
|
|
|
Services.obs.removeObserver(this, TOPIC_XPCOM_SHUTDOWN);
|
|
|
|
Services.obs.removeObserver(this, TOPIC_MOZSETTINGS_CHANGED);
|
2013-05-31 01:14:45 -07:00
|
|
|
Services.obs.removeObserver(this, TOPIC_INTERFACE_REGISTERED);
|
|
|
|
Services.obs.removeObserver(this, TOPIC_INTERFACE_UNREGISTERED);
|
2012-08-29 12:27:08 -07:00
|
|
|
Services.obs.removeObserver(this, TOPIC_INTERFACE_STATE_CHANGED);
|
2012-02-14 12:12:11 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-05-02 04:41:20 -07:00
|
|
|
receiveMessage: function receiveMessage(aMsg) {
|
|
|
|
switch (aMsg.name) {
|
|
|
|
case "NetworkInterfaceList:ListInterface": {
|
|
|
|
let excludeMms = aMsg.json.exculdeMms;
|
|
|
|
let excludeSupl = aMsg.json.exculdeSupl;
|
|
|
|
let interfaces = [];
|
|
|
|
|
|
|
|
for each (let i in this.networkInterfaces) {
|
|
|
|
if ((i.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS && excludeMms) ||
|
|
|
|
(i.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_SUPL && excludeSupl)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
interfaces.push({
|
|
|
|
state: i.state,
|
|
|
|
type: i.type,
|
|
|
|
name: i.name,
|
|
|
|
dhcp: i.dhcp,
|
|
|
|
ip: i.ip,
|
|
|
|
netmask: i.netmask,
|
|
|
|
broadcast: i.broadcast,
|
|
|
|
gateway: i.gateway,
|
|
|
|
dns1: i.dns1,
|
|
|
|
dns2: i.dns2,
|
|
|
|
httpProxyHost: i.httpProxyHost,
|
|
|
|
httpProxyPort: i.httpProxyPort
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return interfaces;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-02-14 12:12:11 -08:00
|
|
|
// nsINetworkManager
|
|
|
|
|
|
|
|
registerNetworkInterface: function registerNetworkInterface(network) {
|
|
|
|
if (!(network instanceof Ci.nsINetworkInterface)) {
|
|
|
|
throw Components.Exception("Argument must be nsINetworkInterface.",
|
|
|
|
Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
if (network.name in this.networkInterfaces) {
|
|
|
|
throw Components.Exception("Network with that name already registered!",
|
|
|
|
Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
this.networkInterfaces[network.name] = network;
|
2012-09-26 05:57:37 -07:00
|
|
|
// Add host route for data calls
|
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE ||
|
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS ||
|
2012-08-30 09:57:33 -07:00
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_SUPL) {
|
|
|
|
this.addHostRoute(network);
|
|
|
|
}
|
2012-09-04 22:29:48 -07:00
|
|
|
// Remove pre-created default route and let setAndConfigureActive()
|
|
|
|
// to set default route only on preferred network
|
|
|
|
this.removeDefaultRoute(network.name);
|
2012-02-14 12:12:11 -08:00
|
|
|
this.setAndConfigureActive();
|
|
|
|
Services.obs.notifyObservers(network, TOPIC_INTERFACE_REGISTERED, null);
|
|
|
|
debug("Network '" + network.name + "' registered.");
|
|
|
|
},
|
|
|
|
|
|
|
|
unregisterNetworkInterface: function unregisterNetworkInterface(network) {
|
|
|
|
if (!(network instanceof Ci.nsINetworkInterface)) {
|
|
|
|
throw Components.Exception("Argument must be nsINetworkInterface.",
|
|
|
|
Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
if (!(network.name in this.networkInterfaces)) {
|
|
|
|
throw Components.Exception("No network with that name registered.",
|
|
|
|
Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
delete this.networkInterfaces[network.name];
|
2012-09-26 05:57:37 -07:00
|
|
|
// Remove host route for data calls
|
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE ||
|
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS ||
|
2012-08-30 09:57:33 -07:00
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_SUPL) {
|
|
|
|
this.removeHostRoute(network);
|
|
|
|
}
|
2012-02-14 12:12:11 -08:00
|
|
|
this.setAndConfigureActive();
|
|
|
|
Services.obs.notifyObservers(network, TOPIC_INTERFACE_UNREGISTERED, null);
|
|
|
|
debug("Network '" + network.name + "' unregistered.");
|
|
|
|
},
|
|
|
|
|
2012-12-29 10:11:04 -08:00
|
|
|
_manageOfflineStatus: true,
|
|
|
|
|
2012-02-14 12:12:11 -08:00
|
|
|
networkInterfaces: null,
|
|
|
|
|
|
|
|
_preferredNetworkType: DEFAULT_PREFERRED_NETWORK_TYPE,
|
|
|
|
get preferredNetworkType() {
|
|
|
|
return this._preferredNetworkType;
|
|
|
|
},
|
|
|
|
set preferredNetworkType(val) {
|
|
|
|
if ([Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
|
2013-07-11 22:01:37 -07:00
|
|
|
Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE].indexOf(val) == -1) {
|
2012-02-14 12:12:11 -08:00
|
|
|
throw "Invalid network type";
|
|
|
|
}
|
|
|
|
this._preferredNetworkType = val;
|
|
|
|
},
|
|
|
|
|
|
|
|
active: null,
|
|
|
|
_overriddenActive: null,
|
|
|
|
|
2013-05-02 03:02:33 -07:00
|
|
|
// Clone network info so we can still get information when network is disconnected
|
|
|
|
_activeInfo: null,
|
|
|
|
|
2012-02-14 12:12:11 -08:00
|
|
|
overrideActive: function overrideActive(network) {
|
2013-07-11 22:01:37 -07:00
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS ||
|
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_SUPL) {
|
|
|
|
throw "Invalid network type";
|
|
|
|
}
|
2012-02-14 12:12:11 -08:00
|
|
|
this._overriddenActive = network;
|
|
|
|
this.setAndConfigureActive();
|
|
|
|
},
|
|
|
|
|
2013-06-06 11:40:50 -07:00
|
|
|
getNetworkInterfaceStats: function getNetworkInterfaceStats(networkName, callback) {
|
|
|
|
debug("getNetworkInterfaceStats for " + networkName);
|
2012-10-04 09:39:59 -07:00
|
|
|
|
|
|
|
let params = {
|
|
|
|
cmd: "getNetworkInterfaceStats",
|
2013-06-06 11:40:50 -07:00
|
|
|
ifname: networkName
|
2012-10-04 09:39:59 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
params.report = true;
|
|
|
|
params.isAsync = true;
|
|
|
|
|
|
|
|
this.controlMessage(params, function(result) {
|
2013-06-06 11:40:50 -07:00
|
|
|
let success = result.resultCode >= NETD_COMMAND_OKAY &&
|
|
|
|
result.resultCode < NETD_COMMAND_ERROR;
|
|
|
|
callback.networkStatsAvailable(success, result.rxBytes,
|
|
|
|
result.txBytes, result.date);
|
2012-10-04 09:39:59 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-07-30 01:07:13 -07:00
|
|
|
setWifiOperationMode: function setWifiOperationMode(interfaceName, mode, callback) {
|
|
|
|
debug("setWifiOperationMode on " + interfaceName + " to " + mode);
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
cmd: "setWifiOperationMode",
|
|
|
|
ifname: interfaceName,
|
|
|
|
mode: mode
|
|
|
|
};
|
|
|
|
|
|
|
|
params.report = true;
|
|
|
|
params.isAsync = true;
|
|
|
|
|
|
|
|
this.controlMessage(params, function(result) {
|
|
|
|
if (isError(result.resultCode)) {
|
|
|
|
callback.wifiOperationModeResult("netd command error");
|
|
|
|
} else {
|
|
|
|
callback.wifiOperationModeResult(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-02-14 12:12:11 -08:00
|
|
|
// Helpers
|
|
|
|
|
2012-08-29 12:27:08 -07:00
|
|
|
controlMessage: function controlMessage(params, callback) {
|
|
|
|
if (callback) {
|
|
|
|
let id = callback.name;
|
|
|
|
params.id = id;
|
|
|
|
this.controlCallbacks[id] = callback;
|
|
|
|
}
|
|
|
|
this.worker.postMessage(params);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleWorkerMessage: function handleWorkerMessage(e) {
|
2012-12-29 10:11:04 -08:00
|
|
|
debug("NetworkManager received message from worker: " + JSON.stringify(e.data));
|
2012-08-29 12:27:08 -07:00
|
|
|
let response = e.data;
|
|
|
|
let id = response.id;
|
2013-04-23 00:27:05 -07:00
|
|
|
if (id == 'broadcast') {
|
|
|
|
Services.obs.notifyObservers(null, response.topic, response.reason);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-29 12:27:08 -07:00
|
|
|
let callback = this.controlCallbacks[id];
|
|
|
|
if (callback) {
|
|
|
|
callback.call(this, response);
|
|
|
|
}
|
2012-02-14 12:12:11 -08:00
|
|
|
},
|
|
|
|
|
2013-07-11 22:01:37 -07:00
|
|
|
setExtraHostRoute: function setExtraHostRoute(network) {
|
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS) {
|
|
|
|
debug("Network '" + network.name + "' registered, adding mmsproxy and/or mmsc route");
|
|
|
|
let mmsHosts = this.resolveHostname(
|
|
|
|
[Services.prefs.getCharPref("ril.mms.mmsproxy"),
|
|
|
|
Services.prefs.getCharPref("ril.mms.mmsc")]);
|
|
|
|
this.addHostRouteWithResolve(network, mmsHosts);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
removeExtraHostRoute: function removeExtraHostRoute(network) {
|
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS) {
|
|
|
|
debug("Network '" + network.name + "' unregistered, removing mmsproxy and/or mmsc route");
|
|
|
|
let mmsHosts = this.resolveHostname(
|
|
|
|
[Services.prefs.getCharPref("ril.mms.mmsproxy"),
|
|
|
|
Services.prefs.getCharPref("ril.mms.mmsc")]);
|
|
|
|
this.removeHostRouteWithResolve(network, mmsHosts);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-02-14 12:12:11 -08:00
|
|
|
/**
|
|
|
|
* Determine the active interface and configure it.
|
|
|
|
*/
|
|
|
|
setAndConfigureActive: function setAndConfigureActive() {
|
|
|
|
debug("Evaluating whether active network needs to be changed.");
|
2012-05-11 13:05:51 -07:00
|
|
|
let oldActive = this.active;
|
2012-11-01 02:09:45 -07:00
|
|
|
let defaultDataNetwork;
|
2012-02-14 12:12:11 -08:00
|
|
|
|
|
|
|
if (this._overriddenActive) {
|
|
|
|
debug("We have an override for the active network: " +
|
|
|
|
this._overriddenActive.name);
|
|
|
|
// The override was just set, so reconfigure the network.
|
|
|
|
if (this.active != this._overriddenActive) {
|
|
|
|
this.active = this._overriddenActive;
|
2012-05-11 13:05:51 -07:00
|
|
|
this.setDefaultRouteAndDNS(oldActive);
|
2013-02-07 00:21:28 -08:00
|
|
|
Services.obs.notifyObservers(this.active, TOPIC_ACTIVE_CHANGED, null);
|
2012-02-14 12:12:11 -08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-20 18:09:16 -08:00
|
|
|
// The active network is already our preferred type.
|
2012-05-11 13:05:51 -07:00
|
|
|
if (this.active &&
|
|
|
|
this.active.state == Ci.nsINetworkInterface.NETWORK_STATE_CONNECTED &&
|
|
|
|
this.active.type == this._preferredNetworkType) {
|
2013-02-20 18:09:16 -08:00
|
|
|
debug("Active network is already our preferred type.");
|
|
|
|
this.setDefaultRouteAndDNS(oldActive);
|
2012-02-14 12:12:11 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a suitable network interface to activate.
|
|
|
|
this.active = null;
|
2013-05-02 03:02:33 -07:00
|
|
|
this._activeInfo = Object.create(null);
|
2012-02-14 12:12:11 -08:00
|
|
|
for each (let network in this.networkInterfaces) {
|
|
|
|
if (network.state != Ci.nsINetworkInterface.NETWORK_STATE_CONNECTED) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-01 02:09:45 -07:00
|
|
|
if (network.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE) {
|
|
|
|
defaultDataNetwork = network;
|
|
|
|
}
|
2012-02-14 12:12:11 -08:00
|
|
|
this.active = network;
|
2013-05-02 03:02:33 -07:00
|
|
|
this._activeInfo = {name:network.name, ip:network.ip, netmask:network.netmask};
|
2012-02-14 12:12:11 -08:00
|
|
|
if (network.type == this.preferredNetworkType) {
|
|
|
|
debug("Found our preferred type of network: " + network.name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-06-20 01:21:24 -07:00
|
|
|
if (this.active) {
|
2012-11-01 02:09:45 -07:00
|
|
|
// Give higher priority to default data APN than seconary APN.
|
|
|
|
// If default data APN is not connected, we still set default route
|
|
|
|
// and DNS on seconary APN.
|
|
|
|
if (defaultDataNetwork &&
|
|
|
|
(this.active.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS ||
|
|
|
|
this.active.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_SUPL) &&
|
|
|
|
this.active.type != this.preferredNetworkType) {
|
|
|
|
this.active = defaultDataNetwork;
|
2012-08-30 09:57:33 -07:00
|
|
|
}
|
2013-07-11 22:01:37 -07:00
|
|
|
// Don't set default route on secondary APN
|
|
|
|
if (this.active.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS ||
|
|
|
|
this.active.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_SUPL) {
|
|
|
|
this.setDNS(this.active);
|
|
|
|
} else {
|
|
|
|
this.setDefaultRouteAndDNS(oldActive);
|
|
|
|
}
|
2013-02-07 00:21:28 -08:00
|
|
|
if (this.active != oldActive) {
|
|
|
|
Services.obs.notifyObservers(this.active, TOPIC_ACTIVE_CHANGED, null);
|
|
|
|
}
|
2012-02-14 12:12:11 -08:00
|
|
|
}
|
2012-12-29 10:11:04 -08:00
|
|
|
|
|
|
|
if (this._manageOfflineStatus) {
|
|
|
|
Services.io.offline = !this.active;
|
|
|
|
}
|
2012-02-14 12:12:11 -08:00
|
|
|
},
|
|
|
|
|
2013-05-02 03:02:33 -07:00
|
|
|
resetRoutingTable: function resetRoutingTable(network) {
|
|
|
|
let options = {
|
|
|
|
cmd: "removeNetworkRoute",
|
|
|
|
ifname: network.name,
|
|
|
|
ip : network.ip,
|
|
|
|
netmask: network.netmask,
|
|
|
|
};
|
|
|
|
this.worker.postMessage(options);
|
|
|
|
},
|
|
|
|
|
2013-07-11 22:01:37 -07:00
|
|
|
setDNS: function setDNS(networkInterface) {
|
|
|
|
debug("Going DNS to " + networkInterface.name);
|
|
|
|
let options = {
|
|
|
|
cmd: "setDNS",
|
|
|
|
ifname: networkInterface.name,
|
|
|
|
dns1_str: networkInterface.dns1,
|
|
|
|
dns2_str: networkInterface.dns2
|
|
|
|
};
|
|
|
|
this.worker.postMessage(options);
|
|
|
|
},
|
|
|
|
|
2012-05-11 13:05:51 -07:00
|
|
|
setDefaultRouteAndDNS: function setDefaultRouteAndDNS(oldInterface) {
|
2012-02-14 12:12:11 -08:00
|
|
|
debug("Going to change route and DNS to " + this.active.name);
|
2012-05-11 13:05:51 -07:00
|
|
|
let options = {
|
|
|
|
cmd: this.active.dhcp ? "runDHCPAndSetDefaultRouteAndDNS" : "setDefaultRouteAndDNS",
|
|
|
|
ifname: this.active.name,
|
2012-11-06 23:12:52 -08:00
|
|
|
oldIfname: (oldInterface && oldInterface != this.active) ? oldInterface.name : null,
|
|
|
|
gateway_str: this.active.gateway,
|
|
|
|
dns1_str: this.active.dns1,
|
|
|
|
dns2_str: this.active.dns2
|
2012-05-11 13:05:51 -07:00
|
|
|
};
|
|
|
|
this.worker.postMessage(options);
|
2012-08-01 07:55:43 -07:00
|
|
|
this.setNetworkProxy();
|
2012-02-14 12:12:11 -08:00
|
|
|
},
|
|
|
|
|
2012-09-04 22:29:48 -07:00
|
|
|
removeDefaultRoute: function removeDefaultRoute(ifname) {
|
|
|
|
debug("Remove default route for " + ifname);
|
|
|
|
let options = {
|
|
|
|
cmd: "removeDefaultRoute",
|
|
|
|
ifname: ifname
|
|
|
|
}
|
|
|
|
this.worker.postMessage(options);
|
|
|
|
},
|
|
|
|
|
2012-08-30 09:57:33 -07:00
|
|
|
addHostRoute: function addHostRoute(network) {
|
|
|
|
debug("Going to add host route on " + network.name);
|
|
|
|
let options = {
|
|
|
|
cmd: "addHostRoute",
|
|
|
|
ifname: network.name,
|
|
|
|
gateway: network.gateway,
|
2013-05-31 01:14:45 -07:00
|
|
|
hostnames: [network.dns1, network.dns2, network.httpProxyHost]
|
2012-08-30 09:57:33 -07:00
|
|
|
};
|
|
|
|
this.worker.postMessage(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeHostRoute: function removeHostRoute(network) {
|
|
|
|
debug("Going to remove host route on " + network.name);
|
|
|
|
let options = {
|
|
|
|
cmd: "removeHostRoute",
|
|
|
|
ifname: network.name,
|
|
|
|
gateway: network.gateway,
|
2013-05-31 01:14:45 -07:00
|
|
|
hostnames: [network.dns1, network.dns2, network.httpProxyHost]
|
|
|
|
};
|
|
|
|
this.worker.postMessage(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
resolveHostname: function resolveHostname(hosts) {
|
|
|
|
let retval = [];
|
|
|
|
|
2013-06-05 02:20:50 -07:00
|
|
|
for (let hostname of hosts) {
|
|
|
|
try {
|
|
|
|
let uri = Services.io.newURI(hostname, null, null);
|
|
|
|
hostname = uri.host;
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
if (hostname.match(this.REGEXP_IPV4) ||
|
|
|
|
hostname.match(this.REGEXP_IPV6)) {
|
|
|
|
retval.push(hostname);
|
2013-05-31 01:14:45 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-05 02:20:50 -07:00
|
|
|
try {
|
|
|
|
let hostnameIps = gDNSService.resolve(hostname, 0);
|
|
|
|
while (hostnameIps.hasMore()) {
|
|
|
|
retval.push(hostnameIps.getNextAddrAsString());
|
|
|
|
debug("Found IP at: " + JSON.stringify(retval));
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
2013-05-31 01:14:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
},
|
|
|
|
|
|
|
|
addHostRouteWithResolve: function addHostRouteWithResolve(network, hosts) {
|
|
|
|
debug("Going to add host route after dns resolution on " + network.name);
|
|
|
|
let options = {
|
|
|
|
cmd: "addHostRoute",
|
|
|
|
ifname: network.name,
|
|
|
|
gateway: network.gateway,
|
|
|
|
hostnames: hosts
|
|
|
|
};
|
|
|
|
this.worker.postMessage(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeHostRouteWithResolve: function removeHostRouteWithResolve(network, hosts) {
|
|
|
|
debug("Going to remove host route after dns resolution on " + network.name);
|
|
|
|
let options = {
|
|
|
|
cmd: "removeHostRoute",
|
|
|
|
ifname: network.name,
|
|
|
|
gateway: network.gateway,
|
|
|
|
hostnames: hosts
|
2012-08-30 09:57:33 -07:00
|
|
|
};
|
|
|
|
this.worker.postMessage(options);
|
|
|
|
},
|
|
|
|
|
2012-08-01 07:55:43 -07:00
|
|
|
setNetworkProxy: function setNetworkProxy() {
|
|
|
|
try {
|
|
|
|
if (!this.active.httpProxyHost || this.active.httpProxyHost == "") {
|
|
|
|
// Sets direct connection to internet.
|
|
|
|
Services.prefs.clearUserPref("network.proxy.type");
|
|
|
|
Services.prefs.clearUserPref("network.proxy.share_proxy_settings");
|
|
|
|
Services.prefs.clearUserPref("network.proxy.http");
|
|
|
|
Services.prefs.clearUserPref("network.proxy.http_port");
|
2012-12-03 12:18:50 -08:00
|
|
|
Services.prefs.clearUserPref("network.proxy.ssl");
|
|
|
|
Services.prefs.clearUserPref("network.proxy.ssl_port");
|
2012-08-01 07:55:43 -07:00
|
|
|
debug("No proxy support for " + this.active.name + " network interface.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Going to set proxy settings for " + this.active.name + " network interface.");
|
|
|
|
// Sets manual proxy configuration.
|
|
|
|
Services.prefs.setIntPref("network.proxy.type", MANUAL_PROXY_CONFIGURATION);
|
|
|
|
// Do not use this proxy server for all protocols.
|
|
|
|
Services.prefs.setBoolPref("network.proxy.share_proxy_settings", false);
|
|
|
|
Services.prefs.setCharPref("network.proxy.http", this.active.httpProxyHost);
|
2012-12-03 12:18:50 -08:00
|
|
|
Services.prefs.setCharPref("network.proxy.ssl", this.active.httpProxyHost);
|
2012-08-01 07:55:43 -07:00
|
|
|
let port = this.active.httpProxyPort == "" ? 8080 : this.active.httpProxyPort;
|
|
|
|
Services.prefs.setIntPref("network.proxy.http_port", port);
|
2012-12-03 12:18:50 -08:00
|
|
|
Services.prefs.setIntPref("network.proxy.ssl_port", port);
|
2012-08-01 07:55:43 -07:00
|
|
|
} catch (ex) {
|
|
|
|
debug("Exception " + ex + ". Unable to set proxy setting for "
|
|
|
|
+ this.active.name + " network interface.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2012-08-29 12:27:08 -07:00
|
|
|
|
|
|
|
// nsISettingsServiceCallback
|
|
|
|
|
|
|
|
tetheringSettings: {},
|
|
|
|
|
2013-02-27 23:22:56 -08:00
|
|
|
initTetheringSettings: function initTetheringSettings() {
|
|
|
|
this.tetheringSettings[SETTINGS_USB_ENABLED] = false;
|
|
|
|
this.tetheringSettings[SETTINGS_USB_IP] = DEFAULT_USB_IP;
|
|
|
|
this.tetheringSettings[SETTINGS_USB_PREFIX] = DEFAULT_USB_PREFIX;
|
|
|
|
this.tetheringSettings[SETTINGS_USB_DHCPSERVER_STARTIP] = DEFAULT_USB_DHCPSERVER_STARTIP;
|
|
|
|
this.tetheringSettings[SETTINGS_USB_DHCPSERVER_ENDIP] = DEFAULT_USB_DHCPSERVER_ENDIP;
|
|
|
|
this.tetheringSettings[SETTINGS_USB_DNS1] = DEFAULT_DNS1;
|
|
|
|
this.tetheringSettings[SETTINGS_USB_DNS2] = DEFAULT_DNS2;
|
2013-06-19 03:57:58 -07:00
|
|
|
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_DHCPSERVER_STARTIP] = DEFAULT_WIFI_DHCPSERVER_STARTIP;
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_DHCPSERVER_ENDIP] = DEFAULT_WIFI_DHCPSERVER_ENDIP;
|
2013-02-27 23:22:56 -08:00
|
|
|
},
|
|
|
|
|
2013-05-16 04:39:58 -07:00
|
|
|
_requestCount: 0,
|
|
|
|
|
2012-08-29 12:27:08 -07:00
|
|
|
handle: function handle(aName, aResult) {
|
|
|
|
switch(aName) {
|
|
|
|
case SETTINGS_USB_ENABLED:
|
2013-05-16 04:39:58 -07:00
|
|
|
this._oldUsbTetheringEnabledState = this.tetheringSettings[SETTINGS_USB_ENABLED];
|
2012-08-29 12:27:08 -07:00
|
|
|
case SETTINGS_USB_IP:
|
|
|
|
case SETTINGS_USB_PREFIX:
|
|
|
|
case SETTINGS_USB_DHCPSERVER_STARTIP:
|
|
|
|
case SETTINGS_USB_DHCPSERVER_ENDIP:
|
2013-02-27 23:22:56 -08:00
|
|
|
case SETTINGS_USB_DNS1:
|
|
|
|
case SETTINGS_USB_DNS2:
|
2013-06-19 03:57:58 -07:00
|
|
|
case SETTINGS_WIFI_DHCPSERVER_STARTIP:
|
|
|
|
case SETTINGS_WIFI_DHCPSERVER_ENDIP:
|
2013-05-16 04:39:58 -07:00
|
|
|
if (aResult !== null) {
|
2013-02-27 23:22:56 -08:00
|
|
|
this.tetheringSettings[aName] = aResult;
|
|
|
|
}
|
2012-08-29 12:27:08 -07:00
|
|
|
debug("'" + aName + "'" + " is now " + this.tetheringSettings[aName]);
|
2013-05-16 04:39:58 -07:00
|
|
|
let index = this._usbTetheringSettingsToRead.indexOf(aName);
|
|
|
|
|
|
|
|
if (index != -1) {
|
|
|
|
this._usbTetheringSettingsToRead.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._usbTetheringSettingsToRead.length) {
|
|
|
|
debug("We haven't read completely the usb Tethering data from settings db.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._oldUsbTetheringEnabledState === this.tetheringSettings[SETTINGS_USB_ENABLED]) {
|
|
|
|
debug("No changes for SETTINGS_USB_ENABLED flag. Nothing to do.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._requestCount++;
|
|
|
|
if (this._requestCount === 1) {
|
|
|
|
this.handleUSBTetheringToggle(aResult);
|
|
|
|
}
|
2012-08-29 12:27:08 -07:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
handleError: function handleError(aErrorMessage) {
|
|
|
|
debug("There was an error while reading Tethering settings.");
|
|
|
|
this.tetheringSettings = {};
|
|
|
|
this.tetheringSettings[SETTINGS_USB_ENABLED] = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
getNetworkInterface: function getNetworkInterface(type) {
|
|
|
|
for each (let network in this.networkInterfaces) {
|
|
|
|
if (network.type == type) {
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2013-05-16 04:39:58 -07:00
|
|
|
_usbTetheringAction: TETHERING_STATE_IDLE,
|
|
|
|
|
|
|
|
_usbTetheringSettingsToRead: [],
|
|
|
|
|
|
|
|
_oldUsbTetheringEnabledState: null,
|
|
|
|
|
2012-08-29 12:27:08 -07:00
|
|
|
// External and internal interface name.
|
|
|
|
_tetheringInterface: null,
|
|
|
|
|
2013-05-16 04:39:58 -07:00
|
|
|
handleLastRequest: function handleLastRequest() {
|
|
|
|
let count = this._requestCount;
|
|
|
|
this._requestCount = 0;
|
|
|
|
|
|
|
|
if (count === 1) {
|
|
|
|
if (this.wantConnectionEvent) {
|
|
|
|
if (this.tetheringSettings[SETTINGS_USB_ENABLED]) {
|
|
|
|
this.wantConnectionEvent.call(this);
|
|
|
|
}
|
|
|
|
this.wantConnectionEvent = null;
|
|
|
|
}
|
2012-08-29 12:27:08 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-16 04:39:58 -07:00
|
|
|
if (count > 1) {
|
|
|
|
this.handleUSBTetheringToggle(this.tetheringSettings[SETTINGS_USB_ENABLED]);
|
|
|
|
this.wantConnectionEvent = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleUSBTetheringToggle: function handleUSBTetheringToggle(enable) {
|
2012-08-29 12:27:08 -07:00
|
|
|
if (!enable) {
|
|
|
|
this.tetheringSettings[SETTINGS_USB_ENABLED] = false;
|
2013-02-14 12:21:08 -08:00
|
|
|
this.enableUsbRndis(false, this.enableUsbRndisResult);
|
2012-08-29 12:27:08 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.active) {
|
|
|
|
this._tetheringInterface[TETHERING_TYPE_USB].externalInterface = this.active.name
|
|
|
|
} else {
|
|
|
|
let mobile = this.getNetworkInterface(Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE);
|
2013-02-27 23:22:56 -08:00
|
|
|
if (mobile) {
|
|
|
|
this._tetheringInterface[TETHERING_TYPE_USB].externalInterface = mobile.name;
|
2012-08-29 12:27:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.tetheringSettings[SETTINGS_USB_ENABLED] = true;
|
2013-02-14 12:21:08 -08:00
|
|
|
this.enableUsbRndis(true, this.enableUsbRndisResult);
|
2012-08-29 12:27:08 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
getUSBTetheringParameters: function getUSBTetheringParameters(enable, tetheringinterface) {
|
|
|
|
let interfaceIp;
|
|
|
|
let prefix;
|
2013-06-19 03:57:58 -07:00
|
|
|
let wifiDhcpStartIp;
|
|
|
|
let wifiDhcpEndIp;
|
|
|
|
let usbDhcpStartIp;
|
|
|
|
let usbDhcpEndIp;
|
2013-02-27 23:22:56 -08:00
|
|
|
let dns1;
|
|
|
|
let dns2;
|
2012-08-29 12:27:08 -07:00
|
|
|
let internalInterface = tetheringinterface.internalInterface;
|
|
|
|
let externalInterface = tetheringinterface.externalInterface;
|
|
|
|
|
|
|
|
interfaceIp = this.tetheringSettings[SETTINGS_USB_IP];
|
|
|
|
prefix = this.tetheringSettings[SETTINGS_USB_PREFIX];
|
2013-06-19 03:57:58 -07:00
|
|
|
wifiDhcpStartIp = this.tetheringSettings[SETTINGS_WIFI_DHCPSERVER_STARTIP];
|
|
|
|
wifiDhcpEndIp = this.tetheringSettings[SETTINGS_WIFI_DHCPSERVER_ENDIP];
|
|
|
|
usbDhcpStartIp = this.tetheringSettings[SETTINGS_USB_DHCPSERVER_STARTIP];
|
|
|
|
usbDhcpEndIp = this.tetheringSettings[SETTINGS_USB_DHCPSERVER_ENDIP];
|
2013-02-27 23:22:56 -08:00
|
|
|
dns1 = this.tetheringSettings[SETTINGS_USB_DNS1];
|
|
|
|
dns2 = this.tetheringSettings[SETTINGS_USB_DNS2];
|
2012-08-29 12:27:08 -07:00
|
|
|
|
|
|
|
// Using the default values here until application support these settings.
|
|
|
|
if (interfaceIp == "" || prefix == "" ||
|
2013-06-19 03:57:58 -07:00
|
|
|
wifiDhcpStartIp == "" || wifiDhcpEndIp == "" ||
|
|
|
|
usbDhcpStartIp == "" || usbDhcpEndIp == "") {
|
2012-08-29 12:27:08 -07:00
|
|
|
debug("Invalid subnet information.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
ifname: internalInterface,
|
|
|
|
ip: interfaceIp,
|
|
|
|
prefix: prefix,
|
2013-06-19 03:57:58 -07:00
|
|
|
wifiStartIp: wifiDhcpStartIp,
|
|
|
|
wifiEndIp: wifiDhcpEndIp,
|
|
|
|
usbStartIp: usbDhcpStartIp,
|
|
|
|
usbEndIp: usbDhcpEndIp,
|
2013-02-27 23:22:56 -08:00
|
|
|
dns1: dns1,
|
|
|
|
dns2: dns2,
|
2012-08-29 12:27:08 -07:00
|
|
|
internalIfname: internalInterface,
|
|
|
|
externalIfname: externalInterface,
|
|
|
|
enable: enable,
|
|
|
|
link: enable ? NETWORK_INTERFACE_UP : NETWORK_INTERFACE_DOWN
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2012-09-13 00:19:08 -07:00
|
|
|
notifyError: function notifyError(resetSettings, callback, msg) {
|
|
|
|
if (resetSettings) {
|
|
|
|
let settingsLock = gSettingsService.createLock();
|
2012-11-30 03:37:34 -08:00
|
|
|
// Disable wifi tethering with a useful error message for the user.
|
|
|
|
settingsLock.set("tethering.wifi.enabled", false, null, msg);
|
2012-09-13 00:19:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
debug("setWifiTethering: " + (msg ? msg : "success"));
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback.wifiTetheringEnabledChange(msg);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-29 12:27:08 -07:00
|
|
|
// Enable/disable WiFi tethering by sending commands to netd.
|
2013-05-16 04:39:58 -07:00
|
|
|
setWifiTethering: function setWifiTethering(enable, network, config, callback) {
|
|
|
|
if (!network) {
|
|
|
|
this.notifyError(true, callback, "invalid network information");
|
2012-09-13 00:19:08 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-08-29 12:27:08 -07:00
|
|
|
|
2013-05-16 04:39:58 -07:00
|
|
|
if (!config) {
|
|
|
|
this.notifyError(true, callback, "invalid configuration");
|
2012-09-13 00:19:08 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-05-16 04:39:58 -07:00
|
|
|
|
2012-09-13 00:19:08 -07:00
|
|
|
this._tetheringInterface[TETHERING_TYPE_WIFI].internalInterface = network.name;
|
|
|
|
|
|
|
|
let mobile = this.getNetworkInterface(Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE);
|
2013-02-27 23:22:56 -08:00
|
|
|
// Update the real interface name
|
|
|
|
if (mobile) {
|
|
|
|
this._tetheringInterface[TETHERING_TYPE_WIFI].externalInterface = mobile.name;
|
2012-09-13 00:19:08 -07:00
|
|
|
}
|
2012-11-30 03:37:34 -08:00
|
|
|
|
2013-05-16 04:39:58 -07:00
|
|
|
config.ifname = this._tetheringInterface[TETHERING_TYPE_WIFI].internalInterface;
|
|
|
|
config.internalIfname = this._tetheringInterface[TETHERING_TYPE_WIFI].internalInterface;
|
|
|
|
config.externalIfname = this._tetheringInterface[TETHERING_TYPE_WIFI].externalInterface;
|
|
|
|
config.wifictrlinterfacename = WIFI_CTRL_INTERFACE;
|
2012-09-13 00:19:08 -07:00
|
|
|
|
2013-05-16 04:39:58 -07:00
|
|
|
config.cmd = "setWifiTethering";
|
2012-08-29 12:27:08 -07:00
|
|
|
// The callback function in controlMessage may not be fired immediately.
|
2013-05-16 04:39:58 -07:00
|
|
|
config.isAsync = true;
|
|
|
|
this.controlMessage(config, function setWifiTetheringResult(data) {
|
2012-09-13 00:19:08 -07:00
|
|
|
let code = data.resultCode;
|
|
|
|
let reason = data.resultReason;
|
|
|
|
let enable = data.enable;
|
|
|
|
let enableString = enable ? "Enable" : "Disable";
|
|
|
|
|
|
|
|
debug(enableString + " Wifi tethering result: Code " + code + " reason " + reason);
|
|
|
|
|
|
|
|
if (isError(code)) {
|
|
|
|
this.notifyError(true, callback, "netd command error");
|
|
|
|
} else {
|
|
|
|
this.notifyError(false, callback, null);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2012-08-29 12:27:08 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// Enable/disable USB tethering by sending commands to netd.
|
2013-05-16 04:39:58 -07:00
|
|
|
setUSBTethering: function setUSBTethering(enable,
|
|
|
|
tetheringInterface,
|
|
|
|
callback) {
|
2012-08-29 12:27:08 -07:00
|
|
|
let params = this.getUSBTetheringParameters(enable, tetheringInterface);
|
|
|
|
|
|
|
|
if (params === null) {
|
|
|
|
params = {
|
|
|
|
enable: enable,
|
|
|
|
resultCode: NETD_COMMAND_ERROR,
|
|
|
|
resultReason: "Invalid parameters"
|
|
|
|
};
|
2013-02-14 12:21:08 -08:00
|
|
|
this.enableUsbRndis(false, null);
|
2013-05-16 04:39:58 -07:00
|
|
|
this.usbTetheringResultReport(params);
|
2012-08-29 12:27:08 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
params.cmd = "setUSBTethering";
|
|
|
|
// The callback function in controlMessage may not be fired immediately.
|
|
|
|
params.isAsync = true;
|
2013-05-16 04:39:58 -07:00
|
|
|
this.controlMessage(params, callback);
|
2012-08-29 12:27:08 -07:00
|
|
|
},
|
|
|
|
|
2013-04-30 04:04:36 -07:00
|
|
|
getUsbInterface: function getUsbInterface() {
|
|
|
|
// Find the rndis interface.
|
|
|
|
for (let i = 0; i < this.possibleInterface.length; i++) {
|
|
|
|
try {
|
|
|
|
let file = new FileUtils.File(KERNEL_NETWORK_ENTRY + "/" +
|
|
|
|
this.possibleInterface[i]);
|
2013-05-23 19:38:31 -07:00
|
|
|
if (file.exists()) {
|
2013-04-30 04:04:36 -07:00
|
|
|
return this.possibleInterface[i];
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
debug("Not " + this.possibleInterface[i] + " interface.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug("Can't find rndis interface in possible lists.");
|
|
|
|
return DEFAULT_USB_INTERFACE_NAME;
|
|
|
|
},
|
|
|
|
|
2013-02-14 12:21:08 -08:00
|
|
|
enableUsbRndisResult: function enableUsbRndisResult(data) {
|
2012-08-29 12:27:08 -07:00
|
|
|
let result = data.result;
|
|
|
|
let enable = data.enable;
|
|
|
|
if (result) {
|
2013-04-30 04:04:36 -07:00
|
|
|
this._tetheringInterface[TETHERING_TYPE_USB].internalInterface = this.getUsbInterface();
|
2013-05-16 04:39:58 -07:00
|
|
|
this.setUSBTethering(enable,
|
|
|
|
this._tetheringInterface[TETHERING_TYPE_USB],
|
|
|
|
this.usbTetheringResultReport);
|
2012-08-29 12:27:08 -07:00
|
|
|
} else {
|
|
|
|
let params = {
|
|
|
|
enable: false,
|
|
|
|
resultCode: NETD_COMMAND_ERROR,
|
|
|
|
resultReason: "Failed to set usb function"
|
|
|
|
};
|
|
|
|
this.usbTetheringResultReport(params);
|
|
|
|
throw new Error("failed to set USB Function to adb");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Switch usb function by modifying property of persist.sys.usb.config.
|
2013-02-14 12:21:08 -08:00
|
|
|
enableUsbRndis: function enableUsbRndis(enable, callback) {
|
|
|
|
debug("enableUsbRndis: " + enable);
|
2012-08-29 12:27:08 -07:00
|
|
|
|
|
|
|
let params = {
|
2013-02-14 12:21:08 -08:00
|
|
|
cmd: "enableUsbRndis",
|
2012-08-29 12:27:08 -07:00
|
|
|
enable: enable
|
|
|
|
};
|
|
|
|
// Ask net work to report the result when this value is set to true.
|
|
|
|
if (callback) {
|
|
|
|
params.report = true;
|
|
|
|
} else {
|
|
|
|
params.report = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The callback function in controlMessage may not be fired immediately.
|
|
|
|
params.isAsync = true;
|
2013-05-16 04:39:58 -07:00
|
|
|
this._usbTetheringAction = TETHERING_STATE_ONGOING;
|
2012-08-29 12:27:08 -07:00
|
|
|
this.controlMessage(params, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
usbTetheringResultReport: function usbTetheringResultReport(data) {
|
|
|
|
let code = data.resultCode;
|
|
|
|
let reason = data.resultReason;
|
|
|
|
let enable = data.enable;
|
|
|
|
let enableString = enable ? "Enable" : "Disable";
|
2012-09-05 17:33:03 -07:00
|
|
|
let settingsLock = gSettingsService.createLock();
|
2012-08-29 12:27:08 -07:00
|
|
|
|
|
|
|
debug(enableString + " USB tethering result: Code " + code + " reason " + reason);
|
2013-05-16 04:39:58 -07:00
|
|
|
this._usbTetheringAction = TETHERING_STATE_IDLE;
|
2012-08-29 12:27:08 -07:00
|
|
|
// Disable tethering settings when fail to enable it.
|
2012-09-05 02:30:48 -07:00
|
|
|
if (isError(code)) {
|
2012-08-29 12:27:08 -07:00
|
|
|
this.tetheringSettings[SETTINGS_USB_ENABLED] = false;
|
|
|
|
settingsLock.set("tethering.usb.enabled", false, null);
|
2013-05-16 04:39:58 -07:00
|
|
|
// Skip others request when we found an error.
|
|
|
|
this._requestCount = 0;
|
|
|
|
} else {
|
|
|
|
this.handleLastRequest();
|
2012-08-29 12:27:08 -07:00
|
|
|
}
|
2013-05-16 04:39:58 -07:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
updateUpStream: function updateUpStream(previous, current, callback) {
|
|
|
|
let params = {
|
|
|
|
cmd: "updateUpStream",
|
|
|
|
isAsync: true,
|
|
|
|
previous: previous,
|
|
|
|
current: current
|
|
|
|
};
|
|
|
|
|
|
|
|
this.controlMessage(params, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
onConnectionChangedReport: function onConnectionChangedReport(data) {
|
|
|
|
let code = data.resultCode;
|
|
|
|
let reason = data.resultReason;
|
|
|
|
|
|
|
|
debug("onConnectionChangedReport result: Code " + code + " reason " + reason);
|
|
|
|
|
|
|
|
if (!isError(code)) {
|
|
|
|
// Update the external interface.
|
|
|
|
this._tetheringInterface[TETHERING_TYPE_USB].externalInterface = data.current.externalIfname;
|
|
|
|
debug("Change the interface name to " + data.current.externalIfname);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onConnectionChanged: function onConnectionChanged(network) {
|
|
|
|
if (network.state != Ci.nsINetworkInterface.NETWORK_STATE_CONNECTED) {
|
|
|
|
debug("We are only interested in CONNECTED event");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.tetheringSettings[SETTINGS_USB_ENABLED]) {
|
|
|
|
debug("Usb tethering settings is not enabled");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._tetheringInterface[TETHERING_TYPE_USB].externalInterface ===
|
|
|
|
this.active.name) {
|
|
|
|
debug("The active interface is the same");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let previous = {
|
|
|
|
internalIfname: this._tetheringInterface[TETHERING_TYPE_USB].internalInterface,
|
|
|
|
externalIfname: this._tetheringInterface[TETHERING_TYPE_USB].externalInterface
|
|
|
|
};
|
|
|
|
|
|
|
|
let current = {
|
|
|
|
internalIfname: this._tetheringInterface[TETHERING_TYPE_USB].internalInterface,
|
|
|
|
externalIfname: network.name
|
|
|
|
};
|
|
|
|
|
|
|
|
let callback = (function () {
|
|
|
|
// Update external network interface.
|
|
|
|
debug("Update upstream interface to " + network.name);
|
|
|
|
this.updateUpStream(previous, current, this.onConnectionChangedReport);
|
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
if (this._usbTetheringAction === TETHERING_STATE_ONGOING) {
|
|
|
|
debug("Postpone the event and handle it when state is idle.");
|
|
|
|
this.wantConnectionEvent = callback;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.wantConnectionEvent = null;
|
|
|
|
|
|
|
|
callback.call(this);
|
2012-08-29 12:27:08 -07:00
|
|
|
}
|
2012-02-14 12:12:11 -08:00
|
|
|
};
|
|
|
|
|
2012-11-14 19:26:43 -08:00
|
|
|
let CaptivePortalDetectionHelper = (function() {
|
|
|
|
|
|
|
|
const EVENT_CONNECT = "Connect";
|
|
|
|
const EVENT_DISCONNECT = "Disconnect";
|
|
|
|
let _ongoingInterface = null;
|
|
|
|
let _available = ("nsICaptivePortalDetector" in Ci);
|
|
|
|
let getService = function () {
|
2013-02-15 14:26:58 -08:00
|
|
|
return Cc['@mozilla.org/toolkit/captive-detector;1'].getService(Ci.nsICaptivePortalDetector);
|
2012-11-14 19:26:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
let _performDetection = function (interfaceName, callback) {
|
|
|
|
let capService = getService();
|
|
|
|
let capCallback = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsICaptivePortalCallback]),
|
|
|
|
prepare: function prepare() {
|
|
|
|
capService.finishPreparation(interfaceName);
|
|
|
|
},
|
|
|
|
complete: function complete(success) {
|
|
|
|
_ongoingInterface = null;
|
|
|
|
callback(success);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Abort any unfinished captive portal detection.
|
|
|
|
if (_ongoingInterface != null) {
|
|
|
|
capService.abort(_ongoingInterface);
|
|
|
|
_ongoingInterface = null;
|
|
|
|
}
|
2013-03-05 06:56:34 -08:00
|
|
|
try {
|
|
|
|
capService.checkCaptivePortal(interfaceName, capCallback);
|
|
|
|
_ongoingInterface = interfaceName;
|
|
|
|
} catch (e) {
|
|
|
|
debug('Fail to detect captive portal due to: ' + e.message);
|
|
|
|
}
|
2012-11-14 19:26:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
let _abort = function (interfaceName) {
|
|
|
|
if (_ongoingInterface !== interfaceName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let capService = getService();
|
|
|
|
capService.abort(_ongoingInterface);
|
|
|
|
_ongoingInterface = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
EVENT_CONNECT: EVENT_CONNECT,
|
|
|
|
EVENT_DISCONNECT: EVENT_DISCONNECT,
|
|
|
|
notify: function notify(eventType, network) {
|
|
|
|
switch (eventType) {
|
|
|
|
case EVENT_CONNECT:
|
|
|
|
// perform captive portal detection on wifi interface
|
|
|
|
if (_available && network &&
|
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_WIFI) {
|
|
|
|
_performDetection(network.name, function () {
|
|
|
|
// TODO: bug 837600
|
|
|
|
// We can disconnect wifi in here if user abort the login procedure.
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case EVENT_DISCONNECT:
|
|
|
|
if (_available &&
|
|
|
|
network.type == Ci.nsINetworkInterface.NETWORK_TYPE_WIFI) {
|
|
|
|
_abort(network.name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
2013-01-23 22:42:10 -08:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(NetworkManager.prototype, "mRIL",
|
|
|
|
"@mozilla.org/ril;1",
|
|
|
|
"nsIRadioInterfaceLayer");
|
2013-01-22 20:05:34 -08:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NetworkManager]);
|
2012-02-14 12:12:11 -08:00
|
|
|
|
|
|
|
|
|
|
|
let debug;
|
|
|
|
if (DEBUG) {
|
|
|
|
debug = function (s) {
|
|
|
|
dump("-*- NetworkManager: " + s + "\n");
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
debug = function (s) {};
|
|
|
|
}
|