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

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: Undo BUILD.gn and fix aw_browser_context 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 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() {
bengr 2014/08/27 04:53:25 Add a comment in data_reduction_proxy_prefs to rem
megjablon 2014/08/28 20:44:07 Done.
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 // need to switch ListValues to scoped_ptr to avoid memory leak
bengr 2014/08/27 04:53:25 They are already, right? Explain.
megjablon 2014/08/28 20:44:07 Whoops ya I forgot to move comment.
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);
bengr 2014/08/27 04:53:25 Can't you just return pref_map_[pref_path] ? Or ar
megjablon 2014/08/28 20:44:07 This method can't be const if the [] operator is u
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
104 if (!delayed_task_posted_)
105 WritePrefsAndPost();
106 return list_pref_map_.get(pref_path);
107 }
108
109 void DataReductionProxyStatisticsPrefs::WritePrefs() {
110 for (DataReductionProxyPrefMap::iterator iter = pref_map_.begin();
111 iter != pref_map_.end(); ++iter) {
112 pref_service_->SetInt64(iter->first, iter->second);
113 }
114
115 for (DataReductionProxyListPrefMap::iterator iter = list_pref_map_.begin();
116 iter != list_pref_map_.end(); ++iter) {
117 TransferList(*(iter->second),
118 ListPrefUpdate(pref_service_, iter->first).Get());
119 }
120 }
121
122 void DataReductionProxyStatisticsPrefs::WritePrefsAndPost() {
123 // Only write after the first time posting the task
bengr 2014/08/27 04:53:25 task.
megjablon 2014/08/28 20:44:07 Done.
124 if (!delayed_task_posted_) {
bengr 2014/08/27 04:53:25 if (delayed_task_posted_) WritePrefs(); else d
megjablon 2014/08/28 20:44:07 Done.
125 delayed_task_posted_ = true;
126 } else {
127 WritePrefs();
128 }
129
130 task_runner_->PostDelayedTask(
131 FROM_HERE,
132 base::Bind(&DataReductionProxyStatisticsPrefs::WritePrefsAndPost,
133 weak_factory_.GetWeakPtr()),
134 delay_);
135 }
136
137 void DataReductionProxyStatisticsPrefs::TransferList(
138 const base::ListValue& from_list,
139 base::ListValue* to_list) {
140 to_list->Clear();
141 for (size_t i = 0; i < from_list.GetSize(); ++i) {
142 to_list->Set(i, new base::StringValue(base::Int64ToString(
143 GetListPrefInt64Value(from_list, i))));
144 }
145 }
146
147 int64 DataReductionProxyStatisticsPrefs::GetListPrefInt64Value(
148 const base::ListValue& list,
149 size_t index) {
150 std::string string_value;
151 if (!list.GetString(index, &string_value)) {
152 NOTREACHED();
153 return 0;
154 }
155
156 int64 value = 0;
157 bool rv = base::StringToInt64(string_value, &value);
158 DCHECK(rv);
159 return value;
160 }
161
162 } // namespace data_reduction_proxy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698