netwerk/base/public/NetStatistics.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     7 #ifndef NetStatistics_h__
     8 #define NetStatistics_h__
    10 #include "mozilla/Assertions.h"
    12 #include "nsCOMPtr.h"
    13 #include "nsError.h"
    14 #include "nsINetworkManager.h"
    15 #include "nsINetworkStatsServiceProxy.h"
    16 #include "nsThreadUtils.h"
    17 #include "nsProxyRelease.h"
    19 namespace mozilla {
    20 namespace net {
    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[] = "";
    26 inline nsresult
    27 GetActiveNetworkInterface(nsCOMPtr<nsINetworkInterface> &aNetworkInterface)
    28 {
    29   MOZ_ASSERT(NS_IsMainThread());
    31   nsresult rv;
    32   nsCOMPtr<nsINetworkManager> networkManager =
    33     do_GetService("@mozilla.org/network/manager;1", &rv);
    35   if (NS_FAILED(rv) || !networkManager) {
    36     aNetworkInterface = nullptr;
    37     return rv;
    38   }
    40   networkManager->GetActive(getter_AddRefs(aNetworkInterface));
    42   return NS_OK;
    43 }
    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   }
    62   NS_IMETHOD Run()
    63   {
    64     MOZ_ASSERT(NS_IsMainThread());
    66     nsresult rv;
    67     nsCOMPtr<nsINetworkStatsServiceProxy> mNetworkStatsServiceProxy =
    68       do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv);
    69     if (NS_FAILED(rv)) {
    70       return rv;
    71     }
    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);
    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 };
    92 } // namespace mozilla:net
    93 } // namespace mozilla
    95 #endif // !NetStatistics_h__

mercurial