OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/browsing_data/browsing_data_counter_utils.h" | 5 #include "chrome/browser/browsing_data/browsing_data_counter_utils.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/browsing_data/cache_counter.h" | 9 #include "chrome/browser/browsing_data/cache_counter.h" |
10 #include "chrome/browser/browsing_data/media_licenses_counter.h" | 10 #include "chrome/browser/browsing_data/media_licenses_counter.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
14 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
15 #include "components/browsing_data/core/counters/autofill_counter.h" | |
16 #include "components/browsing_data/core/counters/history_counter.h" | |
17 #include "components/browsing_data/core/counters/passwords_counter.h" | |
18 #include "components/browsing_data/core/pref_names.h" | 15 #include "components/browsing_data/core/pref_names.h" |
19 #include "components/prefs/pref_service.h" | 16 #include "components/prefs/pref_service.h" |
| 17 #include "grit/components_strings.h" |
20 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/base/text/bytes_formatting.h" | 19 #include "ui/base/text/bytes_formatting.h" |
22 | 20 |
23 #if defined(ENABLE_EXTENSIONS) | 21 #if defined(ENABLE_EXTENSIONS) |
24 #include "base/numerics/safe_conversions.h" | 22 #include "base/numerics/safe_conversions.h" |
25 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
26 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
27 #include "chrome/browser/browsing_data/hosted_apps_counter.h" | 25 #include "chrome/browser/browsing_data/hosted_apps_counter.h" |
28 #endif | 26 #endif |
29 | 27 |
(...skipping 17 matching lines...) Expand all Loading... |
47 // counter. | 45 // counter. |
48 base::string16 FormatBytesMBOrHigher( | 46 base::string16 FormatBytesMBOrHigher( |
49 browsing_data::BrowsingDataCounter::ResultInt bytes) { | 47 browsing_data::BrowsingDataCounter::ResultInt bytes) { |
50 if (ui::GetByteDisplayUnits(bytes) >= ui::DataUnits::DATA_UNITS_MEBIBYTE) | 48 if (ui::GetByteDisplayUnits(bytes) >= ui::DataUnits::DATA_UNITS_MEBIBYTE) |
51 return ui::FormatBytes(bytes); | 49 return ui::FormatBytes(bytes); |
52 | 50 |
53 return ui::FormatBytesWithUnits( | 51 return ui::FormatBytesWithUnits( |
54 bytes, ui::DataUnits::DATA_UNITS_MEBIBYTE, true); | 52 bytes, ui::DataUnits::DATA_UNITS_MEBIBYTE, true); |
55 } | 53 } |
56 | 54 |
57 base::string16 GetCounterTextFromResult( | 55 base::string16 GetChromeCounterTextFromResult( |
58 const browsing_data::BrowsingDataCounter::Result* result) { | 56 const browsing_data::BrowsingDataCounter::Result* result) { |
59 base::string16 text; | |
60 std::string pref_name = result->source()->GetPrefName(); | 57 std::string pref_name = result->source()->GetPrefName(); |
61 | 58 |
62 if (!result->Finished()) { | 59 if (!result->Finished()) { |
63 // The counter is still counting. | 60 // The counter is still counting. |
64 text = l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING); | 61 return l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING); |
| 62 } |
65 | 63 |
66 } else if (pref_name == browsing_data::prefs::kDeletePasswords || | 64 if (pref_name == browsing_data::prefs::kDeleteCache) { |
67 pref_name == browsing_data::prefs::kDeleteDownloadHistory) { | |
68 // Counters with trivially formatted result: passwords and downloads. | |
69 browsing_data::BrowsingDataCounter::ResultInt count = | |
70 static_cast<const browsing_data::BrowsingDataCounter::FinishedResult*>( | |
71 result) | |
72 ->Value(); | |
73 text = l10n_util::GetPluralStringFUTF16( | |
74 pref_name == browsing_data::prefs::kDeletePasswords | |
75 ? IDS_DEL_PASSWORDS_COUNTER | |
76 : IDS_DEL_DOWNLOADS_COUNTER, | |
77 count); | |
78 | |
79 } else if (pref_name == browsing_data::prefs::kDeleteCache) { | |
80 // Cache counter. | 65 // Cache counter. |
81 browsing_data::BrowsingDataCounter::ResultInt cache_size_bytes = | 66 browsing_data::BrowsingDataCounter::ResultInt cache_size_bytes = |
82 static_cast<const browsing_data::BrowsingDataCounter::FinishedResult*>( | 67 static_cast<const browsing_data::BrowsingDataCounter::FinishedResult*>( |
83 result) | 68 result) |
84 ->Value(); | 69 ->Value(); |
85 | 70 |
86 PrefService* prefs = result->source()->GetPrefs(); | 71 PrefService* prefs = result->source()->GetPrefs(); |
87 browsing_data::TimePeriod time_period = | 72 browsing_data::TimePeriod time_period = |
88 static_cast<browsing_data::TimePeriod>( | 73 static_cast<browsing_data::TimePeriod>( |
89 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod)); | 74 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod)); |
90 | 75 |
91 // Three cases: Nonzero result for the entire cache, nonzero result for | 76 // Three cases: Nonzero result for the entire cache, nonzero result for |
92 // a subset of cache (i.e. a finite time interval), and almost zero (< 1MB). | 77 // a subset of cache (i.e. a finite time interval), and almost zero (< 1MB). |
93 static const int kBytesInAMegabyte = 1024 * 1024; | 78 static const int kBytesInAMegabyte = 1024 * 1024; |
94 if (cache_size_bytes >= kBytesInAMegabyte) { | 79 if (cache_size_bytes >= kBytesInAMegabyte) { |
95 base::string16 formatted_size = FormatBytesMBOrHigher(cache_size_bytes); | 80 base::string16 formatted_size = FormatBytesMBOrHigher(cache_size_bytes); |
96 text = time_period == browsing_data::ALL_TIME | 81 return time_period == browsing_data::ALL_TIME |
97 ? formatted_size | 82 ? formatted_size |
98 : l10n_util::GetStringFUTF16( | 83 : l10n_util::GetStringFUTF16( |
99 IDS_DEL_CACHE_COUNTER_UPPER_ESTIMATE, formatted_size); | 84 IDS_DEL_CACHE_COUNTER_UPPER_ESTIMATE, formatted_size); |
100 } else { | |
101 text = l10n_util::GetStringUTF16(IDS_DEL_CACHE_COUNTER_ALMOST_EMPTY); | |
102 } | 85 } |
| 86 return l10n_util::GetStringUTF16(IDS_DEL_CACHE_COUNTER_ALMOST_EMPTY); |
| 87 } |
103 | 88 |
104 } else if (pref_name == browsing_data::prefs::kDeleteBrowsingHistory) { | 89 if (pref_name == browsing_data::prefs::kDeleteMediaLicenses) { |
105 // History counter. | |
106 const browsing_data::HistoryCounter::HistoryResult* history_result = | |
107 static_cast<const browsing_data::HistoryCounter::HistoryResult*>( | |
108 result); | |
109 browsing_data::BrowsingDataCounter::ResultInt local_item_count = | |
110 history_result->Value(); | |
111 bool has_synced_visits = history_result->has_synced_visits(); | |
112 | |
113 text = has_synced_visits | |
114 ? l10n_util::GetPluralStringFUTF16( | |
115 IDS_DEL_BROWSING_HISTORY_COUNTER_SYNCED, local_item_count) | |
116 : l10n_util::GetPluralStringFUTF16( | |
117 IDS_DEL_BROWSING_HISTORY_COUNTER, local_item_count); | |
118 | |
119 } else if (pref_name == browsing_data::prefs::kDeleteFormData) { | |
120 // Autofill counter. | |
121 const browsing_data::AutofillCounter::AutofillResult* autofill_result = | |
122 static_cast<const browsing_data::AutofillCounter::AutofillResult*>( | |
123 result); | |
124 browsing_data::AutofillCounter::ResultInt num_suggestions = | |
125 autofill_result->Value(); | |
126 browsing_data::AutofillCounter::ResultInt num_credit_cards = | |
127 autofill_result->num_credit_cards(); | |
128 browsing_data::AutofillCounter::ResultInt num_addresses = | |
129 autofill_result->num_addresses(); | |
130 | |
131 std::vector<base::string16> displayed_strings; | |
132 | |
133 if (num_credit_cards) { | |
134 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16( | |
135 IDS_DEL_AUTOFILL_COUNTER_CREDIT_CARDS, num_credit_cards)); | |
136 } | |
137 if (num_addresses) { | |
138 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16( | |
139 IDS_DEL_AUTOFILL_COUNTER_ADDRESSES, num_addresses)); | |
140 } | |
141 if (num_suggestions) { | |
142 // We use a different wording for autocomplete suggestions based on the | |
143 // length of the entire string. | |
144 switch (displayed_strings.size()) { | |
145 case 0: | |
146 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16( | |
147 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS, num_suggestions)); | |
148 break; | |
149 case 1: | |
150 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16( | |
151 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS_LONG, num_suggestions)); | |
152 break; | |
153 case 2: | |
154 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16( | |
155 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS_SHORT, num_suggestions)); | |
156 break; | |
157 default: | |
158 NOTREACHED(); | |
159 } | |
160 } | |
161 | |
162 // Construct the resulting string from the sections in |displayed_strings|. | |
163 switch (displayed_strings.size()) { | |
164 case 0: | |
165 text = l10n_util::GetStringUTF16(IDS_DEL_AUTOFILL_COUNTER_EMPTY); | |
166 break; | |
167 case 1: | |
168 text = displayed_strings[0]; | |
169 break; | |
170 case 2: | |
171 text = l10n_util::GetStringFUTF16(IDS_DEL_AUTOFILL_COUNTER_TWO_TYPES, | |
172 displayed_strings[0], | |
173 displayed_strings[1]); | |
174 break; | |
175 case 3: | |
176 text = l10n_util::GetStringFUTF16(IDS_DEL_AUTOFILL_COUNTER_THREE_TYPES, | |
177 displayed_strings[0], | |
178 displayed_strings[1], | |
179 displayed_strings[2]); | |
180 break; | |
181 default: | |
182 NOTREACHED(); | |
183 } | |
184 | |
185 } else if (pref_name == browsing_data::prefs::kDeleteMediaLicenses) { | |
186 const MediaLicensesCounter::MediaLicenseResult* media_license_result = | 90 const MediaLicensesCounter::MediaLicenseResult* media_license_result = |
187 static_cast<const MediaLicensesCounter::MediaLicenseResult*>(result); | 91 static_cast<const MediaLicensesCounter::MediaLicenseResult*>(result); |
188 if (media_license_result->Value() > 0) { | 92 if (media_license_result->Value() > 0) { |
189 text = l10n_util::GetStringFUTF16( | 93 return l10n_util::GetStringFUTF16( |
190 IDS_DEL_MEDIA_LICENSES_COUNTER_SITE_COMMENT, | 94 IDS_DEL_MEDIA_LICENSES_COUNTER_SITE_COMMENT, |
191 base::UTF8ToUTF16(media_license_result->GetOneOrigin())); | 95 base::UTF8ToUTF16(media_license_result->GetOneOrigin())); |
192 } else { | |
193 text = l10n_util::GetStringUTF16( | |
194 IDS_DEL_MEDIA_LICENSES_COUNTER_GENERAL_COMMENT); | |
195 } | 96 } |
| 97 return l10n_util::GetStringUTF16( |
| 98 IDS_DEL_MEDIA_LICENSES_COUNTER_GENERAL_COMMENT); |
| 99 } |
196 | 100 |
197 #if defined(ENABLE_EXTENSIONS) | 101 #if defined(ENABLE_EXTENSIONS) |
198 } else if (pref_name == browsing_data::prefs::kDeleteHostedAppsData) { | 102 if (pref_name == browsing_data::prefs::kDeleteHostedAppsData) { |
199 // Hosted apps counter. | 103 // Hosted apps counter. |
200 const HostedAppsCounter::HostedAppsResult* hosted_apps_result = | 104 const HostedAppsCounter::HostedAppsResult* hosted_apps_result = |
201 static_cast<const HostedAppsCounter::HostedAppsResult*>(result); | 105 static_cast<const HostedAppsCounter::HostedAppsResult*>(result); |
202 int hosted_apps_count = hosted_apps_result->Value(); | 106 int hosted_apps_count = hosted_apps_result->Value(); |
203 | 107 |
204 DCHECK_GE(hosted_apps_result->Value(), | 108 DCHECK_GE(hosted_apps_result->Value(), |
205 base::checked_cast<browsing_data::BrowsingDataCounter::ResultInt>( | 109 base::checked_cast<browsing_data::BrowsingDataCounter::ResultInt>( |
206 hosted_apps_result->examples().size())); | 110 hosted_apps_result->examples().size())); |
207 | 111 |
208 std::vector<base::string16> replacements; | 112 std::vector<base::string16> replacements; |
209 if (hosted_apps_count > 0) { | 113 if (hosted_apps_count > 0) { |
210 replacements.push_back( // App1, | 114 replacements.push_back( // App1, |
211 base::UTF8ToUTF16(hosted_apps_result->examples()[0])); | 115 base::UTF8ToUTF16(hosted_apps_result->examples()[0])); |
212 } | 116 } |
213 if (hosted_apps_count > 1) { | 117 if (hosted_apps_count > 1) { |
214 replacements.push_back( | 118 replacements.push_back( |
215 base::UTF8ToUTF16(hosted_apps_result->examples()[1])); // App2, | 119 base::UTF8ToUTF16(hosted_apps_result->examples()[1])); // App2, |
216 } | 120 } |
217 if (hosted_apps_count > 2) { | 121 if (hosted_apps_count > 2) { |
218 replacements.push_back(l10n_util::GetPluralStringFUTF16( // and X-2 more. | 122 replacements.push_back(l10n_util::GetPluralStringFUTF16( // and X-2 more. |
219 IDS_DEL_HOSTED_APPS_COUNTER_AND_X_MORE, | 123 IDS_DEL_HOSTED_APPS_COUNTER_AND_X_MORE, |
220 hosted_apps_count - 2)); | 124 hosted_apps_count - 2)); |
221 } | 125 } |
222 | 126 |
223 // The output string has both the number placeholder (#) and substitution | 127 // The output string has both the number placeholder (#) and substitution |
224 // placeholders ($1, $2, $3). First fetch the correct plural string first, | 128 // placeholders ($1, $2, $3). First fetch the correct plural string first, |
225 // then substitute the $ placeholders. | 129 // then substitute the $ placeholders. |
226 text = base::ReplaceStringPlaceholders( | 130 return base::ReplaceStringPlaceholders( |
227 l10n_util::GetPluralStringFUTF16( | 131 l10n_util::GetPluralStringFUTF16( |
228 IDS_DEL_HOSTED_APPS_COUNTER, hosted_apps_count), | 132 IDS_DEL_HOSTED_APPS_COUNTER, hosted_apps_count), |
229 replacements, | 133 replacements, |
230 nullptr); | 134 nullptr); |
| 135 } |
231 #endif | 136 #endif |
232 } | |
233 | 137 |
234 return text; | 138 return browsing_data::GetCounterTextFromResult(result); |
235 } | 139 } |
236 | 140 |
237 bool GetDeletionPreferenceFromDataType( | 141 bool GetDeletionPreferenceFromDataType( |
238 browsing_data::BrowsingDataType data_type, | 142 browsing_data::BrowsingDataType data_type, |
239 std::string* out_pref) { | 143 std::string* out_pref) { |
240 switch (data_type) { | 144 switch (data_type) { |
241 case browsing_data::HISTORY: | 145 case browsing_data::HISTORY: |
242 *out_pref = browsing_data::prefs::kDeleteBrowsingHistory; | 146 *out_pref = browsing_data::prefs::kDeleteBrowsingHistory; |
243 return true; | 147 return true; |
244 case browsing_data::CACHE: | 148 case browsing_data::CACHE: |
(...skipping 13 matching lines...) Expand all Loading... |
258 // preference. | 162 // preference. |
259 return false; | 163 return false; |
260 case browsing_data::NUM_TYPES: | 164 case browsing_data::NUM_TYPES: |
261 // This is not an actual type. | 165 // This is not an actual type. |
262 NOTREACHED(); | 166 NOTREACHED(); |
263 return false; | 167 return false; |
264 } | 168 } |
265 NOTREACHED(); | 169 NOTREACHED(); |
266 return false; | 170 return false; |
267 } | 171 } |
OLD | NEW |