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 #include "net/base/network_activity_monitor.h"
11 base::LazyInstance
<NetworkActivityMonitor
>::Leaky g_network_activity_monitor
=
12 LAZY_INSTANCE_INITIALIZER
;
16 NetworkActivityMonitor::NetworkActivityMonitor()
17 : bytes_received_(0), bytes_sent_(0) {
20 NetworkActivityMonitor::~NetworkActivityMonitor() {
24 NetworkActivityMonitor
* NetworkActivityMonitor::GetInstance() {
25 return g_network_activity_monitor
.Pointer();
28 void NetworkActivityMonitor::IncrementBytesReceived(uint64_t bytes_received
) {
29 base::TimeTicks now
= base::TimeTicks::Now();
30 base::AutoLock
lock(lock_
);
31 bytes_received_
+= bytes_received
;
32 last_received_ticks_
= now
;
35 void NetworkActivityMonitor::IncrementBytesSent(uint64_t bytes_sent
) {
36 base::TimeTicks now
= base::TimeTicks::Now();
37 base::AutoLock
lock(lock_
);
38 bytes_sent_
+= bytes_sent
;
39 last_sent_ticks_
= now
;
42 uint64_t NetworkActivityMonitor::GetBytesReceived() const {
43 base::AutoLock
lock(lock_
);
44 return bytes_received_
;
47 uint64_t NetworkActivityMonitor::GetBytesSent() const {
48 base::AutoLock
lock(lock_
);
52 base::TimeDelta
NetworkActivityMonitor::GetTimeSinceLastReceived() const {
53 base::TimeTicks now
= base::TimeTicks::Now();
54 base::AutoLock
lock(lock_
);
55 return now
- last_received_ticks_
;
58 base::TimeDelta
NetworkActivityMonitor::GetTimeSinceLastSent() const {
59 base::TimeTicks now
= base::TimeTicks::Now();
60 base::AutoLock
lock(lock_
);
61 return now
- last_sent_ticks_
;