2014-01-14 13:55:29 +04:00
|
|
|
/* -*- 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"
|
2015-07-29 09:14:00 +03:00
|
|
|
#include "nsINetworkInterface.h"
|
2014-01-14 13:55:29 +04:00
|
|
|
#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
|
2015-07-29 09:14:00 +03:00
|
|
|
GetActiveNetworkInfo(nsCOMPtr<nsINetworkInfo> &aNetworkInfo)
|
2014-01-14 13:55:29 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsINetworkManager> networkManager =
|
|
|
|
do_GetService("@mozilla.org/network/manager;1", &rv);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || !networkManager) {
|
2015-07-29 09:14:00 +03:00
|
|
|
aNetworkInfo = nullptr;
|
2014-01-14 13:55:29 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-07-29 09:14:00 +03:00
|
|
|
networkManager->GetActiveNetworkInfo(getter_AddRefs(aNetworkInfo));
|
2014-01-14 13:55:29 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class SaveNetworkStatsEvent : public Runnable {
|
2014-01-14 13:55:29 +04:00
|
|
|
public:
|
|
|
|
SaveNetworkStatsEvent(uint32_t aAppId,
|
2016-02-18 05:55:57 +03:00
|
|
|
bool aIsInIsolatedMozBrowser,
|
2015-07-29 09:14:00 +03:00
|
|
|
nsMainThreadPtrHandle<nsINetworkInfo> &aActiveNetworkInfo,
|
2014-01-14 13:55:29 +04:00
|
|
|
uint64_t aCountRecv,
|
|
|
|
uint64_t aCountSent,
|
|
|
|
bool aIsAccumulative)
|
|
|
|
: mAppId(aAppId),
|
2016-02-18 05:55:57 +03:00
|
|
|
mIsInIsolatedMozBrowser(aIsInIsolatedMozBrowser),
|
2015-07-29 09:14:00 +03:00
|
|
|
mActiveNetworkInfo(aActiveNetworkInfo),
|
2014-01-14 13:55:29 +04:00
|
|
|
mCountRecv(aCountRecv),
|
|
|
|
mCountSent(aCountSent),
|
|
|
|
mIsAccumulative(aIsAccumulative)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mAppId != NECKO_NO_APP_ID);
|
2015-07-29 09:14:00 +03:00
|
|
|
MOZ_ASSERT(mActiveNetworkInfo);
|
2014-01-14 13:55:29 +04:00
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2014-01-14 13:55:29 +04:00
|
|
|
{
|
|
|
|
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,
|
2016-02-18 05:55:57 +03:00
|
|
|
mIsInIsolatedMozBrowser,
|
2015-07-29 09:14:00 +03:00
|
|
|
mActiveNetworkInfo,
|
2014-01-14 13:55:29 +04:00
|
|
|
PR_Now() / 1000,
|
|
|
|
mCountRecv,
|
|
|
|
mCountSent,
|
|
|
|
mIsAccumulative,
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
uint32_t mAppId;
|
2016-02-18 05:55:57 +03:00
|
|
|
bool mIsInIsolatedMozBrowser;
|
2015-07-29 09:14:00 +03:00
|
|
|
nsMainThreadPtrHandle<nsINetworkInfo> mActiveNetworkInfo;
|
2014-01-14 13:55:29 +04:00
|
|
|
uint64_t mCountRecv;
|
|
|
|
uint64_t mCountSent;
|
|
|
|
bool mIsAccumulative;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla:net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // !NetStatistics_h__
|