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

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

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

Powered by Google App Engine
This is Rietveld 408576698