1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_BASE_NETWORK_ACTIVITY_MONITOR_H_
6 #define NET_BASE_NETWORK_ACTIVITY_MONITOR_H_
8 #include "base/basictypes.h"
9 #include "base/lazy_instance.h"
10 #include "base/synchronization/lock.h"
11 #include "base/time/time.h"
12 #include "net/base/net_export.h"
18 class NetworkActivityMonitorPeer
;
22 // NetworkActivityMonitor tracks network activity across all sockets and
23 // provides cumulative statistics about bytes sent to and received from
24 // the network. It uses a lock to ensure thread-safety.
26 // There are a few caveats:
27 // * Bytes "sent" includes all send attempts and may include
28 // some bytes which were actually never sent over the network.
29 // * Bytes received includes only bytes actually received from the network,
30 // and does not include any bytes read from the the cache.
31 // * Network activity not initiated directly using chromium sockets won't
32 // be reflected here (for instance DNS queries issued by getaddrinfo()).
33 class NET_EXPORT_PRIVATE NetworkActivityMonitor
{
35 // Returns the singleton instance of the monitor.
36 static NetworkActivityMonitor
* GetInstance();
38 void IncrementBytesReceived(uint64_t bytes_received
);
39 void IncrementBytesSent(uint64_t bytes_sent
);
41 uint64_t GetBytesReceived() const;
42 uint64_t GetBytesSent() const;
44 base::TimeDelta
GetTimeSinceLastReceived() const;
45 base::TimeDelta
GetTimeSinceLastSent() const;
48 friend class test::NetworkActivityMonitorPeer
;
50 NetworkActivityMonitor();
51 ~NetworkActivityMonitor();
52 friend struct base::DefaultLazyInstanceTraits
<NetworkActivityMonitor
>;
54 // Protects all the following members.
55 mutable base::Lock lock_
;
57 uint64_t bytes_received_
;
60 base::TimeTicks last_received_ticks_
;
61 base::TimeTicks last_sent_ticks_
;
63 DISALLOW_COPY_AND_ASSIGN(NetworkActivityMonitor
);
68 #endif // NET_BASE_NETWORK_ACTIVITY_MONITOR_H_