|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 const DEBUG = false; |
|
8 function debug(s) { dump("-*- NetworkStatsServiceProxy: " + s + "\n"); } |
|
9 |
|
10 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
|
11 |
|
12 this.EXPORTED_SYMBOLS = ["NetworkStatsServiceProxy"]; |
|
13 |
|
14 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
15 Cu.import("resource://gre/modules/NetworkStatsService.jsm"); |
|
16 |
|
17 const NETWORKSTATSSERVICEPROXY_CONTRACTID = "@mozilla.org/networkstatsServiceProxy;1"; |
|
18 const NETWORKSTATSSERVICEPROXY_CID = Components.ID("705c01d6-8574-464c-8ec9-ac1522a45546"); |
|
19 const nsINetworkStatsServiceProxy = Ci.nsINetworkStatsServiceProxy; |
|
20 |
|
21 function NetworkStatsServiceProxy() { |
|
22 if (DEBUG) { |
|
23 debug("Proxy started"); |
|
24 } |
|
25 } |
|
26 |
|
27 NetworkStatsServiceProxy.prototype = { |
|
28 /* |
|
29 * Function called in the protocol layer (HTTP, FTP, WebSocket ...etc) |
|
30 * to pass the per-app stats to NetworkStatsService. |
|
31 */ |
|
32 saveAppStats: function saveAppStats(aAppId, aNetwork, aTimeStamp, |
|
33 aRxBytes, aTxBytes, aIsAccumulative, |
|
34 aCallback) { |
|
35 if (!aNetwork) { |
|
36 if (DEBUG) { |
|
37 debug("|aNetwork| is not specified. Failed to save stats. Returning."); |
|
38 } |
|
39 return; |
|
40 } |
|
41 |
|
42 if (DEBUG) { |
|
43 debug("saveAppStats: " + aAppId + " " + aNetwork.type + " " + aTimeStamp + |
|
44 " " + aRxBytes + " " + aTxBytes + " " + aIsAccumulative); |
|
45 } |
|
46 |
|
47 if (aCallback) { |
|
48 aCallback = aCallback.notify; |
|
49 } |
|
50 |
|
51 NetworkStatsService.saveStats(aAppId, "", aNetwork, aTimeStamp, |
|
52 aRxBytes, aTxBytes, aIsAccumulative, |
|
53 aCallback); |
|
54 }, |
|
55 |
|
56 /* |
|
57 * Function called in the points of different system services |
|
58 * to pass the per-serive stats to NetworkStatsService. |
|
59 */ |
|
60 saveServiceStats: function saveServiceStats(aServiceType, aNetwork, |
|
61 aTimeStamp, aRxBytes, aTxBytes, |
|
62 aIsAccumulative, aCallback) { |
|
63 if (!aNetwork) { |
|
64 if (DEBUG) { |
|
65 debug("|aNetwork| is not specified. Failed to save stats. Returning."); |
|
66 } |
|
67 return; |
|
68 } |
|
69 |
|
70 if (DEBUG) { |
|
71 debug("saveServiceStats: " + aServiceType + " " + aNetwork.type + " " + |
|
72 aTimeStamp + " " + aRxBytes + " " + aTxBytes + " " + |
|
73 aIsAccumulative); |
|
74 } |
|
75 |
|
76 if (aCallback) { |
|
77 aCallback = aCallback.notify; |
|
78 } |
|
79 |
|
80 NetworkStatsService.saveStats(0, aServiceType ,aNetwork, aTimeStamp, |
|
81 aRxBytes, aTxBytes, aIsAccumulative, |
|
82 aCallback); |
|
83 }, |
|
84 |
|
85 classID : NETWORKSTATSSERVICEPROXY_CID, |
|
86 QueryInterface : XPCOMUtils.generateQI([nsINetworkStatsServiceProxy]), |
|
87 } |
|
88 |
|
89 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NetworkStatsServiceProxy]); |