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