NetStatistics.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #ifndef NetStatistics_h__
  7. #define NetStatistics_h__
  8. #include "mozilla/Assertions.h"
  9. #include "nsCOMPtr.h"
  10. #include "nsError.h"
  11. #include "nsINetworkInterface.h"
  12. #include "nsINetworkManager.h"
  13. #include "nsINetworkStatsServiceProxy.h"
  14. #include "nsThreadUtils.h"
  15. #include "nsProxyRelease.h"
  16. namespace mozilla {
  17. namespace net {
  18. // The following members are used for network per-app metering.
  19. const static uint64_t NETWORK_STATS_THRESHOLD = 65536;
  20. const static char NETWORK_STATS_NO_SERVICE_TYPE[] = "";
  21. inline nsresult
  22. GetActiveNetworkInfo(nsCOMPtr<nsINetworkInfo> &aNetworkInfo)
  23. {
  24. MOZ_ASSERT(NS_IsMainThread());
  25. nsresult rv;
  26. nsCOMPtr<nsINetworkManager> networkManager =
  27. do_GetService("@mozilla.org/network/manager;1", &rv);
  28. if (NS_FAILED(rv) || !networkManager) {
  29. aNetworkInfo = nullptr;
  30. return rv;
  31. }
  32. networkManager->GetActiveNetworkInfo(getter_AddRefs(aNetworkInfo));
  33. return NS_OK;
  34. }
  35. class SaveNetworkStatsEvent : public Runnable {
  36. public:
  37. SaveNetworkStatsEvent(uint32_t aAppId,
  38. bool aIsInIsolatedMozBrowser,
  39. nsMainThreadPtrHandle<nsINetworkInfo> &aActiveNetworkInfo,
  40. uint64_t aCountRecv,
  41. uint64_t aCountSent,
  42. bool aIsAccumulative)
  43. : mAppId(aAppId),
  44. mIsInIsolatedMozBrowser(aIsInIsolatedMozBrowser),
  45. mActiveNetworkInfo(aActiveNetworkInfo),
  46. mCountRecv(aCountRecv),
  47. mCountSent(aCountSent),
  48. mIsAccumulative(aIsAccumulative)
  49. {
  50. MOZ_ASSERT(mAppId != NECKO_NO_APP_ID);
  51. MOZ_ASSERT(mActiveNetworkInfo);
  52. }
  53. NS_IMETHOD Run() override
  54. {
  55. MOZ_ASSERT(NS_IsMainThread());
  56. nsresult rv;
  57. nsCOMPtr<nsINetworkStatsServiceProxy> mNetworkStatsServiceProxy =
  58. do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv);
  59. if (NS_FAILED(rv)) {
  60. return rv;
  61. }
  62. // save the network stats through NetworkStatsServiceProxy
  63. mNetworkStatsServiceProxy->SaveAppStats(mAppId,
  64. mIsInIsolatedMozBrowser,
  65. mActiveNetworkInfo,
  66. PR_Now() / 1000,
  67. mCountRecv,
  68. mCountSent,
  69. mIsAccumulative,
  70. nullptr);
  71. return NS_OK;
  72. }
  73. private:
  74. uint32_t mAppId;
  75. bool mIsInIsolatedMozBrowser;
  76. nsMainThreadPtrHandle<nsINetworkInfo> mActiveNetworkInfo;
  77. uint64_t mCountRecv;
  78. uint64_t mCountSent;
  79. bool mIsAccumulative;
  80. };
  81. } // namespace mozilla:net
  82. } // namespace mozilla
  83. #endif // !NetStatistics_h__