Bug 1070944 - Part 2: Implementation of saving statistics. r=albert

This commit is contained in:
Ethan Tseng 2015-01-27 17:15:44 +08:00
Родитель 0f9ee2a3cf
Коммит dc7d20d249
11 изменённых файлов: 40 добавлений и 27 удалений

Просмотреть файл

@ -705,6 +705,7 @@ this.NetworkStatsService = {
}
let stats = { appId: 0,
isInBrowser: false,
serviceType: "",
networkId: this._networks[aNetId].network.id,
networkType: this._networks[aNetId].network.type,
@ -730,8 +731,8 @@ this.NetworkStatsService = {
/*
* Function responsible for receiving stats which are not from netd.
*/
saveStats: function saveStats(aAppId, aServiceType, aNetwork, aTimeStamp,
aRxBytes, aTxBytes, aIsAccumulative,
saveStats: function saveStats(aAppId, aIsInBrowser, aServiceType, aNetwork,
aTimeStamp, aRxBytes, aTxBytes, aIsAccumulative,
aCallback) {
let netId = this.convertNetworkInterface(aNetwork);
if (!netId) {
@ -753,6 +754,7 @@ this.NetworkStatsService = {
}
let stats = { appId: aAppId,
isInBrowser: aIsInBrowser,
serviceType: aServiceType,
networkId: this._networks[netId].network.id,
networkType: this._networks[netId].network.type,
@ -772,9 +774,10 @@ this.NetworkStatsService = {
*
*/
writeCache: function writeCache(aStats, aCallback) {
debug("saveStats: " + aStats.appId + " " + aStats.serviceType + " " +
aStats.networkId + " " + aStats.networkType + " " + aStats.date + " "
+ aStats.date + " " + aStats.rxBytes + " " + aStats.txBytes);
debug("saveStats: " + aStats.appId + " " + aStats.isInBrowser + " " +
aStats.serviceType + " " + aStats.networkId + " " +
aStats.networkType + " " + aStats.date + " " +
aStats.rxBytes + " " + aStats.txBytes);
// Generate an unique key from |appId|, |serviceType| and |netId|,
// which is used to retrieve data in |cachedStats|.

Просмотреть файл

@ -15,7 +15,7 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/NetworkStatsService.jsm");
const NETWORKSTATSSERVICEPROXY_CONTRACTID = "@mozilla.org/networkstatsServiceProxy;1";
const NETWORKSTATSSERVICEPROXY_CID = Components.ID("705c01d6-8574-464c-8ec9-ac1522a45546");
const NETWORKSTATSSERVICEPROXY_CID = Components.ID("98fd8f69-784e-4626-aa59-56d6436a3c24");
const nsINetworkStatsServiceProxy = Ci.nsINetworkStatsServiceProxy;
function NetworkStatsServiceProxy() {
@ -29,7 +29,7 @@ NetworkStatsServiceProxy.prototype = {
* Function called in the protocol layer (HTTP, FTP, WebSocket ...etc)
* to pass the per-app stats to NetworkStatsService.
*/
saveAppStats: function saveAppStats(aAppId, aNetwork, aTimeStamp,
saveAppStats: function saveAppStats(aAppId, aIsInBrowser, aNetwork, aTimeStamp,
aRxBytes, aTxBytes, aIsAccumulative,
aCallback) {
if (!aNetwork) {
@ -40,22 +40,23 @@ NetworkStatsServiceProxy.prototype = {
}
if (DEBUG) {
debug("saveAppStats: " + aAppId + " " + aNetwork.type + " " + aTimeStamp +
" " + aRxBytes + " " + aTxBytes + " " + aIsAccumulative);
debug("saveAppStats: " + aAppId + " " + aIsInBrowser + " " +
aNetwork.type + " " + aTimeStamp + " " +
aRxBytes + " " + aTxBytes + " " + aIsAccumulative);
}
if (aCallback) {
aCallback = aCallback.notify;
}
NetworkStatsService.saveStats(aAppId, "", aNetwork, aTimeStamp,
aRxBytes, aTxBytes, aIsAccumulative,
aCallback);
NetworkStatsService.saveStats(aAppId, aIsInBrowser, "", aNetwork,
aTimeStamp, aRxBytes, aTxBytes,
aIsAccumulative, aCallback);
},
/*
* Function called in the points of different system services
* to pass the per-serive stats to NetworkStatsService.
* to pass the per-service stats to NetworkStatsService.
*/
saveServiceStats: function saveServiceStats(aServiceType, aNetwork,
aTimeStamp, aRxBytes, aTxBytes,
@ -77,7 +78,7 @@ NetworkStatsServiceProxy.prototype = {
aCallback = aCallback.notify;
}
NetworkStatsService.saveStats(0, aServiceType ,aNetwork, aTimeStamp,
NetworkStatsService.saveStats(0, false, aServiceType ,aNetwork, aTimeStamp,
aRxBytes, aTxBytes, aIsAccumulative,
aCallback);
},

Просмотреть файл

@ -1,2 +1,2 @@
component {705c01d6-8574-464c-8ec9-ac1522a45546} NetworkStatsServiceProxy.js
contract @mozilla.org/networkstatsServiceProxy;1 {705c01d6-8574-464c-8ec9-ac1522a45546}
component {98fd8f69-784e-4626-aa59-56d6436a3c24} NetworkStatsServiceProxy.js
contract @mozilla.org/networkstatsServiceProxy;1 {98fd8f69-784e-4626-aa59-56d6436a3c24}

Просмотреть файл

@ -374,8 +374,8 @@ TCPSocket.prototype = {
LOG("Error: Ci.nsINetworkStatsServiceProxy service is not available.");
return;
}
nssProxy.saveAppStats(this._appId, this._activeNetwork, Date.now(),
this._rxBytes, this._txBytes, false);
nssProxy.saveAppStats(this._appId, this._inBrowser, this._activeNetwork,
Date.now(), this._rxBytes, this._txBytes, false);
// Reset the counters once the statistics is saved to NetworkStatsServiceProxy.
this._txBytes = this._rxBytes = 0;

Просмотреть файл

@ -16,12 +16,13 @@ interface nsINetworkStatsServiceProxyCallback : nsISupports
void notify(in boolean aResult, in jsval aMessage);
};
[scriptable, uuid(705c01d6-8574-464c-8ec9-ac1522a45546)]
[scriptable, uuid(98fd8f69-784e-4626-aa59-56d6436a3c24)]
interface nsINetworkStatsServiceProxy : nsISupports
{
/*
* An interface used to record per-app traffic data.
* @param aAppId app id
* @param aIsInBrowser true if the iframe element is mozbrowser
* @param aNetworkInterface network
* @param aTimeStamp time stamp
* @param aRxBytes received data amount
@ -30,6 +31,7 @@ interface nsINetworkStatsServiceProxy : nsISupports
* @param aCallback an optional callback
*/
void saveAppStats(in unsigned long aAppId,
in boolean aIsInBrowser,
in nsINetworkInterface aNetwork,
in unsigned long long aTimeStamp,
in unsigned long long aRxBytes,

Просмотреть файл

@ -45,11 +45,13 @@ GetActiveNetworkInterface(nsCOMPtr<nsINetworkInterface> &aNetworkInterface)
class SaveNetworkStatsEvent : public nsRunnable {
public:
SaveNetworkStatsEvent(uint32_t aAppId,
bool aIsInBrowser,
nsMainThreadPtrHandle<nsINetworkInterface> &aActiveNetwork,
uint64_t aCountRecv,
uint64_t aCountSent,
bool aIsAccumulative)
: mAppId(aAppId),
mIsInBrowser(aIsInBrowser),
mActiveNetwork(aActiveNetwork),
mCountRecv(aCountRecv),
mCountSent(aCountSent),
@ -72,6 +74,7 @@ public:
// save the network stats through NetworkStatsServiceProxy
mNetworkStatsServiceProxy->SaveAppStats(mAppId,
mIsInBrowser,
mActiveNetwork,
PR_Now() / 1000,
mCountRecv,
@ -83,6 +86,7 @@ public:
}
private:
uint32_t mAppId;
bool mIsInBrowser;
nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
uint64_t mCountRecv;
uint64_t mCountSent;

Просмотреть файл

@ -2313,7 +2313,8 @@ nsFtpState::SaveNetworkStats(bool enforce)
// Create the event to save the network statistics.
// the event is then dispathed to the main thread.
nsRefPtr<nsRunnable> event =
new SaveNetworkStatsEvent(appId, mActiveNetwork, mCountRecv, 0, false);
new SaveNetworkStatsEvent(appId, isInBrowser, mActiveNetwork,
mCountRecv, 0, false);
NS_DispatchToMainThread(event);
// Reset the counters after saving.

Просмотреть файл

@ -136,6 +136,7 @@ nsHttpTransaction::nsHttpTransaction()
, mCountRecv(0)
, mCountSent(0)
, mAppId(NECKO_NO_APP_ID)
, mIsInBrowser(false)
, mClassOfService(0)
{
LOG(("Creating nsHttpTransaction @%p\n", this));
@ -244,8 +245,7 @@ nsHttpTransaction::Init(uint32_t caps,
mChannel = do_QueryInterface(eventsink);
nsCOMPtr<nsIChannel> channel = do_QueryInterface(eventsink);
if (channel) {
bool isInBrowser;
NS_GetAppInfo(channel, &mAppId, &isInBrowser);
NS_GetAppInfo(channel, &mAppId, &mIsInBrowser);
}
#ifdef MOZ_WIDGET_GONK
@ -792,7 +792,7 @@ nsHttpTransaction::SaveNetworkStats(bool enforce)
// Create the event to save the network statistics.
// the event is then dispathed to the main thread.
nsRefPtr<nsRunnable> event =
new SaveNetworkStatsEvent(mAppId, mActiveNetwork,
new SaveNetworkStatsEvent(mAppId, mIsInBrowser, mActiveNetwork,
mCountRecv, mCountSent, false);
NS_DispatchToMainThread(event);

Просмотреть файл

@ -391,6 +391,7 @@ private:
uint64_t mCountRecv;
uint64_t mCountSent;
uint32_t mAppId;
bool mIsInBrowser;
#ifdef MOZ_WIDGET_GONK
nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
#endif

Просмотреть файл

@ -1140,7 +1140,8 @@ WebSocketChannel::WebSocketChannel() :
mConnectionLogService(nullptr),
mCountRecv(0),
mCountSent(0),
mAppId(NECKO_NO_APP_ID)
mAppId(NECKO_NO_APP_ID),
mIsInBrowser(false)
{
NS_ABORT_IF_FALSE(NS_IsMainThread(), "not main thread");
@ -1320,8 +1321,7 @@ WebSocketChannel::BeginOpen()
}
if (localChannel) {
bool isInBrowser;
NS_GetAppInfo(localChannel, &mAppId, &isInBrowser);
NS_GetAppInfo(localChannel, &mAppId, &mIsInBrowser);
}
#ifdef MOZ_WIDGET_GONK
@ -3632,7 +3632,7 @@ WebSocketChannel::SaveNetworkStats(bool enforce)
// Create the event to save the network statistics.
// the event is then dispathed to the main thread.
nsRefPtr<nsRunnable> event =
new SaveNetworkStatsEvent(mAppId, mActiveNetwork,
new SaveNetworkStatsEvent(mAppId, mIsInBrowser, mActiveNetwork,
mCountRecv, mCountSent, false);
NS_DispatchToMainThread(event);

Просмотреть файл

@ -278,6 +278,7 @@ private:
uint64_t mCountRecv;
uint64_t mCountSent;
uint32_t mAppId;
bool mIsInBrowser;
#ifdef MOZ_WIDGET_GONK
nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
#endif