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

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: Fixing net internals bandwidth page 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/time/time.h"
14 #include "base/values.h"
mmenke 2014/09/15 15:11:02 include base/logging.h for DCHECK.
megjablon 2014/09/15 18:19:23 Done.
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){
mmenke 2014/09/15 15:11:02 nit: Space before "{"
megjablon 2014/09/15 18:19:23 Done.
27 DCHECK(prefs);
28 DCHECK_GE(delay.InMilliseconds(), 0);
29 Init();
30 }
31
32 DataReductionProxyStatisticsPrefs::~DataReductionProxyStatisticsPrefs() {}
33
34 void DataReductionProxyStatisticsPrefs::Init() {
35 if (delay_ == base::TimeDelta())
36 return;
37
38 //Init all int64 prefs
mmenke 2014/09/15 15:11:02 Space between "//" and comments, comments that are
megjablon 2014/09/15 18:19:23 Done.
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 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();
mmenke 2014/09/15 15:11:02 I hadn't looked at this code before, but checking
megjablon 2014/09/15 18:19:23 Thanks for this suggestion!
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
mmenke 2014/09/15 15:11:02 nit: 2 spaces before comment.
megjablon 2014/09/15 18:19:23 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698