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

Side by Side Diff: components/browsing_data/core/browsing_data_utils.cc

Issue 2228913003: Move part of browsing data counter text util method to components (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 4 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/browsing_data/core/browsing_data_utils.h" 5 #include "components/browsing_data/core/browsing_data_utils.h"
6 6
7 #include "components/browsing_data/core/counters/autofill_counter.h"
8 #include "components/browsing_data/core/counters/history_counter.h"
9 #include "components/browsing_data/core/counters/passwords_counter.h"
10 #include "components/browsing_data/core/pref_names.h"
11 #include "grit/components_strings.h"
12 #include "ui/base/l10n/l10n_util.h"
13
7 namespace browsing_data { 14 namespace browsing_data {
8 15
9 base::Time CalculateBeginDeleteTime(TimePeriod time_period) { 16 base::Time CalculateBeginDeleteTime(TimePeriod time_period) {
10 base::TimeDelta diff; 17 base::TimeDelta diff;
11 base::Time delete_begin_time = base::Time::Now(); 18 base::Time delete_begin_time = base::Time::Now();
12 switch (time_period) { 19 switch (time_period) {
13 case LAST_HOUR: 20 case LAST_HOUR:
14 diff = base::TimeDelta::FromHours(1); 21 diff = base::TimeDelta::FromHours(1);
15 break; 22 break;
16 case LAST_DAY: 23 case LAST_DAY:
17 diff = base::TimeDelta::FromHours(24); 24 diff = base::TimeDelta::FromHours(24);
18 break; 25 break;
19 case LAST_WEEK: 26 case LAST_WEEK:
20 diff = base::TimeDelta::FromHours(7 * 24); 27 diff = base::TimeDelta::FromHours(7 * 24);
21 break; 28 break;
22 case FOUR_WEEKS: 29 case FOUR_WEEKS:
23 diff = base::TimeDelta::FromHours(4 * 7 * 24); 30 diff = base::TimeDelta::FromHours(4 * 7 * 24);
24 break; 31 break;
25 case ALL_TIME: 32 case ALL_TIME:
26 delete_begin_time = base::Time(); 33 delete_begin_time = base::Time();
27 break; 34 break;
28 } 35 }
29 return delete_begin_time - diff; 36 return delete_begin_time - diff;
30 } 37 }
31 38
39 base::string16 GetCounterTextFromResult(
40 const browsing_data::BrowsingDataCounter::Result* result) {
41 base::string16 text;
42 std::string pref_name = result->source()->GetPrefName();
43
44 if (!result->Finished()) {
45 // The counter is still counting.
46 text = l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING);
47
48 } else if (pref_name == browsing_data::prefs::kDeletePasswords ||
49 pref_name == browsing_data::prefs::kDeleteDownloadHistory) {
50 // Counters with trivially formatted result: passwords and downloads.
51 browsing_data::BrowsingDataCounter::ResultInt count =
52 static_cast<const browsing_data::BrowsingDataCounter::FinishedResult*>(
53 result)
54 ->Value();
55 text = l10n_util::GetPluralStringFUTF16(
56 pref_name == browsing_data::prefs::kDeletePasswords
57 ? IDS_DEL_PASSWORDS_COUNTER
58 : IDS_DEL_DOWNLOADS_COUNTER,
59 count);
60 } else if (pref_name == browsing_data::prefs::kDeleteBrowsingHistory) {
61 // History counter.
62 const browsing_data::HistoryCounter::HistoryResult* history_result =
63 static_cast<const browsing_data::HistoryCounter::HistoryResult*>(
64 result);
65 browsing_data::BrowsingDataCounter::ResultInt local_item_count =
66 history_result->Value();
67 bool has_synced_visits = history_result->has_synced_visits();
68
69 text = has_synced_visits
70 ? l10n_util::GetPluralStringFUTF16(
71 IDS_DEL_BROWSING_HISTORY_COUNTER_SYNCED, local_item_count)
72 : l10n_util::GetPluralStringFUTF16(
73 IDS_DEL_BROWSING_HISTORY_COUNTER, local_item_count);
74
75 } else if (pref_name == browsing_data::prefs::kDeleteFormData) {
76 // Autofill counter.
77 const browsing_data::AutofillCounter::AutofillResult* autofill_result =
78 static_cast<const browsing_data::AutofillCounter::AutofillResult*>(
79 result);
80 browsing_data::AutofillCounter::ResultInt num_suggestions =
81 autofill_result->Value();
82 browsing_data::AutofillCounter::ResultInt num_credit_cards =
83 autofill_result->num_credit_cards();
84 browsing_data::AutofillCounter::ResultInt num_addresses =
85 autofill_result->num_addresses();
86
87 std::vector<base::string16> displayed_strings;
88
89 if (num_credit_cards) {
90 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
91 IDS_DEL_AUTOFILL_COUNTER_CREDIT_CARDS, num_credit_cards));
92 }
93 if (num_addresses) {
94 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
95 IDS_DEL_AUTOFILL_COUNTER_ADDRESSES, num_addresses));
96 }
97 if (num_suggestions) {
98 // We use a different wording for autocomplete suggestions based on the
99 // length of the entire string.
100 switch (displayed_strings.size()) {
101 case 0:
102 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
103 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS, num_suggestions));
104 break;
105 case 1:
106 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
107 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS_LONG, num_suggestions));
108 break;
109 case 2:
110 displayed_strings.push_back(l10n_util::GetPluralStringFUTF16(
111 IDS_DEL_AUTOFILL_COUNTER_SUGGESTIONS_SHORT, num_suggestions));
112 break;
113 default:
114 NOTREACHED();
115 }
116 }
117
118 // Construct the resulting string from the sections in |displayed_strings|.
119 switch (displayed_strings.size()) {
120 case 0:
121 text = l10n_util::GetStringUTF16(IDS_DEL_AUTOFILL_COUNTER_EMPTY);
122 break;
123 case 1:
124 text = displayed_strings[0];
125 break;
126 case 2:
127 text = l10n_util::GetStringFUTF16(IDS_DEL_AUTOFILL_COUNTER_TWO_TYPES,
128 displayed_strings[0],
129 displayed_strings[1]);
130 break;
131 case 3:
132 text = l10n_util::GetStringFUTF16(
133 IDS_DEL_AUTOFILL_COUNTER_THREE_TYPES, displayed_strings[0],
134 displayed_strings[1], displayed_strings[2]);
135 break;
136 default:
137 NOTREACHED();
138 }
139 }
140
141 return text;
142 }
143
32 } // namespace browsing_data 144 } // namespace browsing_data
OLDNEW
« no previous file with comments | « components/browsing_data/core/browsing_data_utils.h ('k') | components/browsing_data/core/browsing_data_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698