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( |
51 kDailyHttpContentLengthLastUpdateDate); | 51 data_reduction_proxy::prefs::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 |
55 // Init all list prefs. | 55 // Init all list prefs. |
56 InitListPref(data_reduction_proxy::prefs:: | 56 InitListPref(data_reduction_proxy::prefs:: |
57 kDailyContentLengthHttpsWithDataReductionProxyEnabled); | 57 kDailyContentLengthHttpsWithDataReductionProxyEnabled); |
58 InitListPref(data_reduction_proxy::prefs:: | 58 InitListPref(data_reduction_proxy::prefs:: |
59 kDailyContentLengthLongBypassWithDataReductionProxyEnabled); | 59 kDailyContentLengthLongBypassWithDataReductionProxyEnabled); |
60 InitListPref(data_reduction_proxy::prefs:: | 60 InitListPref(data_reduction_proxy::prefs:: |
61 kDailyContentLengthShortBypassWithDataReductionProxyEnabled); | 61 kDailyContentLengthShortBypassWithDataReductionProxyEnabled); |
62 InitListPref(data_reduction_proxy::prefs:: | 62 InitListPref(data_reduction_proxy::prefs:: |
63 kDailyContentLengthUnknownWithDataReductionProxyEnabled); | 63 kDailyContentLengthUnknownWithDataReductionProxyEnabled); |
64 InitListPref( | |
65 data_reduction_proxy::prefs::kDailyContentLengthViaDataReductionProxy); | |
64 InitListPref(data_reduction_proxy::prefs:: | 66 InitListPref(data_reduction_proxy::prefs:: |
65 kDailyContentLengthViaDataReductionProxy); | 67 kDailyContentLengthWithDataReductionProxyEnabled); |
66 InitListPref(data_reduction_proxy::prefs:: | |
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( |
82 base::Bind(&DataReductionProxyStatisticsPrefs::OnUpdateContentLengths, | 83 prefs::kUpdateDailyReceivedContentLengths, |
84 base::Bind(&DataReductionProxyCompressionStats::OnUpdateContentLengths, | |
83 GetWeakPtr())); | 85 GetWeakPtr())); |
84 } | 86 } |
85 | 87 |
86 void DataReductionProxyStatisticsPrefs::OnUpdateContentLengths() { | 88 void DataReductionProxyCompressionStats::OnUpdateContentLengths() { |
87 DCHECK(thread_checker_.CalledOnValidThread()); | 89 DCHECK(thread_checker_.CalledOnValidThread()); |
88 if (!pref_service_->GetBoolean(prefs::kUpdateDailyReceivedContentLengths)) | 90 if (!pref_service_->GetBoolean(prefs::kUpdateDailyReceivedContentLengths)) |
89 return; | 91 return; |
90 | 92 |
91 WritePrefs(); | 93 WritePrefs(); |
92 pref_service_->SetBoolean(prefs::kUpdateDailyReceivedContentLengths, false); | 94 pref_service_->SetBoolean(prefs::kUpdateDailyReceivedContentLengths, false); |
93 } | 95 } |
94 | 96 |
95 void DataReductionProxyStatisticsPrefs::InitInt64Pref(const char* pref) { | 97 void DataReductionProxyCompressionStats::InitInt64Pref(const char* pref) { |
96 int64 pref_value = pref_service_->GetInt64(pref); | 98 int64 pref_value = pref_service_->GetInt64(pref); |
97 pref_map_[pref] = pref_value; | 99 pref_map_[pref] = pref_value; |
98 } | 100 } |
99 | 101 |
100 void DataReductionProxyStatisticsPrefs::InitListPref(const char* pref) { | 102 void DataReductionProxyCompressionStats::InitListPref(const char* pref) { |
101 scoped_ptr<base::ListValue> pref_value = scoped_ptr<base::ListValue>( | 103 scoped_ptr<base::ListValue> pref_value = |
102 pref_service_->GetList(pref)->DeepCopy()); | 104 scoped_ptr<base::ListValue>(pref_service_->GetList(pref)->DeepCopy()); |
103 list_pref_map_.add(pref, pref_value.Pass()); | 105 list_pref_map_.add(pref, pref_value.Pass()); |
104 } | 106 } |
105 | 107 |
106 int64 DataReductionProxyStatisticsPrefs::GetInt64(const char* pref_path) { | 108 int64 DataReductionProxyCompressionStats::GetInt64(const char* pref_path) { |
107 if (delay_ == base::TimeDelta()) | 109 if (delay_ == base::TimeDelta()) |
108 return pref_service_->GetInt64(pref_path); | 110 return pref_service_->GetInt64(pref_path); |
109 | 111 |
110 DataReductionProxyPrefMap::iterator iter = pref_map_.find(pref_path); | 112 DataReductionProxyPrefMap::iterator iter = pref_map_.find(pref_path); |
111 return iter->second; | 113 return iter->second; |
112 } | 114 } |
113 | 115 |
114 void DataReductionProxyStatisticsPrefs::SetInt64(const char* pref_path, | 116 void DataReductionProxyCompressionStats::SetInt64(const char* pref_path, |
115 int64 pref_value) { | 117 int64 pref_value) { |
116 if (delay_ == base::TimeDelta()) { | 118 if (delay_ == base::TimeDelta()) { |
117 pref_service_->SetInt64(pref_path, pref_value); | 119 pref_service_->SetInt64(pref_path, pref_value); |
118 return; | 120 return; |
119 } | 121 } |
120 | 122 |
121 DelayedWritePrefs(); | 123 DelayedWritePrefs(); |
122 pref_map_[pref_path] = pref_value; | 124 pref_map_[pref_path] = pref_value; |
123 } | 125 } |
124 | 126 |
125 base::ListValue* DataReductionProxyStatisticsPrefs::GetList( | 127 base::ListValue* DataReductionProxyCompressionStats::GetList( |
126 const char* pref_path) { | 128 const char* pref_path) { |
127 if (delay_ == base::TimeDelta()) | 129 if (delay_ == base::TimeDelta()) |
128 return ListPrefUpdate(pref_service_, pref_path).Get(); | 130 return ListPrefUpdate(pref_service_, pref_path).Get(); |
129 | 131 |
130 DelayedWritePrefs(); | 132 DelayedWritePrefs(); |
131 return list_pref_map_.get(pref_path); | 133 return list_pref_map_.get(pref_path); |
132 } | 134 } |
133 | 135 |
134 void DataReductionProxyStatisticsPrefs::WritePrefs() { | 136 void DataReductionProxyCompressionStats::WritePrefs() { |
135 DCHECK(thread_checker_.CalledOnValidThread()); | 137 DCHECK(thread_checker_.CalledOnValidThread()); |
136 if (delay_ == base::TimeDelta()) | 138 if (delay_ == base::TimeDelta()) |
137 return; | 139 return; |
138 | 140 |
139 for (DataReductionProxyPrefMap::iterator iter = pref_map_.begin(); | 141 for (DataReductionProxyPrefMap::iterator iter = pref_map_.begin(); |
140 iter != pref_map_.end(); ++iter) { | 142 iter != pref_map_.end(); ++iter) { |
141 pref_service_->SetInt64(iter->first, iter->second); | 143 pref_service_->SetInt64(iter->first, iter->second); |
142 } | 144 } |
143 | 145 |
144 for (DataReductionProxyListPrefMap::iterator iter = list_pref_map_.begin(); | 146 for (DataReductionProxyListPrefMap::iterator iter = list_pref_map_.begin(); |
145 iter != list_pref_map_.end(); ++iter) { | 147 iter != list_pref_map_.end(); ++iter) { |
146 TransferList(*(iter->second), | 148 TransferList(*(iter->second), |
147 ListPrefUpdate(pref_service_, iter->first).Get()); | 149 ListPrefUpdate(pref_service_, iter->first).Get()); |
148 } | 150 } |
149 | 151 |
150 delayed_task_posted_ = false; | 152 delayed_task_posted_ = false; |
151 } | 153 } |
152 | 154 |
153 base::Value* | 155 base::Value* |
154 DataReductionProxyStatisticsPrefs::HistoricNetworkStatsInfoToValue() { | 156 DataReductionProxyCompressionStats::HistoricNetworkStatsInfoToValue() { |
155 DCHECK(thread_checker_.CalledOnValidThread()); | 157 DCHECK(thread_checker_.CalledOnValidThread()); |
156 int64 total_received = GetInt64( | 158 int64 total_received = |
157 data_reduction_proxy::prefs::kHttpReceivedContentLength); | 159 GetInt64(data_reduction_proxy::prefs::kHttpReceivedContentLength); |
158 int64 total_original = GetInt64( | 160 int64 total_original = |
159 data_reduction_proxy::prefs::kHttpOriginalContentLength); | 161 GetInt64(data_reduction_proxy::prefs::kHttpOriginalContentLength); |
160 | 162 |
161 base::DictionaryValue* dict = new base::DictionaryValue(); | 163 base::DictionaryValue* dict = new base::DictionaryValue(); |
162 // Use strings to avoid overflow. base::Value only supports 32-bit integers. | 164 // Use strings to avoid overflow. base::Value only supports 32-bit integers. |
163 dict->SetString("historic_received_content_length", | 165 dict->SetString("historic_received_content_length", |
164 base::Int64ToString(total_received)); | 166 base::Int64ToString(total_received)); |
165 dict->SetString("historic_original_content_length", | 167 dict->SetString("historic_original_content_length", |
166 base::Int64ToString(total_original)); | 168 base::Int64ToString(total_original)); |
167 return dict; | 169 return dict; |
168 } | 170 } |
169 | 171 |
170 void DataReductionProxyStatisticsPrefs::DelayedWritePrefs() { | 172 void DataReductionProxyCompressionStats::DelayedWritePrefs() { |
171 // Only write after the first time posting the task. | 173 // Only write after the first time posting the task. |
172 if (delayed_task_posted_) | 174 if (delayed_task_posted_) |
173 return; | 175 return; |
174 | 176 |
175 task_runner_->PostDelayedTask( | 177 task_runner_->PostDelayedTask( |
176 FROM_HERE, | 178 FROM_HERE, |
177 base::Bind(&DataReductionProxyStatisticsPrefs::WritePrefs, | 179 base::Bind(&DataReductionProxyCompressionStats::WritePrefs, GetWeakPtr()), |
178 GetWeakPtr()), | 180 delay_); |
179 delay_); | |
180 | 181 |
181 delayed_task_posted_ = true; | 182 delayed_task_posted_ = true; |
182 } | 183 } |
183 | 184 |
184 void DataReductionProxyStatisticsPrefs::TransferList( | 185 void DataReductionProxyCompressionStats::TransferList( |
185 const base::ListValue& from_list, | 186 const base::ListValue& from_list, |
186 base::ListValue* to_list) { | 187 base::ListValue* to_list) { |
187 to_list->Clear(); | 188 to_list->Clear(); |
188 for (size_t i = 0; i < from_list.GetSize(); ++i) { | 189 for (size_t i = 0; i < from_list.GetSize(); ++i) { |
189 to_list->Set(i, new base::StringValue(base::Int64ToString( | 190 to_list->Set(i, new base::StringValue(base::Int64ToString( |
190 GetListPrefInt64Value(from_list, i)))); | 191 GetListPrefInt64Value(from_list, i)))); |
191 } | 192 } |
192 } | 193 } |
193 | 194 |
194 int64 DataReductionProxyStatisticsPrefs::GetListPrefInt64Value( | 195 int64 DataReductionProxyCompressionStats::GetListPrefInt64Value( |
195 const base::ListValue& list, | 196 const base::ListValue& list, |
196 size_t index) { | 197 size_t index) { |
197 std::string string_value; | 198 std::string string_value; |
198 if (!list.GetString(index, &string_value)) { | 199 if (!list.GetString(index, &string_value)) { |
199 NOTREACHED(); | 200 NOTREACHED(); |
200 return 0; | 201 return 0; |
201 } | 202 } |
202 | 203 |
203 int64 value = 0; | 204 int64 value = 0; |
204 bool rv = base::StringToInt64(string_value, &value); | 205 bool rv = base::StringToInt64(string_value, &value); |
205 DCHECK(rv); | 206 DCHECK(rv); |
206 return value; | 207 return value; |
207 } | 208 } |
208 | 209 |
209 void DataReductionProxyStatisticsPrefs::ClearDataSavingStatistics() { | 210 void DataReductionProxyCompressionStats::ClearDataSavingStatistics() { |
210 list_pref_map_.get(data_reduction_proxy::prefs:: | 211 list_pref_map_.get(data_reduction_proxy::prefs:: |
bengr
2015/03/24 15:51:17
Please revert formatting changes that have nothing
zhuoyu.qian
2015/03/30 03:13:48
Done.
| |
211 kDailyContentLengthHttpsWithDataReductionProxyEnabled)->Clear(); | 212 kDailyContentLengthHttpsWithDataReductionProxyEnabled) |
213 ->Clear(); | |
214 list_pref_map_ | |
215 .get(data_reduction_proxy::prefs:: | |
216 kDailyContentLengthLongBypassWithDataReductionProxyEnabled) | |
217 ->Clear(); | |
218 list_pref_map_ | |
219 .get(data_reduction_proxy::prefs:: | |
220 kDailyContentLengthShortBypassWithDataReductionProxyEnabled) | |
221 ->Clear(); | |
222 list_pref_map_ | |
223 .get(data_reduction_proxy::prefs:: | |
224 kDailyContentLengthUnknownWithDataReductionProxyEnabled) | |
225 ->Clear(); | |
212 list_pref_map_.get(data_reduction_proxy::prefs:: | 226 list_pref_map_.get(data_reduction_proxy::prefs:: |
213 kDailyContentLengthLongBypassWithDataReductionProxyEnabled)->Clear(); | 227 kDailyContentLengthViaDataReductionProxy)->Clear(); |
214 list_pref_map_.get(data_reduction_proxy::prefs:: | 228 list_pref_map_.get(data_reduction_proxy::prefs:: |
215 kDailyContentLengthShortBypassWithDataReductionProxyEnabled)->Clear(); | 229 kDailyContentLengthWithDataReductionProxyEnabled) |
230 ->Clear(); | |
231 list_pref_map_ | |
232 .get(data_reduction_proxy::prefs::kDailyHttpOriginalContentLength) | |
233 ->Clear(); | |
234 list_pref_map_ | |
235 .get(data_reduction_proxy::prefs::kDailyHttpReceivedContentLength) | |
236 ->Clear(); | |
216 list_pref_map_.get(data_reduction_proxy::prefs:: | 237 list_pref_map_.get(data_reduction_proxy::prefs:: |
217 kDailyContentLengthUnknownWithDataReductionProxyEnabled)->Clear(); | 238 kDailyOriginalContentLengthViaDataReductionProxy) |
218 list_pref_map_.get( | 239 ->Clear(); |
219 data_reduction_proxy::prefs::kDailyContentLengthViaDataReductionProxy)-> | 240 list_pref_map_ |
220 Clear(); | 241 .get(data_reduction_proxy::prefs:: |
221 list_pref_map_.get(data_reduction_proxy::prefs:: | 242 kDailyOriginalContentLengthWithDataReductionProxyEnabled) |
222 kDailyContentLengthWithDataReductionProxyEnabled)->Clear(); | 243 ->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 | 244 |
232 WritePrefs(); | 245 WritePrefs(); |
233 } | 246 } |
234 | 247 |
235 base::WeakPtr<DataReductionProxyStatisticsPrefs> | 248 base::WeakPtr<DataReductionProxyCompressionStats> |
236 DataReductionProxyStatisticsPrefs::GetWeakPtr() { | 249 DataReductionProxyCompressionStats::GetWeakPtr() { |
237 return weak_factory_.GetWeakPtr(); | 250 return weak_factory_.GetWeakPtr(); |
238 } | 251 } |
239 | 252 |
240 } // namespace data_reduction_proxy | 253 } // namespace data_reduction_proxy |
OLD | NEW |