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

Side by Side Diff: chrome/browser/ui/webui/options/clear_browser_data_handler.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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/ui/webui/options/clear_browser_data_handler.h" 5 #include "chrome/browser/ui/webui/options/clear_browser_data_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/macros.h" 12 #include "base/macros.h"
14 #include "base/metrics/histogram_macros.h" 13 #include "base/metrics/histogram_macros.h"
15 #include "base/metrics/sparse_histogram.h" 14 #include "base/metrics/sparse_histogram.h"
16 #include "base/prefs/pref_service.h" 15 #include "base/prefs/pref_service.h"
17 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
18 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
19 #include "base/values.h" 18 #include "base/values.h"
20 #include "chrome/app/chrome_command_ids.h" 19 #include "chrome/app/chrome_command_ids.h"
21 #include "chrome/browser/browser_process.h" 20 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/browsing_data/autofill_counter.h" 21 #include "chrome/browser/browsing_data/autofill_counter.h"
23 #include "chrome/browser/browsing_data/browsing_data_counter.h" 22 #include "chrome/browser/browsing_data/browsing_data_counter.h"
23 #include "chrome/browser/browsing_data/browsing_data_counter_utils.h"
24 #include "chrome/browser/browsing_data/browsing_data_helper.h" 24 #include "chrome/browser/browsing_data/browsing_data_helper.h"
25 #include "chrome/browser/browsing_data/browsing_data_remover.h" 25 #include "chrome/browser/browsing_data/browsing_data_remover.h"
26 #include "chrome/browser/browsing_data/cache_counter.h" 26 #include "chrome/browser/browsing_data/cache_counter.h"
27 #include "chrome/browser/browsing_data/history_counter.h" 27 #include "chrome/browser/browsing_data/history_counter.h"
28 #include "chrome/browser/browsing_data/passwords_counter.h" 28 #include "chrome/browser/browsing_data/passwords_counter.h"
29 #include "chrome/browser/prefs/incognito_mode_prefs.h" 29 #include "chrome/browser/prefs/incognito_mode_prefs.h"
30 #include "chrome/browser/profiles/profile.h" 30 #include "chrome/browser/profiles/profile.h"
31 #include "chrome/browser/sync/profile_sync_service_factory.h" 31 #include "chrome/browser/sync/profile_sync_service_factory.h"
32 #include "chrome/browser/ui/accelerator_utils.h" 32 #include "chrome/browser/ui/accelerator_utils.h"
33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/pref_names.h" 33 #include "chrome/common/pref_names.h"
35 #include "chrome/grit/generated_resources.h" 34 #include "chrome/grit/generated_resources.h"
36 #include "chrome/grit/locale_settings.h" 35 #include "chrome/grit/locale_settings.h"
37 #include "components/google/core/browser/google_util.h" 36 #include "components/google/core/browser/google_util.h"
38 #include "content/public/browser/notification_details.h" 37 #include "content/public/browser/notification_details.h"
39 #include "content/public/browser/web_ui.h" 38 #include "content/public/browser/web_ui.h"
40 #include "ui/base/accelerators/accelerator.h" 39 #include "ui/base/accelerators/accelerator.h"
41 #include "ui/base/l10n/l10n_util.h" 40 #include "ui/base/l10n/l10n_util.h"
42 #include "ui/base/text/bytes_formatting.h"
43 #include "ui/events/keycodes/keyboard_codes.h" 41 #include "ui/events/keycodes/keyboard_codes.h"
44 42
45 namespace { 43 namespace {
46 44
47 const char kClearBrowsingDataLearnMoreUrl[] = 45 const char kClearBrowsingDataLearnMoreUrl[] =
48 "https://support.google.com/chrome/?p=settings_clear_browsing_data"; 46 "https://support.google.com/chrome/?p=settings_clear_browsing_data";
49 47
50 bool AreCountersEnabled() {
51 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
52 switches::kEnableClearBrowsingDataCounters)) {
53 return true;
54 }
55
56 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
57 switches::kDisableClearBrowsingDataCounters)) {
58 return false;
59 }
60
61 // Enabled by default.
62 return true;
63 }
64
65 // A helper function to display the size of cache in units of MB or higher.
66 // We need this, as 1 MB is the lowest nonzero cache size displayed by the
67 // counter.
68 base::string16 FormatBytesMBOrHigher(BrowsingDataCounter::ResultInt bytes) {
69 if (ui::GetByteDisplayUnits(bytes) >= ui::DataUnits::DATA_UNITS_MEBIBYTE)
70 return ui::FormatBytes(bytes);
71
72 return ui::FormatBytesWithUnits(
73 bytes, ui::DataUnits::DATA_UNITS_MEBIBYTE, true);
74 }
75
76 } // namespace 48 } // namespace
77 49
78 namespace options { 50 namespace options {
79 51
80 ClearBrowserDataHandler::ClearBrowserDataHandler() 52 ClearBrowserDataHandler::ClearBrowserDataHandler()
81 : remover_(nullptr), 53 : remover_(nullptr),
82 sync_service_(nullptr) { 54 sync_service_(nullptr) {
83 } 55 }
84 56
85 ClearBrowserDataHandler::~ClearBrowserDataHandler() { 57 ClearBrowserDataHandler::~ClearBrowserDataHandler() {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 DCHECK_NE(ui::VKEY_UNKNOWN, acc.key_code()); 116 DCHECK_NE(ui::VKEY_UNKNOWN, acc.key_code());
145 text = l10n_util::GetStringFUTF16(IDS_CLEAR_BROWSING_DATA_INFO_BAR_TEXT, 117 text = l10n_util::GetStringFUTF16(IDS_CLEAR_BROWSING_DATA_INFO_BAR_TEXT,
146 acc.GetShortcutText()); 118 acc.GetShortcutText());
147 } 119 }
148 } 120 }
149 121
150 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.setBannerText", 122 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.setBannerText",
151 base::StringValue(text)); 123 base::StringValue(text));
152 } 124 }
153 125
154 // static
155 base::string16 ClearBrowserDataHandler::GetCounterTextFromResult(
156 const BrowsingDataCounter::Result* result) {
157 base::string16 text;
158 std::string pref_name = result->source()->GetPrefName();
159
160 if (!result->Finished()) {
161 // The counter is still counting.
162 text = l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING);
163
164 } else if (pref_name == prefs::kDeletePasswords) {
165 // Passwords counter.
166 BrowsingDataCounter::ResultInt passwords_count =
167 static_cast<const BrowsingDataCounter::FinishedResult*>(
168 result)->Value();
169 text = l10n_util::GetPluralStringFUTF16(
170 IDS_DEL_PASSWORDS_COUNTER, passwords_count);
171
172 } else if (pref_name == prefs::kDeleteCache) {
173 // Cache counter.
174 BrowsingDataCounter::ResultInt cache_size_bytes =
175 static_cast<const BrowsingDataCounter::FinishedResult*>(
176 result)->Value();
177
178 PrefService* prefs = result->source()->GetProfile()->GetPrefs();
179 BrowsingDataRemover::TimePeriod time_period =
180 static_cast<BrowsingDataRemover::TimePeriod>(
181 prefs->GetInteger(prefs::kDeleteTimePeriod));
182
183 // Three cases: Nonzero result for the entire cache, nonzero result for
184 // a subset of cache (i.e. a finite time interval), and almost zero (< 1MB).
185 static const int kBytesInAMegabyte = 1024 * 1024;
186 if (cache_size_bytes >= kBytesInAMegabyte) {
187 base::string16 formatted_size = FormatBytesMBOrHigher(cache_size_bytes);
188 text = time_period == BrowsingDataRemover::EVERYTHING
189 ? formatted_size
190 : l10n_util::GetStringFUTF16(IDS_DEL_CACHE_COUNTER_UPPER_ESTIMATE,
191 formatted_size);
192 } else {
193 text = l10n_util::GetStringUTF16(IDS_DEL_CACHE_COUNTER_ALMOST_EMPTY);
194 }
195
196 } else if (pref_name == prefs::kDeleteBrowsingHistory) {
197 // History counter.
198 const HistoryCounter::HistoryResult* history_result =
199 static_cast<const HistoryCounter::HistoryResult*>(result);
200 BrowsingDataCounter::ResultInt local_item_count = history_result->Value();
201 bool has_synced_visits = history_result->has_synced_visits();
202
203 text = has_synced_visits
204 ? l10n_util::GetPluralStringFUTF16(
205 IDS_DEL_BROWSING_HISTORY_COUNTER_SYNCED, local_item_count)
206 : l10n_util::GetPluralStringFUTF16(
207 IDS_DEL_BROWSING_HISTORY_COUNTER, local_item_count);
208
209 } else if (pref_name == prefs::kDeleteFormData) {
210 // Autofill counter.
211 const AutofillCounter::AutofillResult* autofill_result =
212 static_cast<const AutofillCounter::AutofillResult*>(result);
213 AutofillCounter::ResultInt num_suggestions = autofill_result->Value();
214 AutofillCounter::ResultInt num_credit_cards =
215 autofill_result->num_credit_cards();
216 AutofillCounter::ResultInt num_addresses = autofill_result->num_addresses();
217
218 std::vector<base::string16> displayed_strings;
219
220 if (num_credit_cards) {
221 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
222 IDS_DEL_AUTOFILL_COUNTER_CREDIT_CARDS, num_credit_cards));
223 }
224 if (num_addresses) {
225 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
226 IDS_DEL_AUTOFILL_COUNTER_ADDRESSES, num_addresses));
227 }
228 if (num_suggestions) {
229 // We use a different wording for autocomplete suggestions based on the
230 // length of the entire string.
231 switch (displayed_strings.size()) {
232 case 0:
233 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
234 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS, num_suggestions));
235 break;
236 case 1:
237 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
238 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS_LONG, num_suggestions));
239 break;
240 case 2:
241 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
242 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS_SHORT, num_suggestions));
243 break;
244 default:
245 NOTREACHED();
246 }
247 }
248
249 // Construct the resulting string from the sections in |displayed_strings|.
250 switch (displayed_strings.size()) {
251 case 0:
252 text = l10n_util::GetStringUTF16(IDS_DEL_AUTOFILL_COUNTER_EMPTY);
253 break;
254 case 1:
255 text = displayed_strings[0];
256 break;
257 case 2:
258 text = l10n_util::GetStringFUTF16(IDS_DEL_AUTOFILL_COUNTER_TWO_TYPES,
259 displayed_strings[0],
260 displayed_strings[1]);
261 break;
262 case 3:
263 text = l10n_util::GetStringFUTF16(IDS_DEL_AUTOFILL_COUNTER_THREE_TYPES,
264 displayed_strings[0],
265 displayed_strings[1],
266 displayed_strings[2]);
267 break;
268 default:
269 NOTREACHED();
270 }
271 }
272
273 return text;
274 }
275
276 void ClearBrowserDataHandler::OnPageOpened(const base::ListValue* value) { 126 void ClearBrowserDataHandler::OnPageOpened(const base::ListValue* value) {
277 for (BrowsingDataCounter* counter : counters_) { 127 for (BrowsingDataCounter* counter : counters_) {
278 DCHECK(AreCountersEnabled()); 128 DCHECK(AreCountersEnabled());
279 counter->Restart(); 129 counter->Restart();
280 } 130 }
281 } 131 }
282 132
283 void ClearBrowserDataHandler::GetLocalizedValues( 133 void ClearBrowserDataHandler::GetLocalizedValues(
284 base::DictionaryValue* localized_strings) { 134 base::DictionaryValue* localized_strings) {
285 DCHECK(localized_strings); 135 DCHECK(localized_strings);
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 base::StringValue(GetCounterTextFromResult(result.get()))); 325 base::StringValue(GetCounterTextFromResult(result.get())));
476 } 326 }
477 327
478 void ClearBrowserDataHandler::OnStateChanged() { 328 void ClearBrowserDataHandler::OnStateChanged() {
479 web_ui()->CallJavascriptFunction( 329 web_ui()->CallJavascriptFunction(
480 "ClearBrowserDataOverlay.updateSyncWarning", 330 "ClearBrowserDataOverlay.updateSyncWarning",
481 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive())); 331 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()));
482 } 332 }
483 333
484 } // namespace options 334 } // namespace options
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698