File: data_use_aggregator.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (108 lines) | stat: -rw-r--r-- 3,877 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_
#define COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_

#include <stdint.h>

#include <memory>
#include <string>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/threading/thread_checker.h"
#include "components/data_usage/core/data_use_amortizer.h"
#include "components/data_usage/core/data_use_annotator.h"
#include "net/base/network_change_notifier.h"

namespace net {
class URLRequest;
}

namespace data_usage {

struct DataUse;

// Class that collects and aggregates network usage, reporting the usage to
// observers. Should only be used on the IO thread.
class DataUseAggregator
    : public net::NetworkChangeNotifier::ConnectionTypeObserver {
 public:
  class Observer {
   public:
    virtual ~Observer() {}
    virtual void OnDataUse(const DataUse& data_use) = 0;
  };

  // Constructs a new DataUseAggregator with the given |annotator| and
  // |amortizer|. A NULL |annotator| will be treated as a no-op annotator, and a
  // NULL |amortizer| will be treated as a no-op amortizer.
  DataUseAggregator(std::unique_ptr<DataUseAnnotator> annotator,
                    std::unique_ptr<DataUseAmortizer> amortizer);

  ~DataUseAggregator() override;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Virtual for testing.
  virtual void ReportDataUse(net::URLRequest* request,
                             int64_t tx_bytes,
                             int64_t rx_bytes);

  // Account for off-the-record data use. This usage is only kept track of here
  // so that it can be taken out of any amortized data usage calculations, and a
  // per-request breakdown of off-the-record data usage will never leave the
  // DataUseAggregator.
  // Virtual for testing.
  virtual void ReportOffTheRecordDataUse(int64_t tx_bytes, int64_t rx_bytes);

  base::WeakPtr<DataUseAggregator> GetWeakPtr();

 protected:
  // net::NetworkChangeNotifier::ConnectionTypeObserver implementation.
  // Protected for testing.
  void OnConnectionTypeChanged(
      net::NetworkChangeNotifier::ConnectionType type) override;

  // Protected for testing.
  void SetMccMncForTests(const std::string& mcc_mnc);

 private:
  // Passes |data_use| to |amortizer_| if it exists, or calls
  // OnAmortizationComplete directly if |amortizer_| doesn't exist.
  void PassDataUseToAmortizer(std::unique_ptr<DataUse> data_use);

  // Notifies observers with the data use from |amortized_data_use|.
  void OnAmortizationComplete(std::unique_ptr<DataUse> amortized_data_use);

  base::ThreadChecker thread_checker_;
  std::unique_ptr<DataUseAnnotator> annotator_;
  std::unique_ptr<DataUseAmortizer> amortizer_;
  base::ObserverList<Observer> observer_list_;

  // Current connection type as notified by NetworkChangeNotifier.
  net::NetworkChangeNotifier::ConnectionType connection_type_;

  // MCC+MNC (mobile country code + mobile network code) of the current SIM
  // provider.  Set to empty string if SIM is not present. |mcc_mnc_| is set
  // even if the current active network is not a cellular network.
  std::string mcc_mnc_;

  // As an optimization, re-use the same callbacks to avoid creating and
  // allocating a new Callback object for each call into the |annotator_| or
  // |amortizer_|. These callbacks are lazily initialized.
  DataUseAnnotator::DataUseConsumerCallback annotation_callback_;
  DataUseAmortizer::AmortizationCompleteCallback amortization_callback_;

  base::WeakPtrFactory<DataUseAggregator> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(DataUseAggregator);
};

}  // namespace data_usage

#endif  // COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_