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 "chrome/browser/browsing_data/autofill_counter.h" | 8 #include "chrome/browser/browsing_data/autofill_counter.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/history_counter.h" | 10 #include "chrome/browser/browsing_data/history_counter.h" |
11 #include "chrome/browser/browsing_data/passwords_counter.h" | 11 #include "chrome/browser/browsing_data/passwords_counter.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
14 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 16 #include "components/browsing_data/pref_names.h" |
15 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
16 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
17 #include "ui/base/text/bytes_formatting.h" | 19 #include "ui/base/text/bytes_formatting.h" |
18 | 20 |
19 #if defined(ENABLE_EXTENSIONS) | 21 #if defined(ENABLE_EXTENSIONS) |
20 #include "base/numerics/safe_conversions.h" | 22 #include "base/numerics/safe_conversions.h" |
21 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
22 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
23 #include "chrome/browser/browsing_data/hosted_apps_counter.h" | 25 #include "chrome/browser/browsing_data/hosted_apps_counter.h" |
24 #endif | 26 #endif |
25 | 27 |
26 bool AreCountersEnabled() { | 28 bool AreCountersEnabled() { |
27 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 29 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
28 switches::kEnableClearBrowsingDataCounters)) { | 30 switches::kEnableClearBrowsingDataCounters)) { |
29 return true; | 31 return true; |
30 } | 32 } |
31 | 33 |
32 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 34 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
33 switches::kDisableClearBrowsingDataCounters)) { | 35 switches::kDisableClearBrowsingDataCounters)) { |
34 return false; | 36 return false; |
35 } | 37 } |
36 | 38 |
37 // Enabled by default. | 39 // Enabled by default. |
38 return true; | 40 return true; |
39 } | 41 } |
40 | 42 |
41 // A helper function to display the size of cache in units of MB or higher. | 43 // A helper function to display the size of cache in units of MB or higher. |
42 // We need this, as 1 MB is the lowest nonzero cache size displayed by the | 44 // We need this, as 1 MB is the lowest nonzero cache size displayed by the |
43 // counter. | 45 // counter. |
44 base::string16 FormatBytesMBOrHigher(BrowsingDataCounter::ResultInt bytes) { | 46 base::string16 FormatBytesMBOrHigher( |
| 47 browsing_data::BrowsingDataCounter::ResultInt bytes) { |
45 if (ui::GetByteDisplayUnits(bytes) >= ui::DataUnits::DATA_UNITS_MEBIBYTE) | 48 if (ui::GetByteDisplayUnits(bytes) >= ui::DataUnits::DATA_UNITS_MEBIBYTE) |
46 return ui::FormatBytes(bytes); | 49 return ui::FormatBytes(bytes); |
47 | 50 |
48 return ui::FormatBytesWithUnits( | 51 return ui::FormatBytesWithUnits( |
49 bytes, ui::DataUnits::DATA_UNITS_MEBIBYTE, true); | 52 bytes, ui::DataUnits::DATA_UNITS_MEBIBYTE, true); |
50 } | 53 } |
51 | 54 |
52 base::string16 GetCounterTextFromResult( | 55 base::string16 GetCounterTextFromResult( |
53 const BrowsingDataCounter::Result* result) { | 56 const browsing_data::BrowsingDataCounter::Result* result) { |
54 base::string16 text; | 57 base::string16 text; |
55 std::string pref_name = result->source()->GetPrefName(); | 58 std::string pref_name = result->source()->GetPrefName(); |
56 | 59 |
57 if (!result->Finished()) { | 60 if (!result->Finished()) { |
58 // The counter is still counting. | 61 // The counter is still counting. |
59 text = l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING); | 62 text = l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING); |
60 | 63 |
61 } else if (pref_name == prefs::kDeletePasswords || | 64 } else if (pref_name == prefs::kDeletePasswords || |
62 pref_name == prefs::kDeleteDownloadHistory) { | 65 pref_name == prefs::kDeleteDownloadHistory) { |
63 // Counters with trivially formatted result: passwords and downloads. | 66 // Counters with trivially formatted result: passwords and downloads. |
64 BrowsingDataCounter::ResultInt count = | 67 browsing_data::BrowsingDataCounter::ResultInt count = |
65 static_cast<const BrowsingDataCounter::FinishedResult*>( | 68 static_cast<const browsing_data::BrowsingDataCounter::FinishedResult*>( |
66 result)->Value(); | 69 result) |
| 70 ->Value(); |
67 text = l10n_util::GetPluralStringFUTF16( | 71 text = l10n_util::GetPluralStringFUTF16( |
68 pref_name == prefs::kDeletePasswords | 72 pref_name == prefs::kDeletePasswords |
69 ? IDS_DEL_PASSWORDS_COUNTER | 73 ? IDS_DEL_PASSWORDS_COUNTER |
70 : IDS_DEL_DOWNLOADS_COUNTER, | 74 : IDS_DEL_DOWNLOADS_COUNTER, |
71 count); | 75 count); |
72 | 76 |
73 } else if (pref_name == prefs::kDeleteCache) { | 77 } else if (pref_name == prefs::kDeleteCache) { |
74 // Cache counter. | 78 // Cache counter. |
75 BrowsingDataCounter::ResultInt cache_size_bytes = | 79 browsing_data::BrowsingDataCounter::ResultInt cache_size_bytes = |
76 static_cast<const BrowsingDataCounter::FinishedResult*>( | 80 static_cast<const browsing_data::BrowsingDataCounter::FinishedResult*>( |
77 result)->Value(); | 81 result) |
| 82 ->Value(); |
78 | 83 |
79 PrefService* prefs = result->source()->GetProfile()->GetPrefs(); | 84 PrefService* prefs = result->source()->GetPrefs(); |
80 BrowsingDataRemover::TimePeriod time_period = | 85 browsing_data::TimePeriod time_period = |
81 static_cast<BrowsingDataRemover::TimePeriod>( | 86 static_cast<browsing_data::TimePeriod>( |
82 prefs->GetInteger(prefs::kDeleteTimePeriod)); | 87 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod)); |
83 | 88 |
84 // Three cases: Nonzero result for the entire cache, nonzero result for | 89 // Three cases: Nonzero result for the entire cache, nonzero result for |
85 // a subset of cache (i.e. a finite time interval), and almost zero (< 1MB). | 90 // a subset of cache (i.e. a finite time interval), and almost zero (< 1MB). |
86 static const int kBytesInAMegabyte = 1024 * 1024; | 91 static const int kBytesInAMegabyte = 1024 * 1024; |
87 if (cache_size_bytes >= kBytesInAMegabyte) { | 92 if (cache_size_bytes >= kBytesInAMegabyte) { |
88 base::string16 formatted_size = FormatBytesMBOrHigher(cache_size_bytes); | 93 base::string16 formatted_size = FormatBytesMBOrHigher(cache_size_bytes); |
89 text = time_period == BrowsingDataRemover::EVERYTHING | 94 text = time_period == browsing_data::EVERYTHING |
90 ? formatted_size | 95 ? formatted_size |
91 : l10n_util::GetStringFUTF16(IDS_DEL_CACHE_COUNTER_UPPER_ESTIMATE, | 96 : l10n_util::GetStringFUTF16( |
92 formatted_size); | 97 IDS_DEL_CACHE_COUNTER_UPPER_ESTIMATE, formatted_size); |
93 } else { | 98 } else { |
94 text = l10n_util::GetStringUTF16(IDS_DEL_CACHE_COUNTER_ALMOST_EMPTY); | 99 text = l10n_util::GetStringUTF16(IDS_DEL_CACHE_COUNTER_ALMOST_EMPTY); |
95 } | 100 } |
96 | 101 |
97 } else if (pref_name == prefs::kDeleteBrowsingHistory) { | 102 } else if (pref_name == prefs::kDeleteBrowsingHistory) { |
98 // History counter. | 103 // History counter. |
99 const HistoryCounter::HistoryResult* history_result = | 104 const HistoryCounter::HistoryResult* history_result = |
100 static_cast<const HistoryCounter::HistoryResult*>(result); | 105 static_cast<const HistoryCounter::HistoryResult*>(result); |
101 BrowsingDataCounter::ResultInt local_item_count = history_result->Value(); | 106 browsing_data::BrowsingDataCounter::ResultInt local_item_count = |
| 107 history_result->Value(); |
102 bool has_synced_visits = history_result->has_synced_visits(); | 108 bool has_synced_visits = history_result->has_synced_visits(); |
103 | 109 |
104 text = has_synced_visits | 110 text = has_synced_visits |
105 ? l10n_util::GetPluralStringFUTF16( | 111 ? l10n_util::GetPluralStringFUTF16( |
106 IDS_DEL_BROWSING_HISTORY_COUNTER_SYNCED, local_item_count) | 112 IDS_DEL_BROWSING_HISTORY_COUNTER_SYNCED, local_item_count) |
107 : l10n_util::GetPluralStringFUTF16( | 113 : l10n_util::GetPluralStringFUTF16( |
108 IDS_DEL_BROWSING_HISTORY_COUNTER, local_item_count); | 114 IDS_DEL_BROWSING_HISTORY_COUNTER, local_item_count); |
109 | 115 |
110 } else if (pref_name == prefs::kDeleteFormData) { | 116 } else if (pref_name == prefs::kDeleteFormData) { |
111 // Autofill counter. | 117 // Autofill counter. |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 } | 177 } |
172 | 178 |
173 #if defined(ENABLE_EXTENSIONS) | 179 #if defined(ENABLE_EXTENSIONS) |
174 } else if (pref_name == prefs::kDeleteHostedAppsData) { | 180 } else if (pref_name == prefs::kDeleteHostedAppsData) { |
175 // Hosted apps counter. | 181 // Hosted apps counter. |
176 const HostedAppsCounter::HostedAppsResult* hosted_apps_result = | 182 const HostedAppsCounter::HostedAppsResult* hosted_apps_result = |
177 static_cast<const HostedAppsCounter::HostedAppsResult*>(result); | 183 static_cast<const HostedAppsCounter::HostedAppsResult*>(result); |
178 int hosted_apps_count = hosted_apps_result->Value(); | 184 int hosted_apps_count = hosted_apps_result->Value(); |
179 | 185 |
180 DCHECK_GE(hosted_apps_result->Value(), | 186 DCHECK_GE(hosted_apps_result->Value(), |
181 base::checked_cast<BrowsingDataCounter::ResultInt>( | 187 base::checked_cast<browsing_data::BrowsingDataCounter::ResultInt>( |
182 hosted_apps_result->examples().size())); | 188 hosted_apps_result->examples().size())); |
183 | 189 |
184 std::vector<base::string16> replacements; | 190 std::vector<base::string16> replacements; |
185 if (hosted_apps_count > 0) { | 191 if (hosted_apps_count > 0) { |
186 replacements.push_back( // App1, | 192 replacements.push_back( // App1, |
187 base::UTF8ToUTF16(hosted_apps_result->examples()[0])); | 193 base::UTF8ToUTF16(hosted_apps_result->examples()[0])); |
188 } | 194 } |
189 if (hosted_apps_count > 1) { | 195 if (hosted_apps_count > 1) { |
190 replacements.push_back( | 196 replacements.push_back( |
191 base::UTF8ToUTF16(hosted_apps_result->examples()[1])); // App2, | 197 base::UTF8ToUTF16(hosted_apps_result->examples()[1])); // App2, |
(...skipping 13 matching lines...) Expand all Loading... |
205 replacements, | 211 replacements, |
206 nullptr); | 212 nullptr); |
207 #endif | 213 #endif |
208 | 214 |
209 } | 215 } |
210 | 216 |
211 return text; | 217 return text; |
212 } | 218 } |
213 | 219 |
214 bool GetDeletionPreferenceFromDataType( | 220 bool GetDeletionPreferenceFromDataType( |
215 BrowsingDataType data_type, std::string* out_pref) { | 221 browsing_data::BrowsingDataType data_type, |
| 222 std::string* out_pref) { |
216 switch (data_type) { | 223 switch (data_type) { |
217 case HISTORY: | 224 case browsing_data::HISTORY: |
218 *out_pref = prefs::kDeleteBrowsingHistory; | 225 *out_pref = prefs::kDeleteBrowsingHistory; |
219 return true; | 226 return true; |
220 case CACHE: | 227 case browsing_data::CACHE: |
221 *out_pref = prefs::kDeleteCache; | 228 *out_pref = prefs::kDeleteCache; |
222 return true; | 229 return true; |
223 case COOKIES: | 230 case browsing_data::COOKIES: |
224 *out_pref = prefs::kDeleteCookies; | 231 *out_pref = prefs::kDeleteCookies; |
225 return true; | 232 return true; |
226 case PASSWORDS: | 233 case browsing_data::PASSWORDS: |
227 *out_pref = prefs::kDeletePasswords; | 234 *out_pref = prefs::kDeletePasswords; |
228 return true; | 235 return true; |
229 case FORM_DATA: | 236 case browsing_data::FORM_DATA: |
230 *out_pref = prefs::kDeleteFormData; | 237 *out_pref = prefs::kDeleteFormData; |
231 return true; | 238 return true; |
232 case BOOKMARKS: | 239 case browsing_data::BOOKMARKS: |
233 // Bookmarks are deleted on the Android side. No corresponding deletion | 240 // Bookmarks are deleted on the Android side. No corresponding deletion |
234 // preference. | 241 // preference. |
235 return false; | 242 return false; |
236 case NUM_TYPES: | 243 case browsing_data::NUM_TYPES: |
237 // This is not an actual type. | 244 // This is not an actual type. |
238 NOTREACHED(); | 245 NOTREACHED(); |
239 return false; | 246 return false; |
240 } | 247 } |
241 NOTREACHED(); | 248 NOTREACHED(); |
242 return false; | 249 return false; |
243 } | 250 } |
244 | 251 |
245 BrowsingDataCounter* CreateCounterForPreference(std::string pref_name) { | 252 browsing_data::BrowsingDataCounter* CreateCounterForPreference( |
| 253 std::string pref_name, |
| 254 Profile* profile) { |
246 if (!AreCountersEnabled()) | 255 if (!AreCountersEnabled()) |
247 return nullptr; | 256 return nullptr; |
248 | 257 |
249 if (pref_name == prefs::kDeleteBrowsingHistory) | 258 if (pref_name == prefs::kDeleteBrowsingHistory) |
250 return new HistoryCounter(); | 259 return new HistoryCounter(profile); |
251 if (pref_name == prefs::kDeleteCache) | 260 if (pref_name == prefs::kDeleteCache) |
252 return new CacheCounter(); | 261 return new CacheCounter(profile); |
253 if (pref_name == prefs::kDeletePasswords) | 262 if (pref_name == prefs::kDeletePasswords) |
254 return new PasswordsCounter(); | 263 return new PasswordsCounter(profile); |
255 if (pref_name == prefs::kDeleteFormData) | 264 if (pref_name == prefs::kDeleteFormData) |
256 return new AutofillCounter(); | 265 return new AutofillCounter(profile); |
257 | 266 |
258 return nullptr; | 267 return nullptr; |
259 } | 268 } |
OLD | NEW |