diff --git a/netwerk/base/public/nsNetUtil.h b/netwerk/base/public/nsNetUtil.h index 3bd42287f472..bc6ec1670e91 100644 --- a/netwerk/base/public/nsNetUtil.h +++ b/netwerk/base/public/nsNetUtil.h @@ -86,6 +86,10 @@ #include +#ifdef MOZ_WIDGET_GONK +#include "nsINetworkManager.h" +#endif + #ifdef MOZILLA_INTERNAL_API #include "nsReadableUtils.h" @@ -2360,4 +2364,28 @@ NS_IsSrcdocChannel(nsIChannel *aChannel) return false; } +// The following members are used for network per-app metering. +const static uint64_t NETWORK_STATS_THRESHOLD = 65536; + +#ifdef MOZ_WIDGET_GONK +inline nsresult +NS_GetActiveNetworkInterface(nsCOMPtr &aNetworkInterface) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsresult rv; + nsCOMPtr networkManager = + do_GetService("@mozilla.org/network/manager;1", &rv); + + if (NS_FAILED(rv) || !networkManager) { + aNetworkInterface = nullptr; + return rv; + } + + networkManager->GetActive(getter_AddRefs(aNetworkInterface)); + + return NS_OK; +} +#endif + #endif // !nsNetUtil_h__ diff --git a/netwerk/protocol/ftp/nsFtpConnectionThread.cpp b/netwerk/protocol/ftp/nsFtpConnectionThread.cpp index 62af781d5b01..61c043c0ef3e 100644 --- a/netwerk/protocol/ftp/nsFtpConnectionThread.cpp +++ b/netwerk/protocol/ftp/nsFtpConnectionThread.cpp @@ -1691,6 +1691,10 @@ nsFtpState::Init(nsFtpChannel *channel) // initialize counter for network metering mCountRecv = 0; +#ifdef MOZ_WIDGET_GONK + NS_GetActiveNetworkInterface(mActiveNetwork); +#endif + mKeepRunning = true; mSuppliedEntityID = channel->EntityID(); @@ -2199,22 +2203,6 @@ nsresult nsFtpState::SaveNetworkStats(bool enforce) { #ifdef MOZ_WIDGET_GONK - MOZ_ASSERT(NS_IsMainThread()); - - // Obtain active network - nsresult rv; - if (!mActiveNetwork) { - nsCOMPtr networkManager = - do_GetService("@mozilla.org/network/manager;1", &rv); - - if (NS_FAILED(rv) || !networkManager) { - mActiveNetwork = nullptr; - return rv; - } - - networkManager->GetActive(getter_AddRefs(mActiveNetwork)); - } - // Obtain app id uint32_t appId; bool isInBrowser; @@ -2237,6 +2225,7 @@ nsFtpState::SaveNetworkStats(bool enforce) return NS_OK; } + nsresult rv; nsCOMPtr networkStatsServiceProxy = do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv); if (NS_FAILED(rv)) { diff --git a/netwerk/protocol/ftp/nsFtpConnectionThread.h b/netwerk/protocol/ftp/nsFtpConnectionThread.h index ccb6a392f78f..2f0a67c7ff0a 100644 --- a/netwerk/protocol/ftp/nsFtpConnectionThread.h +++ b/netwerk/protocol/ftp/nsFtpConnectionThread.h @@ -262,10 +262,6 @@ private: // These members are used for network per-app metering (bug 855948) // Currently, they are only available on gonk. -public: - const static uint64_t NETWORK_STATS_THRESHOLD = 65536; - -private: uint64_t mCountRecv; #ifdef MOZ_WIDGET_GONK nsCOMPtr mActiveNetwork; diff --git a/netwerk/protocol/http/nsHttpTransaction.cpp b/netwerk/protocol/http/nsHttpTransaction.cpp index d58c4f06f147..8c8e979566cb 100644 --- a/netwerk/protocol/http/nsHttpTransaction.cpp +++ b/netwerk/protocol/http/nsHttpTransaction.cpp @@ -230,17 +230,20 @@ nsHttpTransaction::Init(uint32_t caps, mActivityDistributor = nullptr; } - // obtain app info - bool isInBrowser; nsCOMPtr channel = do_QueryInterface(eventsink); if (channel) { + bool isInBrowser; NS_GetAppInfo(channel, &mAppId, &isInBrowser); } - // obtain active connection type +#ifdef MOZ_WIDGET_GONK if (mAppId != NECKO_NO_APP_ID) { - GetActiveNetwork(); + nsCOMPtr activeNetwork; + NS_GetActiveNetworkInterface(activeNetwork); + mActiveNetwork = + new nsMainThreadPtrHolder(activeNetwork); } +#endif // create transport event sink proxy. it coalesces all events if and only // if the activity observer is not active. when the observer is active @@ -707,28 +710,6 @@ nsHttpTransaction::WriteSegments(nsAHttpSegmentWriter *writer, return rv; } -void -nsHttpTransaction::GetActiveNetwork() -{ -#ifdef MOZ_WIDGET_GONK - MOZ_ASSERT(NS_IsMainThread()); - - nsresult rv; - nsCOMPtr networkManager = - do_GetService("@mozilla.org/network/manager;1", &rv); - - if (NS_FAILED(rv) || !networkManager) { - mActiveNetwork = nullptr; - return; - } - - nsCOMPtr activeNetwork; - networkManager->GetActive(getter_AddRefs(activeNetwork)); - mActiveNetwork = - new nsMainThreadPtrHolder(activeNetwork); -#endif -} - //----------------------------------------------------------------------------- // nsHttpTransaction save network statistics event //----------------------------------------------------------------------------- diff --git a/netwerk/protocol/http/nsHttpTransaction.h b/netwerk/protocol/http/nsHttpTransaction.h index 70f13bee41d9..f2cefe82f03f 100644 --- a/netwerk/protocol/http/nsHttpTransaction.h +++ b/netwerk/protocol/http/nsHttpTransaction.h @@ -340,10 +340,6 @@ private: // These members are used for network per-app metering (bug 746073) // Currently, they are only available on gonk. -public: - const static uint64_t NETWORK_STATS_THRESHOLD = 65536; - -private: uint64_t mCountRecv; uint64_t mCountSent; uint32_t mAppId; @@ -351,7 +347,6 @@ private: nsMainThreadPtrHandle mActiveNetwork; #endif nsresult SaveNetworkStats(bool); - void GetActiveNetwork(); void CountRecvBytes(uint64_t recvBytes) { mCountRecv += recvBytes; diff --git a/netwerk/protocol/websocket/WebSocketChannel.cpp b/netwerk/protocol/websocket/WebSocketChannel.cpp index ddad218aa7ec..7d884a63251c 100644 --- a/netwerk/protocol/websocket/WebSocketChannel.cpp +++ b/netwerk/protocol/websocket/WebSocketChannel.cpp @@ -965,8 +965,7 @@ WebSocketChannel::WebSocketChannel() : mConnectionLogService(nullptr), mCountRecv(0), mCountSent(0), - mAppId(0), - mIsInBrowser(false) + mAppId(NECKO_NO_APP_ID) { NS_ABORT_IF_FALSE(NS_IsMainThread(), "not main thread"); @@ -1075,15 +1074,16 @@ WebSocketChannel::BeginOpen() return; } - // obtain app info if (localChannel) { - NS_GetAppInfo(localChannel, &mAppId, &mIsInBrowser); + bool isInBrowser; + NS_GetAppInfo(localChannel, &mAppId, &isInBrowser); } - // obtain active network +#ifdef MOZ_WIDGET_GONK if (mAppId != NECKO_NO_APP_ID) { - GetActiveNetwork(); + NS_GetActiveNetworkInterface(mActiveNetwork); } +#endif rv = localChannel->AsyncOpen(this, mHttpChannel); if (NS_FAILED(rv)) { @@ -3271,28 +3271,6 @@ WebSocketChannel::OnDataAvailable(nsIRequest *aRequest, return NS_OK; } -nsresult -WebSocketChannel::GetActiveNetwork() -{ -#ifdef MOZ_WIDGET_GONK - MOZ_ASSERT(NS_IsMainThread()); - - nsresult result; - nsCOMPtr networkManager = do_GetService("@mozilla.org/network/manager;1", &result); - - if (NS_FAILED(result) || !networkManager) { - mActiveNetwork = nullptr; - return NS_ERROR_UNEXPECTED; - } - - result = networkManager->GetActive(getter_AddRefs(mActiveNetwork)); - - return NS_OK; -#else - return NS_ERROR_NOT_IMPLEMENTED; -#endif -} - nsresult WebSocketChannel::SaveNetworkStats(bool enforce) { diff --git a/netwerk/protocol/websocket/WebSocketChannel.h b/netwerk/protocol/websocket/WebSocketChannel.h index 745a95e479a4..32fcda146da4 100644 --- a/netwerk/protocol/websocket/WebSocketChannel.h +++ b/netwerk/protocol/websocket/WebSocketChannel.h @@ -257,18 +257,12 @@ private: // These members are used for network per-app metering (bug 855949) // Currently, they are only available on gonk. -public: - const static uint64_t NETWORK_STATS_THRESHOLD = 65536; - -private: uint64_t mCountRecv; uint64_t mCountSent; uint32_t mAppId; - bool mIsInBrowser; #ifdef MOZ_WIDGET_GONK nsCOMPtr mActiveNetwork; #endif - nsresult GetActiveNetwork(); nsresult SaveNetworkStats(bool); void CountRecvBytes(uint64_t recvBytes) {