Bug 922924 - Centralize the code for network per-app metering. r=jduell

This commit is contained in:
John Shih 2013-10-30 18:00:17 +08:00
parent 72cc66f841
commit 863ce1fb06
7 changed files with 47 additions and 85 deletions

View File

@ -86,6 +86,11 @@
#include <limits>
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkManager.h"
#include "nsThreadUtils.h" // for NS_IsMainThread
#endif
#ifdef MOZILLA_INTERNAL_API
#include "nsReadableUtils.h"
@ -2360,4 +2365,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<nsINetworkInterface> &aNetworkInterface)
{
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
nsCOMPtr<nsINetworkManager> 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__

View File

@ -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<nsINetworkManager> 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<nsINetworkStatsServiceProxy> networkStatsServiceProxy =
do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv);
if (NS_FAILED(rv)) {

View File

@ -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<nsINetworkInterface> mActiveNetwork;

View File

@ -230,17 +230,20 @@ nsHttpTransaction::Init(uint32_t caps,
mActivityDistributor = nullptr;
}
// obtain app info
bool isInBrowser;
nsCOMPtr<nsIChannel> 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<nsINetworkInterface> activeNetwork;
NS_GetActiveNetworkInterface(activeNetwork);
mActiveNetwork =
new nsMainThreadPtrHolder<nsINetworkInterface>(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<nsINetworkManager> networkManager =
do_GetService("@mozilla.org/network/manager;1", &rv);
if (NS_FAILED(rv) || !networkManager) {
mActiveNetwork = nullptr;
return;
}
nsCOMPtr<nsINetworkInterface> activeNetwork;
networkManager->GetActive(getter_AddRefs(activeNetwork));
mActiveNetwork =
new nsMainThreadPtrHolder<nsINetworkInterface>(activeNetwork);
#endif
}
//-----------------------------------------------------------------------------
// nsHttpTransaction save network statistics event
//-----------------------------------------------------------------------------

View File

@ -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<nsINetworkInterface> mActiveNetwork;
#endif
nsresult SaveNetworkStats(bool);
void GetActiveNetwork();
void CountRecvBytes(uint64_t recvBytes)
{
mCountRecv += recvBytes;

View File

@ -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<nsINetworkManager> 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)
{

View File

@ -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<nsINetworkInterface> mActiveNetwork;
#endif
nsresult GetActiveNetwork();
nsresult SaveNetworkStats(bool);
void CountRecvBytes(uint64_t recvBytes)
{