1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/network/src/NetworkStatsServiceProxy.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,89 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const DEBUG = false; 1.11 +function debug(s) { dump("-*- NetworkStatsServiceProxy: " + s + "\n"); } 1.12 + 1.13 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; 1.14 + 1.15 +this.EXPORTED_SYMBOLS = ["NetworkStatsServiceProxy"]; 1.16 + 1.17 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.18 +Cu.import("resource://gre/modules/NetworkStatsService.jsm"); 1.19 + 1.20 +const NETWORKSTATSSERVICEPROXY_CONTRACTID = "@mozilla.org/networkstatsServiceProxy;1"; 1.21 +const NETWORKSTATSSERVICEPROXY_CID = Components.ID("705c01d6-8574-464c-8ec9-ac1522a45546"); 1.22 +const nsINetworkStatsServiceProxy = Ci.nsINetworkStatsServiceProxy; 1.23 + 1.24 +function NetworkStatsServiceProxy() { 1.25 + if (DEBUG) { 1.26 + debug("Proxy started"); 1.27 + } 1.28 +} 1.29 + 1.30 +NetworkStatsServiceProxy.prototype = { 1.31 + /* 1.32 + * Function called in the protocol layer (HTTP, FTP, WebSocket ...etc) 1.33 + * to pass the per-app stats to NetworkStatsService. 1.34 + */ 1.35 + saveAppStats: function saveAppStats(aAppId, aNetwork, aTimeStamp, 1.36 + aRxBytes, aTxBytes, aIsAccumulative, 1.37 + aCallback) { 1.38 + if (!aNetwork) { 1.39 + if (DEBUG) { 1.40 + debug("|aNetwork| is not specified. Failed to save stats. Returning."); 1.41 + } 1.42 + return; 1.43 + } 1.44 + 1.45 + if (DEBUG) { 1.46 + debug("saveAppStats: " + aAppId + " " + aNetwork.type + " " + aTimeStamp + 1.47 + " " + aRxBytes + " " + aTxBytes + " " + aIsAccumulative); 1.48 + } 1.49 + 1.50 + if (aCallback) { 1.51 + aCallback = aCallback.notify; 1.52 + } 1.53 + 1.54 + NetworkStatsService.saveStats(aAppId, "", aNetwork, aTimeStamp, 1.55 + aRxBytes, aTxBytes, aIsAccumulative, 1.56 + aCallback); 1.57 + }, 1.58 + 1.59 + /* 1.60 + * Function called in the points of different system services 1.61 + * to pass the per-serive stats to NetworkStatsService. 1.62 + */ 1.63 + saveServiceStats: function saveServiceStats(aServiceType, aNetwork, 1.64 + aTimeStamp, aRxBytes, aTxBytes, 1.65 + aIsAccumulative, aCallback) { 1.66 + if (!aNetwork) { 1.67 + if (DEBUG) { 1.68 + debug("|aNetwork| is not specified. Failed to save stats. Returning."); 1.69 + } 1.70 + return; 1.71 + } 1.72 + 1.73 + if (DEBUG) { 1.74 + debug("saveServiceStats: " + aServiceType + " " + aNetwork.type + " " + 1.75 + aTimeStamp + " " + aRxBytes + " " + aTxBytes + " " + 1.76 + aIsAccumulative); 1.77 + } 1.78 + 1.79 + if (aCallback) { 1.80 + aCallback = aCallback.notify; 1.81 + } 1.82 + 1.83 + NetworkStatsService.saveStats(0, aServiceType ,aNetwork, aTimeStamp, 1.84 + aRxBytes, aTxBytes, aIsAccumulative, 1.85 + aCallback); 1.86 + }, 1.87 + 1.88 + classID : NETWORKSTATSSERVICEPROXY_CID, 1.89 + QueryInterface : XPCOMUtils.generateQI([nsINetworkStatsServiceProxy]), 1.90 +} 1.91 + 1.92 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NetworkStatsServiceProxy]);