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

Side by Side Diff: components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.cc

Issue 949533004: Rename DataReductionProxyStatisticsPrefs to DataReductionProxyCompressionStats (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 8 months 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
(Empty)
1 // Copyright 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.
4
5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_stat istics_prefs.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/prefs/pref_change_registrar.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/prefs/scoped_user_pref_update.h"
14 #include "base/sequenced_task_runner.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/time/time.h"
17 #include "base/values.h"
18 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc hes.h"
19
20 namespace data_reduction_proxy {
21
22 DataReductionProxyStatisticsPrefs::DataReductionProxyStatisticsPrefs(
23 PrefService* prefs,
24 scoped_refptr<base::SequencedTaskRunner> task_runner,
25 const base::TimeDelta& delay)
26 : pref_service_(prefs),
27 task_runner_(task_runner),
28 delay_(delay),
29 delayed_task_posted_(false),
30 pref_change_registrar_(new PrefChangeRegistrar()),
31 weak_factory_(this) {
32 DCHECK(prefs);
33 DCHECK_GE(delay.InMilliseconds(), 0);
34 Init();
35 }
36
37 DataReductionProxyStatisticsPrefs::~DataReductionProxyStatisticsPrefs() {
38 DCHECK(thread_checker_.CalledOnValidThread());
39 WritePrefs();
40 pref_change_registrar_->RemoveAll();
41 weak_factory_.InvalidateWeakPtrs();
42 }
43
44 void DataReductionProxyStatisticsPrefs::Init() {
45 DCHECK(thread_checker_.CalledOnValidThread());
46 if (delay_ == base::TimeDelta())
47 return;
48
49 // Init all int64 prefs.
50 InitInt64Pref(data_reduction_proxy::prefs::
51 kDailyHttpContentLengthLastUpdateDate);
52 InitInt64Pref(data_reduction_proxy::prefs::kHttpReceivedContentLength);
53 InitInt64Pref(data_reduction_proxy::prefs::kHttpOriginalContentLength);
54
55 // Init all list prefs.
56 InitListPref(data_reduction_proxy::prefs::
57 kDailyContentLengthHttpsWithDataReductionProxyEnabled);
58 InitListPref(data_reduction_proxy::prefs::
59 kDailyContentLengthLongBypassWithDataReductionProxyEnabled);
60 InitListPref(data_reduction_proxy::prefs::
61 kDailyContentLengthShortBypassWithDataReductionProxyEnabled);
62 InitListPref(data_reduction_proxy::prefs::
63 kDailyContentLengthUnknownWithDataReductionProxyEnabled);
64 InitListPref(data_reduction_proxy::prefs::
65 kDailyContentLengthViaDataReductionProxy);
66 InitListPref(data_reduction_proxy::prefs::
67 kDailyContentLengthWithDataReductionProxyEnabled);
68 InitListPref(data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
69 InitListPref(data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
70 InitListPref(data_reduction_proxy::prefs::
71 kDailyOriginalContentLengthViaDataReductionProxy);
72 InitListPref(data_reduction_proxy::prefs::
73 kDailyOriginalContentLengthWithDataReductionProxyEnabled);
74
75 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
76 data_reduction_proxy::switches::kClearDataReductionProxyDataSavings)) {
77 ClearDataSavingStatistics();
78 }
79
80 pref_change_registrar_->Init(pref_service_);
81 pref_change_registrar_->Add(prefs::kUpdateDailyReceivedContentLengths,
82 base::Bind(&DataReductionProxyStatisticsPrefs::OnUpdateContentLengths,
83 GetWeakPtr()));
84 }
85
86 void DataReductionProxyStatisticsPrefs::OnUpdateContentLengths() {
87 DCHECK(thread_checker_.CalledOnValidThread());
88 if (!pref_service_->GetBoolean(prefs::kUpdateDailyReceivedContentLengths))
89 return;
90
91 WritePrefs();
92 pref_service_->SetBoolean(prefs::kUpdateDailyReceivedContentLengths, false);
93 }
94
95 void DataReductionProxyStatisticsPrefs::InitInt64Pref(const char* pref) {
96 int64 pref_value = pref_service_->GetInt64(pref);
97 pref_map_[pref] = pref_value;
98 }
99
100 void DataReductionProxyStatisticsPrefs::InitListPref(const char* pref) {
101 scoped_ptr<base::ListValue> pref_value = scoped_ptr<base::ListValue>(
102 pref_service_->GetList(pref)->DeepCopy());
103 list_pref_map_.add(pref, pref_value.Pass());
104 }
105
106 int64 DataReductionProxyStatisticsPrefs::GetInt64(const char* pref_path) {
107 if (delay_ == base::TimeDelta())
108 return pref_service_->GetInt64(pref_path);
109
110 DataReductionProxyPrefMap::iterator iter = pref_map_.find(pref_path);
111 return iter->second;
112 }
113
114 void DataReductionProxyStatisticsPrefs::SetInt64(const char* pref_path,
115 int64 pref_value) {
116 if (delay_ == base::TimeDelta()) {
117 pref_service_->SetInt64(pref_path, pref_value);
118 return;
119 }
120
121 DelayedWritePrefs();
122 pref_map_[pref_path] = pref_value;
123 }
124
125 base::ListValue* DataReductionProxyStatisticsPrefs::GetList(
126 const char* pref_path) {
127 if (delay_ == base::TimeDelta())
128 return ListPrefUpdate(pref_service_, pref_path).Get();
129
130 DelayedWritePrefs();
131 return list_pref_map_.get(pref_path);
132 }
133
134 void DataReductionProxyStatisticsPrefs::WritePrefs() {
135 DCHECK(thread_checker_.CalledOnValidThread());
136 if (delay_ == base::TimeDelta())
137 return;
138
139 for (DataReductionProxyPrefMap::iterator iter = pref_map_.begin();
140 iter != pref_map_.end(); ++iter) {
141 pref_service_->SetInt64(iter->first, iter->second);
142 }
143
144 for (DataReductionProxyListPrefMap::iterator iter = list_pref_map_.begin();
145 iter != list_pref_map_.end(); ++iter) {
146 TransferList(*(iter->second),
147 ListPrefUpdate(pref_service_, iter->first).Get());
148 }
149
150 delayed_task_posted_ = false;
151 }
152
153 base::Value*
154 DataReductionProxyStatisticsPrefs::HistoricNetworkStatsInfoToValue() {
155 DCHECK(thread_checker_.CalledOnValidThread());
156 int64 total_received = GetInt64(
157 data_reduction_proxy::prefs::kHttpReceivedContentLength);
158 int64 total_original = GetInt64(
159 data_reduction_proxy::prefs::kHttpOriginalContentLength);
160
161 base::DictionaryValue* dict = new base::DictionaryValue();
162 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
163 dict->SetString("historic_received_content_length",
164 base::Int64ToString(total_received));
165 dict->SetString("historic_original_content_length",
166 base::Int64ToString(total_original));
167 return dict;
168 }
169
170 void DataReductionProxyStatisticsPrefs::DelayedWritePrefs() {
171 // Only write after the first time posting the task.
172 if (delayed_task_posted_)
173 return;
174
175 task_runner_->PostDelayedTask(
176 FROM_HERE,
177 base::Bind(&DataReductionProxyStatisticsPrefs::WritePrefs,
178 GetWeakPtr()),
179 delay_);
180
181 delayed_task_posted_ = true;
182 }
183
184 void DataReductionProxyStatisticsPrefs::TransferList(
185 const base::ListValue& from_list,
186 base::ListValue* to_list) {
187 to_list->Clear();
188 for (size_t i = 0; i < from_list.GetSize(); ++i) {
189 to_list->Set(i, new base::StringValue(base::Int64ToString(
190 GetListPrefInt64Value(from_list, i))));
191 }
192 }
193
194 int64 DataReductionProxyStatisticsPrefs::GetListPrefInt64Value(
195 const base::ListValue& list,
196 size_t index) {
197 std::string string_value;
198 if (!list.GetString(index, &string_value)) {
199 NOTREACHED();
200 return 0;
201 }
202
203 int64 value = 0;
204 bool rv = base::StringToInt64(string_value, &value);
205 DCHECK(rv);
206 return value;
207 }
208
209 void DataReductionProxyStatisticsPrefs::ClearDataSavingStatistics() {
210 list_pref_map_.get(data_reduction_proxy::prefs::
211 kDailyContentLengthHttpsWithDataReductionProxyEnabled)->Clear();
212 list_pref_map_.get(data_reduction_proxy::prefs::
213 kDailyContentLengthLongBypassWithDataReductionProxyEnabled)->Clear();
214 list_pref_map_.get(data_reduction_proxy::prefs::
215 kDailyContentLengthShortBypassWithDataReductionProxyEnabled)->Clear();
216 list_pref_map_.get(data_reduction_proxy::prefs::
217 kDailyContentLengthUnknownWithDataReductionProxyEnabled)->Clear();
218 list_pref_map_.get(
219 data_reduction_proxy::prefs::kDailyContentLengthViaDataReductionProxy)->
220 Clear();
221 list_pref_map_.get(data_reduction_proxy::prefs::
222 kDailyContentLengthWithDataReductionProxyEnabled)->Clear();
223 list_pref_map_.get(data_reduction_proxy::prefs::
224 kDailyHttpOriginalContentLength)->Clear();
225 list_pref_map_.get(data_reduction_proxy::prefs::
226 kDailyHttpReceivedContentLength)->Clear();
227 list_pref_map_.get(data_reduction_proxy::prefs::
228 kDailyOriginalContentLengthViaDataReductionProxy)->Clear();
229 list_pref_map_.get(data_reduction_proxy::prefs::
230 kDailyOriginalContentLengthWithDataReductionProxyEnabled)->Clear();
231
232 WritePrefs();
233 }
234
235 base::WeakPtr<DataReductionProxyStatisticsPrefs>
236 DataReductionProxyStatisticsPrefs::GetWeakPtr() {
237 return weak_factory_.GetWeakPtr();
238 }
239
240 } // namespace data_reduction_proxy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698