Bug 1070944 - Part 2: Implementation of saving statistics. r=albert
authorEthan Tseng <ettseng@mozilla.com>
Tue, 27 Jan 2015 17:15:44 +0800
changeset 228243 07f55ff86e156526fff1910acd4326d3046c169a
parent 228242 901aaee07b807b8aa210b0dc69b0bb1f44344136
child 228244 a67a9f37cb211c69426032b3032918943bc50836
push id13814
push usercbook@mozilla.com
push dateTue, 10 Feb 2015 11:49:28 +0000
treeherderb2g-inbound@2ca4450d6a33 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersalbert
bugs1070944
milestone38.0a1
Bug 1070944 - Part 2: Implementation of saving statistics. r=albert
dom/network/NetworkStatsService.jsm
dom/network/NetworkStatsServiceProxy.js
dom/network/NetworkStatsServiceProxy.manifest
dom/network/TCPSocket.js
dom/network/interfaces/nsINetworkStatsServiceProxy.idl
netwerk/base/NetStatistics.h
netwerk/protocol/ftp/nsFtpConnectionThread.cpp
netwerk/protocol/http/nsHttpTransaction.cpp
netwerk/protocol/http/nsHttpTransaction.h
netwerk/protocol/websocket/WebSocketChannel.cpp
netwerk/protocol/websocket/WebSocketChannel.h
--- a/dom/network/NetworkStatsService.jsm
+++ b/dom/network/NetworkStatsService.jsm
@@ -700,16 +700,17 @@ this.NetworkStatsService = {
     if (!aResult) {
       if (aCallback) {
         aCallback(false, "Netd IPC error");
       }
       return;
     }
 
     let stats = { appId:          0,
+                  isInBrowser:    false,
                   serviceType:    "",
                   networkId:      this._networks[aNetId].network.id,
                   networkType:    this._networks[aNetId].network.type,
                   date:           new Date(aTimestamp),
                   rxBytes:        aTxBytes,
                   txBytes:        aRxBytes,
                   isAccumulative: true };
 
@@ -725,18 +726,18 @@ this.NetworkStatsService = {
         aCallback(true, "OK");
       }
     });
   },
 
   /*
    * 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) {
       if (aCallback) {
         aCallback(false, "Invalid network type");
       }
       return;
     }
@@ -748,16 +749,17 @@ this.NetworkStatsService = {
     // b. Both |aAppId| is zero and |aServiceType| is empty.
     if (!this._networks[netId] || (aAppId && aServiceType) ||
         (!aAppId && !aServiceType)) {
       debug("Invalid network interface, appId or serviceType");
       return;
     }
 
     let stats = { appId:          aAppId,
+                  isInBrowser:    aIsInBrowser,
                   serviceType:    aServiceType,
                   networkId:      this._networks[netId].network.id,
                   networkType:    this._networks[netId].network.type,
                   date:           new Date(aTimeStamp),
                   rxBytes:        aRxBytes,
                   txBytes:        aTxBytes,
                   isAccumulative: aIsAccumulative };
 
@@ -767,19 +769,20 @@ this.NetworkStatsService = {
 
     this.processQueue();
   },
 
   /*
    *
    */
   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|.
     let netId = this.getNetworkId(aStats.networkId, aStats.networkType);
     let key = aStats.appId + "" + aStats.serviceType + "" + netId;
 
     // |cachedStats| only keeps the data with the same date.
     // If the incoming date is different from |cachedStatsDate|,
--- a/dom/network/NetworkStatsServiceProxy.js
+++ b/dom/network/NetworkStatsServiceProxy.js
@@ -10,57 +10,58 @@ function debug(s) { dump("-*- NetworkSta
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 this.EXPORTED_SYMBOLS = ["NetworkStatsServiceProxy"];
 
 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() {
   if (DEBUG) {
     debug("Proxy started");
   }
 }
 
 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) {
       if (DEBUG) {
         debug("|aNetwork| is not specified. Failed to save stats. Returning.");
       }
       return;
     }
 
     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,
                                               aIsAccumulative, aCallback) {
     if (!aNetwork) {
       if (DEBUG) {
         debug("|aNetwork| is not specified. Failed to save stats. Returning.");
       }
@@ -72,17 +73,17 @@ NetworkStatsServiceProxy.prototype = {
             aTimeStamp + " " + aRxBytes + " " + aTxBytes + " " +
             aIsAccumulative);
     }
 
     if (aCallback) {
       aCallback = aCallback.notify;
     }
 
-    NetworkStatsService.saveStats(0, aServiceType ,aNetwork, aTimeStamp,
+    NetworkStatsService.saveStats(0, false, aServiceType ,aNetwork, aTimeStamp,
                                   aRxBytes, aTxBytes, aIsAccumulative,
                                   aCallback);
   },
 
   classID : NETWORKSTATSSERVICEPROXY_CID,
   QueryInterface : XPCOMUtils.generateQI([nsINetworkStatsServiceProxy]),
 }
 
--- a/dom/network/NetworkStatsServiceProxy.manifest
+++ b/dom/network/NetworkStatsServiceProxy.manifest
@@ -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}
--- a/dom/network/TCPSocket.js
+++ b/dom/network/TCPSocket.js
@@ -369,18 +369,18 @@ TCPSocket.prototype = {
     }
 
     let nssProxy = Cc["@mozilla.org/networkstatsServiceProxy;1"]
                      .getService(Ci.nsINetworkStatsServiceProxy);
     if (!nssProxy) {
       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;
   },
   // End of helper method for network statistics.
 #endif
 
   callListener: function ts_callListener(type, data) {
--- a/dom/network/interfaces/nsINetworkStatsServiceProxy.idl
+++ b/dom/network/interfaces/nsINetworkStatsServiceProxy.idl
@@ -11,30 +11,32 @@ interface nsINetworkStatsServiceProxyCal
 {
   /*
    * @param aResult callback result with boolean value
    * @param aMessage message
    */
   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
    * @param aTxBytes transmitted data amount
    * @param aIsAccumulative is stats accumulative
    * @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,
                     in unsigned long long aTxBytes,
                     in boolean aIsAccumulative,
          [optional] in nsINetworkStatsServiceProxyCallback aCallback);
 
   /*
--- a/netwerk/base/NetStatistics.h
+++ b/netwerk/base/NetStatistics.h
@@ -40,21 +40,23 @@ GetActiveNetworkInterface(nsCOMPtr<nsINe
   networkManager->GetActive(getter_AddRefs(aNetworkInterface));
 
   return NS_OK;
 }
 
 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),
       mIsAccumulative(aIsAccumulative)
   {
     MOZ_ASSERT(mAppId != NECKO_NO_APP_ID);
     MOZ_ASSERT(mActiveNetwork);
   }
@@ -67,27 +69,29 @@ public:
     nsCOMPtr<nsINetworkStatsServiceProxy> mNetworkStatsServiceProxy =
       do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv);
     if (NS_FAILED(rv)) {
       return rv;
     }
 
     // save the network stats through NetworkStatsServiceProxy
     mNetworkStatsServiceProxy->SaveAppStats(mAppId,
+                                            mIsInBrowser,
                                             mActiveNetwork,
                                             PR_Now() / 1000,
                                             mCountRecv,
                                             mCountSent,
                                             mIsAccumulative,
                                             nullptr);
 
     return NS_OK;
   }
 private:
   uint32_t mAppId;
+  bool     mIsInBrowser;
   nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
   uint64_t mCountRecv;
   uint64_t mCountSent;
   bool mIsAccumulative;
 };
 
 } // namespace mozilla:net
 } // namespace mozilla
--- a/netwerk/protocol/ftp/nsFtpConnectionThread.cpp
+++ b/netwerk/protocol/ftp/nsFtpConnectionThread.cpp
@@ -2308,17 +2308,18 @@ nsFtpState::SaveNetworkStats(bool enforc
     // threshold.
     if (!enforce && mCountRecv < NETWORK_STATS_THRESHOLD) {
         return NS_OK;
     }
 
     // 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.
     mCountRecv = 0;
 
     return NS_OK;
 #else
     return NS_ERROR_NOT_IMPLEMENTED;
--- a/netwerk/protocol/http/nsHttpTransaction.cpp
+++ b/netwerk/protocol/http/nsHttpTransaction.cpp
@@ -131,16 +131,17 @@ nsHttpTransaction::nsHttpTransaction()
     , mForTakeResponseHead(nullptr)
     , mResponseHeadTaken(false)
     , mSubmittedRatePacing(false)
     , mPassedRatePacing(false)
     , mSynchronousRatePaceRequest(false)
     , mCountRecv(0)
     , mCountSent(0)
     , mAppId(NECKO_NO_APP_ID)
+    , mIsInBrowser(false)
     , mClassOfService(0)
 {
     LOG(("Creating nsHttpTransaction @%p\n", this));
     gHttpHandler->GetMaxPipelineObjectSize(&mMaxPipelineObjectSize);
 }
 
 nsHttpTransaction::~nsHttpTransaction()
 {
@@ -239,18 +240,17 @@ nsHttpTransaction::Init(uint32_t caps,
     } else {
         // there is no observer, so don't use it
         activityDistributorActive = false;
         mActivityDistributor = nullptr;
     }
     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
     if (mAppId != NECKO_NO_APP_ID) {
         nsCOMPtr<nsINetworkInterface> activeNetwork;
         GetActiveNetworkInterface(activeNetwork);
         mActiveNetwork =
             new nsMainThreadPtrHolder<nsINetworkInterface>(activeNetwork);
@@ -787,17 +787,17 @@ nsHttpTransaction::SaveNetworkStats(bool
     uint64_t totalBytes = mCountRecv + mCountSent;
     if (!enforce && totalBytes < NETWORK_STATS_THRESHOLD) {
         return NS_OK;
     }
 
     // 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);
 
     // Reset the counters after saving.
     mCountSent = 0;
     mCountRecv = 0;
 
     return NS_OK;
--- a/netwerk/protocol/http/nsHttpTransaction.h
+++ b/netwerk/protocol/http/nsHttpTransaction.h
@@ -386,16 +386,17 @@ private:
     bool mSynchronousRatePaceRequest;
     nsCOMPtr<nsICancelable> mTokenBucketCancel;
 
 // These members are used for network per-app metering (bug 746073)
 // Currently, they are only available on gonk.
     uint64_t                           mCountRecv;
     uint64_t                           mCountSent;
     uint32_t                           mAppId;
+    bool                               mIsInBrowser;
 #ifdef MOZ_WIDGET_GONK
     nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
 #endif
     nsresult                           SaveNetworkStats(bool);
     void                               CountRecvBytes(uint64_t recvBytes)
     {
         mCountRecv += recvBytes;
         SaveNetworkStats(false);
--- a/netwerk/protocol/websocket/WebSocketChannel.cpp
+++ b/netwerk/protocol/websocket/WebSocketChannel.cpp
@@ -1135,17 +1135,18 @@ WebSocketChannel::WebSocketChannel() :
   mCurrentOut(nullptr),
   mCurrentOutSent(0),
   mDynamicOutputSize(0),
   mDynamicOutput(nullptr),
   mPrivateBrowsing(false),
   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");
 
   LOG(("WebSocketChannel::WebSocketChannel() %p\n", this));
 
   nsWSAdmissionManager::Init();
 
   mFramePtr = mBuffer = static_cast<uint8_t *>(moz_xmalloc(mBufferSize));
@@ -1315,18 +1316,17 @@ WebSocketChannel::BeginOpen()
   nsCOMPtr<nsIChannel> localChannel = do_QueryInterface(mChannel, &rv);
   if (NS_FAILED(rv)) {
     LOG(("WebSocketChannel::BeginOpen: cannot async open\n"));
     AbortSession(NS_ERROR_UNEXPECTED);
     return;
   }
 
   if (localChannel) {
-    bool isInBrowser;
-    NS_GetAppInfo(localChannel, &mAppId, &isInBrowser);
+    NS_GetAppInfo(localChannel, &mAppId, &mIsInBrowser);
   }
 
 #ifdef MOZ_WIDGET_GONK
   if (mAppId != NECKO_NO_APP_ID) {
     nsCOMPtr<nsINetworkInterface> activeNetwork;
     GetActiveNetworkInterface(activeNetwork);
     mActiveNetwork =
       new nsMainThreadPtrHolder<nsINetworkInterface>(activeNetwork);
@@ -3627,17 +3627,17 @@ WebSocketChannel::SaveNetworkStats(bool 
   uint64_t totalBytes = mCountRecv + mCountSent;
   if (!enforce && totalBytes < NETWORK_STATS_THRESHOLD) {
     return NS_OK;
   }
 
   // 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);
 
   // Reset the counters after saving.
   mCountSent = 0;
   mCountRecv = 0;
 
   return NS_OK;
--- a/netwerk/protocol/websocket/WebSocketChannel.h
+++ b/netwerk/protocol/websocket/WebSocketChannel.h
@@ -273,16 +273,17 @@ private:
   uint32_t mSerial;
   static uint32_t sSerialSeed;
 
 // These members are used for network per-app metering (bug 855949)
 // Currently, they are only available on gonk.
   uint64_t                        mCountRecv;
   uint64_t                        mCountSent;
   uint32_t                        mAppId;
+  bool                            mIsInBrowser;
 #ifdef MOZ_WIDGET_GONK
   nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
 #endif
   nsresult                        SaveNetworkStats(bool);
   void                            CountRecvBytes(uint64_t recvBytes)
   {
     mCountRecv += recvBytes;
     SaveNetworkStats(false);