Bug 949956 - Move duplicated code to NetStatistics.h. r=mcmanus

This commit is contained in:
John Shih 2014-01-14 17:55:29 +08:00
parent a4a7e10084
commit 80ae34ca1f
9 changed files with 123 additions and 160 deletions

View File

@ -0,0 +1,95 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:set ts=4 sw=4 sts=4 et cin: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef NetStatistics_h__
#define NetStatistics_h__
#include "mozilla/Assertions.h"
#include "nsCOMPtr.h"
#include "nsError.h"
#include "nsINetworkManager.h"
#include "nsINetworkStatsServiceProxy.h"
#include "nsThreadUtils.h"
#include "nsProxyRelease.h"
namespace mozilla {
namespace net {
// The following members are used for network per-app metering.
const static uint64_t NETWORK_STATS_THRESHOLD = 65536;
const static char NETWORK_STATS_NO_SERVICE_TYPE[] = "";
inline nsresult
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;
}
class SaveNetworkStatsEvent : public nsRunnable {
public:
SaveNetworkStatsEvent(uint32_t aAppId,
nsMainThreadPtrHandle<nsINetworkInterface> &aActiveNetwork,
uint64_t aCountRecv,
uint64_t aCountSent,
bool aIsAccumulative)
: mAppId(aAppId),
mActiveNetwork(aActiveNetwork),
mCountRecv(aCountRecv),
mCountSent(aCountSent),
mIsAccumulative(aIsAccumulative)
{
MOZ_ASSERT(mAppId != NECKO_NO_APP_ID);
MOZ_ASSERT(mActiveNetwork);
}
NS_IMETHOD Run()
{
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
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,
mActiveNetwork,
PR_Now() / 1000,
mCountRecv,
mCountSent,
mIsAccumulative,
nullptr);
return NS_OK;
}
private:
uint32_t mAppId;
nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
uint64_t mCountRecv;
uint64_t mCountSent;
bool mIsAccumulative;
};
} // namespace mozilla:net
} // namespace mozilla
#endif // !NetStatistics_h__

View File

@ -141,5 +141,10 @@ EXPORTS += [
'nsURIHashKey.h',
]
if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
EXPORTS += [
'NetStatistics.h',
]
FAIL_ON_WARNINGS = True

View File

@ -86,11 +86,6 @@
#include <limits>
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkManager.h"
#include "nsThreadUtils.h" // for NS_IsMainThread
#endif
#ifdef MOZILLA_INTERNAL_API
#include "nsReadableUtils.h"
@ -2404,29 +2399,4 @@ NS_IsSrcdocChannel(nsIChannel *aChannel)
return false;
}
// The following members are used for network per-app metering.
const static uint64_t NETWORK_STATS_THRESHOLD = 65536;
const static char NETWORK_STATS_NO_SERVICE_TYPE[] = "";
#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

@ -43,7 +43,7 @@
#include "nsICacheSession.h"
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkStatsServiceProxy.h"
#include "NetStatistics.h"
#endif
#if defined(PR_LOGGING)
@ -1691,7 +1691,10 @@ nsFtpState::Init(nsFtpChannel *channel)
mCountRecv = 0;
#ifdef MOZ_WIDGET_GONK
NS_GetActiveNetworkInterface(mActiveNetwork);
nsCOMPtr<nsINetworkInterface> activeNetwork;
GetActiveNetworkInterface(activeNetwork);
mActiveNetwork =
new nsMainThreadPtrHolder<nsINetworkInterface>(activeNetwork);
#endif
mKeepRunning = true;
@ -2218,20 +2221,11 @@ nsFtpState::SaveNetworkStats(bool enforce)
return NS_OK;
}
nsresult rv;
nsCOMPtr<nsINetworkStatsServiceProxy> networkStatsServiceProxy =
do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv);
if (NS_FAILED(rv)) {
return rv;
}
networkStatsServiceProxy->SaveAppStats(appId,
mActiveNetwork,
PR_Now() / 1000,
mCountRecv,
0,
false,
nullptr);
// 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);
NS_DispatchToMainThread(event);
// Reset the counters after saving.
mCountRecv = 0;

View File

@ -20,6 +20,7 @@
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkManager.h"
#include "nsProxyRelease.h"
#endif
// ftp server types
@ -263,7 +264,7 @@ private:
// Currently, they are only available on gonk.
uint64_t mCountRecv;
#ifdef MOZ_WIDGET_GONK
nsCOMPtr<nsINetworkInterface> mActiveNetwork;
nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
#endif
nsresult SaveNetworkStats(bool);
void CountRecvBytes(uint64_t recvBytes)

View File

@ -36,10 +36,9 @@
#include <algorithm>
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkStatsServiceProxy.h"
#include "NetStatistics.h"
#endif
//-----------------------------------------------------------------------------
#ifdef DEBUG
@ -55,6 +54,8 @@ static NS_DEFINE_CID(kMultiplexInputStream, NS_MULTIPLEXINPUTSTREAM_CID);
// looking for a response header
#define MAX_INVALID_RESPONSE_BODY_SIZE (1024 * 128)
using namespace mozilla::net;
namespace mozilla {
namespace net {
@ -244,7 +245,7 @@ nsHttpTransaction::Init(uint32_t caps,
#ifdef MOZ_WIDGET_GONK
if (mAppId != NECKO_NO_APP_ID) {
nsCOMPtr<nsINetworkInterface> activeNetwork;
NS_GetActiveNetworkInterface(activeNetwork);
GetActiveNetworkInterface(activeNetwork);
mActiveNetwork =
new nsMainThreadPtrHolder<nsINetworkInterface>(activeNetwork);
}
@ -733,58 +734,6 @@ nsHttpTransaction::WriteSegments(nsAHttpSegmentWriter *writer,
return rv;
}
//-----------------------------------------------------------------------------
// nsHttpTransaction save network statistics event
//-----------------------------------------------------------------------------
#ifdef MOZ_WIDGET_GONK
namespace {
class SaveNetworkStatsEvent : public nsRunnable {
public:
SaveNetworkStatsEvent(uint32_t aAppId,
nsMainThreadPtrHandle<nsINetworkInterface> &aActiveNetwork,
uint64_t aCountRecv,
uint64_t aCountSent)
: mAppId(aAppId),
mActiveNetwork(aActiveNetwork),
mCountRecv(aCountRecv),
mCountSent(aCountSent)
{
MOZ_ASSERT(mAppId != NECKO_NO_APP_ID);
MOZ_ASSERT(mActiveNetwork);
}
NS_IMETHOD Run()
{
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
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,
mActiveNetwork,
PR_Now() / 1000,
mCountRecv,
mCountSent,
false,
nullptr);
return NS_OK;
}
private:
uint32_t mAppId;
nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
uint64_t mCountRecv;
uint64_t mCountSent;
};
};
#endif
nsresult
nsHttpTransaction::SaveNetworkStats(bool enforce)
{
@ -811,7 +760,7 @@ nsHttpTransaction::SaveNetworkStats(bool enforce)
// the event is then dispathed to the main thread.
nsRefPtr<nsRunnable> event =
new SaveNetworkStatsEvent(mAppId, mActiveNetwork,
mCountRecv, mCountSent);
mCountRecv, mCountSent, false);
NS_DispatchToMainThread(event);
// Reset the counters after saving.

View File

@ -15,10 +15,10 @@
#include "nsILoadGroup.h"
#include "nsIInterfaceRequestor.h"
#include "TimingStruct.h"
#include "nsProxyRelease.h"
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkManager.h"
#include "nsProxyRelease.h"
#endif
//-----------------------------------------------------------------------------

View File

@ -57,7 +57,7 @@
#include <algorithm>
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkStatsServiceProxy.h"
#include "NetStatistics.h"
#endif
// rather than slurp up all of nsIWebSocket.idl, which lives outside necko, just
@ -67,6 +67,7 @@
extern PRThread *gSocketThread;
using namespace mozilla;
using namespace mozilla::net;
namespace mozilla {
namespace net {
@ -1144,7 +1145,7 @@ WebSocketChannel::BeginOpen()
#ifdef MOZ_WIDGET_GONK
if (mAppId != NECKO_NO_APP_ID) {
nsCOMPtr<nsINetworkInterface> activeNetwork;
NS_GetActiveNetworkInterface(activeNetwork);
GetActiveNetworkInterface(activeNetwork);
mActiveNetwork =
new nsMainThreadPtrHolder<nsINetworkInterface>(activeNetwork);
}
@ -3364,58 +3365,6 @@ WebSocketChannel::OnDataAvailable(nsIRequest *aRequest,
return NS_OK;
}
//-----------------------------------------------------------------------------
// WebSocketChannel save network statistics event
//-----------------------------------------------------------------------------
#ifdef MOZ_WIDGET_GONK
namespace {
class SaveNetworkStatsEvent : public nsRunnable {
public:
SaveNetworkStatsEvent(uint32_t aAppId,
nsMainThreadPtrHandle<nsINetworkInterface> &aActiveNetwork,
uint64_t aCountRecv,
uint64_t aCountSent)
: mAppId(aAppId),
mActiveNetwork(aActiveNetwork),
mCountRecv(aCountRecv),
mCountSent(aCountSent)
{
MOZ_ASSERT(mAppId != NECKO_NO_APP_ID);
MOZ_ASSERT(mActiveNetwork);
}
NS_IMETHOD Run()
{
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
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,
mActiveNetwork,
PR_Now() / 1000,
mCountRecv,
mCountSent,
false,
nullptr);
return NS_OK;
}
private:
uint32_t mAppId;
nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
uint64_t mCountRecv;
uint64_t mCountSent;
};
};
#endif
nsresult
WebSocketChannel::SaveNetworkStats(bool enforce)
{
@ -3442,7 +3391,7 @@ WebSocketChannel::SaveNetworkStats(bool enforce)
// the event is then dispathed to the main thread.
nsRefPtr<nsRunnable> event =
new SaveNetworkStatsEvent(mAppId, mActiveNetwork,
mCountRecv, mCountSent);
mCountRecv, mCountSent, false);
NS_DispatchToMainThread(event);
// Reset the counters after saving.

View File

@ -21,7 +21,7 @@
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkManager.h"
#include "nsProxyRelease.h" // for nsMainThreadPtrHandle
#include "nsProxyRelease.h"
#endif
#include "nsCOMPtr.h"