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