Bug 993311 - Convert Network Stats API to WebIDL. r=bzbarsky.

--HG--
extra : rebase_source : c2523e634288c95213f44a789e6d46bfd40b8681
This commit is contained in:
John Shih 2014-04-08 14:42:12 +08:00
parent 46af3ae4ab
commit d212525a4c
14 changed files with 69 additions and 162 deletions

View File

@ -111,7 +111,6 @@ NetworkStatsAlarm.prototype = {
const NETWORKSTATSMANAGER_CONTRACTID = "@mozilla.org/networkStatsManager;1";
const NETWORKSTATSMANAGER_CID = Components.ID("{ceb874cd-cc1a-4e65-b404-cc2d3e42425f}");
const nsIDOMMozNetworkStatsManager = Ci.nsIDOMMozNetworkStatsManager;
function NetworkStatsManager() {
if (DEBUG) {
@ -122,40 +121,25 @@ function NetworkStatsManager() {
NetworkStatsManager.prototype = {
__proto__: DOMRequestIpcHelper.prototype,
checkPrivileges: function checkPrivileges() {
if (!this.hasPrivileges) {
throw Components.Exception("Permission denied", Cr.NS_ERROR_FAILURE);
}
},
getSamples: function getSamples(aNetwork, aStart, aEnd, aOptions) {
this.checkPrivileges();
if (aStart.constructor.name !== "Date" ||
aEnd.constructor.name !== "Date" ||
!(aNetwork instanceof this.window.MozNetworkStatsInterface) ||
aStart > aEnd) {
if (aStart > aEnd) {
throw Components.results.NS_ERROR_INVALID_ARG;
}
let appManifestURL = null;
let browsingTrafficOnly = false;
let serviceType = null;
if (aOptions) {
// appManifestURL is used to query network statistics by app;
// serviceType is used to query network statistics by system service.
// It is illegal to specify both of them at the same time.
if (aOptions.appManifestURL && aOptions.serviceType) {
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
}
// browsingTrafficOnly is meaningful only when querying by app.
if (!aOptions.appManifestURL && aOptions.browsingTrafficOnly) {
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
}
appManifestURL = aOptions.appManifestURL;
serviceType = aOptions.serviceType;
browsingTrafficOnly = aOptions.browsingTrafficOnly || false;
// appManifestURL is used to query network statistics by app;
// serviceType is used to query network statistics by system service.
// It is illegal to specify both of them at the same time.
if (aOptions.appManifestURL && aOptions.serviceType) {
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
}
// browsingTrafficOnly is meaningful only when querying by app.
if (!aOptions.appManifestURL && aOptions.browsingTrafficOnly) {
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
}
let appManifestURL = aOptions.appManifestURL;
let serviceType = aOptions.serviceType;
let browsingTrafficOnly = aOptions.browsingTrafficOnly;
// TODO Bug 929410 Date object cannot correctly pass through cpmm/ppmm IPC
// This is just a work-around by passing timestamp numbers.
@ -175,12 +159,6 @@ NetworkStatsManager.prototype = {
},
clearStats: function clearStats(aNetwork) {
this.checkPrivileges();
if (!aNetwork instanceof this.window.MozNetworkStatsInterface) {
throw Components.results.NS_ERROR_INVALID_ARG;
}
let request = this.createRequest();
cpmm.sendAsyncMessage("NetworkStats:Clear",
{ network: aNetwork.toJSON(),
@ -189,8 +167,6 @@ NetworkStatsManager.prototype = {
},
clearAllStats: function clearAllStats() {
this.checkPrivileges();
let request = this.createRequest();
cpmm.sendAsyncMessage("NetworkStats:ClearAll",
{id: this.getRequestId(request)});
@ -198,17 +174,6 @@ NetworkStatsManager.prototype = {
},
addAlarm: function addAlarm(aNetwork, aThreshold, aOptions) {
this.checkPrivileges();
if (!aOptions) {
aOptions = Object.create(null);
}
if (aOptions.startTime && aOptions.startTime.constructor.name !== "Date" ||
!(aNetwork instanceof this.window.MozNetworkStatsInterface)) {
throw Components.results.NS_ERROR_INVALID_ARG;
}
let request = this.createRequest();
cpmm.sendAsyncMessage("NetworkStats:SetAlarm",
{id: this.getRequestId(request),
@ -222,13 +187,8 @@ NetworkStatsManager.prototype = {
},
getAllAlarms: function getAllAlarms(aNetwork) {
this.checkPrivileges();
let network = null;
if (aNetwork) {
if (!aNetwork instanceof this.window.MozNetworkStatsInterface) {
throw Components.results.NS_ERROR_INVALID_ARG;
}
network = aNetwork.toJSON();
}
@ -241,8 +201,6 @@ NetworkStatsManager.prototype = {
},
removeAlarms: function removeAlarms(aAlarmId) {
this.checkPrivileges();
if (aAlarmId == 0) {
aAlarmId = -1;
}
@ -257,8 +215,6 @@ NetworkStatsManager.prototype = {
},
getAvailableNetworks: function getAvailableNetworks() {
this.checkPrivileges();
let request = this.createRequest();
cpmm.sendAsyncMessage("NetworkStats:GetAvailableNetworks",
{ id: this.getRequestId(request) });
@ -266,8 +222,6 @@ NetworkStatsManager.prototype = {
},
getAvailableServiceTypes: function getAvailableServiceTypes() {
this.checkPrivileges();
let request = this.createRequest();
cpmm.sendAsyncMessage("NetworkStats:GetAvailableServiceTypes",
{ id: this.getRequestId(request) });
@ -275,12 +229,10 @@ NetworkStatsManager.prototype = {
},
get sampleRate() {
this.checkPrivileges();
return cpmm.sendSyncMessage("NetworkStats:SampleRate")[0];
},
get maxStorageAge() {
this.checkPrivileges();
return cpmm.sendSyncMessage("NetworkStats:MaxStorageAge")[0];
},
@ -390,27 +342,7 @@ NetworkStatsManager.prototype = {
},
init: function(aWindow) {
// Set navigator.mozNetworkStats to null.
if (!Services.prefs.getBoolPref("dom.mozNetworkStats.enabled")) {
return null;
}
let principal = aWindow.document.nodePrincipal;
let secMan = Services.scriptSecurityManager;
let perm = principal == secMan.getSystemPrincipal() ?
Ci.nsIPermissionManager.ALLOW_ACTION :
Services.perms.testExactPermissionFromPrincipal(principal,
"networkstats-manage");
// Only pages with perm set can use the netstats.
this.hasPrivileges = perm == Ci.nsIPermissionManager.ALLOW_ACTION;
if (DEBUG) {
debug("has privileges: " + this.hasPrivileges);
}
if (!this.hasPrivileges) {
return null;
}
this.initDOMRequestHelper(aWindow, ["NetworkStats:Get:Return",
"NetworkStats:GetAvailableNetworks:Return",
@ -443,16 +375,10 @@ NetworkStatsManager.prototype = {
},
classID : NETWORKSTATSMANAGER_CID,
QueryInterface : XPCOMUtils.generateQI([nsIDOMMozNetworkStatsManager,
Ci.nsIDOMGlobalPropertyInitializer,
Ci.nsISupportsWeakReference,
Ci.nsIObserver]),
classInfo : XPCOMUtils.generateCI({classID: NETWORKSTATSMANAGER_CID,
contractID: NETWORKSTATSMANAGER_CONTRACTID,
classDescription: "NetworkStatsManager",
interfaces: [nsIDOMMozNetworkStatsManager],
flags: nsIClassInfo.DOM_OBJECT})
contractID : NETWORKSTATSMANAGER_CONTRACTID,
QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
Ci.nsISupportsWeakReference,
Ci.nsIObserver]),
}
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NetworkStatsAlarm,

View File

@ -12,4 +12,3 @@ contract @mozilla.org/networkstatsalarm;1 {a93ea13e-409c-4189-9b1e-95fff220be55}
component {ceb874cd-cc1a-4e65-b404-cc2d3e42425f} NetworkStatsManager.js
contract @mozilla.org/networkStatsManager;1 {ceb874cd-cc1a-4e65-b404-cc2d3e42425f}
category JavaScript-navigator-property mozNetworkStats @mozilla.org/networkStatsManager;1

View File

@ -12,7 +12,6 @@ XPIDL_SOURCES += [
if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
XPIDL_SOURCES += [
'nsIDOMNetworkStatsManager.idl',
'nsINetworkStatsServiceProxy.idl',
]

View File

@ -48,32 +48,30 @@ var steps = [
};
req.onerror = function () {
ok(req.error.name == "InvalidInterface", "Get InvalidInterface error");
is(req.error.name, "InvalidInterface", "Get InvalidInterface error");
next();
}
},
function () {
ok(true, "Calling addAlarm() with invalid network or parameters.");
var msg = "TypeError: Not enough arguments to MozNetworkStatsManager.addAlarm.";
try {
navigator.mozNetworkStats.addAlarm();
} catch(ex) {
ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
"addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no parameters");
is(ex.toString(), msg, "addAlarm() throws \"" + msg + "\" when no parameters");
}
try {
navigator.mozNetworkStats.addAlarm(100000);
} catch(ex) {
ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
"addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no network");
is(ex.toString(), msg, "addAlarm() throws " + msg + " when no network");
}
try {
navigator.mozNetworkStats.addAlarm(new window.MozNetworkStatsInterface(wifi));
} catch(ex) {
ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
"addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no threshold");
is(ex.toString(), msg, "addAlarm() throws " + msg + " when no threshold");
}
req = navigator.mozNetworkStats
@ -84,7 +82,7 @@ var steps = [
};
req.onerror = function () {
ok(req.error.name == "InvalidThresholdValue", "Get InvalidThresholdValue error");
is(req.error.name, "InvalidThresholdValue", "Get InvalidThresholdValue error");
next();
}
},
@ -109,8 +107,8 @@ var steps = [
.getAllAlarms(new window.MozNetworkStatsInterface(wifi));
req.onsuccess = function () {
ok(req.result.length == 1, "Only one alarm");
ok(req.result[0].alarmId == 1, "Get correct alarmId");
is(req.result.length, 1, "Only one alarm");
is(req.result[0].alarmId, 1, "Get correct alarmId");
next();
};
@ -226,8 +224,8 @@ SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]},
ok('mozNetworkStats' in navigator, "navigator.mozNetworkStats should exist");
ok(navigator.mozNetworkStats instanceof SpecialPowers.Ci.nsIDOMMozNetworkStatsManager,
"navigator.mozNetworkStats should be a nsIDOMMozNetworkStatsManager object");
ok(navigator.mozNetworkStats instanceof MozNetworkStatsManager,
"navigator.mozNetworkStats should be a MozNetworkStatsManager object");
test();
});

View File

@ -333,8 +333,8 @@ SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]},
ok('mozNetworkStats' in navigator, "navigator.mozNetworkStats should exist");
ok(navigator.mozNetworkStats instanceof SpecialPowers.Ci.nsIDOMMozNetworkStatsManager,
"navigator.mozNetworkStats should be a nsIDOMMozNetworkStatsManager object");
ok(navigator.mozNetworkStats instanceof MozNetworkStatsManager,
"navigator.mozNetworkStats should be a MozNetworkStatsManager object");
test();
});

View File

@ -22,10 +22,8 @@ SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", false]]},
ok(!SpecialPowers.getBoolPref("dom.mozNetworkStats.enabled"),
"Preference 'dom.mozNetworkStats.enabled' is false.");
ok('mozNetworkStats' in navigator, "navigator.mozNetworkStats should exist");
is(navigator.mozNetworkStats, null,
"mozNetworkStats should be null when not enabled.");
ok(!('mozNetworkStats' in navigator),
"navigator.mozNetworkStats should not exist when pref not set");
SimpleTest.finish();
});

View File

@ -27,25 +27,11 @@
ok(!SpecialPowers.hasPermission("networkstats-manage", document),
"Has no permission 'networkstats-manage'.");
ok('mozNetworkStats' in navigator, "navigator.mozNetworkStats should exist");
ok(!('mozNetworkStats' in navigator),
"navigator.mozNetworkStats should not exist when permission not set");
is(navigator.mozNetworkStats, null,
"mozNetworkStats should be null when no permission.");
var error;
try {
navigator.mozNetworkStats.getAvailableNetworks;
ok(false,
"Accessing navigator.mozNetworkStats.getAvailableNetworks should throw!");
} catch (ex) {
error = ex;
}
ok(error,
"Got an exception accessing navigator.mozNetworkStats.getAvailableNetworks");
SimpleTest.finish();
}
SimpleTest.finish();
}
</script>
</pre>
</body>

View File

@ -30,8 +30,8 @@ SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]},
ok('mozNetworkStats' in navigator, "navigator.mozNetworkStats should exist");
ok(navigator.mozNetworkStats instanceof SpecialPowers.Ci.nsIDOMMozNetworkStatsManager,
"navigator.mozNetworkStats should be a nsIDOMMozNetworkStatsManager object");
ok(navigator.mozNetworkStats instanceof MozNetworkStatsManager,
"navigator.mozNetworkStats should be a MozNetworkStatsManager object");
SimpleTest.finish();
});

View File

@ -41,8 +41,8 @@ function filterTimestamp(date) {
}
function getNetworks() {
return [{ id: '0', type: Ci.nsIDOMMozNetworkStatsManager.WIFI },
{ id: '1234', type: Ci.nsIDOMMozNetworkStatsManager.MOBILE }];
return [{ id: '0', type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI },
{ id: '1234', type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE }];
}
function compareNetworks(networkA, networkB) {

View File

@ -158,12 +158,12 @@ add_test(function test_queue() {
};
// Fill networks with fake network interfaces to enable netd async requests.
var network = {id: "1234", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE};
var network = {id: "1234", type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE};
var netId1 = NetworkStatsService.getNetworkId(network.id, network.type);
NetworkStatsService._networks[netId1] = { network: network,
interfaceName: "net1" };
network = {id: "5678", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE};
network = {id: "5678", type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE};
var netId2 = NetworkStatsService.getNetworkId(network.id, network.type);
NetworkStatsService._networks[netId2] = { network: network,
interfaceName: "net2" };

View File

@ -20,7 +20,7 @@ var gData = [
{
perm: ["networkstats-manage"],
obj: "mozNetworkStats",
idl: "nsIDOMMozNetworkStatsManager",
webidl: "MozNetworkStatsManager",
settings: [["dom.mozNetworkStats.enabled", true]],
},
]

View File

@ -14,15 +14,15 @@ dictionary NetworkStatsGetOptions
* Note that, these two options cannot be specified at the same time for now;
* others, an NS_ERROR_NOT_IMPLMENTED exception will be thrown.
*/
DOMString appManifestURL;
DOMString serviceType;
DOMString appManifestURL? = null;
DOMString serviceType = "";
/**
* If it is set as true, only the browsing traffic, which is generated from
* the mozbrowser iframe element within an app, is returned in result.
* If it is set as false or not set, the total traffic, which is generated
* from both the mozapp and mozbrowser iframe elements, is returned.
*/
boolean browsingTrafficOnly;
boolean browsingTrafficOnly = false;
};
dictionary NetworkStatsAlarmOptions

View File

@ -1,14 +1,14 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "nsISupports.idl"
interface nsIDOMDOMRequest;
[scriptable, uuid(72c4e583-389d-4d1b-9424-702feabb6055)]
interface nsIDOMMozNetworkStatsManager : nsISupports
{
[NavigatorProperty="mozNetworkStats",
JSImplementation="@mozilla.org/networkStatsManager;1",
CheckAnyPermissions="networkstats-manage",
Pref="dom.mozNetworkStats.enabled"]
interface MozNetworkStatsManager {
/**
* Constants for known interface types.
*/
@ -21,12 +21,12 @@ interface nsIDOMMozNetworkStatsManager : nsISupports
* If options is provided, per-app or per-system service usage will be
* retrieved; otherwise the target will be overall system usage.
*
* If success, the request result will be an nsIDOMMozNetworkStats object.
* If success, the request result will be an MozNetworkStats object.
*/
nsIDOMDOMRequest getSamples(in nsISupports network,
in jsval start,
in jsval end,
[optional] in jsval options /* NetworkStatsGetOptions */);
DOMRequest getSamples(MozNetworkStatsInterface network,
Date start,
Date end,
optional NetworkStatsGetOptions options);
/**
* Install an alarm on a network. The network must be in the return of
@ -39,9 +39,9 @@ interface nsIDOMMozNetworkStatsManager : nsISupports
*
* If success, the |result| field of the DOMRequest keeps the alarm Id.
*/
nsIDOMDOMRequest addAlarm(in nsISupports network,
in long long threshold,
[optional] in jsval options /* NetworkStatsAlarmOptions */);
DOMRequest addAlarm(MozNetworkStatsInterface network,
long long threshold,
optional NetworkStatsAlarmOptions options);
/**
* Obtain all alarms for those networks returned by getAvailableNetworks().
@ -55,33 +55,33 @@ interface nsIDOMMozNetworkStatsManager : nsISupports
* - threshold
* - data
*/
nsIDOMDOMRequest getAllAlarms([optional] in nsISupports network);
DOMRequest getAllAlarms(optional MozNetworkStatsInterface network);
/**
* Remove all network alarms. If an |alarmId| is provided, then only that
* alarm is removed.
*/
nsIDOMDOMRequest removeAlarms([optional] in long alarmId);
DOMRequest removeAlarms(optional unsigned long alarmId = 0);
/**
* Remove all stats related with the provided network from DB.
*/
nsIDOMDOMRequest clearStats(in nsISupports network);
DOMRequest clearStats(MozNetworkStatsInterface network);
/**
* Remove all stats in the database.
*/
nsIDOMDOMRequest clearAllStats();
DOMRequest clearAllStats();
/**
* Return available networks that used to be saved in the database.
*/
nsIDOMDOMRequest getAvailableNetworks(); // array of MozNetworkStatsInterface.
DOMRequest getAvailableNetworks(); // array of MozNetworkStatsInterface.
/**
* Return available service types that used to be saved in the database.
*/
nsIDOMDOMRequest getAvailableServiceTypes(); // array of string.
DOMRequest getAvailableServiceTypes(); // array of string.
/**
* Minimum time in milliseconds between samples stored in the database.

View File

@ -750,6 +750,7 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
'MozNetworkStatsAlarm.webidl',
'MozNetworkStatsData.webidl',
'MozNetworkStatsInterface.webidl',
'MozNetworkStatsManager.webidl',
'MozSpeakerManager.webidl',
'MozWifiCapabilities.webidl',
'MozWifiManager.webidl',