OLD | NEW |
(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/browser/data_reduction_proxy_statistic
s_prefs.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/prefs/scoped_user_pref_update.h" |
| 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/time/time.h" |
| 14 #include "base/values.h" |
| 15 |
| 16 namespace data_reduction_proxy { |
| 17 |
| 18 DataReductionProxyStatisticsPrefs::DataReductionProxyStatisticsPrefs( |
| 19 PrefService* prefs, |
| 20 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 21 const base::TimeDelta& delay) |
| 22 : pref_service_(prefs), |
| 23 task_runner_(task_runner), |
| 24 weak_factory_(this), |
| 25 delay_(delay), |
| 26 delayed_task_posted_(false){ |
| 27 DCHECK(prefs); |
| 28 DCHECK_GE(delay.InMilliseconds(), 0); |
| 29 Init(); |
| 30 } |
| 31 |
| 32 DataReductionProxyStatisticsPrefs::~DataReductionProxyStatisticsPrefs() { |
| 33 WritePrefs(); |
| 34 } |
| 35 |
| 36 void DataReductionProxyStatisticsPrefs::Init() { |
| 37 if (delay_ == base::TimeDelta()) |
| 38 return; |
| 39 |
| 40 //Init all int64 prefs |
| 41 InitInt64Pref(data_reduction_proxy::prefs:: |
| 42 kDailyHttpContentLengthLastUpdateDate); |
| 43 InitInt64Pref(data_reduction_proxy::prefs::kHttpReceivedContentLength); |
| 44 InitInt64Pref(data_reduction_proxy::prefs::kHttpOriginalContentLength); |
| 45 |
| 46 //Init all list prefs |
| 47 InitListPref(data_reduction_proxy::prefs:: |
| 48 kDailyContentLengthHttpsWithDataReductionProxyEnabled); |
| 49 InitListPref(data_reduction_proxy::prefs:: |
| 50 kDailyContentLengthLongBypassWithDataReductionProxyEnabled); |
| 51 InitListPref(data_reduction_proxy::prefs:: |
| 52 kDailyContentLengthShortBypassWithDataReductionProxyEnabled); |
| 53 InitListPref(data_reduction_proxy::prefs:: |
| 54 kDailyContentLengthUnknownWithDataReductionProxyEnabled); |
| 55 InitListPref(data_reduction_proxy::prefs:: |
| 56 kDailyContentLengthViaDataReductionProxy); |
| 57 InitListPref(data_reduction_proxy::prefs:: |
| 58 kDailyContentLengthWithDataReductionProxyEnabled); |
| 59 InitListPref(data_reduction_proxy::prefs::kDailyHttpOriginalContentLength); |
| 60 InitListPref(data_reduction_proxy::prefs::kDailyHttpReceivedContentLength); |
| 61 InitListPref(data_reduction_proxy::prefs:: |
| 62 kDailyOriginalContentLengthViaDataReductionProxy); |
| 63 InitListPref(data_reduction_proxy::prefs:: |
| 64 kDailyOriginalContentLengthWithDataReductionProxyEnabled); |
| 65 } |
| 66 |
| 67 void DataReductionProxyStatisticsPrefs::InitInt64Pref(const char* pref) { |
| 68 int64 pref_value = pref_service_->GetInt64(pref); |
| 69 pref_map_[pref] = pref_value; |
| 70 } |
| 71 |
| 72 void DataReductionProxyStatisticsPrefs::InitListPref(const char* pref) { |
| 73 scoped_ptr<base::ListValue> pref_value = scoped_ptr<base::ListValue>( |
| 74 pref_service_->GetList(pref)->DeepCopy()); |
| 75 list_pref_map_.add(pref, pref_value.Pass()); |
| 76 } |
| 77 |
| 78 int64 DataReductionProxyStatisticsPrefs::GetInt64(const char* pref_path) { |
| 79 if (delay_ == base::TimeDelta()) |
| 80 return pref_service_->GetInt64(pref_path); |
| 81 |
| 82 DataReductionProxyPrefMap::iterator iter = pref_map_.find(pref_path); |
| 83 return iter->second; |
| 84 } |
| 85 |
| 86 void DataReductionProxyStatisticsPrefs::SetInt64(const char* pref_path, |
| 87 int64 pref_value) { |
| 88 if (delay_ == base::TimeDelta()) { |
| 89 pref_service_->SetInt64(pref_path, pref_value); |
| 90 return; |
| 91 } |
| 92 |
| 93 if (!delayed_task_posted_) |
| 94 WritePrefsAndPost(); |
| 95 pref_map_[pref_path] = pref_value; |
| 96 } |
| 97 |
| 98 base::ListValue* DataReductionProxyStatisticsPrefs::GetList( |
| 99 const char* pref_path) { |
| 100 if (delay_ == base::TimeDelta()) |
| 101 return ListPrefUpdate(pref_service_, pref_path).Get(); |
| 102 |
| 103 if (!delayed_task_posted_) |
| 104 WritePrefsAndPost(); |
| 105 return list_pref_map_.get(pref_path); |
| 106 } |
| 107 |
| 108 void DataReductionProxyStatisticsPrefs::WritePrefs() { |
| 109 for (DataReductionProxyPrefMap::iterator iter = pref_map_.begin(); |
| 110 iter != pref_map_.end(); ++iter) { |
| 111 pref_service_->SetInt64(iter->first, iter->second); |
| 112 } |
| 113 |
| 114 for (DataReductionProxyListPrefMap::iterator iter = list_pref_map_.begin(); |
| 115 iter != list_pref_map_.end(); ++iter) { |
| 116 TransferList(*(iter->second), |
| 117 ListPrefUpdate(pref_service_, iter->first).Get()); |
| 118 } |
| 119 } |
| 120 |
| 121 void DataReductionProxyStatisticsPrefs::WritePrefsAndPost() { |
| 122 // Only write after the first time posting the task. |
| 123 if (!delayed_task_posted_) |
| 124 delayed_task_posted_ = true; |
| 125 else |
| 126 WritePrefs(); |
| 127 |
| 128 task_runner_->PostDelayedTask( |
| 129 FROM_HERE, |
| 130 base::Bind(&DataReductionProxyStatisticsPrefs::WritePrefsAndPost, |
| 131 weak_factory_.GetWeakPtr()), |
| 132 delay_); |
| 133 } |
| 134 |
| 135 void DataReductionProxyStatisticsPrefs::TransferList( |
| 136 const base::ListValue& from_list, |
| 137 base::ListValue* to_list) { |
| 138 to_list->Clear(); |
| 139 for (size_t i = 0; i < from_list.GetSize(); ++i) { |
| 140 to_list->Set(i, new base::StringValue(base::Int64ToString( |
| 141 GetListPrefInt64Value(from_list, i)))); |
| 142 } |
| 143 } |
| 144 |
| 145 int64 DataReductionProxyStatisticsPrefs::GetListPrefInt64Value( |
| 146 const base::ListValue& list, |
| 147 size_t index) { |
| 148 std::string string_value; |
| 149 if (!list.GetString(index, &string_value)) { |
| 150 NOTREACHED(); |
| 151 return 0; |
| 152 } |
| 153 |
| 154 int64 value = 0; |
| 155 bool rv = base::StringToInt64(string_value, &value); |
| 156 DCHECK(rv); |
| 157 return value; |
| 158 } |
| 159 |
| 160 } // namespace data_reduction_proxy |
OLD | NEW |