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

Side by Side Diff: chrome/browser/browsing_data/browsing_data_counter_utils.cc

Issue 1527653004: Move browsing data counters helper functions to an 'utils' file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase, solved conflict in #includes Created 4 years, 11 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 2015 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 "chrome/browser/browsing_data/browsing_data_counter_utils.h"
6
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/browsing_data/autofill_counter.h"
10 #include "chrome/browser/browsing_data/cache_counter.h"
11 #include "chrome/browser/browsing_data/history_counter.h"
12 #include "chrome/browser/browsing_data/passwords_counter.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/text/bytes_formatting.h"
18
19 bool AreCountersEnabled() {
20 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
21 switches::kEnableClearBrowsingDataCounters)) {
22 return true;
23 }
24
25 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
26 switches::kDisableClearBrowsingDataCounters)) {
27 return false;
28 }
29
30 // Enabled by default on desktop. Disabled on Android until the feature
31 // is finished.
32 #if defined(OS_ANDROID)
33 return false;
34 #else
35 return true;
36 #endif
37 }
38
39 // A helper function to display the size of cache in units of MB or higher.
40 // We need this, as 1 MB is the lowest nonzero cache size displayed by the
41 // counter.
42 base::string16 FormatBytesMBOrHigher(BrowsingDataCounter::ResultInt bytes) {
43 if (ui::GetByteDisplayUnits(bytes) >= ui::DataUnits::DATA_UNITS_MEBIBYTE)
44 return ui::FormatBytes(bytes);
45
46 return ui::FormatBytesWithUnits(
47 bytes, ui::DataUnits::DATA_UNITS_MEBIBYTE, true);
48 }
49
50 base::string16 GetCounterTextFromResult(
51 const BrowsingDataCounter::Result* result) {
52 base::string16 text;
53 std::string pref_name = result->source()->GetPrefName();
54
55 if (!result->Finished()) {
56 // The counter is still counting.
57 text = l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING);
58
59 } else if (pref_name == prefs::kDeletePasswords) {
60 // Passwords counter.
61 BrowsingDataCounter::ResultInt passwords_count =
62 static_cast<const BrowsingDataCounter::FinishedResult*>(
63 result)->Value();
64 text = l10n_util::GetPluralStringFUTF16(
65 IDS_DEL_PASSWORDS_COUNTER, passwords_count);
66
67 } else if (pref_name == prefs::kDeleteCache) {
68 // Cache counter.
69 BrowsingDataCounter::ResultInt cache_size_bytes =
70 static_cast<const BrowsingDataCounter::FinishedResult*>(
71 result)->Value();
72
73 PrefService* prefs = result->source()->GetProfile()->GetPrefs();
74 BrowsingDataRemover::TimePeriod time_period =
75 static_cast<BrowsingDataRemover::TimePeriod>(
76 prefs->GetInteger(prefs::kDeleteTimePeriod));
77
78 // Three cases: Nonzero result for the entire cache, nonzero result for
79 // a subset of cache (i.e. a finite time interval), and almost zero (< 1MB).
80 static const int kBytesInAMegabyte = 1024 * 1024;
81 if (cache_size_bytes >= kBytesInAMegabyte) {
82 base::string16 formatted_size = FormatBytesMBOrHigher(cache_size_bytes);
83 text = time_period == BrowsingDataRemover::EVERYTHING
84 ? formatted_size
85 : l10n_util::GetStringFUTF16(IDS_DEL_CACHE_COUNTER_UPPER_ESTIMATE,
86 formatted_size);
87 } else {
88 text = l10n_util::GetStringUTF16(IDS_DEL_CACHE_COUNTER_ALMOST_EMPTY);
89 }
90
91 } else if (pref_name == prefs::kDeleteBrowsingHistory) {
92 // History counter.
93 const HistoryCounter::HistoryResult* history_result =
94 static_cast<const HistoryCounter::HistoryResult*>(result);
95 BrowsingDataCounter::ResultInt local_item_count = history_result->Value();
96 bool has_synced_visits = history_result->has_synced_visits();
97
98 text = has_synced_visits
99 ? l10n_util::GetPluralStringFUTF16(
100 IDS_DEL_BROWSING_HISTORY_COUNTER_SYNCED, local_item_count)
101 : l10n_util::GetPluralStringFUTF16(
102 IDS_DEL_BROWSING_HISTORY_COUNTER, local_item_count);
103
104 } else if (pref_name == prefs::kDeleteFormData) {
105 // Autofill counter.
106 const AutofillCounter::AutofillResult* autofill_result =
107 static_cast<const AutofillCounter::AutofillResult*>(result);
108 AutofillCounter::ResultInt num_suggestions = autofill_result->Value();
109 AutofillCounter::ResultInt num_credit_cards =
110 autofill_result->num_credit_cards();
111 AutofillCounter::ResultInt num_addresses = autofill_result->num_addresses();
112
113 std::vector<base::string16> displayed_strings;
114
115 if (num_credit_cards) {
116 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
117 IDS_DEL_AUTOFILL_COUNTER_CREDIT_CARDS, num_credit_cards));
118 }
119 if (num_addresses) {
120 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
121 IDS_DEL_AUTOFILL_COUNTER_ADDRESSES, num_addresses));
122 }
123 if (num_suggestions) {
124 // We use a different wording for autocomplete suggestions based on the
125 // length of the entire string.
126 switch (displayed_strings.size()) {
127 case 0:
128 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
129 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS, num_suggestions));
130 break;
131 case 1:
132 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
133 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS_LONG, num_suggestions));
134 break;
135 case 2:
136 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
137 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS_SHORT, num_suggestions));
138 break;
139 default:
140 NOTREACHED();
141 }
142 }
143
144 // Construct the resulting string from the sections in |displayed_strings|.
145 switch (displayed_strings.size()) {
146 case 0:
147 text = l10n_util::GetStringUTF16(IDS_DEL_AUTOFILL_COUNTER_EMPTY);
148 break;
149 case 1:
150 text = displayed_strings[0];
151 break;
152 case 2:
153 text = l10n_util::GetStringFUTF16(IDS_DEL_AUTOFILL_COUNTER_TWO_TYPES,
154 displayed_strings[0],
155 displayed_strings[1]);
156 break;
157 case 3:
158 text = l10n_util::GetStringFUTF16(IDS_DEL_AUTOFILL_COUNTER_THREE_TYPES,
159 displayed_strings[0],
160 displayed_strings[1],
161 displayed_strings[2]);
162 break;
163 default:
164 NOTREACHED();
165 }
166 }
167
168 return text;
169 }
170
171 BrowsingDataCounter* CreateCounterForPreference(std::string pref_name) {
172 if (!AreCountersEnabled())
173 return nullptr;
174
175 if (pref_name == prefs::kDeleteBrowsingHistory)
176 return new HistoryCounter();
177 if (pref_name == prefs::kDeleteCache)
178 return new CacheCounter();
179 if (pref_name == prefs::kDeletePasswords)
180 return new PasswordsCounter();
181 if (pref_name == prefs::kDeleteFormData)
182 return new AutofillCounter();
183
184 return nullptr;
185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698