Bug 887699 - Part 3: Fix access of websocket channel and TCPSocket to NetworkStats API. r=jduell, r=gene
authorAlbert Crespell <acperez@tid.es>
Fri, 11 Oct 2013 08:54:34 +0200
changeset 165550 57b03d7055e86388d330d8af204df4d68c886506
parent 165549 ea06175feb4f7069750bdac30bb73a9645098533
child 165551 5c878c48d7320232a7ecbe79354b2a1e8ece5576
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjduell, gene
bugs887699
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 887699 - Part 3: Fix access of websocket channel and TCPSocket to NetworkStats API. r=jduell, r=gene
dom/network/src/TCPSocket.js
netwerk/protocol/websocket/WebSocketChannel.cpp
netwerk/protocol/websocket/WebSocketChannel.h
--- a/dom/network/src/TCPSocket.js
+++ b/dom/network/src/TCPSocket.js
@@ -162,17 +162,17 @@ TCPSocket.prototype = {
   _waitingForStartTLS: false,
   _pendingDataAfterStartTLS: [],
 
 #ifdef MOZ_WIDGET_GONK
   // Network statistics (Gonk-specific feature)
   _txBytes: 0,
   _rxBytes: 0,
   _appId: Ci.nsIScriptSecurityManager.NO_APP_ID,
-  _connectionType: Ci.nsINetworkInterface.NETWORK_TYPE_UNKNOWN,
+  _activeNetwork: null,
 #endif
 
   // Public accessors.
   get readyState() {
     return this._readyState;
   },
   get binaryType() {
     return this._binaryType;
@@ -342,17 +342,17 @@ 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._connectionType, Date.now(),
+    nssProxy.saveAppStats(this._appId, this._activeNetwork, Date.now(),
                           this._rxBytes, this._txBytes);
 
     // Reset the counters once the statistics is saved to NetworkStatsServiceProxy.
     this._txBytes = this._rxBytes = 0;
   },
   // End of helper method for network statistics.
 #endif
 
@@ -525,22 +525,22 @@ TCPSocket.prototype = {
       return that;
     }
 
     let transport = that._transport = this._createTransport(host, port, that._ssl);
     transport.setEventSink(that, Services.tm.currentThread);
     that._initStream(that._binaryType);
 
 #ifdef MOZ_WIDGET_GONK
-    // Set _connectionType, which is only required for network statistics.
+    // Set _activeNetwork, which is only required for network statistics.
     // Note that nsINetworkManager, as well as nsINetworkStatsServiceProxy, is
     // Gonk-specific.
     let networkManager = Cc["@mozilla.org/network/manager;1"].getService(Ci.nsINetworkManager);
-    if (networkManager && networkManager.active) {
-      that._connectionType = networkManager.active.type;
+    if (networkManager) {
+      that._activeNetwork = networkManager.active;
     }
 #endif
 
     return that;
   },
 
   upgradeToSecure: function ts_upgradeToSecure() {
     if (this._readyState !== kOPEN) {
--- a/netwerk/protocol/websocket/WebSocketChannel.cpp
+++ b/netwerk/protocol/websocket/WebSocketChannel.cpp
@@ -48,17 +48,16 @@
 #include "plbase64.h"
 #include "prmem.h"
 #include "prnetdb.h"
 #include "prbit.h"
 #include "zlib.h"
 #include <algorithm>
 
 #ifdef MOZ_WIDGET_GONK
-#include "nsINetworkManager.h"
 #include "nsINetworkStatsServiceProxy.h"
 #endif
 
 // rather than slurp up all of nsIWebSocket.idl, which lives outside necko, just
 // dupe one constant we need from it
 #define CLOSE_GOING_AWAY 1001
 
 extern PRThread *gSocketThread;
@@ -962,17 +961,16 @@ WebSocketChannel::WebSocketChannel() :
   mCompressor(nullptr),
   mDynamicOutputSize(0),
   mDynamicOutput(nullptr),
   mPrivateBrowsing(false),
   mConnectionLogService(nullptr),
   mCountRecv(0),
   mCountSent(0),
   mAppId(0),
-  mConnectionType(NETWORK_NO_TYPE),
   mIsInBrowser(false)
 {
   NS_ABORT_IF_FALSE(NS_IsMainThread(), "not main thread");
 
   LOG(("WebSocketChannel::WebSocketChannel() %p\n", this));
 
   if (!sWebSocketAdmissions)
     sWebSocketAdmissions = new nsWSAdmissionManager();
@@ -1077,19 +1075,19 @@ WebSocketChannel::BeginOpen()
     return;
   }
 
   // obtain app info
   if (localChannel) {
     NS_GetAppInfo(localChannel, &mAppId, &mIsInBrowser);
   }
 
-  // obtain active connection type
+  // obtain active network
   if (mAppId != NECKO_NO_APP_ID) {
-    GetConnectionType(&mConnectionType);
+    GetActiveNetwork();
   }
 
   rv = localChannel->AsyncOpen(this, mHttpChannel);
   if (NS_FAILED(rv)) {
     LOG(("WebSocketChannel::BeginOpen: cannot async open\n"));
     AbortSession(NS_ERROR_CONNECTION_REFUSED);
     return;
   }
@@ -3269,48 +3267,43 @@ WebSocketChannel::OnDataAvailable(nsIReq
 
   LOG(("WebSocketChannel::OnDataAvailable: HTTP data unexpected len>=%u\n",
          aCount));
 
   return NS_OK;
 }
 
 nsresult
-WebSocketChannel::GetConnectionType(int32_t *type)
+WebSocketChannel::GetActiveNetwork()
 {
 #ifdef MOZ_WIDGET_GONK
   MOZ_ASSERT(NS_IsMainThread());
 
   nsresult result;
   nsCOMPtr<nsINetworkManager> networkManager = do_GetService("@mozilla.org/network/manager;1", &result);
 
   if (NS_FAILED(result) || !networkManager) {
-    *type = NETWORK_NO_TYPE;
+    mActiveNetwork = nullptr;
+    return NS_ERROR_UNEXPECTED;
   }
 
-  nsCOMPtr<nsINetworkInterface> networkInterface;
-  result = networkManager->GetActive(getter_AddRefs(networkInterface));
-
-  if (networkInterface) {
-    result = networkInterface->GetType(type);
-  }
+  result = networkManager->GetActive(getter_AddRefs(mActiveNetwork));
 
   return NS_OK;
 #else
   return NS_ERROR_NOT_IMPLEMENTED;
 #endif
 }
 
 nsresult
 WebSocketChannel::SaveNetworkStats(bool enforce)
 {
 #ifdef MOZ_WIDGET_GONK
-  // Check if the connection type and app id are valid.
-  if(mConnectionType == NETWORK_NO_TYPE ||
-     mAppId == NECKO_NO_APP_ID) {
+  // Check if the active network and app id are valid.
+  if(!mActiveNetwork || mAppId == NECKO_NO_APP_ID) {
     return NS_OK;
   }
 
   if (mCountRecv <= 0 && mCountSent <= 0) {
     // There is no traffic, no need to save.
     return NS_OK;
   }
 
@@ -3324,17 +3317,17 @@ WebSocketChannel::SaveNetworkStats(bool 
 
   nsresult rv;
   nsCOMPtr<nsINetworkStatsServiceProxy> mNetworkStatsServiceProxy =
     do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
-  mNetworkStatsServiceProxy->SaveAppStats(mAppId, mConnectionType, PR_Now() / 1000,
+  mNetworkStatsServiceProxy->SaveAppStats(mAppId, mActiveNetwork, PR_Now() / 1000,
                                           mCountRecv, mCountSent, nullptr);
 
   // Reset the counters after saving.
   mCountSent = 0;
   mCountRecv = 0;
 
   return NS_OK;
 #else
--- a/netwerk/protocol/websocket/WebSocketChannel.h
+++ b/netwerk/protocol/websocket/WebSocketChannel.h
@@ -13,16 +13,20 @@
 #include "nsIAsyncInputStream.h"
 #include "nsIAsyncOutputStream.h"
 #include "nsITimer.h"
 #include "nsIDNSListener.h"
 #include "nsIChannelEventSink.h"
 #include "nsIHttpChannelInternal.h"
 #include "BaseWebSocketChannel.h"
 
+#ifdef MOZ_WIDGET_GONK
+#include "nsINetworkManager.h"
+#endif
+
 #include "nsCOMPtr.h"
 #include "nsString.h"
 #include "nsDeque.h"
 
 class nsIAsyncVerifyRedirectCallback;
 class nsIDashboardEventNotifier;
 class nsIEventTarget;
 class nsIHttpChannel;
@@ -249,26 +253,27 @@ private:
 
   nsCOMPtr<nsIDashboardEventNotifier> mConnectionLogService;
   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.
 public:
-  const static int32_t NETWORK_NO_TYPE = -1; // default conntection type
   const static uint64_t NETWORK_STATS_THRESHOLD = 65536;
 
 private:
   uint64_t                        mCountRecv;
   uint64_t                        mCountSent;
   uint32_t                        mAppId;
-  int32_t                         mConnectionType;
   bool                            mIsInBrowser;
-  nsresult                        GetConnectionType(int32_t *);
+#ifdef MOZ_WIDGET_GONK
+  nsCOMPtr<nsINetworkInterface>   mActiveNetwork;
+#endif
+  nsresult                        GetActiveNetwork();
   nsresult                        SaveNetworkStats(bool);
   void                            CountRecvBytes(uint64_t recvBytes)
   {
     mCountRecv += recvBytes;
     SaveNetworkStats(false);
   }
   void                            CountSentBytes(uint64_t sentBytes)
   {