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