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