Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim:set ts=4 sw=4 sts=4 et cin: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef NetStatistics_h__ |
michael@0 | 8 | #define NetStatistics_h__ |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/Assertions.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "nsCOMPtr.h" |
michael@0 | 13 | #include "nsError.h" |
michael@0 | 14 | #include "nsINetworkManager.h" |
michael@0 | 15 | #include "nsINetworkStatsServiceProxy.h" |
michael@0 | 16 | #include "nsThreadUtils.h" |
michael@0 | 17 | #include "nsProxyRelease.h" |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | namespace net { |
michael@0 | 21 | |
michael@0 | 22 | // The following members are used for network per-app metering. |
michael@0 | 23 | const static uint64_t NETWORK_STATS_THRESHOLD = 65536; |
michael@0 | 24 | const static char NETWORK_STATS_NO_SERVICE_TYPE[] = ""; |
michael@0 | 25 | |
michael@0 | 26 | inline nsresult |
michael@0 | 27 | GetActiveNetworkInterface(nsCOMPtr<nsINetworkInterface> &aNetworkInterface) |
michael@0 | 28 | { |
michael@0 | 29 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 30 | |
michael@0 | 31 | nsresult rv; |
michael@0 | 32 | nsCOMPtr<nsINetworkManager> networkManager = |
michael@0 | 33 | do_GetService("@mozilla.org/network/manager;1", &rv); |
michael@0 | 34 | |
michael@0 | 35 | if (NS_FAILED(rv) || !networkManager) { |
michael@0 | 36 | aNetworkInterface = nullptr; |
michael@0 | 37 | return rv; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | networkManager->GetActive(getter_AddRefs(aNetworkInterface)); |
michael@0 | 41 | |
michael@0 | 42 | return NS_OK; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | class SaveNetworkStatsEvent : public nsRunnable { |
michael@0 | 46 | public: |
michael@0 | 47 | SaveNetworkStatsEvent(uint32_t aAppId, |
michael@0 | 48 | nsMainThreadPtrHandle<nsINetworkInterface> &aActiveNetwork, |
michael@0 | 49 | uint64_t aCountRecv, |
michael@0 | 50 | uint64_t aCountSent, |
michael@0 | 51 | bool aIsAccumulative) |
michael@0 | 52 | : mAppId(aAppId), |
michael@0 | 53 | mActiveNetwork(aActiveNetwork), |
michael@0 | 54 | mCountRecv(aCountRecv), |
michael@0 | 55 | mCountSent(aCountSent), |
michael@0 | 56 | mIsAccumulative(aIsAccumulative) |
michael@0 | 57 | { |
michael@0 | 58 | MOZ_ASSERT(mAppId != NECKO_NO_APP_ID); |
michael@0 | 59 | MOZ_ASSERT(mActiveNetwork); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | NS_IMETHOD Run() |
michael@0 | 63 | { |
michael@0 | 64 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 65 | |
michael@0 | 66 | nsresult rv; |
michael@0 | 67 | nsCOMPtr<nsINetworkStatsServiceProxy> mNetworkStatsServiceProxy = |
michael@0 | 68 | do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv); |
michael@0 | 69 | if (NS_FAILED(rv)) { |
michael@0 | 70 | return rv; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | // save the network stats through NetworkStatsServiceProxy |
michael@0 | 74 | mNetworkStatsServiceProxy->SaveAppStats(mAppId, |
michael@0 | 75 | mActiveNetwork, |
michael@0 | 76 | PR_Now() / 1000, |
michael@0 | 77 | mCountRecv, |
michael@0 | 78 | mCountSent, |
michael@0 | 79 | mIsAccumulative, |
michael@0 | 80 | nullptr); |
michael@0 | 81 | |
michael@0 | 82 | return NS_OK; |
michael@0 | 83 | } |
michael@0 | 84 | private: |
michael@0 | 85 | uint32_t mAppId; |
michael@0 | 86 | nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork; |
michael@0 | 87 | uint64_t mCountRecv; |
michael@0 | 88 | uint64_t mCountSent; |
michael@0 | 89 | bool mIsAccumulative; |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | } // namespace mozilla:net |
michael@0 | 93 | } // namespace mozilla |
michael@0 | 94 | |
michael@0 | 95 | #endif // !NetStatistics_h__ |