1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/base/public/NetStatistics.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim:set ts=4 sw=4 sts=4 et cin: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef NetStatistics_h__ 1.11 +#define NetStatistics_h__ 1.12 + 1.13 +#include "mozilla/Assertions.h" 1.14 + 1.15 +#include "nsCOMPtr.h" 1.16 +#include "nsError.h" 1.17 +#include "nsINetworkManager.h" 1.18 +#include "nsINetworkStatsServiceProxy.h" 1.19 +#include "nsThreadUtils.h" 1.20 +#include "nsProxyRelease.h" 1.21 + 1.22 +namespace mozilla { 1.23 +namespace net { 1.24 + 1.25 +// The following members are used for network per-app metering. 1.26 +const static uint64_t NETWORK_STATS_THRESHOLD = 65536; 1.27 +const static char NETWORK_STATS_NO_SERVICE_TYPE[] = ""; 1.28 + 1.29 +inline nsresult 1.30 +GetActiveNetworkInterface(nsCOMPtr<nsINetworkInterface> &aNetworkInterface) 1.31 +{ 1.32 + MOZ_ASSERT(NS_IsMainThread()); 1.33 + 1.34 + nsresult rv; 1.35 + nsCOMPtr<nsINetworkManager> networkManager = 1.36 + do_GetService("@mozilla.org/network/manager;1", &rv); 1.37 + 1.38 + if (NS_FAILED(rv) || !networkManager) { 1.39 + aNetworkInterface = nullptr; 1.40 + return rv; 1.41 + } 1.42 + 1.43 + networkManager->GetActive(getter_AddRefs(aNetworkInterface)); 1.44 + 1.45 + return NS_OK; 1.46 +} 1.47 + 1.48 +class SaveNetworkStatsEvent : public nsRunnable { 1.49 +public: 1.50 + SaveNetworkStatsEvent(uint32_t aAppId, 1.51 + nsMainThreadPtrHandle<nsINetworkInterface> &aActiveNetwork, 1.52 + uint64_t aCountRecv, 1.53 + uint64_t aCountSent, 1.54 + bool aIsAccumulative) 1.55 + : mAppId(aAppId), 1.56 + mActiveNetwork(aActiveNetwork), 1.57 + mCountRecv(aCountRecv), 1.58 + mCountSent(aCountSent), 1.59 + mIsAccumulative(aIsAccumulative) 1.60 + { 1.61 + MOZ_ASSERT(mAppId != NECKO_NO_APP_ID); 1.62 + MOZ_ASSERT(mActiveNetwork); 1.63 + } 1.64 + 1.65 + NS_IMETHOD Run() 1.66 + { 1.67 + MOZ_ASSERT(NS_IsMainThread()); 1.68 + 1.69 + nsresult rv; 1.70 + nsCOMPtr<nsINetworkStatsServiceProxy> mNetworkStatsServiceProxy = 1.71 + do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv); 1.72 + if (NS_FAILED(rv)) { 1.73 + return rv; 1.74 + } 1.75 + 1.76 + // save the network stats through NetworkStatsServiceProxy 1.77 + mNetworkStatsServiceProxy->SaveAppStats(mAppId, 1.78 + mActiveNetwork, 1.79 + PR_Now() / 1000, 1.80 + mCountRecv, 1.81 + mCountSent, 1.82 + mIsAccumulative, 1.83 + nullptr); 1.84 + 1.85 + return NS_OK; 1.86 + } 1.87 +private: 1.88 + uint32_t mAppId; 1.89 + nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork; 1.90 + uint64_t mCountRecv; 1.91 + uint64_t mCountSent; 1.92 + bool mIsAccumulative; 1.93 +}; 1.94 + 1.95 +} // namespace mozilla:net 1.96 +} // namespace mozilla 1.97 + 1.98 +#endif // !NetStatistics_h__