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