Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: components/metrics/data_use_tracker.cc

Issue 2517363002: Decouple Metrics Initialization from IO Thread Initialization (Closed)
Patch Set: Fix Unit Test Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/metrics/data_use_tracker.h" 5 #include "components/metrics/data_use_tracker.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "components/metrics/metrics_pref_names.h" 11 #include "components/metrics/metrics_pref_names.h"
12 #include "components/prefs/scoped_user_pref_update.h" 12 #include "components/prefs/scoped_user_pref_update.h"
13 #include "components/variations/variations_associated_data.h" 13 #include "components/variations/variations_associated_data.h"
14 14
15 namespace metrics { 15 namespace metrics {
16 16
17 namespace { 17 namespace {
18 18
19 // Default weekly quota and allowed UMA ratio for UMA log uploads for Android. 19 // Default weekly quota and allowed UMA ratio for UMA log uploads for Android.
20 // These defaults will not be used for non-Android as |DataUseTracker| will not 20 // These defaults will not be used for non-Android as |DataUseTracker| will not
21 // be initialized. Default values can be overridden by variation params. 21 // be initialized. Default values can be overridden by variation params.
22 const int kDefaultUMAWeeklyQuotaBytes = 204800; 22 const int kDefaultUMAWeeklyQuotaBytes = 204800;
23 const double kDefaultUMARatio = 0.05; 23 const double kDefaultUMARatio = 0.05;
24 24
25 // This function is for forwarding metrics usage pref changes to the appropriate
26 // callback on the appropriate thread.
27 // TODO(gayane): Reduce the frequency of posting tasks from IO to UI thread.
28 void UpdateMetricsUsagePrefs(
29 const UpdateUsagePrefCallbackType& update_on_ui_callback,
30 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
31 const std::string& service_name,
32 int message_size,
33 bool is_cellular) {
34 ui_task_runner->PostTask(
35 FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size,
36 is_cellular));
37 }
38
39 } // namespace 25 } // namespace
40 26
41 DataUseTracker::DataUseTracker(PrefService* local_state) 27 DataUseTracker::DataUseTracker(PrefService* local_state)
42 : local_state_(local_state), weak_ptr_factory_(this) {} 28 : local_state_(local_state) {}
43 29
44 DataUseTracker::~DataUseTracker() {} 30 DataUseTracker::~DataUseTracker() {}
45 31
46 // static 32 // static
47 std::unique_ptr<DataUseTracker> DataUseTracker::Create( 33 std::unique_ptr<DataUseTracker> DataUseTracker::Create(
48 PrefService* local_state) { 34 PrefService* local_state) {
49 std::unique_ptr<DataUseTracker> data_use_tracker; 35 std::unique_ptr<DataUseTracker> data_use_tracker;
50 #if defined(OS_ANDROID) 36 #if defined(OS_ANDROID)
51 data_use_tracker.reset(new DataUseTracker(local_state)); 37 data_use_tracker.reset(new DataUseTracker(local_state));
52 #endif 38 #endif
53 return data_use_tracker; 39 return data_use_tracker;
54 } 40 }
55 41
56 // static 42 // static
57 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) { 43 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) {
58 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse); 44 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse);
59 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse); 45 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse);
60 } 46 }
61 47
62 UpdateUsagePrefCallbackType DataUseTracker::GetDataUseForwardingCallback( 48 void DataUseTracker::UpdateMetricsUsagePrefs(
63 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { 49 const std::string& service_name, int message_size, bool is_cellular) {
64 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); 50 DCHECK(thread_checker_.CalledOnValidThread());
65 51
66 return base::Bind( 52 if (!is_cellular)
67 &UpdateMetricsUsagePrefs, 53 return;
68 base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread, 54
69 weak_ptr_factory_.GetWeakPtr()), 55 UpdateUsagePref(prefs::kUserCellDataUse, message_size);
70 ui_task_runner); 56 if (service_name == "UMA")
57 UpdateUsagePref(prefs::kUmaCellDataUse, message_size);
71 } 58 }
72 59
73 bool DataUseTracker::ShouldUploadLogOnCellular(int log_bytes) { 60 bool DataUseTracker::ShouldUploadLogOnCellular(int log_bytes) {
74 DCHECK(thread_checker_.CalledOnValidThread()); 61 DCHECK(thread_checker_.CalledOnValidThread());
75 62
76 RemoveExpiredEntries(); 63 RemoveExpiredEntries();
77 64
78 int uma_weekly_quota_bytes; 65 int uma_weekly_quota_bytes;
79 if (!GetUmaWeeklyQuota(&uma_weekly_quota_bytes)) 66 if (!GetUmaWeeklyQuota(&uma_weekly_quota_bytes))
80 return true; 67 return true;
(...skipping 10 matching lines...) Expand all
91 return true; 78 return true;
92 79
93 int user_total_data_use = ComputeTotalDataUse(prefs::kUserCellDataUse); 80 int user_total_data_use = ComputeTotalDataUse(prefs::kUserCellDataUse);
94 // If after adding the new log the uma ratio is still under the allowed ratio 81 // If after adding the new log the uma ratio is still under the allowed ratio
95 // then the log should be uploaded and vice versa. 82 // then the log should be uploaded and vice versa.
96 return new_uma_total_data_use / 83 return new_uma_total_data_use /
97 static_cast<double>(log_bytes + user_total_data_use) <= 84 static_cast<double>(log_bytes + user_total_data_use) <=
98 uma_ratio; 85 uma_ratio;
99 } 86 }
100 87
101 void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread(
102 const std::string& service_name,
103 int message_size,
104 bool is_celllular) {
105 DCHECK(thread_checker_.CalledOnValidThread());
106
107 if (!is_celllular)
108 return;
109
110 UpdateUsagePref(prefs::kUserCellDataUse, message_size);
111 if (service_name == "UMA")
112 UpdateUsagePref(prefs::kUmaCellDataUse, message_size);
113 }
114
115 void DataUseTracker::UpdateUsagePref(const std::string& pref_name, 88 void DataUseTracker::UpdateUsagePref(const std::string& pref_name,
116 int message_size) { 89 int message_size) {
117 DCHECK(thread_checker_.CalledOnValidThread()); 90 DCHECK(thread_checker_.CalledOnValidThread());
118 91
119 DictionaryPrefUpdate pref_updater(local_state_, pref_name); 92 DictionaryPrefUpdate pref_updater(local_state_, pref_name);
120 int todays_traffic = 0; 93 int todays_traffic = 0;
121 std::string todays_key = GetCurrentMeasurementDateAsString(); 94 std::string todays_key = GetCurrentMeasurementDateAsString();
122 95
123 const base::DictionaryValue* user_pref_dict = 96 const base::DictionaryValue* user_pref_dict =
124 local_state_->GetDictionary(pref_name); 97 local_state_->GetDictionary(pref_name);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 std::string DataUseTracker::GetCurrentMeasurementDateAsString() const { 174 std::string DataUseTracker::GetCurrentMeasurementDateAsString() const {
202 DCHECK(thread_checker_.CalledOnValidThread()); 175 DCHECK(thread_checker_.CalledOnValidThread());
203 176
204 base::Time::Exploded today_exploded; 177 base::Time::Exploded today_exploded;
205 GetCurrentMeasurementDate().LocalExplode(&today_exploded); 178 GetCurrentMeasurementDate().LocalExplode(&today_exploded);
206 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year, 179 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year,
207 today_exploded.month, today_exploded.day_of_month); 180 today_exploded.month, today_exploded.day_of_month);
208 } 181 }
209 182
210 } // namespace metrics 183 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698