File: data_use_tracker.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 (88 lines) | stat: -rw-r--r-- 3,196 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
// Copyright 2016 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_METRICS_DATA_USE_TRACKER_H_
#define COMPONENTS_METRICS_DATA_USE_TRACKER_H_

#include <string>

#include "base/callback.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"

namespace metrics {

typedef base::Callback<void(const std::string&, int, bool)>
    UpdateUsagePrefCallbackType;

// Records the data use of user traffic and UMA traffic in user prefs. Taking
// into account those prefs it can verify whether certain UMA log upload is
// allowed.
class DataUseTracker {
 public:
  explicit DataUseTracker(PrefService* local_state);
  ~DataUseTracker();

  // Returns an instance of |DataUseTracker| with provided |local_state| if
  // users data use should be tracked and null pointer otherwise.
  static std::unique_ptr<DataUseTracker> Create(PrefService* local_state);

  // Registers data use prefs using provided |registry|.
  static void RegisterPrefs(PrefRegistrySimple* registry);

  // Updates data usage tracking prefs with the specified values.
  void UpdateMetricsUsagePrefs(const std::string& service_name,
                               int message_size,
                               bool is_cellular);

  // Returns whether a log with provided |log_bytes| can be uploaded according
  // to data use ratio and UMA quota provided by variations.
  bool ShouldUploadLogOnCellular(int log_bytes);

 private:
  FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckUpdateUsagePref);
  FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckRemoveExpiredEntries);
  FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckComputeTotalDataUse);
  FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckCanUploadUMALog);

  // Updates provided |pref_name| for a current date with the given message
  // size.
  void UpdateUsagePref(const std::string& pref_name, int message_size);

  // Removes entries from the all data use  prefs.
  void RemoveExpiredEntries();

  // Removes entries from the given |pref_name| if they are more than 7 days
  // old.
  void RemoveExpiredEntriesForPref(const std::string& pref_name);

  // Computes data usage according to all the entries in the given dictionary
  // pref.
  int ComputeTotalDataUse(const std::string& pref_name);

  // Returns the weekly allowed quota for UMA data use.
  virtual bool GetUmaWeeklyQuota(int* uma_weekly_quota_bytes) const;

  // Returns the allowed ratio for UMA data use over overall data use.
  virtual bool GetUmaRatio(double* ratio) const;

  // Returns the current date for measurement.
  virtual base::Time GetCurrentMeasurementDate() const;

  // Returns the current date as a string with a proper formatting.
  virtual std::string GetCurrentMeasurementDateAsString() const;

  PrefService* local_state_;

  base::ThreadChecker thread_checker_;

  DISALLOW_COPY_AND_ASSIGN(DataUseTracker);
};

}  // namespace metrics
#endif  // COMPONENTS_METRICS_DATA_USE_TRACKER_H_